101 lines
3.1 KiB
C#
101 lines
3.1 KiB
C#
using BankContracts.BindingModels;
|
|
using BankContracts.ViewModels;
|
|
using BankDataModels.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 BankDataBaseImplement.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 ClientId { get; private set; }
|
|
public int AdditionsId { get; private set; }
|
|
public int Id { get; private set; }
|
|
|
|
public virtual Client Client { get; set; }
|
|
public virtual Additions Additions { get; set; }
|
|
|
|
[ForeignKey("RoomId")]
|
|
public virtual List<RoomCredit> Credits { get; set; }
|
|
|
|
private Dictionary<int, ICreditModel> _roomDinners = null;
|
|
public Dictionary<int, ICreditModel> RoomDinners
|
|
{
|
|
get
|
|
{
|
|
if (_roomDinners == null)
|
|
{
|
|
_roomDinners = Credits.ToDictionary(recPC => recPC.DinnerId, recPC => (recPC.Credit as ICreditModel));
|
|
}
|
|
return _roomDinners;
|
|
}
|
|
}
|
|
|
|
public static Room Create(BankDataBase context, RoomBindingModel model)
|
|
{
|
|
return new Room()
|
|
{
|
|
Id = model.Id,
|
|
RoomName = model.RoomName,
|
|
RoomFrame = model.RoomFrame,
|
|
RoomPrice = model.RoomPrice,
|
|
Credits = model.RoomCredits.Select(x => new RoomCredit
|
|
{
|
|
Credit = context.Credits.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(BankDataBase context, RoomBindingModel model)
|
|
{
|
|
var roomDinners = context.RoomCredits.Where(rec => rec.RoomId == model.Id).ToList();
|
|
|
|
if (roomDinners != null)
|
|
{
|
|
context.RoomCredits.RemoveRange(roomDinners.Where(rec => !model.RoomCredits.ContainsKey(rec.DinnerId)));
|
|
context.SaveChanges();
|
|
}
|
|
|
|
var room = context.Rooms.First(x => x.Id == Id);
|
|
|
|
foreach (var cm in model.RoomCredits)
|
|
{
|
|
context.RoomCredits.Add(new RoomCredit
|
|
{
|
|
Room = room,
|
|
Credit = context.Credits.First(x => x.Id == cm.Key)
|
|
});
|
|
context.SaveChanges();
|
|
}
|
|
_roomDinners = null;
|
|
}
|
|
}
|
|
}
|