2023-05-12 12:35:01 +04:00
|
|
|
|
using BookShopContracts.BindingModels;
|
|
|
|
|
using BookShopContracts.ViewModels;
|
|
|
|
|
using BookShopDataModels.Models;
|
2023-05-13 14:30:05 +04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2023-05-12 12:35:01 +04:00
|
|
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
2023-05-13 14:30:05 +04:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
2023-05-12 12:35:01 +04:00
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
|
|
|
|
namespace BookShopDataBaseImplement.Models
|
|
|
|
|
{
|
|
|
|
|
public class Book : IBookModel
|
|
|
|
|
{
|
|
|
|
|
public int Id { get; private set; }
|
|
|
|
|
[Required]
|
|
|
|
|
public string BookName { get; set; } = string.Empty;
|
|
|
|
|
[Required]
|
|
|
|
|
public int Count { get; set; }
|
|
|
|
|
[Required]
|
|
|
|
|
public double Cost { get; set; }
|
|
|
|
|
[Required]
|
|
|
|
|
public int GenreId { get; set; }
|
|
|
|
|
public virtual Genre Genre { get; set; }
|
|
|
|
|
private Dictionary<int, IAuthorModel>? _bookAuthors = null;
|
|
|
|
|
[NotMapped]
|
|
|
|
|
public Dictionary<int, IAuthorModel> BookAuthors
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_bookAuthors == null)
|
|
|
|
|
{
|
|
|
|
|
_bookAuthors = Authors.ToDictionary(recPC => recPC.AuthorId, recPC => recPC.Author as IAuthorModel);
|
|
|
|
|
}
|
|
|
|
|
return _bookAuthors;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
[ForeignKey("BookId")]
|
|
|
|
|
public virtual List<BookAuthor> Authors { get; set; } = new();
|
|
|
|
|
[ForeignKey("BookId")]
|
|
|
|
|
public virtual List<Order> Orders { get; set; } = new();
|
|
|
|
|
public static Book Create(BookShopDatabase context, BookBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
return new Book()
|
|
|
|
|
{
|
|
|
|
|
Id = model.Id,
|
|
|
|
|
BookName = model.BookName,
|
|
|
|
|
Cost = model.Cost,
|
|
|
|
|
Count = model.Count,
|
|
|
|
|
GenreId = model.GenreId,
|
|
|
|
|
Authors = model.BookAuthors.Select(x => new BookAuthor
|
|
|
|
|
{
|
|
|
|
|
Author = context.Authors.First(y => y.Id == x.Key)
|
|
|
|
|
}).ToList()
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
public void Update(BookBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
BookName = model.BookName;
|
|
|
|
|
Cost = model.Cost;
|
|
|
|
|
Cost = model.Cost;
|
|
|
|
|
GenreId = model.GenreId;
|
|
|
|
|
}
|
|
|
|
|
public BookViewModel GetViewModel
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
get
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
using var context = new BookShopDatabase();
|
|
|
|
|
return new BookViewModel
|
|
|
|
|
{
|
|
|
|
|
Id = Id,
|
|
|
|
|
BookName = BookName,
|
|
|
|
|
Cost = Cost,
|
|
|
|
|
GenreId= GenreId,
|
|
|
|
|
GenreName = context.Genres.FirstOrDefault(x => x.Id == GenreId)?.GenreName ?? string.Empty,
|
|
|
|
|
BookAuthors = BookAuthors
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public void UpdateAuthors(BookShopDatabase context, BookBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
var bookAuthors = context.BookAuthors.Where(rec =>
|
|
|
|
|
rec.BookId == model.Id).ToList();
|
|
|
|
|
if (bookAuthors != null && bookAuthors.Count > 0)
|
|
|
|
|
{ // удалили те, которых нет в модели
|
|
|
|
|
context.BookAuthors.RemoveRange(bookAuthors.Where(rec
|
|
|
|
|
=> !model.BookAuthors.ContainsKey(rec.AuthorId)));
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
// обновили количество у существующих записей
|
|
|
|
|
foreach (var updateAuthor in bookAuthors)
|
|
|
|
|
{
|
|
|
|
|
model.BookAuthors.Remove(updateAuthor.AuthorId);
|
|
|
|
|
}
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
var book = context.Books.First(x => x.Id == Id);
|
|
|
|
|
foreach (var pc in model.BookAuthors)
|
|
|
|
|
{
|
|
|
|
|
context.BookAuthors.Add(new BookAuthor
|
|
|
|
|
{
|
|
|
|
|
Book = book,
|
|
|
|
|
Author = context.Authors.First(x => x.Id == pc.Key)
|
|
|
|
|
});
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
_bookAuthors = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|