This commit is contained in:
Николай 2023-04-02 15:43:05 +04:00
parent c4c8069645
commit 934080cd54
15 changed files with 63 additions and 98 deletions

View File

@ -10,10 +10,12 @@ namespace HardwareShopContracts.BusinessLogicsContracts
{
private readonly ILogger _logger;
private readonly IBuildStorage _buildStorage;
public BuildLogic(ILogger<BuildLogic> logger, IBuildStorage buildStorage)
private readonly IComponentStorage _componentStorage;
public BuildLogic(ILogger<BuildLogic> logger, IBuildStorage buildStorage, IComponentStorage componentStorage)
{
_logger = logger;
_buildStorage = buildStorage;
_componentStorage = componentStorage;
}
public List<BuildViewModel>? ReadList(BuildSearchModel? model)
@ -116,5 +118,24 @@ namespace HardwareShopContracts.BusinessLogicsContracts
throw new InvalidOperationException("Продукт с таким названием уже есть");
}
}
//нужно переделать
public double Price(ComponentSearchModel model)
{
_logger.LogInformation("ReadList. Id:{Id}", model?.Id);
var list = _componentStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return 0;
}
double sum = 0;
foreach (var component in list)
{
sum += component.Cost;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return sum;
}
}
}

View File

@ -6,12 +6,12 @@ namespace HardwareShopContracts.BindingModels
{
public int Id { get; set; }
public decimal Price { get; set; }
public double Price { get; set; }
public string BuildName { get; set; } = string.Empty;
public int UserId { get; set; }
public Dictionary<int, (IComponentModel, int)>? BuildComponents { get; set; }
public Dictionary<int, (IPurchaseModel, int)> BuildPurchases { get; set; } = new();
}
}

View File

@ -7,7 +7,7 @@ namespace HardwareShopContracts.BindingModels
{
public int Id { get; set; }
public decimal Sum { get; set; }
public double Sum { get; set; }
public PurchaseStatus PurchaseStatus { get; set; } = PurchaseStatus.Неизвестен;
@ -15,7 +15,6 @@ namespace HardwareShopContracts.BindingModels
public int UserId { get; set; }
public Dictionary<int, (IBuildModel, int)>? PurchaseBuilds { get; set; }
public Dictionary<int, (IGoodModel, int)> PurchaseGoods { get; set; } = new();
}

View File

@ -8,6 +8,7 @@ namespace HardwareShopContracts.BusinessLogicsContracts
{
List<BuildViewModel>? ReadList(BuildSearchModel? model);
BuildViewModel? ReadElement(BuildSearchModel model);
double Price(ComponentSearchModel model);
bool Create(BuildBindingModel model);
bool Update(BuildBindingModel model);
bool Delete(BuildBindingModel model);

View File

@ -7,7 +7,7 @@ namespace HardwareShopContracts.ViewModels
public int Id { get; set; }
[DisplayName("Цена")]
public decimal Price { get; set; }
public double Price { get; set; }
[DisplayName("Название компонента")]
public string BuildName { get; set; } = string.Empty;
@ -17,6 +17,7 @@ namespace HardwareShopContracts.ViewModels
public int UserId { get; set; }
public Dictionary<int, (IComponentModel, int)>? BuildComponents { get; set; }
public Dictionary<int, (IPurchaseModel, int)> BuildPurchases { get; set; } = new();
}
}

View File

@ -8,7 +8,7 @@ namespace HardwareShopContracts.ViewModels
public int Id { get; set; }
[DisplayName("Цена")]
public decimal Sum { get; set; }
public double Sum { get; set; }
[DisplayName("Статус покупки")]
public PurchaseStatus PurchaseStatus { get; set; } = PurchaseStatus.Неизвестен;
@ -21,8 +21,6 @@ namespace HardwareShopContracts.ViewModels
[DisplayName("Логин пользователя")]
public string UserLogin { get; set; } = string.Empty;
public Dictionary<int, (IBuildModel, int)>? PurchaseBuilds { get; set; }
public Dictionary<int, (IGoodModel, int)> PurchaseGoods { get; set; } = new();
}
}

View File

@ -9,12 +9,12 @@ namespace HardwareShopDataModels.Models
{
public interface IBuildModel : IId
{
decimal Price { get; }
double Price { get; }
string BuildName { get; }
int UserId { get; }
Dictionary<int, (IComponentModel, int)>? BuildComponents { get; }
Dictionary<int, (IPurchaseModel, int)> BuildPurchases { get; }
}
}

View File

@ -4,17 +4,14 @@ namespace HardwareShopDataModels.Models
{
public interface IPurchaseModel : IId
{
decimal Sum { get; }
double Sum { get; }
PurchaseStatus PurchaseStatus { get; }
//через "?" обозначается что поле может быть null
DateTime? DatePurchase { get; }
int UserId { get; }
Dictionary<int, (IBuildModel, int)>? PurchaseBuilds { get; }
Dictionary<int, (IGoodModel, int)> PurchaseGoods { get; }
}
}

View File

@ -97,7 +97,7 @@ namespace HardwareShopDatabaseImplement.Implements.Worker
}
build.Update(model);
context.SaveChanges();
build.UpdateComponents(context, model);
build.UpdatePurchases(context, model);
transaction.Commit();
return build?.GetViewModel;
}

View File

@ -93,7 +93,6 @@ namespace HardwareShopDatabaseImplement.Implements.Worker
purchase.Update(model);
context.SaveChanges();
purchase.UpdateGoods(context, model);
purchase.UpdateBuilds(context, model);
transaction.Commit();
return purchase.GetViewModel;
}

View File

@ -243,7 +243,7 @@ namespace HardwareShopDatabaseImplement.Migrations
b.Property<int>("ClientId")
.HasColumnType("integer");
b.Property<decimal>("Price")
b.Property<double>("Price")
.HasColumnType("numeric");
b.Property<int>("UserId")
@ -303,7 +303,7 @@ namespace HardwareShopDatabaseImplement.Migrations
b.Property<int>("PurchaseStatus")
.HasColumnType("integer");
b.Property<decimal>("Sum")
b.Property<double>("Sum")
.HasColumnType("numeric");
b.Property<int>("UserId")

View File

@ -34,7 +34,7 @@ namespace HardwareShopDatabaseImplement.Migrations
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
Price = table.Column<decimal>(type: "numeric", nullable: false),
Price = table.Column<double>(type: "numeric", nullable: false),
BuildName = table.Column<string>(type: "text", nullable: false),
UserId = table.Column<int>(type: "integer", nullable: false),
ClientId = table.Column<int>(type: "integer", nullable: false)
@ -100,7 +100,7 @@ namespace HardwareShopDatabaseImplement.Migrations
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
Sum = table.Column<decimal>(type: "numeric", nullable: false),
Sum = table.Column<double>(type: "numeric", nullable: false),
PurchaseStatus = table.Column<int>(type: "integer", nullable: false),
DatePurchase = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
UserId = table.Column<int>(type: "integer", nullable: false),

View File

@ -240,7 +240,7 @@ namespace HardwareShopDatabaseImplement.Migrations
b.Property<int>("ClientId")
.HasColumnType("integer");
b.Property<decimal>("Price")
b.Property<double>("Price")
.HasColumnType("numeric");
b.Property<int>("UserId")
@ -300,7 +300,7 @@ namespace HardwareShopDatabaseImplement.Migrations
b.Property<int>("PurchaseStatus")
.HasColumnType("integer");
b.Property<decimal>("Sum")
b.Property<double>("Sum")
.HasColumnType("numeric");
b.Property<int>("UserId")

View File

@ -12,7 +12,7 @@ namespace HardwareShopDatabaseImplement.Models.Worker
public int Id { get; set; }
[Required]
public decimal Price { get; set; }
public double Price { get; set; }
[Required]
public string BuildName { get; set; } = string.Empty;
@ -32,19 +32,18 @@ namespace HardwareShopDatabaseImplement.Models.Worker
[ForeignKey("BuildId")]
public virtual List<PurchaseBuild> Purchases { get; set; } = new();
private Dictionary<int, (IComponentModel, int)>? _buildComponents = null;
public Dictionary<int, (IPurchaseModel, int)>? _buildPurchases = null;
[NotMapped]
public Dictionary<int, (IComponentModel, int)> BuildComponents
public Dictionary<int, (IPurchaseModel, int)> BuildPurchases
{
get
{
if (_buildComponents == null)
if (_buildPurchases == null)
{
_buildComponents = Components.ToDictionary(recBC => recBC.ComponentId, recBC => (recBC.Component as IComponentModel, recBC.Count));
_buildPurchases = Purchases.ToDictionary(recBP => recBP.PurchaseId, recBP => (recBP.Purchase as IPurchaseModel, recBP.Count));
}
return _buildComponents;
return _buildPurchases;
}
}
@ -53,12 +52,11 @@ namespace HardwareShopDatabaseImplement.Models.Worker
return new Build()
{
Id = model.Id,
Price = model.Price,
BuildName = model.BuildName,
UserId = model.UserId,
Components = model.BuildComponents.Select(x => new BuildComponent
Purchases = model.BuildPurchases.Select(x => new PurchaseBuild
{
Component = context.Components.First(y => y.Id == x.Key),
Purchase = context.Purchases.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
@ -77,37 +75,36 @@ namespace HardwareShopDatabaseImplement.Models.Worker
Price = Price,
UserLogin = User.Login,
UserId = UserId,
BuildComponents = BuildComponents
};
public void UpdateComponents(HardwareShopDatabase context, BuildBindingModel model)
public void UpdatePurchases(HardwareShopDatabase context, BuildBindingModel model)
{
var buildComponents = context.BuildsComponents.Where(rec => rec.BuildId == model.Id).ToList();
if (buildComponents != null && buildComponents.Count > 0)
var buildPurchases = context.PurchasesBuilds.Where(rec => rec.BuildId == model.Id).ToList();
if (buildPurchases != null && buildPurchases.Count > 0)
{ // удалили те в бд, которых нет в модели
context.BuildsComponents.RemoveRange(buildComponents.Where(rec => !model.BuildComponents.ContainsKey(rec.ComponentId)));
context.PurchasesBuilds.RemoveRange(buildPurchases.Where(rec => !model.BuildPurchases.ContainsKey(rec.PurchaseId)));
context.SaveChanges();
// обновили количество у существующих записей
foreach (var updateComponent in buildComponents)
foreach (var updateComponent in buildPurchases)
{
updateComponent.Count = model.BuildComponents[updateComponent.ComponentId].Item2;
model.BuildComponents.Remove(updateComponent.ComponentId);
updateComponent.Count = model.BuildPurchases[updateComponent.PurchaseId].Item2;
model.BuildPurchases.Remove(updateComponent.PurchaseId);
}
context.SaveChanges();
}
var build = context.Builds.First(x => x.Id == Id);
//добавляем в бд блюда которые есть в моделе, но ещё нет в бд
foreach (var dc in model.BuildComponents)
foreach (var dc in model.BuildPurchases)
{
context.BuildsComponents.Add(new BuildComponent
context.PurchasesBuilds.Add(new PurchaseBuild
{
Build = build,
Component = context.Components.First(x => x.Id == dc.Key),
Purchase = context.Purchases.First(x => x.Id == dc.Key),
Count = dc.Value.Item2
});
context.SaveChanges();
}
_buildComponents = null;
_buildPurchases = null;
}
}

View File

@ -14,7 +14,7 @@ namespace HardwareShopDatabaseImplement.Models.Worker
public int Id { get; set; }
[Required]
public decimal Sum { get; set; }
public double Sum { get; set; }
[Required]
public PurchaseStatus PurchaseStatus { get; set; } = PurchaseStatus.Неизвестен;
@ -29,27 +29,13 @@ namespace HardwareShopDatabaseImplement.Models.Worker
[ForeignKey("PurchaseId")]
public virtual List<PurchaseBuild>? Builds { get; set; }
public Dictionary<int, (IBuildModel, int)>? _purchaseBuilds = null;
[NotMapped]
public Dictionary<int, (IBuildModel, int)>? PurchaseBuilds
{
get
{
if (_purchaseBuilds == null)
{
_purchaseBuilds = Builds.ToDictionary(recPB => recPB.BuildId, recPB => (recPB.Build as IBuildModel, recPB.Count));
}
return _purchaseBuilds;
}
}
[ForeignKey("PurchaseId")]
public virtual List<PurchaseGood> Goods { get; set; } = new();
public Dictionary<int, (IGoodModel, int)>? _purchaseGoods = null;
[NotMapped]
public Dictionary<int, (IGoodModel, int)>? PurchaseGoods
public Dictionary<int, (IGoodModel, int)> PurchaseGoods
{
get
{
@ -70,16 +56,11 @@ namespace HardwareShopDatabaseImplement.Models.Worker
PurchaseStatus = model.PurchaseStatus,
DatePurchase = model.DatePurchase,
UserId = model.UserId,
Builds = model.PurchaseBuilds.Select(x => new PurchaseBuild
{
Build = context.Builds.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList(),
Goods = model.PurchaseGoods.Select(x => new PurchaseGood
{
Good = context.Goods.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
}).ToList(),
};
}
@ -97,38 +78,9 @@ namespace HardwareShopDatabaseImplement.Models.Worker
DatePurchase = DatePurchase,
UserId = UserId,
UserLogin = User.Login,
PurchaseBuilds = PurchaseBuilds,
PurchaseGoods = PurchaseGoods
};
public void UpdateBuilds(HardwareShopDatabase context, PurchaseBindingModel model)
{
var purchaseBuilds = context.PurchasesBuilds.Where(rec => rec.PurchaseId == model.Id).ToList();
if (purchaseBuilds != null && purchaseBuilds.Count > 0)
{
context.PurchasesBuilds.RemoveRange(purchaseBuilds.Where(rec => !model.PurchaseBuilds.ContainsKey(rec.BuildId)));
context.SaveChanges();
foreach (var updateBuild in purchaseBuilds)
{
updateBuild.Count = model.PurchaseBuilds[updateBuild.BuildId].Item2;
model.PurchaseBuilds.Remove(updateBuild.BuildId);
}
context.SaveChanges();
}
var purchase = context.Purchases.First(x => x.Id == Id);
foreach (var dc in model.PurchaseBuilds)
{
context.PurchasesBuilds.Add(new PurchaseBuild
{
Purchase = purchase,
Build = context.Builds.First(x => x.Id == dc.Key),
Count = dc.Value.Item2
});
context.SaveChanges();
}
_purchaseBuilds = null;
}
public void UpdateGoods(HardwareShopDatabase context, PurchaseBindingModel model)
{
var purchaseGoods = context.PurchasesGoods.Where(rec => rec.PurchaseId == model.Id).ToList();