закончена папка Models и начата Implements

This commit is contained in:
ekallin 2024-03-09 21:37:08 +04:00
parent 2510a97130
commit cb4cd1184b
4 changed files with 101 additions and 7 deletions

View File

@ -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
{
}
}

View File

@ -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,
};
}
}

View File

@ -22,9 +22,7 @@ namespace SushiBarDatabaseImplement.Models
{ {
if (_sushiComponents == null) if (_sushiComponents == null)
{ {
_sushiComponents = Components _sushiComponents = Components.ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count));
.ToDictionary(recPC => recPC.ComponentId, recPC =>
(recPC.Component as IComponentModel, recPC.Count));
} }
return _sushiComponents; return _sushiComponents;
} }
@ -42,8 +40,7 @@ namespace SushiBarDatabaseImplement.Models
Price = model.Price, Price = model.Price,
Components = model.SushiComponents.Select(x => new SushiComponent Components = model.SushiComponents.Select(x => new SushiComponent
{ {
Component = context.Components.First(y => y.Id == x.Key), Component = context.Components.First(y => y.Id == x.Key), Count = x.Value.Item2
Count = x.Value.Item2
}).ToList() }).ToList()
}; };
} }
@ -64,8 +61,7 @@ namespace SushiBarDatabaseImplement.Models
var SushiComponents = context.SushiComponents.Where(rec => rec.SushiId == model.Id).ToList(); var SushiComponents = context.SushiComponents.Where(rec => rec.SushiId == model.Id).ToList();
if (SushiComponents != null && SushiComponents.Count > 0) if (SushiComponents != null && SushiComponents.Count > 0)
{ // удалили те, которых нет в модели { // удалили те, которых нет в модели
context.SushiComponents.RemoveRange(SushiComponents.Where(rec context.SushiComponents.RemoveRange(SushiComponents.Where(rec => !model.SushiComponents.ContainsKey(rec.ComponentId)));
=> !model.SushiComponents.ContainsKey(rec.ComponentId)));
context.SaveChanges(); context.SaveChanges();
// обновили количество у существующих записей // обновили количество у существующих записей
foreach (var updateComponent in SushiComponents) foreach (var updateComponent in SushiComponents)

View File

@ -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<Component> Components { set; get; }
public virtual DbSet<Sushi> Sushis { set; get; }
public virtual DbSet<SushiComponent> SushiComponents { set; get; }
public virtual DbSet<Order> Orders { set; get; }
}
}