@@ -14,8 +14,8 @@ public class EFBookRepository : EFStoreRepository<Book>, IBookRepository
14
14
public IQueryable < Book > GetBooksByLetter ( string letter )
15
15
{
16
16
var num = Enumerable . Range ( 0 , 10 ) . Select ( i => i . ToString ( ) ) ;
17
- return context . Books . Include ( b=> b . Authors ) . Include ( b=> b . Genres ) . Include ( b=> b . Tages )
18
- . Where ( p => letter == "All"
17
+ return context . Books . Include ( b => b . Authors ) . Include ( b => b . Genres ) . Include ( b => b . Tages )
18
+ . Where ( p => letter == "All"
19
19
|| p . Title . StartsWith ( letter )
20
20
|| ( num . Contains ( p . Title . Substring ( 0 , 1 ) ) && letter == "0-9" ) ) ;
21
21
}
@@ -28,7 +28,7 @@ public IQueryable<Book> GetBooksByAuthor(string last_name)
28
28
public IQueryable < Book > GetBooksByGenre ( string genre )
29
29
{
30
30
return context . Books . Include ( b => b . Authors ) . Include ( b => b . Genres ) . Include ( b => b . Tages )
31
- . Where ( p => genre == null || p . Genres . Any ( g=> g . Genre_Name == genre ) ) ;
31
+ . Where ( p => genre == null || p . Genres . Any ( g => g . Genre_Name == genre ) ) ;
32
32
}
33
33
34
34
public IQueryable < Book > GetBooksByTitle ( string title )
@@ -56,5 +56,104 @@ public override Book GetByID(int ID)
56
56
{
57
57
return context . Books . Include ( a => a . Authors ) . Include ( a => a . Genres ) . Include ( a => a . Tages ) . FirstOrDefault ( b => b . Book_ID == ID ) ;
58
58
}
59
+ public override Book Delete ( int ID )
60
+ {
61
+ Book book = context . Books . FirstOrDefault ( b => b . Book_ID == ID ) ;
62
+ if ( book != null )
63
+ {
64
+ context . Books . Remove ( book ) ;
65
+ context . SaveChanges ( ) ;
66
+ }
67
+ return book ;
68
+ }
69
+ public override void Save ( Book obj )
70
+ {
71
+ Book bookForSave = context . Books . FirstOrDefault ( b => b . Book_ID == obj . Book_ID ) ;
72
+ if ( bookForSave == null )
73
+ {
74
+ context . Books . Add ( obj ) ;
75
+ }
76
+ else
77
+ {
78
+ bookForSave . Annotation = obj . Annotation ;
79
+ bookForSave . Image_url = obj . Image_url ;
80
+ bookForSave . Price = obj . Price ;
81
+ bookForSave . Rating = obj . Rating ;
82
+ bookForSave . Title = obj . Title ;
83
+ ICollection < Author > authorsNew = obj . Authors ;
84
+ ICollection < Author > authorsOld = bookForSave . Authors ;
85
+ foreach ( var author in authorsNew )
86
+ {
87
+ if ( ! authorsOld . Any ( x => x . Last_Name == author . Last_Name && x . First_Name == author . First_Name ) )
88
+ {
89
+ var _author = context . Authors . FirstOrDefault ( a => a . Last_Name == author . Last_Name && author . First_Name == a . First_Name ) ;
90
+ if ( _author != null )
91
+ {
92
+ bookForSave . Authors . Add ( author ) ;
93
+ }
94
+ else
95
+ {
96
+ bookForSave . Authors . Add ( new Author ( ) { Last_Name = author . Last_Name , First_Name = author . First_Name , Middle_Name = author . Middle_Name } ) ;
97
+ }
98
+ }
99
+ }
100
+ ICollection < Tag > tagsNew = obj . Tages ;
101
+ ICollection < Tag > tagsOld = bookForSave . Tages ;
102
+ if ( tagsNew != null )
103
+ {
104
+ foreach ( var tag in tagsNew )
105
+ {
106
+ if ( ! tagsOld . Any ( x => x . Tag_Name == tag . Tag_Name ) )
107
+ {
108
+ var _tag = context . Tages . FirstOrDefault ( a => a . Tag_Name == tag . Tag_Name ) ;
109
+ if ( _tag != null )
110
+ {
111
+ bookForSave . Tages . Add ( _tag ) ;
112
+ }
113
+ else
114
+ {
115
+ bookForSave . Tages . Add ( new Tag ( ) { Tag_Name = tag . Tag_Name } ) ;
116
+ }
117
+ }
118
+ }
119
+ }
120
+ ICollection < Genre > genresNew = obj . Genres ;
121
+ ICollection < Genre > genresOld = bookForSave . Genres ;
122
+ if ( genresNew != null )
123
+ {
124
+ foreach ( var genre in genresNew )
125
+ {
126
+ if ( ! genresOld . Any ( x => x . Genre_Name == genre . Genre_Name ) )
127
+ {
128
+ var _genre = context . Genres . FirstOrDefault ( a => a . Genre_Name == genre . Genre_Name ) ;
129
+ if ( _genre != null )
130
+ {
131
+ bookForSave . Genres . Add ( _genre ) ;
132
+ }
133
+ else
134
+ {
135
+ bookForSave . Genres . Add ( new Genre ( ) { Genre_Name = genre . Genre_Name } ) ;
136
+ }
137
+ }
138
+ }
139
+ }
140
+ }
141
+ context . SaveChanges ( ) ;
142
+ }
143
+ public override void Create ( Book obj )
144
+ {
145
+ ICollection < Author > authors = obj . Authors ;
146
+ obj . Authors = null ;
147
+ foreach ( var author in authors )
148
+ {
149
+ Author authorForSave = context . Authors . FirstOrDefault ( a => a . Last_Name == author . Last_Name && author . First_Name == a . First_Name ) ;
150
+ if ( authorForSave == null )
151
+ {
152
+ authorForSave = new Author ( ) { Last_Name = author . Last_Name , First_Name = author . First_Name , Middle_Name = author . Middle_Name } ;
153
+ }
154
+ authorForSave . Books . Add ( obj ) ;
155
+ }
156
+ context . SaveChanges ( ) ;
157
+ }
59
158
}
60
159
}
0 commit comments