-
Notifications
You must be signed in to change notification settings - Fork 1
/
Author+AuthorModel.m
62 lines (45 loc) · 1.81 KB
/
Author+AuthorModel.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//
// Author+AuthorModel.m
// Ludow2
//
// Created by Guillaume ESTREM on 30/03/2015.
// Copyright (c) 2015 JACQUEZ_ESTREM. All rights reserved.
//
#import "Author+AuthorModel.h"
@implementation Author (AuthorModel)
+(int) addAuthor:(NSString *) authorName withEntity:(NSEntityDescription *) entityDesc inManagedObjectContext :(NSManagedObjectContext *) context {
Author* anAuthor=nil;
NSError* error = nil;
NSFetchRequest* request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
//On vérifie qu'il n'y ait pas déjà cette fonction dans la base
NSPredicate* predicat = [NSPredicate predicateWithFormat:@"%K LIKE [c]%@",@"authorName",authorName];
[request setPredicate:predicat];
NSArray* result = [context executeFetchRequest:request error:&error];
int res=0;
if(!error){
if([result count]==0){// il faut en créer un
anAuthor = [[Author alloc] initWithEntity:entityDesc insertIntoManagedObjectContext:context];
anAuthor.authorName=authorName;
res=1;
}
}
return res;
}
+(int) updateAuthor :(NSString *) authorName withNewName:(NSString*) newAuthorName withEntity:(NSEntityDescription *) entityDesc inManagedObjectContext :(NSManagedObjectContext *) context {
Author* anAuthor=nil;
NSError* error = nil;
NSFetchRequest* request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
NSPredicate* predicat = [NSPredicate predicateWithFormat:@"%K LIKE [c]%@",@"authorName",authorName];
[request setPredicate:predicat];
NSArray* result = [context executeFetchRequest:request error:&error];
int res=0;
if(!error){
anAuthor=result[0];
anAuthor.authorName=newAuthorName;
res=1;
}
return res;
}
@end