2023-04-05 11:31:08 +04:00
|
|
|
|
using HotelContracts.BindingModels;
|
|
|
|
|
using HotelContracts.ViewModels;
|
|
|
|
|
using HotelDataModels.Models;
|
2023-04-02 14:51:29 +04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2023-04-05 11:31:08 +04:00
|
|
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
2023-04-02 14:51:29 +04:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace HotelDataBaseImplement.Models
|
|
|
|
|
{
|
|
|
|
|
public class Room : IRoomModel
|
|
|
|
|
{
|
2023-04-05 11:31:08 +04:00
|
|
|
|
[Required]
|
|
|
|
|
public string RoomName { get; set; } = string.Empty;
|
|
|
|
|
[Required]
|
|
|
|
|
public string RoomFrame { get; set; } = string.Empty;
|
|
|
|
|
[Required]
|
|
|
|
|
public double RoomPrice { get; set; }
|
|
|
|
|
public int HeadwaiterId { get; private set; }
|
|
|
|
|
public int MealPlanId { get; private set; }
|
|
|
|
|
public int Id { get; private set; }
|
2023-04-02 14:51:29 +04:00
|
|
|
|
|
2023-04-05 11:31:08 +04:00
|
|
|
|
public virtual Headwaiter Headwaiter { get; set; }
|
|
|
|
|
public virtual MealPlan MealPlan { get; set; }
|
2023-04-02 14:51:29 +04:00
|
|
|
|
|
2023-04-05 11:31:08 +04:00
|
|
|
|
[ForeignKey("RoomId")]
|
|
|
|
|
public virtual List<RoomDinner> Dinners { get; set; }
|
2023-04-02 14:51:29 +04:00
|
|
|
|
|
2023-04-05 11:31:08 +04:00
|
|
|
|
private Dictionary<int, IDinnerModel> _roomDinners = null;
|
|
|
|
|
public Dictionary<int, IDinnerModel> RoomDinners
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_roomDinners == null)
|
|
|
|
|
{
|
|
|
|
|
_roomDinners = Dinners.ToDictionary(recPC => recPC.DinnerId, recPC => (recPC.Dinner as IDinnerModel));
|
|
|
|
|
}
|
|
|
|
|
return _roomDinners;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-02 14:51:29 +04:00
|
|
|
|
|
2023-04-05 11:31:08 +04:00
|
|
|
|
public static Room Create(HotelDataBase context, RoomBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
return new Room()
|
|
|
|
|
{
|
|
|
|
|
Id = model.Id,
|
|
|
|
|
RoomName = model.RoomName,
|
|
|
|
|
RoomFrame = model.RoomFrame,
|
|
|
|
|
RoomPrice = model.RoomPrice,
|
2023-05-16 01:11:49 +04:00
|
|
|
|
HeadwaiterId = model.HeadwaiterId,
|
|
|
|
|
MealPlanId = model.MealPlanId,
|
2023-04-05 11:31:08 +04:00
|
|
|
|
Dinners = model.RoomDinners.Select(x => new RoomDinner
|
|
|
|
|
{
|
|
|
|
|
Dinner = context.Dinners.First(y => y.Id == x.Key),
|
|
|
|
|
}).ToList()
|
|
|
|
|
};
|
|
|
|
|
}
|
2023-04-02 14:51:29 +04:00
|
|
|
|
|
2023-04-05 11:31:08 +04:00
|
|
|
|
public void Update(RoomBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
RoomName = model.RoomName;
|
|
|
|
|
RoomFrame = model.RoomFrame;
|
|
|
|
|
RoomPrice = model.RoomPrice;
|
2023-05-16 01:11:49 +04:00
|
|
|
|
HeadwaiterId = model.HeadwaiterId;
|
|
|
|
|
MealPlanId = model.MealPlanId;
|
2023-04-05 11:31:08 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public RoomViewModel GetViewModel => new()
|
|
|
|
|
{
|
|
|
|
|
Id = Id,
|
|
|
|
|
RoomName = RoomName,
|
|
|
|
|
RoomFrame = RoomFrame,
|
2023-05-16 01:11:49 +04:00
|
|
|
|
HeadwaiterId = HeadwaiterId,
|
|
|
|
|
MealPlanId = MealPlanId,
|
2023-04-05 11:31:08 +04:00
|
|
|
|
RoomPrice = RoomPrice,
|
|
|
|
|
RoomDinners = RoomDinners
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public void UpdateDinners(HotelDataBase context, RoomBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
var roomDinners = context.RoomDinners.Where(rec => rec.RoomId == model.Id).ToList();
|
|
|
|
|
|
|
|
|
|
if (roomDinners != null)
|
|
|
|
|
{
|
|
|
|
|
context.RoomDinners.RemoveRange(roomDinners.Where(rec => !model.RoomDinners.ContainsKey(rec.DinnerId)));
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var room = context.Rooms.First(x => x.Id == Id);
|
|
|
|
|
|
|
|
|
|
foreach (var cm in model.RoomDinners)
|
|
|
|
|
{
|
|
|
|
|
context.RoomDinners.Add(new RoomDinner
|
|
|
|
|
{
|
|
|
|
|
Room = room,
|
|
|
|
|
Dinner = context.Dinners.First(x => x.Id == cm.Key)
|
|
|
|
|
});
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
_roomDinners = null;
|
|
|
|
|
}
|
2023-04-02 14:51:29 +04:00
|
|
|
|
}
|
|
|
|
|
}
|