Skip to content

Commit cf55b90

Browse files
committed
Saving Book
1 parent b4373a6 commit cf55b90

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1870
-225
lines changed

BookStore.DAL/Abstract/IAuthorRepository.cs

+1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ namespace BookStore.DAL.Abstract
1010
public interface IAuthorRepository:IStoreRepository<Author>
1111
{
1212
Author GetByName(string last_name, string first_name);
13+
void AddBook(Book book, Author toAuthor);
1314
}
1415
}

BookStore.DAL/Abstract/IRoleRepository.cs

+1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ namespace BookStore.DAL.Abstract
99
{
1010
public interface IRoleRepository:IStoreRepository<Role>
1111
{
12+
Role GetRoleByName(string role_name);
1213
}
1314
}

BookStore.DAL/Abstract/IStoreRepository.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ public interface IStoreRepository<T>
1010
{
1111
T GetByID(int ID);
1212
IQueryable<T> GetAll();
13+
void Create(T obj);
1314
void Save(T obj);
14-
void Delete(int id);
15+
T Delete(int id);
1516
}
1617
}

BookStore.DAL/Concrete/EFAuthorRepository.cs

+5
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,10 @@ public override IQueryable<Author> GetAll()
1919
{
2020
return context.Authors.Include(b=>b.Books);
2121
}
22+
public void AddBook(Book book, Author toAuthor)
23+
{
24+
toAuthor.Books.Add(book);
25+
context.SaveChanges();
26+
}
2227
}
2328
}

BookStore.DAL/Concrete/EFBookRepository.cs

+102-3
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ public class EFBookRepository : EFStoreRepository<Book>, IBookRepository
1414
public IQueryable<Book> GetBooksByLetter(string letter)
1515
{
1616
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"
1919
|| p.Title.StartsWith(letter)
2020
|| (num.Contains(p.Title.Substring(0, 1)) && letter == "0-9"));
2121
}
@@ -28,7 +28,7 @@ public IQueryable<Book> GetBooksByAuthor(string last_name)
2828
public IQueryable<Book> GetBooksByGenre(string genre)
2929
{
3030
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));
3232
}
3333

3434
public IQueryable<Book> GetBooksByTitle(string title)
@@ -56,5 +56,104 @@ public override Book GetByID(int ID)
5656
{
5757
return context.Books.Include(a => a.Authors).Include(a => a.Genres).Include(a => a.Tages).FirstOrDefault(b => b.Book_ID == ID);
5858
}
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+
}
59158
}
60159
}

BookStore.DAL/Concrete/EFRoleRepository.cs

+4
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,9 @@ public override void Save(Role obj)
2020
context.Roles.Add(obj);
2121
context.SaveChanges();
2222
}
23+
public Role GetRoleByName(string role_name)
24+
{
25+
return context.Roles.FirstOrDefault(r => r.Name == role_name);
26+
}
2327
}
2428
}

BookStore.DAL/Concrete/EFStoreRepository.cs

+12-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
namespace BookStore.DAL.Concrete
1111
{
12-
public class EFStoreRepository<T> : IStoreRepository<T> where T:class
12+
public class EFStoreRepository<T> : IStoreRepository<T> where T : class
1313
{
1414
protected readonly EFDbContext context = new EFDbContext();
1515
public virtual T GetByID(int ID)
@@ -23,10 +23,20 @@ public virtual IQueryable<T> GetAll()
2323

2424
public virtual void Save(T obj)
2525
{
26+
context.Set<T>().Add(obj);
27+
context.SaveChanges();
2628
}
2729

28-
public virtual void Delete(int id)
30+
public virtual T Delete(int ID)
2931
{
32+
//context.Set<T>().Remove()
33+
return context.Set<T>().Find(ID);
34+
}
35+
36+
37+
public virtual void Create(T obj)
38+
{
39+
throw new NotImplementedException();
3040
}
3141
}
3242
}

BookStore.Domain/Entities/Author.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ public class Author
1212
[Key]
1313
[ScaffoldColumn(false)]
1414
public int Author_ID { get; set; }
15-
[Display(Name = "Фамилия")]
15+
[Display(Name = "Фамилия Автора")]
1616
public string Last_Name { get; set; }
17-
[Display(Name = "Имя")]
17+
[Display(Name = "Имя Автора")]
1818
public string First_Name { get; set; }
19-
[Display(Name = "Отчество")]
19+
[Display(Name = "Отчество Автора")]
2020
public string Middle_Name { get; set; }
2121
public string Image_Url { get; set; }
2222
[Display(Name = "Биография")]

BookStore.Domain/Entities/Book.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class Book
3939

4040
//[HiddenInput(DisplayValue = false)]
4141
public string Image_url { get; set; }
42-
[Display(Name = "Теги")]
42+
[Display(Name = "Автор")]
4343
public virtual ICollection<Author> Authors { get; set; }
4444
public virtual ICollection<Comment> Comments { get; set; }
4545

BookStore/App_Start/RouteConfig.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,17 @@ public static void RegisterRoutes(RouteCollection routes)
3131
routes.MapRoute(null, "{genre}", new
3232
{
3333
controller = "Book",
34-
action = "List",
34+
action = "ListBy",
3535
page = 1
3636
});
3737

3838
routes.MapRoute(null, "{genre}/Page{page}", new
3939
{
4040
controller = "Book",
41-
action = "List"
41+
action = "ListBy"
4242
},
43-
new { page = @"\d+" });
43+
new { page = @"\d+" });
44+
4445

4546
routes.MapRoute(
4647
name: "Default",

BookStore/Content/Admin.css

+13-22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

BookStore/Content/Admin.css.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

BookStore/Content/Admin.min.css

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)