2023-04-07 13:46:29 +04:00

70 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 ReservationStorage : IReservationStorage
{
public List<ReservationViewModel> GetFullList()
{
using var context = new HotelDataBase();
return context.Reservations
.Select(x => x.GetView)
.ToList();
}
public List<ReservationViewModel> GetFilteredList(ReservationSearchModel model)
{
if (!model.From.HasValue || !model.To.HasValue)
return new List<ReservationViewModel>();
using var context = new HotelDataBase();
return context.Reservations
.Where(x => x.StartDate >= model.From && x.StartDate <= model.To)
.Select(x => x.GetView)
.ToList();
}
public ReservationViewModel? GetElement(ReservationSearchModel model)
{
if (!model.Id.HasValue) return null;
using var context = new HotelDataBase();
return context.Reservations.FirstOrDefault(x => x.Id == model.Id)?.GetView;
}
public ReservationViewModel? Insert(ReservationBindingModel model)
{
using var context = new HotelDataBase();
var item = Reservation.Create(context, model);
if (item == null) return null;
context.Reservations.Add(item);
context.SaveChanges();
return item.GetView;
}
public ReservationViewModel? Update(ReservationBindingModel model)
{
using var context = new HotelDataBase();
var item = context.Reservations.FirstOrDefault(x => x.Id == model.Id);
if (item == null) return null;
item.Update(model);
context.SaveChanges();
return item.GetView;
}
public ReservationViewModel? Delete(ReservationBindingModel model)
{
using var context = new HotelDataBase();
var item = context.Reservations.FirstOrDefault(x => x.Id == model.Id);
if (item == null) return null;
context.Reservations.Remove(item);
context.SaveChanges();
return item.GetView;
}
}