CourseWork_Hotel/Hotel/HotelDataBaseImplement/Models/Room.cs

101 lines
3.1 KiB
C#
Raw Normal View History

using HotelContracts.BindingModels;
using HotelContracts.ViewModels;
using HotelDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HotelDataBaseImplement.Models
{
public class Room : IRoomModel
{
[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; }
public virtual Headwaiter Headwaiter { get; set; }
public virtual MealPlan MealPlan { get; set; }
[ForeignKey("RoomId")]
public virtual List<RoomDinner> Dinners { get; set; }
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;
}
}
public static Room Create(HotelDataBase context, RoomBindingModel model)
{
return new Room()
{
Id = model.Id,
RoomName = model.RoomName,
RoomFrame = model.RoomFrame,
RoomPrice = model.RoomPrice,
Dinners = model.RoomDinners.Select(x => new RoomDinner
{
Dinner = context.Dinners.First(y => y.Id == x.Key),
}).ToList()
};
}
public void Update(RoomBindingModel model)
{
RoomName = model.RoomName;
RoomFrame = model.RoomFrame;
RoomPrice = model.RoomPrice;
}
public RoomViewModel GetViewModel => new()
{
Id = Id,
RoomName = RoomName,
RoomFrame = RoomFrame,
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;
}
}
}