86 lines
2.2 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;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HotelDataBaseImplement.Models
{
public class Room : IRoomModel
{
public int Id { get; private set; }
[Required]
public int RoomNumber { get; set; }
[Required]
public int CountBeds { get; set; }
[Required]
public double RoomPrice { get; set; }
public int AdministratorId { get; private set; }
public int? MealPlanId { get; private set; }
public virtual Administrator Administrator { get; set; }
public virtual MealPlan? MealPlan { get; set; }
[ForeignKey("RoomId")]
public virtual List<RoomDinner> Dinners { get; set; }
private Dictionary<int, IDinnerModel> _roomDinners = null;
[NotMapped]
public Dictionary<int, IDinnerModel> RoomDinners
{
get
{
if (_roomDinners == null)
{
using var context = new HotelDataBase();
_roomDinners = Dinners
.ToDictionary(x => x.DinnerId, x => (context.Dinners
.FirstOrDefault(y => y.Id == x.DinnerId)! as IDinnerModel));
}
return _roomDinners;
}
}
public static Room Create(HotelDataBase context, RoomBindingModel model)
{
return new Room()
{
Id = model.Id,
RoomNumber = model.RoomNumber,
CountBeds = model.CountBeds,
RoomPrice = model.RoomPrice,
AdministratorId = model.AdministratorId,
MealPlanId = model.MealPlanId,
Dinners = model.RoomDinners.Select(x => new RoomDinner
{
Dinner = context.Dinners.First(y => y.Id == x.Key),
}).ToList()
};
}
public void Update(RoomBindingModel model)
{
RoomNumber = model.RoomNumber;
CountBeds = model.CountBeds;
RoomPrice = model.RoomPrice;
AdministratorId = model.AdministratorId;
MealPlanId = model.MealPlanId;
}
public RoomViewModel GetViewModel => new()
{
Id = Id,
RoomNumber = RoomNumber,
CountBeds = CountBeds,
RoomPrice = RoomPrice,
AdministratorId = AdministratorId,
MealPlanId = MealPlanId,
RoomDinners = RoomDinners
};
}
}