diff --git a/SushiBarDatabaseImplement/Implements/ComponentStorage.cs b/SushiBarDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..c10183b --- /dev/null +++ b/SushiBarDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SushiBarDatabaseImplement.Implements +{ + public class ComponentStorage + { + } +} diff --git a/SushiBarDatabaseImplement/Models/Order.cs b/SushiBarDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..21d251d --- /dev/null +++ b/SushiBarDatabaseImplement/Models/Order.cs @@ -0,0 +1,62 @@ +using SushiBarContracts.BindingModel; +using SushiBarContracts.ViewModels; +using SushiBarDataModels; +using SushiBarDataModels.Enums; +using System.ComponentModel.DataAnnotations; + +namespace SushiBarDatabaseImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; set; } + [Required] + public int SushiId { get; set; } + [Required] + public int Count { get; set; } + [Required] + public double Sum { get; set; } + [Required] + public OrderStatus Status { get; set; } + [Required] + public DateTime DateCreate { get; set; } + public DateTime? DateImplement { get; set; } + + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + SushiId = model.SushiId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement + }; + } + + public void Update(OrderBindingModel model) + { + if (model == null) + { + return; + } + Status = model.Status; + DateImplement = model.DateImplement; + } + public OrderViewModel GetViewModel => new() + { + Id = Id, + SushiId = SushiId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + }; + } +} diff --git a/SushiBarDatabaseImplement/Models/Sushi.cs b/SushiBarDatabaseImplement/Models/Sushi.cs index 52a7371..9e3a64e 100644 --- a/SushiBarDatabaseImplement/Models/Sushi.cs +++ b/SushiBarDatabaseImplement/Models/Sushi.cs @@ -22,9 +22,7 @@ namespace SushiBarDatabaseImplement.Models { if (_sushiComponents == null) { - _sushiComponents = Components - .ToDictionary(recPC => recPC.ComponentId, recPC => - (recPC.Component as IComponentModel, recPC.Count)); + _sushiComponents = Components.ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count)); } return _sushiComponents; } @@ -42,8 +40,7 @@ namespace SushiBarDatabaseImplement.Models Price = model.Price, Components = model.SushiComponents.Select(x => new SushiComponent { - Component = context.Components.First(y => y.Id == x.Key), - Count = x.Value.Item2 + Component = context.Components.First(y => y.Id == x.Key), Count = x.Value.Item2 }).ToList() }; } @@ -64,8 +61,7 @@ namespace SushiBarDatabaseImplement.Models 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.SushiComponents.RemoveRange(SushiComponents.Where(rec => !model.SushiComponents.ContainsKey(rec.ComponentId))); context.SaveChanges(); // обновили количество у существующих записей foreach (var updateComponent in SushiComponents) diff --git a/SushiBarDatabaseImplement/SushiBarDatabase.cs b/SushiBarDatabaseImplement/SushiBarDatabase.cs new file mode 100644 index 0000000..5ea8f02 --- /dev/null +++ b/SushiBarDatabaseImplement/SushiBarDatabase.cs @@ -0,0 +1,24 @@ +using Microsoft.EntityFrameworkCore; +using SushiBarDatabaseImplement.Models; + +namespace SushiBarDatabaseImplement +{ + public class SushiBarDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Server=localhost\SQLEXPRESS;Database=master;Trusted_Connection=True;ESS; + Initial Catalog=AbstractShopDatabaseFull;Integrated + Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True" + ); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Components { set; get; } + public virtual DbSet Sushis { set; get; } + public virtual DbSet SushiComponents { set; get; } + public virtual DbSet Orders { set; get; } + } +}