using LibraryContracts.StorageContracts; using LibraryDatabase; using LibraryDatabase.Models; using LibraryDataModels.Dtos; using LibraryDataModels.Views; using System; using System.Collections.Generic; using System.Linq; using System.Runtime.ConstrainedExecution; using System.Text; using System.Threading.Tasks; namespace LibraryDatabase.Storages { public class AuthorStorage : IAuthorStorage { public List GetFullList() { using var context = new LibraryDatabaseContext(); return context.Authors .Select(x => x.GetView) .ToList(); } public AuthorView? GetElement(AuthorDto model) { using var context = new LibraryDatabaseContext(); return context.Authors .FirstOrDefault(x => x.Id == model.Id) ?.GetView; } public AuthorView? Insert(AuthorDto model) { var newAuthor = Author.Create(model); if (newAuthor == null) { return null; } using var context = new LibraryDatabaseContext(); context.Authors.Add(newAuthor); context.SaveChanges(); return newAuthor.GetView; } public AuthorView? Update(AuthorDto model) { using var context = new LibraryDatabaseContext(); var author = context.Authors.FirstOrDefault(x => x.Id == model.Id); if (author == null) { return null; } author.Update(model); context.SaveChanges(); return author.GetView; } public AuthorView? Delete(AuthorDto model) { using var context = new LibraryDatabaseContext(); var element = context.Authors.FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { context.Authors.Remove(element); context.SaveChanges(); return element.GetView; } return null; } } }