Добавил модель магазина и ShopSecure
This commit is contained in:
parent
1b27ec3be2
commit
f78d6310ed
@ -13,12 +13,14 @@ namespace SecuritySystemBusinessLogic.BusinessLogics
|
||||
private readonly ILogger _logger;
|
||||
private readonly IOrderStorage _orderStorage;
|
||||
private readonly IShopLogic _shopLogic;
|
||||
private readonly ISecureStorage _secureStorage;
|
||||
|
||||
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage, IShopLogic shopLogic)
|
||||
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage, IShopLogic shopLogic, ISecureStorage secureStorage)
|
||||
{
|
||||
_logger = logger;
|
||||
_orderStorage = orderStorage;
|
||||
_shopLogic = shopLogic;
|
||||
_secureStorage = secureStorage;
|
||||
}
|
||||
|
||||
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
|
||||
@ -61,9 +63,10 @@ namespace SecuritySystemBusinessLogic.BusinessLogics
|
||||
_logger.LogWarning("Status change operation failed");
|
||||
throw new InvalidOperationException("Текущий статус заказа не может быть переведен в выбранный");
|
||||
}
|
||||
var secure = _secureStorage.GetElement(new SecureSearchModel { Id = model.SecureId });
|
||||
if (targetStatus == OrderStatus.Выдан)
|
||||
{
|
||||
_shopLogic.SupplySecures();
|
||||
_shopLogic.SupplySecures(secure, model.Count);
|
||||
}
|
||||
model.Status = targetStatus;
|
||||
if (model.Status == OrderStatus.Выдан)
|
||||
|
@ -0,0 +1,40 @@
|
||||
using SecuritySystemContracts.BindingModels;
|
||||
using SecuritySystemContracts.SearchModels;
|
||||
using SecuritySystemContracts.StoragesContracts;
|
||||
using SecuritySystemContracts.ViewModels;
|
||||
|
||||
namespace SecuritySystemDatabaseImplement.Implements
|
||||
{
|
||||
public class ShopStorage : IShopStorage
|
||||
{
|
||||
public ShopViewModel? Delete(ShopBindingModel model)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ShopViewModel? GetElement(ShopSearchModel model)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public List<ShopViewModel> GetFullList()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ShopViewModel? Insert(ShopBindingModel model)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ShopViewModel? Update(ShopBindingModel model)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,99 @@
|
||||
using SecuritySystemContracts.BindingModels;
|
||||
using SecuritySystemContracts.ViewModels;
|
||||
using SecuritySystemDataModels.Models;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace SecuritySystemDatabaseImplement.Models
|
||||
{
|
||||
public class Shop : IShopModel
|
||||
{
|
||||
public int Id { get; set; }
|
||||
[Required]
|
||||
public string Name { get; set; } = string.Empty;
|
||||
[Required]
|
||||
public string Address { get; set; } = string.Empty;
|
||||
[Required]
|
||||
public DateTime OpeningDate { get; set; }
|
||||
[Required]
|
||||
public int MaxSecuresCount { get; set; }
|
||||
[ForeignKey("ShopId")]
|
||||
public virtual List<ShopSecure> Secures { get; set; } = new();
|
||||
private Dictionary<int, (ISecureModel, int)>? _shopSecures = null;
|
||||
public Dictionary<int, (ISecureModel, int)> ShopSecures
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_shopSecures == null)
|
||||
{
|
||||
_shopSecures = Secures
|
||||
.ToDictionary(shopSecure => shopSecure.SecureId, shopSecure => (shopSecure.Secure as ISecureModel, shopSecure.Count));
|
||||
}
|
||||
return _shopSecures;
|
||||
}
|
||||
}
|
||||
public static Shop Create(SecuritySystemDatabase context, ShopBindingModel model)
|
||||
{
|
||||
return new Shop()
|
||||
{
|
||||
Id = model.Id,
|
||||
Name = model.Name,
|
||||
Address = model.Address,
|
||||
OpeningDate = model.OpeningDate,
|
||||
MaxSecuresCount = model.MaxSecuresCount,
|
||||
Secures = model.ShopSecures.Select(x => new ShopSecure
|
||||
{
|
||||
Secure = context.Secures.First(y => y.Id == x.Key),
|
||||
Count = x.Value.Item2
|
||||
}).ToList()
|
||||
};
|
||||
}
|
||||
public void Update(ShopBindingModel model)
|
||||
{
|
||||
Name = model.Name;
|
||||
Address = model.Address;
|
||||
OpeningDate = model.OpeningDate;
|
||||
MaxSecuresCount = model.MaxSecuresCount;
|
||||
}
|
||||
public ShopViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
Name = Name,
|
||||
Address = Address,
|
||||
OpeningDate = OpeningDate,
|
||||
MaxSecuresCount = MaxSecuresCount,
|
||||
ShopSecures = ShopSecures
|
||||
|
||||
};
|
||||
public void UpdateComponents(SecuritySystemDatabase context, ShopBindingModel model)
|
||||
{
|
||||
var shopSecures = context.ShopSecures.Where(rec => rec.SecureId == model.Id).ToList();
|
||||
if (shopSecures != null && shopSecures.Count > 0)
|
||||
{
|
||||
// удалили те, которых нет в модели
|
||||
context.ShopSecures.RemoveRange(shopSecures.Where(rec => !model.ShopSecures.ContainsKey(rec.SecureId)));
|
||||
context.SaveChanges();
|
||||
// обновили количество у существующих записей
|
||||
foreach (var updateSecure in shopSecures)
|
||||
{
|
||||
updateSecure.Count = model.ShopSecures[updateSecure.SecureId].Item2;
|
||||
model.ShopSecures.Remove(updateSecure.SecureId);
|
||||
}
|
||||
context.SaveChanges();
|
||||
}
|
||||
var shop = context.Shops.First(x => x.Id == Id);
|
||||
foreach (var obj in model.ShopSecures)
|
||||
{
|
||||
context.ShopSecures.Add(new ShopSecure
|
||||
{
|
||||
Shop = shop,
|
||||
Secure = context.Secures.First(x => x.Id == obj.Key),
|
||||
Count = obj.Value.Item2
|
||||
});
|
||||
context.SaveChanges();
|
||||
}
|
||||
_shopSecures = null;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace SecuritySystemDatabaseImplement.Models
|
||||
{
|
||||
public class ShopSecure
|
||||
{
|
||||
public int Id { get; set; }
|
||||
[Required]
|
||||
public int SecureId { get; set; }
|
||||
[Required]
|
||||
public int ShopId { get; set; }
|
||||
[Required]
|
||||
public int Count { get; set; }
|
||||
public virtual Shop Shop { get; set; } = new();
|
||||
public virtual Secure Secure { get; set; } = new();
|
||||
}
|
||||
}
|
@ -12,7 +12,7 @@ namespace SecuritySystemDatabaseImplement
|
||||
{
|
||||
if (optionsBuilder.IsConfigured == false)
|
||||
{
|
||||
optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=SecuritySystemDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
|
||||
optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=SecuritySystemDatabaseHard;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
|
||||
}
|
||||
base.OnConfiguring(optionsBuilder);
|
||||
}
|
||||
@ -20,6 +20,8 @@ namespace SecuritySystemDatabaseImplement
|
||||
public virtual DbSet<Secure> Secures { set; get; }
|
||||
public virtual DbSet<SecureComponent> SecureComponents { set; get; }
|
||||
public virtual DbSet<Order> Orders { set; get; }
|
||||
public virtual DbSet<ShopSecure> ShopSecures { get; set; }
|
||||
public virtual DbSet<Shop> Shops { get; set; }
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user