Completed lab 8
This commit is contained in:
parent
324abc8c74
commit
68cb1e64d1
@ -1,18 +1,24 @@
|
|||||||
using System.ComponentModel.DataAnnotations;
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using System.Runtime.Serialization;
|
||||||
using SushiBarContracts.BindingModels;
|
using SushiBarContracts.BindingModels;
|
||||||
using SushiBarContracts.ViewModels;
|
using SushiBarContracts.ViewModels;
|
||||||
using SushiBarDataModels.Models;
|
using SushiBarDataModels.Models;
|
||||||
|
|
||||||
namespace SushiBarDatabaseImplement.Models;
|
namespace SushiBarDatabaseImplement.Models;
|
||||||
|
|
||||||
|
[DataContract]
|
||||||
public class Client : IClientModel
|
public class Client : IClientModel
|
||||||
{
|
{
|
||||||
|
[DataMember]
|
||||||
public int Id { get; private init; }
|
public int Id { get; private init; }
|
||||||
[Required]
|
[Required]
|
||||||
|
[DataMember]
|
||||||
public string ClientFio { get; private set; } = string.Empty;
|
public string ClientFio { get; private set; } = string.Empty;
|
||||||
[Required]
|
[Required]
|
||||||
|
[DataMember]
|
||||||
public string Email { get; private set; } = string.Empty;
|
public string Email { get; private set; } = string.Empty;
|
||||||
[Required]
|
[Required]
|
||||||
|
[DataMember]
|
||||||
public string Password { get; private set; } = string.Empty;
|
public string Password { get; private set; } = string.Empty;
|
||||||
|
|
||||||
public static Client? Create(ClientBindingModel? model)
|
public static Client? Create(ClientBindingModel? model)
|
||||||
|
@ -3,55 +3,59 @@ using SushiBarContracts.ViewModels;
|
|||||||
using SushiBarDataModels.Models;
|
using SushiBarDataModels.Models;
|
||||||
using System.ComponentModel.DataAnnotations.Schema;
|
using System.ComponentModel.DataAnnotations.Schema;
|
||||||
using System.ComponentModel.DataAnnotations;
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using System.Runtime.Serialization;
|
||||||
|
|
||||||
namespace SushiBarDatabaseImplement.Models
|
namespace SushiBarDatabaseImplement.Models;
|
||||||
|
|
||||||
|
[DataContract]
|
||||||
|
public class Component : IComponentModel
|
||||||
{
|
{
|
||||||
public class Component : IComponentModel
|
[DataMember]
|
||||||
|
public int Id { get; private set; }
|
||||||
|
[Required]
|
||||||
|
[DataMember]
|
||||||
|
public string ComponentName { get; private set; } = string.Empty;
|
||||||
|
[Required]
|
||||||
|
[DataMember]
|
||||||
|
public double Cost { get; set; }
|
||||||
|
[ForeignKey("ComponentId")]
|
||||||
|
public virtual List<SushiComponent> SushiComponent { get; set; } = new();
|
||||||
|
public static Component? Create(ComponentBindingModel model)
|
||||||
{
|
{
|
||||||
public int Id { get; private set; }
|
if (model == null)
|
||||||
[Required]
|
|
||||||
public string ComponentName { get; private set; } = string.Empty;
|
|
||||||
[Required]
|
|
||||||
public double Cost { get; set; }
|
|
||||||
[ForeignKey("ComponentId")]
|
|
||||||
public virtual List<SushiComponent> SushiComponent { get; set; } = new();
|
|
||||||
public static Component? Create(ComponentBindingModel model)
|
|
||||||
{
|
{
|
||||||
if (model == null)
|
return null;
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return new Component()
|
|
||||||
{
|
|
||||||
Id = model.Id,
|
|
||||||
ComponentName = model.ComponentName,
|
|
||||||
Cost = model.Cost
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
public static Component Create(ComponentViewModel model)
|
return new Component()
|
||||||
{
|
{
|
||||||
return new Component
|
Id = model.Id,
|
||||||
{
|
ComponentName = model.ComponentName,
|
||||||
Id = model.Id,
|
Cost = model.Cost
|
||||||
ComponentName = model.ComponentName,
|
|
||||||
Cost = model.Cost
|
|
||||||
};
|
|
||||||
}
|
|
||||||
public void Update(ComponentBindingModel model)
|
|
||||||
{
|
|
||||||
if (model == null)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
ComponentName = model.ComponentName;
|
|
||||||
Cost = model.Cost;
|
|
||||||
}
|
|
||||||
public ComponentViewModel GetViewModel => new()
|
|
||||||
{
|
|
||||||
Id = Id,
|
|
||||||
ComponentName = ComponentName,
|
|
||||||
Cost = Cost
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
public static Component Create(ComponentViewModel model)
|
||||||
|
{
|
||||||
|
return new Component
|
||||||
|
{
|
||||||
|
Id = model.Id,
|
||||||
|
ComponentName = model.ComponentName,
|
||||||
|
Cost = model.Cost
|
||||||
|
};
|
||||||
|
}
|
||||||
|
public void Update(ComponentBindingModel model)
|
||||||
|
{
|
||||||
|
if (model == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
ComponentName = model.ComponentName;
|
||||||
|
Cost = model.Cost;
|
||||||
|
}
|
||||||
|
public ComponentViewModel GetViewModel => new()
|
||||||
|
{
|
||||||
|
Id = Id,
|
||||||
|
ComponentName = ComponentName,
|
||||||
|
Cost = Cost
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
@ -1,17 +1,20 @@
|
|||||||
using System.ComponentModel.DataAnnotations;
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using System.Runtime.Serialization;
|
||||||
using SushiBarContracts.BindingModels;
|
using SushiBarContracts.BindingModels;
|
||||||
using SushiBarContracts.ViewModels;
|
using SushiBarContracts.ViewModels;
|
||||||
using SushiBarDataModels.Models;
|
using SushiBarDataModels.Models;
|
||||||
|
|
||||||
namespace SushiBarDatabaseImplement.Models;
|
namespace SushiBarDatabaseImplement.Models;
|
||||||
|
|
||||||
|
[DataContract]
|
||||||
public class Implementer : IImplementerModel
|
public class Implementer : IImplementerModel
|
||||||
{
|
{
|
||||||
|
[DataMember]
|
||||||
public int Id { get; private init; }
|
public int Id { get; private init; }
|
||||||
[Required] public string ImplementerFio { get; private set; } = string.Empty;
|
[Required] [DataMember] public string ImplementerFio { get; private set; } = string.Empty;
|
||||||
[Required] public string Password { get; private set; } = string.Empty;
|
[Required] [DataMember] public string Password { get; private set; } = string.Empty;
|
||||||
public int WorkExperience { get; private set; }
|
[DataMember] public int WorkExperience { get; private set; }
|
||||||
public int Qualification { get; private set; }
|
[DataMember] public int Qualification { get; private set; }
|
||||||
|
|
||||||
public static Implementer? Create(ImplementerBindingModel? model)
|
public static Implementer? Create(ImplementerBindingModel? model)
|
||||||
{
|
{
|
||||||
|
@ -1,18 +1,23 @@
|
|||||||
using System.ComponentModel.DataAnnotations;
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using System.Runtime.Serialization;
|
||||||
using SushiBarContracts.BindingModels;
|
using SushiBarContracts.BindingModels;
|
||||||
using SushiBarContracts.ViewModels;
|
using SushiBarContracts.ViewModels;
|
||||||
using SushiBarDataModels.Models;
|
using SushiBarDataModels.Models;
|
||||||
|
|
||||||
namespace SushiBarDatabaseImplement.Models;
|
namespace SushiBarDatabaseImplement.Models;
|
||||||
|
|
||||||
|
[DataContract]
|
||||||
public class Message : IMessageInfoModel
|
public class Message : IMessageInfoModel
|
||||||
{
|
{
|
||||||
[Key]
|
[Key]
|
||||||
|
[DataMember]
|
||||||
public string MessageId { get; private set; } = string.Empty;
|
public string MessageId { get; private set; } = string.Empty;
|
||||||
public int? ClientId { get; private set; }
|
public int? ClientId { get; private set; }
|
||||||
[Required]
|
[Required]
|
||||||
|
[DataMember]
|
||||||
public string SenderName { get; private set; } = string.Empty;
|
public string SenderName { get; private set; } = string.Empty;
|
||||||
[Required]
|
[Required]
|
||||||
|
[DataMember]
|
||||||
public DateTime DateDelivery { get; private set; } = DateTime.Now;
|
public DateTime DateDelivery { get; private set; } = DateTime.Now;
|
||||||
[Required]
|
[Required]
|
||||||
public string Subject { get; private set; } = string.Empty;
|
public string Subject { get; private set; } = string.Empty;
|
||||||
|
@ -3,100 +3,109 @@ using SushiBarContracts.ViewModels;
|
|||||||
using SushiBarDataModels.Enums;
|
using SushiBarDataModels.Enums;
|
||||||
using SushiBarDataModels.Models;
|
using SushiBarDataModels.Models;
|
||||||
using System.ComponentModel.DataAnnotations;
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using System.Runtime.Serialization;
|
||||||
|
|
||||||
namespace SushiBarDatabaseImplement.Models
|
namespace SushiBarDatabaseImplement.Models;
|
||||||
|
|
||||||
|
[DataContract]
|
||||||
|
public class Order : IOrderModel
|
||||||
{
|
{
|
||||||
public class Order : IOrderModel
|
[DataMember]
|
||||||
|
public int Id { get; private set; }
|
||||||
|
|
||||||
|
[Required]
|
||||||
|
[DataMember]
|
||||||
|
public int SushiId { get; private set; }
|
||||||
|
|
||||||
|
[Required]
|
||||||
|
[DataMember]
|
||||||
|
public int ClientId { get; private set; }
|
||||||
|
[DataMember]
|
||||||
|
public int? ImplementerId { get; private set; }
|
||||||
|
[DataMember]
|
||||||
|
public string SushiName { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
[Required]
|
||||||
|
[DataMember]
|
||||||
|
public int Count { get; private set; }
|
||||||
|
|
||||||
|
[Required]
|
||||||
|
[DataMember]
|
||||||
|
public double Sum { get; private set; }
|
||||||
|
|
||||||
|
[Required]
|
||||||
|
[DataMember]
|
||||||
|
public OrderStatus Status { get; private set; } = OrderStatus.Unknown;
|
||||||
|
|
||||||
|
[Required]
|
||||||
|
[DataMember]
|
||||||
|
public DateTime DateCreate { get; private set; } = DateTime.Now;
|
||||||
|
|
||||||
|
public DateTime? DateImplement { get; private set; }
|
||||||
|
|
||||||
|
public virtual Sushi Sushi { get; set; }
|
||||||
|
|
||||||
|
public virtual Client Client { get; set; }
|
||||||
|
|
||||||
|
public virtual Implementer? Implementer { get; private set; }
|
||||||
|
|
||||||
|
public static Order? Create(OrderBindingModel? model)
|
||||||
{
|
{
|
||||||
public int Id { get; private set; }
|
if (model == null)
|
||||||
|
|
||||||
[Required]
|
|
||||||
public int SushiId { get; private set; }
|
|
||||||
|
|
||||||
[Required]
|
|
||||||
public int ClientId { get; private set; }
|
|
||||||
public int? ImplementerId { get; private set; } = null;
|
|
||||||
|
|
||||||
public string SushiName { get; set; } = string.Empty;
|
|
||||||
|
|
||||||
[Required]
|
|
||||||
public int Count { get; private set; }
|
|
||||||
|
|
||||||
[Required]
|
|
||||||
public double Sum { get; private set; }
|
|
||||||
|
|
||||||
[Required]
|
|
||||||
public OrderStatus Status { get; private set; } = OrderStatus.Unknown;
|
|
||||||
|
|
||||||
[Required]
|
|
||||||
public DateTime DateCreate { get; private set; } = DateTime.Now;
|
|
||||||
|
|
||||||
public DateTime? DateImplement { get; private set; }
|
|
||||||
|
|
||||||
public virtual Sushi Sushi { get; set; }
|
|
||||||
|
|
||||||
public virtual Client Client { get; set; }
|
|
||||||
|
|
||||||
public virtual Implementer? Implementer { get; private set; }
|
|
||||||
|
|
||||||
public static Order? Create(OrderBindingModel? model)
|
|
||||||
{
|
{
|
||||||
if (model == null)
|
return null;
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return new Order
|
|
||||||
{
|
|
||||||
Id = model.Id,
|
|
||||||
SushiId = model.SushiId,
|
|
||||||
SushiName = model.SushiName,
|
|
||||||
ClientId = model.ClientId,
|
|
||||||
ImplementerId = model.ImplementerId,
|
|
||||||
Count = model.Count,
|
|
||||||
Sum = model.Sum,
|
|
||||||
Status = model.Status,
|
|
||||||
DateCreate = model.DateCreate,
|
|
||||||
DateImplement = model.DateImplement
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Update(OrderBindingModel? model)
|
return new Order
|
||||||
{
|
{
|
||||||
if (model == null)
|
Id = model.Id,
|
||||||
{
|
SushiId = model.SushiId,
|
||||||
return;
|
SushiName = model.SushiName,
|
||||||
}
|
ClientId = model.ClientId,
|
||||||
|
ImplementerId = model.ImplementerId,
|
||||||
SushiId = model.SushiId;
|
Count = model.Count,
|
||||||
SushiName = model.SushiName;
|
Sum = model.Sum,
|
||||||
ClientId = model.ClientId;
|
Status = model.Status,
|
||||||
ImplementerId = model.ImplementerId;
|
DateCreate = model.DateCreate,
|
||||||
Count = model.Count;
|
DateImplement = model.DateImplement
|
||||||
Sum = model.Sum;
|
};
|
||||||
Status = model.Status;
|
|
||||||
DateCreate = model.DateCreate;
|
|
||||||
DateImplement = model.DateImplement;
|
|
||||||
}
|
|
||||||
|
|
||||||
public OrderViewModel GetViewModel { get
|
|
||||||
{
|
|
||||||
var context = new SushiBarDatabase();
|
|
||||||
return new OrderViewModel
|
|
||||||
{
|
|
||||||
Id = Id,
|
|
||||||
ClientId = ClientId,
|
|
||||||
SushiId = SushiId,
|
|
||||||
Count = Count,
|
|
||||||
DateCreate = DateCreate,
|
|
||||||
DateImplement = DateImplement,
|
|
||||||
ImplementerId = ImplementerId,
|
|
||||||
Sum = Sum,
|
|
||||||
Status = Status,
|
|
||||||
ClientFio = context.Clients.FirstOrDefault(x => x.Id == ClientId)?.ClientFio ?? string.Empty,
|
|
||||||
SushiName = context.Sushi.FirstOrDefault(x => x.Id == SushiId)?.SushiName ?? string.Empty,
|
|
||||||
ImplementerFio = context.Implementers.FirstOrDefault(x => x.Id == ImplementerId)?.ImplementerFio ?? string.Empty,
|
|
||||||
};
|
|
||||||
} }
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
public void Update(OrderBindingModel? model)
|
||||||
|
{
|
||||||
|
if (model == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
SushiId = model.SushiId;
|
||||||
|
SushiName = model.SushiName;
|
||||||
|
ClientId = model.ClientId;
|
||||||
|
ImplementerId = model.ImplementerId;
|
||||||
|
Count = model.Count;
|
||||||
|
Sum = model.Sum;
|
||||||
|
Status = model.Status;
|
||||||
|
DateCreate = model.DateCreate;
|
||||||
|
DateImplement = model.DateImplement;
|
||||||
|
}
|
||||||
|
|
||||||
|
public OrderViewModel GetViewModel { get
|
||||||
|
{
|
||||||
|
var context = new SushiBarDatabase();
|
||||||
|
return new OrderViewModel
|
||||||
|
{
|
||||||
|
Id = Id,
|
||||||
|
ClientId = ClientId,
|
||||||
|
SushiId = SushiId,
|
||||||
|
Count = Count,
|
||||||
|
DateCreate = DateCreate,
|
||||||
|
DateImplement = DateImplement,
|
||||||
|
ImplementerId = ImplementerId,
|
||||||
|
Sum = Sum,
|
||||||
|
Status = Status,
|
||||||
|
ClientFio = context.Clients.FirstOrDefault(x => x.Id == ClientId)?.ClientFio ?? string.Empty,
|
||||||
|
SushiName = context.Sushi.FirstOrDefault(x => x.Id == SushiId)?.SushiName ?? string.Empty,
|
||||||
|
ImplementerFio = context.Implementers.FirstOrDefault(x => x.Id == ImplementerId)?.ImplementerFio ?? string.Empty,
|
||||||
|
};
|
||||||
|
} }
|
||||||
|
}
|
@ -1,88 +1,92 @@
|
|||||||
using SushiBarDataModels.Models;
|
using SushiBarDataModels.Models;
|
||||||
using System.ComponentModel.DataAnnotations.Schema;
|
using System.ComponentModel.DataAnnotations.Schema;
|
||||||
using System.ComponentModel.DataAnnotations;
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using System.Runtime.Serialization;
|
||||||
using SushiBarContracts.BindingModels;
|
using SushiBarContracts.BindingModels;
|
||||||
using SushiBarContracts.ViewModels;
|
using SushiBarContracts.ViewModels;
|
||||||
|
|
||||||
namespace SushiBarDatabaseImplement.Models
|
namespace SushiBarDatabaseImplement.Models;
|
||||||
{
|
|
||||||
public class Sushi : ISushiModel
|
|
||||||
{
|
|
||||||
public int Id { get; set; }
|
|
||||||
[Required]
|
|
||||||
public string SushiName { get; set; } = string.Empty;
|
|
||||||
[Required]
|
|
||||||
public double Price { get; set; }
|
|
||||||
private Dictionary<int, (IComponentModel, int)>? _sushiComponents = null;
|
|
||||||
[NotMapped]
|
|
||||||
public Dictionary<int, (IComponentModel, int)> SushiComponents
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
_sushiComponents ??= Components
|
|
||||||
.ToDictionary(recPC => recPC.ComponentId, recPC =>
|
|
||||||
(recPC.Component as IComponentModel, recPC.Count));
|
|
||||||
return _sushiComponents;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
[ForeignKey("SushiId")]
|
|
||||||
public virtual List<SushiComponent> Components { get; set; } = new();
|
|
||||||
[ForeignKey("SushiId")]
|
|
||||||
public virtual List<Order> Orders { get; set; } = new();
|
|
||||||
public static Sushi Create(SushiBarDatabase context, SushiBindingModel model)
|
|
||||||
{
|
|
||||||
return new Sushi()
|
|
||||||
{
|
|
||||||
Id = model.Id,
|
|
||||||
SushiName = model.SushiName,
|
|
||||||
Price = model.Price,
|
|
||||||
Components = model.SushiComponents.Select(x => new SushiComponent
|
|
||||||
{
|
|
||||||
Component = context.Components.First(y => y.Id == x.Key),
|
|
||||||
Count = x.Value.Item2
|
|
||||||
}).ToList()
|
|
||||||
};
|
|
||||||
}
|
|
||||||
public void Update(SushiBindingModel model)
|
|
||||||
{
|
|
||||||
SushiName = model.SushiName;
|
|
||||||
Price = model.Price;
|
|
||||||
}
|
|
||||||
public SushiViewModel GetViewModel => new()
|
|
||||||
{
|
|
||||||
Id = Id,
|
|
||||||
SushiName = SushiName,
|
|
||||||
Price = Price,
|
|
||||||
SushiComponents = SushiComponents
|
|
||||||
};
|
|
||||||
public void UpdateComponents(SushiBarDatabase context, SushiBindingModel model)
|
|
||||||
{
|
|
||||||
var sushiComponents = context.SushiComponents.Where(rec => rec.SushiId == model.Id).ToList();
|
|
||||||
if (sushiComponents != null && sushiComponents.Count > 0)
|
|
||||||
{
|
|
||||||
context.SushiComponents.RemoveRange(sushiComponents.Where(rec => !model.SushiComponents.ContainsKey(rec.ComponentId)));
|
|
||||||
context.SaveChanges();
|
|
||||||
|
|
||||||
foreach (var updateComponent in sushiComponents)
|
|
||||||
{
|
|
||||||
updateComponent.Count = model.SushiComponents[updateComponent.ComponentId].Item2;
|
|
||||||
model.SushiComponents.Remove(updateComponent.ComponentId);
|
|
||||||
}
|
|
||||||
context.SaveChanges();
|
|
||||||
}
|
|
||||||
var sushi = context.Sushi.First(x => x.Id == Id);
|
|
||||||
foreach (var pc in model.SushiComponents)
|
|
||||||
{
|
|
||||||
context.SushiComponents.Add(new SushiComponent
|
|
||||||
{
|
|
||||||
Sushi = sushi,
|
|
||||||
Component = context.Components.First(x => x.Id == pc.Key),
|
|
||||||
Count = pc.Value.Item2
|
|
||||||
});
|
|
||||||
context.SaveChanges();
|
|
||||||
}
|
|
||||||
_sushiComponents = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
[DataContract]
|
||||||
|
public class Sushi : ISushiModel
|
||||||
|
{
|
||||||
|
[DataMember]
|
||||||
|
public int Id { get; set; }
|
||||||
|
[Required]
|
||||||
|
[DataMember]
|
||||||
|
public string SushiName { get; set; } = string.Empty;
|
||||||
|
[Required]
|
||||||
|
[DataMember]
|
||||||
|
public double Price { get; set; }
|
||||||
|
private Dictionary<int, (IComponentModel, int)>? _sushiComponents = null;
|
||||||
|
[NotMapped]
|
||||||
|
public Dictionary<int, (IComponentModel, int)> SushiComponents
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
_sushiComponents ??= Components
|
||||||
|
.ToDictionary(recPC => recPC.ComponentId, recPC =>
|
||||||
|
(recPC.Component as IComponentModel, recPC.Count));
|
||||||
|
return _sushiComponents;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
[ForeignKey("SushiId")]
|
||||||
|
public virtual List<SushiComponent> Components { get; set; } = new();
|
||||||
|
[ForeignKey("SushiId")]
|
||||||
|
public virtual List<Order> Orders { get; set; } = new();
|
||||||
|
public static Sushi Create(SushiBarDatabase context, SushiBindingModel model)
|
||||||
|
{
|
||||||
|
return new Sushi()
|
||||||
|
{
|
||||||
|
Id = model.Id,
|
||||||
|
SushiName = model.SushiName,
|
||||||
|
Price = model.Price,
|
||||||
|
Components = model.SushiComponents.Select(x => new SushiComponent
|
||||||
|
{
|
||||||
|
Component = context.Components.First(y => y.Id == x.Key),
|
||||||
|
Count = x.Value.Item2
|
||||||
|
}).ToList()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
public void Update(SushiBindingModel model)
|
||||||
|
{
|
||||||
|
SushiName = model.SushiName;
|
||||||
|
Price = model.Price;
|
||||||
|
}
|
||||||
|
public SushiViewModel GetViewModel => new()
|
||||||
|
{
|
||||||
|
Id = Id,
|
||||||
|
SushiName = SushiName,
|
||||||
|
Price = Price,
|
||||||
|
SushiComponents = SushiComponents
|
||||||
|
};
|
||||||
|
public void UpdateComponents(SushiBarDatabase context, SushiBindingModel model)
|
||||||
|
{
|
||||||
|
var sushiComponents = context.SushiComponents.Where(rec => rec.SushiId == model.Id).ToList();
|
||||||
|
if (sushiComponents != null && sushiComponents.Count > 0)
|
||||||
|
{
|
||||||
|
context.SushiComponents.RemoveRange(sushiComponents.Where(rec => !model.SushiComponents.ContainsKey(rec.ComponentId)));
|
||||||
|
context.SaveChanges();
|
||||||
|
|
||||||
|
foreach (var updateComponent in sushiComponents)
|
||||||
|
{
|
||||||
|
updateComponent.Count = model.SushiComponents[updateComponent.ComponentId].Item2;
|
||||||
|
model.SushiComponents.Remove(updateComponent.ComponentId);
|
||||||
|
}
|
||||||
|
context.SaveChanges();
|
||||||
|
}
|
||||||
|
var sushi = context.Sushi.First(x => x.Id == Id);
|
||||||
|
foreach (var pc in model.SushiComponents)
|
||||||
|
{
|
||||||
|
context.SushiComponents.Add(new SushiComponent
|
||||||
|
{
|
||||||
|
Sushi = sushi,
|
||||||
|
Component = context.Components.First(x => x.Id == pc.Key),
|
||||||
|
Count = pc.Value.Item2
|
||||||
|
});
|
||||||
|
context.SaveChanges();
|
||||||
|
}
|
||||||
|
_sushiComponents = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,17 +1,16 @@
|
|||||||
using System.ComponentModel.DataAnnotations;
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
namespace SushiBarDatabaseImplement.Models
|
namespace SushiBarDatabaseImplement.Models;
|
||||||
|
|
||||||
|
public class SushiComponent
|
||||||
{
|
{
|
||||||
public class SushiComponent
|
public int Id { get; set; }
|
||||||
{
|
[Required]
|
||||||
public int Id { get; set; }
|
public int SushiId { get; set; }
|
||||||
[Required]
|
[Required]
|
||||||
public int SushiId { get; set; }
|
public int ComponentId { get; set; }
|
||||||
[Required]
|
[Required]
|
||||||
public int ComponentId { get; set; }
|
public int Count { get; set; }
|
||||||
[Required]
|
public virtual Component Component { get; set; } = new();
|
||||||
public int Count { get; set; }
|
public virtual Sushi Sushi { get; set; } = new();
|
||||||
public virtual Component Component { get; set; } = new();
|
}
|
||||||
public virtual Sushi Sushi { get; set; } = new();
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user