PIbd-22_Bazunov_AI_Hotel/Hotel/HotelDatabaseImplement/Implements/CleaningStorage.cs
2023-05-19 17:52:18 +04:00

74 lines
2.2 KiB
C#

using HotelContracts.BindingModels;
using HotelContracts.SearchModels;
using HotelContracts.StoragesContracts;
using HotelContracts.ViewModels;
using HotelDatabaseImplement.Models;
namespace HotelDatabaseImplement.Implements;
public class CleaningStorage : ICleaningStorage
{
public List<CleaningViewModel> GetFullList()
{
using var context = new HotelDataBase();
return context.Cleanings
.Select(x => x.GetView)
.ToList();
}
public List<CleaningViewModel> GetFilteredList(CleaningSearchModel model)
{
if (!model.To.HasValue || !model.From.HasValue)
return new List<CleaningViewModel>();
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;
}
}