supply model

This commit is contained in:
the 2024-06-22 12:18:43 +04:00
parent 2cc2ac4280
commit 43aef44d98
7 changed files with 186 additions and 3 deletions

View File

@ -0,0 +1,47 @@
using Contracts.BindingModels;
using Contracts.BusinessLogicContracts;
using Contracts.SearchModels;
using Contracts.StorageContracts;
using Contracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BusinessLogic.BusinessLogic
{
public class ProductLogic : IProductLogic
{
private readonly IProductStorage _productStorage;
public ProductLogic(IProductStorage productStorage)
{
_productStorage = productStorage;
}
public bool Create(ProductBindingModel model)
{
throw new NotImplementedException();
}
public bool Delete(ProductBindingModel model)
{
throw new NotImplementedException();
}
public ProductViewModel? ReadElement(ProductSearchModel model)
{
throw new NotImplementedException();
}
public List<ProductViewModel>? ReadList(ProductSearchModel? model)
{
throw new NotImplementedException();
}
public bool Update(ProductBindingModel model)
{
throw new NotImplementedException();
}
}
}

View File

@ -15,6 +15,6 @@ namespace Contracts.BindingModels
public DateTime Date { get; set; } public DateTime Date { get; set; }
public double Price { get; set; } public double Price { get; set; }
public SupplyStatus Status { get; set; } public SupplyStatus Status { get; set; }
public Dictionary<int, (IProduct, int)> SupplyProducts { get; set; } = new(); public Dictionary<Guid, (IProduct, int)> SupplyProducts { get; set; } = new();
} }
} }

View File

@ -1,4 +1,5 @@
using DataModels.Enums; using DataModels.Enums;
using DataModels.Models;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -10,9 +11,10 @@ namespace Contracts.ViewModels
public class SupplyViewModel public class SupplyViewModel
{ {
public Guid Id { get; set; } public Guid Id { get; set; }
public string Name { get; set; } = string.Empty;
public string SupplierName { get; set; } = string.Empty; public string SupplierName { get; set; } = string.Empty;
public double Price { get; set; } public double Price { get; set; }
public Dictionary<Guid, string> Products { get; set; } = new(); public Dictionary<Guid, (IProduct, int)> Products { get; set; } = new();
public DateTime Date { get; set; } public DateTime Date { get; set; }
public SupplyStatus Status { get; set; } public SupplyStatus Status { get; set; }
} }

View File

@ -13,6 +13,6 @@ namespace DataModels.Models
double Price { get; } double Price { get; }
DateTime Date { get; } DateTime Date { get; }
SupplyStatus Status { get; } SupplyStatus Status { get; }
Dictionary<int, (IProduct, int)> SupplyProducts { get; } Dictionary<Guid, (IProduct, int)> SupplyProducts { get; }
} }
} }

View File

@ -24,6 +24,8 @@ namespace DatabaseImplement
public virtual DbSet<Sell> Sells { get; set; } = null!; public virtual DbSet<Sell> Sells { get; set; } = null!;
public virtual DbSet<Purchase> Purchases { get; set; } = null!; public virtual DbSet<Purchase> Purchases { get; set; } = null!;
public virtual DbSet<Product> Products { get; set; } = null!; public virtual DbSet<Product> Products { get; set; } = null!;
public virtual DbSet<Supply> Supplies { get; set; } = null!;
public virtual DbSet<SupplyProduct> SupplyProducts { get; set; } = null!;
public virtual DbSet<MediaFile> MediaFiles { get; set; } = null!; public virtual DbSet<MediaFile> MediaFiles { get; set; } = null!;
} }
} }

View File

@ -0,0 +1,108 @@
using Contracts.BindingModels;
using Contracts.ViewModels;
using DataModels.Enums;
using DataModels.Models;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
namespace DatabaseImplement.Models
{
public class Supply : ISupply
{
[Required]
public Guid Id { get; set; }
[Required]
public string Name { get; set; } = string.Empty;
[Required]
public double Price { get; set; }
[Required]
public DateTime Date { get; set; }
[Required]
public SupplyStatus Status { get; set; } = SupplyStatus.Pending;
private Dictionary<Guid, (IProduct, int)>? _supplyProducts = null;
[NotMapped]
public Dictionary<Guid, (IProduct, int)> SupplyProducts
{
get
{
if (_supplyProducts == null)
{
_supplyProducts = Products
.ToDictionary(recPC => recPC.Id, recPC =>
(recPC.Product as IProduct, recPC.Count));
}
return _supplyProducts;
}
}
[ForeignKey("SupplyId")]
public virtual List<SupplyProduct> Products { get; set; } = new();
public static Supply Create(Database context, SupplyBindingModel model)
{
return new Supply()
{
Id = model.Id,
Name = model.Name,
Price = model.Price,
Products = model.SupplyProducts.Select(x => new
SupplyProduct
{
Product = context.Products.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(SupplyBindingModel model)
{
Name = model.Name;
Price = model.Price;
}
public SupplyViewModel GetViewModel => new()
{
Id = Id,
Name = Name,
Price = Price,
Products = SupplyProducts,
//supplierName сделать
Date = Date,
Status = Status
};
public void UpdateComponents(Database context, SupplyBindingModel model)
{
var supplyProducts = context.SupplyProducts.Where(rec =>
rec.Id == model.Id).ToList();
if (supplyProducts != null && supplyProducts.Count > 0)
{ // удалили те, которых нет в модели
context.SupplyProducts.RemoveRange(supplyProducts.Where(rec
=> !model.SupplyProducts.ContainsKey(rec.ProductId)));
context.SaveChanges();
// обновили количество у существующих записей
foreach (var updateProduct in supplyProducts)
{
updateProduct.Count = model.SupplyProducts[updateProduct.ProductId].Item2;
model.SupplyProducts.Remove(updateProduct.ProductId);
}
context.SaveChanges();
}
var supply = context.Supplies.First(x => x.Id == Id);
foreach (var pc in model.SupplyProducts)
{
context.SupplyProducts.Add(new SupplyProduct
{
Supply = supply,
Product = context.Products.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();
}
_supplyProducts = null;
}
}
}

View File

@ -0,0 +1,24 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DatabaseImplement.Models
{
public class SupplyProduct
{
public Guid Id { get; set; }
[Required]
public Guid SupplyId { get; set; }
[Required]
public Guid ProductId { get; set; }
[Required]
public int Count { get; set; }
public virtual Supply Supply { get; set; } = new();
public virtual Product Product { get; set; } = new();
}
}