91 lines
2.7 KiB
C#
91 lines
2.7 KiB
C#
|
using System.ComponentModel.DataAnnotations.Schema;
|
|||
|
using HotelContracts.BindingModels;
|
|||
|
using HotelContracts.ViewModels;
|
|||
|
using HotelDataModels.Models;
|
|||
|
|
|||
|
namespace HotelDatabaseImplement.Models;
|
|||
|
|
|||
|
public class Reservation : IReservationModel
|
|||
|
{
|
|||
|
public int Id { get; set; }
|
|||
|
public DateTime StartDate { get; set; }
|
|||
|
public DateTime EndDate { get; set; }
|
|||
|
public int GuestId { get; set; }
|
|||
|
public int MaitreId { get; set; }
|
|||
|
|
|||
|
[ForeignKey("RoomId")]
|
|||
|
public virtual List<ReservationRoom> Rooms { get; set; } = new();
|
|||
|
|
|||
|
private Dictionary<int, IRoomModel>? _reservationsRooms = null;
|
|||
|
public Dictionary<int, IRoomModel> ReservationsRooms
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
_reservationsRooms ??= Rooms
|
|||
|
.ToDictionary(record => record.RoomId, room => room.Room as IRoomModel);
|
|||
|
return _reservationsRooms;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public static Reservation? Create(HotelDataBase dataBase, ReservationBindingModel model)
|
|||
|
{
|
|||
|
return new Reservation
|
|||
|
{
|
|||
|
Id = model.Id,
|
|||
|
StartDate = model.StartDate,
|
|||
|
EndDate = model.EndDate,
|
|||
|
GuestId = model.GuestId,
|
|||
|
MaitreId = model.MaitreId,
|
|||
|
Rooms = model.ReservationsRooms.Select(x => new ReservationRoom
|
|||
|
{
|
|||
|
Room = dataBase.Rooms.First(y => y.Id == x.Key)
|
|||
|
}).ToList()
|
|||
|
};
|
|||
|
}
|
|||
|
|
|||
|
public void Update(ReservationBindingModel model)
|
|||
|
{
|
|||
|
StartDate = model.StartDate;
|
|||
|
EndDate = model.EndDate;
|
|||
|
}
|
|||
|
|
|||
|
public void UpdateRooms(HotelDataBase dataBase, ReservationBindingModel model)
|
|||
|
{
|
|||
|
var rooms = dataBase.ReservationRooms
|
|||
|
.Where(x => x.ReservationId == model.Id)
|
|||
|
.ToList();
|
|||
|
dataBase.ReservationRooms.RemoveRange(
|
|||
|
rooms.Where(x => !model.ReservationsRooms.ContainsKey(x.RoomId))
|
|||
|
);
|
|||
|
|
|||
|
foreach (var toUpdate in rooms)
|
|||
|
{
|
|||
|
model.ReservationsRooms.Remove(toUpdate.RoomId);
|
|||
|
}
|
|||
|
|
|||
|
dataBase.SaveChanges();
|
|||
|
|
|||
|
var reservation = dataBase.Reservations.First(x => x.Id == Id);
|
|||
|
foreach (var reservationRoom in model.ReservationsRooms)
|
|||
|
{
|
|||
|
dataBase.ReservationRooms.Add(new ReservationRoom
|
|||
|
{
|
|||
|
Room = dataBase.Rooms.First(x => x.Id == reservationRoom.Key),
|
|||
|
Reservation = reservation
|
|||
|
});
|
|||
|
dataBase.SaveChanges();
|
|||
|
}
|
|||
|
|
|||
|
_reservationsRooms = null;
|
|||
|
}
|
|||
|
|
|||
|
public ReservationViewModel GetView => new ReservationViewModel
|
|||
|
{
|
|||
|
Id = Id,
|
|||
|
StartDate = StartDate,
|
|||
|
EndDate = EndDate,
|
|||
|
GuestId = GuestId,
|
|||
|
MaitreId = MaitreId,
|
|||
|
ReservationsRooms = ReservationsRooms
|
|||
|
};
|
|||
|
}
|