101 lines
3.3 KiB
C#
101 lines
3.3 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)
|
|
{
|
|
using var context = new HotelDataBase();
|
|
if (model.GuestId.HasValue)
|
|
{
|
|
return context.Reservations
|
|
.Where(x => x.GuestId == model.GuestId)
|
|
.Select(x => x.GetView)
|
|
.ToList();
|
|
}
|
|
if (!model.From.HasValue || !model.To.HasValue || !model.GuestId.HasValue)
|
|
return new List<ReservationViewModel>();
|
|
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 void SwitchRoomReserve(Reservation reservation, HotelDataBase context, bool state = true)
|
|
{
|
|
if (reservation.Rooms.Count > 0)
|
|
{
|
|
foreach (var roomItem in reservation.Rooms
|
|
.Select(x => context.Rooms.FirstOrDefault(y => y.Id == x.RoomId)))
|
|
{
|
|
if (roomItem is null) return;
|
|
roomItem.IsReserved = state;
|
|
context.Rooms.Update(roomItem);
|
|
}
|
|
return;
|
|
}
|
|
foreach (var roomItem in reservation.ReservationsRooms
|
|
.Select(x => context.Rooms.FirstOrDefault(y => y.Id == x.Key)))
|
|
{
|
|
if (roomItem is null) return;
|
|
roomItem.IsReserved = state;
|
|
context.Rooms.Update(roomItem);
|
|
}
|
|
}
|
|
|
|
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);
|
|
SwitchRoomReserve(item, context);
|
|
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);
|
|
SwitchRoomReserve(item, context, false);
|
|
context.SaveChanges();
|
|
return item.GetView;
|
|
}
|
|
} |