using HotelContracts.BindingModels; using HotelContracts.SearchModels; using HotelContracts.StoragesContracts; using HotelContracts.ViewModels; using HotelDatabaseImplement.Models; namespace HotelDatabaseImplement.Implements; public class CleaningStorage : ICleaningStorage { public List GetFullList() { using var context = new HotelDataBase(); return context.Cleanings .Select(x => x.GetView) .ToList(); } public List GetFilteredList(CleaningSearchModel model) { if (!model.To.HasValue || !model.From.HasValue) return new List(); using var context = new HotelDataBase(); return context.Cleanings .Where(x => x.Date >= model.From && x.Date <= model.To) .Select(x => x.GetView) .ToList(); } public CleaningViewModel? GetElement(CleaningSearchModel model) { using var context = new HotelDataBase(); if (model.RoomId.HasValue) { return context.Cleanings.FirstOrDefault(x => x.RoomId == model.RoomId)?.GetView; } return !model.Id.HasValue ? null : context.Cleanings.FirstOrDefault(x => x.Id == model.Id)?.GetView; } public CleaningViewModel? Insert(CleaningBindingModel model) { using var context = new HotelDataBase(); var item = Cleaning.Create(context, model); context.Cleanings.Add(item); context.SaveChanges(); return item.GetView; } public CleaningViewModel? Update(CleaningBindingModel model) { using var context = new HotelDataBase(); var item = context.Cleanings.FirstOrDefault(x => x.Id == model.Id); if (item == null) return null; item.Update(model); context.SaveChanges(); return item.GetView; } public CleaningViewModel? Delete(CleaningBindingModel model) { using var context = new HotelDataBase(); var item = context.Cleanings.FirstOrDefault(x => x.Id == model.Id); if (item == null) return null; context.Cleanings.Remove(item); context.SaveChanges(); return item.GetView; } }