127 lines
3.9 KiB
C#
127 lines
3.9 KiB
C#
|
using BookContract.BindingModels;
|
|||
|
using BookContract.StorageContracts;
|
|||
|
using BookContract.ViewModels;
|
|||
|
using BookDatabaseImplement.Model;
|
|||
|
using Microsoft.EntityFrameworkCore;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
|
|||
|
namespace BookDatabaseImplement.Implements
|
|||
|
{
|
|||
|
public class BookStorage : IBookStorage
|
|||
|
{
|
|||
|
private Book CreateModel(BookBindingModel model, Book book)
|
|||
|
{
|
|||
|
book.Title = model.Title;
|
|||
|
book.BookType = model.BookType;
|
|||
|
book.Annotation = model.Annotation;
|
|||
|
|
|||
|
return book;
|
|||
|
}
|
|||
|
|
|||
|
private BookViewModel CreateModel(Book book)
|
|||
|
{
|
|||
|
return new BookViewModel
|
|||
|
{
|
|||
|
Id = book.Id,
|
|||
|
Title = book.Title,
|
|||
|
BookType = book.BookType,
|
|||
|
Annotation = book.Annotation,
|
|||
|
LastReaders = book.LastReaders
|
|||
|
.OrderByDescending(h => h.DateBorrowed)
|
|||
|
.Select(h => new ReaderViewModel
|
|||
|
{
|
|||
|
Id = h.Reader.Id,
|
|||
|
Name = h.Reader.Name
|
|||
|
})
|
|||
|
.Take(6)
|
|||
|
.ToList()
|
|||
|
};
|
|||
|
}
|
|||
|
|
|||
|
public List<BookViewModel> GetFullList()
|
|||
|
{
|
|||
|
using var context = new BookLibraryDatabase();
|
|||
|
return context.Books
|
|||
|
.Include(b => b.LastReaders)
|
|||
|
.ThenInclude(h => h.Reader)
|
|||
|
.ToList()
|
|||
|
.Select(CreateModel)
|
|||
|
.ToList();
|
|||
|
}
|
|||
|
|
|||
|
public List<BookViewModel> GetFilterList(BookBindingModel model)
|
|||
|
{
|
|||
|
using var context = new BookLibraryDatabase();
|
|||
|
return context.Books
|
|||
|
.Where(book => book.BookType.Contains(model.BookType))
|
|||
|
.ToList().Select(CreateModel).ToList();
|
|||
|
}
|
|||
|
|
|||
|
public BookViewModel? GetElement(BookBindingModel model)
|
|||
|
{
|
|||
|
using var context = new BookLibraryDatabase();
|
|||
|
var book = context.Books.FirstOrDefault(rec => rec.Id == model.Id || rec.Title == model.Title);
|
|||
|
return book != null ? CreateModel(book) : null;
|
|||
|
}
|
|||
|
|
|||
|
public void Insert(BookBindingModel model)
|
|||
|
{
|
|||
|
using var context = new BookLibraryDatabase();
|
|||
|
using var transaction = context.Database.BeginTransaction();
|
|||
|
try
|
|||
|
{
|
|||
|
context.Books.Add(CreateModel(model, new Book()));
|
|||
|
context.SaveChanges();
|
|||
|
transaction.Commit();
|
|||
|
}
|
|||
|
catch
|
|||
|
{
|
|||
|
transaction.Rollback();
|
|||
|
throw;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public void Update(BookBindingModel model)
|
|||
|
{
|
|||
|
using var context = new BookLibraryDatabase();
|
|||
|
using var transaction = context.Database.BeginTransaction();
|
|||
|
try
|
|||
|
{
|
|||
|
var book = context.Books.FirstOrDefault(rec => rec.Id == model.Id);
|
|||
|
if (book == null)
|
|||
|
{
|
|||
|
throw new Exception("Книга не найдена");
|
|||
|
}
|
|||
|
CreateModel(model, book);
|
|||
|
context.SaveChanges();
|
|||
|
transaction.Commit();
|
|||
|
}
|
|||
|
catch
|
|||
|
{
|
|||
|
transaction.Rollback();
|
|||
|
throw;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public void Delete(BookBindingModel model)
|
|||
|
{
|
|||
|
using var context = new BookLibraryDatabase();
|
|||
|
var book = context.Books.FirstOrDefault(rec => rec.Id == model.Id);
|
|||
|
|
|||
|
if (book != null)
|
|||
|
{
|
|||
|
context.Books.Remove(book);
|
|||
|
context.SaveChanges();
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
throw new Exception("Книга не найдена");
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|