using LibraryContracts.StorageContracts; using LibraryDatabase.Models; using LibraryDataModels.Dtos; using LibraryDataModels.Views; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LibraryDatabase.Storages { public class BookStorage : IBookStorage { public List<BookView> GetFullList() { using var context = new LibraryDatabaseContext(); return context.Books .Select(x => x.GetView) .ToList(); } public BookView? GetElement(BookDto model) { using var context = new LibraryDatabaseContext(); return context.Books .FirstOrDefault(x => x.Id == model.Id) ?.GetView; } public BookView? Insert(BookDto model) { var newBook = Book.Create(model); if (newBook == null) { return null; } using var context = new LibraryDatabaseContext(); context.Books.Add(newBook); context.SaveChanges(); return newBook.GetView; } public BookView? Update(BookDto model) { using var context = new LibraryDatabaseContext(); var Book = context.Books.FirstOrDefault(x => x.Id == model.Id); if (Book == null) { return null; } Book.Update(model); context.SaveChanges(); return Book.GetView; } public BookView? Delete(BookDto model) { using var context = new LibraryDatabaseContext(); var element = context.Books.FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { context.Books.Remove(element); context.SaveChanges(); return element.GetView; } return null; } } }