Промежуточное сохранение.
This commit is contained in:
parent
cc59bf5dd5
commit
9851d284ca
@ -0,0 +1,34 @@
|
||||
using BlacksmithWorkshopDatabaseImplement.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BlacksmithWorkshopDatabaseImplement
|
||||
{
|
||||
public class ManufactureShopDatabase : DbContext
|
||||
{
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
if (optionsBuilder.IsConfigured == false)
|
||||
{
|
||||
optionsBuilder.UseSqlServer(@"Data Source=LAPTOP-CFLH20EE\SQLEXPRESS;Initial Catalog=ManufactureShopDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
|
||||
}
|
||||
base.OnConfiguring(optionsBuilder);
|
||||
}
|
||||
|
||||
public virtual DbSet<WorkPiece> WorkPieces { set; get; }
|
||||
|
||||
public virtual DbSet<Manufacture> Manufactures { set; get; }
|
||||
|
||||
public virtual DbSet<ManufactureWorkPiece> ManufactureWorkPieces { set; get; }
|
||||
|
||||
public virtual DbSet<Order> Orders { set; get; }
|
||||
|
||||
public virtual DbSet<Shop> Shops { set; get; }
|
||||
|
||||
public virtual DbSet<ShopManufacture> ShopManufactures { get; set; }
|
||||
}
|
||||
}
|
@ -47,6 +47,9 @@ namespace BlacksmithWorkshopDatabaseImplement.Models
|
||||
[ForeignKey("ManufactureId")]
|
||||
public virtual List<Order> Orders { get; set; } = new();
|
||||
|
||||
[ForeignKey("ManufactureId")]
|
||||
public virtual List<ShopManufacture> Shops { get; set; } = new();
|
||||
|
||||
public static Manufacture Create(BlacksmithWorkshopDatabase context, ManufactureBindingModel model)
|
||||
{
|
||||
return new Manufacture()
|
||||
|
@ -4,6 +4,7 @@ using BlacksmithWorkshopDataModels.Enums;
|
||||
using BlacksmithWorkshopDataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
@ -15,14 +16,19 @@ namespace BlacksmithWorkshopDatabaseImplement.Models
|
||||
{
|
||||
public int Id { get; private set; }
|
||||
|
||||
[Required]
|
||||
public int ManufactureId { get; private set; }
|
||||
|
||||
[Required]
|
||||
public int Count { get; private set; }
|
||||
|
||||
[Required]
|
||||
public double Sum { get; private set; }
|
||||
|
||||
[Required]
|
||||
public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен;
|
||||
|
||||
[Required]
|
||||
public DateTime DateCreate { get; private set; } = DateTime.Now;
|
||||
|
||||
public DateTime? DateImplement { get; private set; }
|
||||
@ -68,7 +74,8 @@ namespace BlacksmithWorkshopDatabaseImplement.Models
|
||||
Sum = Sum,
|
||||
Status = Status,
|
||||
DateCreate = DateCreate,
|
||||
DateImplement = DateImplement
|
||||
DateImplement = DateImplement,
|
||||
ManufactureName = Manufacture.ManufactureName
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,128 @@
|
||||
using BlacksmithWorkshopContracts.BindingModels;
|
||||
using BlacksmithWorkshopContracts.ViewModels;
|
||||
using BlacksmithWorkshopDataModels.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 BlacksmithWorkshopDatabaseImplement.Models
|
||||
{
|
||||
public class Shop : IShopModel
|
||||
{
|
||||
public int Id { get; private set; }
|
||||
|
||||
[Required]
|
||||
public string ShopName { get; private set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
public string Address { get; private set; } = string.Empty;
|
||||
|
||||
public DateTime DateOpen { get; private set; } = DateTime.Now;
|
||||
|
||||
[Required]
|
||||
public int MaxCountManufactures { get; private set; }
|
||||
|
||||
private Dictionary<int, (IManufactureModel, int)>? _shopManufactures = null;
|
||||
|
||||
[Required]
|
||||
public Dictionary<int, (IManufactureModel, int)> ShopManufactures
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_shopManufactures == null)
|
||||
{
|
||||
_shopManufactures = Manufactures.ToDictionary(recSI => recSI.ManufactureId, recSI => (recSI.Manufacture as IManufactureModel, recSI.Count));
|
||||
}
|
||||
return _shopManufactures;
|
||||
}
|
||||
}
|
||||
|
||||
[ForeignKey("ShopId")]
|
||||
public virtual List<ShopManufacture> Manufactures { get; set; } = new();
|
||||
|
||||
public static Shop? Create(ManufactureShopDatabase context, ShopBindingModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return new Shop()
|
||||
{
|
||||
Id = model.Id,
|
||||
ShopName = model.ShopName,
|
||||
Address = model.Address,
|
||||
DateOpen = model.DateOpen,
|
||||
Manufactures = model.ShopManufactures.Select(x => new ShopManufacture
|
||||
{
|
||||
Manufacture = context.Manufactures.First(y => y.Id == x.Key),
|
||||
Count = x.Value.Item2
|
||||
}).ToList(),
|
||||
MaxCountManufactures = model.MaxCountManufactures
|
||||
};
|
||||
}
|
||||
|
||||
public void Update(ShopBindingModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ShopName = model.ShopName;
|
||||
Address = model.Address;
|
||||
DateOpen = model.DateOpen;
|
||||
MaxCountManufactures = model.MaxCountManufactures;
|
||||
}
|
||||
|
||||
public ShopViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
ShopName = ShopName,
|
||||
Address = Address,
|
||||
DateOpen = DateOpen,
|
||||
MaxCountManufactures = MaxCountManufactures,
|
||||
ShopManufactures = ShopManufactures
|
||||
};
|
||||
|
||||
public void UpdateIceCreams(ManufactureShopDatabase context, ShopBindingModel model)
|
||||
{
|
||||
var shopIceCreams = context.ShopManufactures.Where(rec => rec.ShopId == model.Id).ToList();
|
||||
|
||||
if (shopIceCreams != null && shopIceCreams.Count > 0)
|
||||
{ // удалили те, которых нет в модели
|
||||
context.ShopManufactures.RemoveRange(shopIceCreams.Where(rec => !model.ShopManufactures.ContainsKey(rec.ManufactureId)));
|
||||
context.SaveChanges();
|
||||
|
||||
// обновили количество у существующих записей
|
||||
foreach (var updateIceCream in shopIceCreams)
|
||||
{
|
||||
updateIceCream.Count = model.ShopManufactures[updateIceCream.ManufactureId].Item2;
|
||||
model.ShopManufactures.Remove(updateIceCream.ManufactureId);
|
||||
}
|
||||
|
||||
context.SaveChanges();
|
||||
}
|
||||
|
||||
var shop = context.Shops.First(x => x.Id == Id);
|
||||
|
||||
foreach (var si in model.ShopManufactures)
|
||||
{
|
||||
context.ShopManufactures.Add(new ShopManufacture
|
||||
{
|
||||
Shop = shop,
|
||||
Manufacture = context.Manufactures.First(x => x.Id == si.Key),
|
||||
Count = si.Value.Item2
|
||||
});
|
||||
|
||||
context.SaveChanges();
|
||||
}
|
||||
|
||||
_shopManufactures = null;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BlacksmithWorkshopDatabaseImplement.Models
|
||||
{
|
||||
public class ShopManufacture
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
[Required]
|
||||
public int ShopId { get; set; }
|
||||
|
||||
[Required]
|
||||
public int ManufactureId { get; set; }
|
||||
|
||||
[Required]
|
||||
public int Count { get; set; }
|
||||
|
||||
public virtual Shop Shop { get; set; } = new();
|
||||
|
||||
public virtual Manufacture Manufacture { get; set; } = new();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user