время получать обновки #15

Merged
mfnefd merged 14 commits from main into registration 2024-06-22 19:02:02 +04:00
62 changed files with 2173 additions and 0 deletions

View File

@ -0,0 +1,125 @@
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;
private readonly ILogger _logger;
public ProductLogic(IProductStorage productStorage, ILogger<ProductLogic> logger)
{
_productStorage = productStorage;
_logger = logger;
}
public bool Create(ProductBindingModel model)
{
CheckModel(model);
if (_productStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(ProductBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_productStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public ProductViewModel? ReadElement(ProductSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Name:{Name}.Id:{ Id}", model.Name, model.Id);
var element = _productStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
public List<ProductViewModel>? ReadList(ProductSearchModel? model)
{
_logger.LogInformation("ReadList. Name:{Name}.Id:{ Id}", model?.Name, model?.Id);
var list = model == null ? _productStorage.GetFullList() : _productStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public bool Update(ProductBindingModel model)
{
CheckModel(model);
if (_productStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(ProductBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Name))
{
throw new ArgumentNullException("Нет названия продукта", nameof(model.Name));
}
if (model.Price < 0)
{
throw new Exception("Цена продукта не может быть ниже нуля");
}
if (model.Amount < 0)
{
throw new Exception("Кол-во продуктов не может быть ниже нуля");
}
if (model.Rate < 0.0 || model.Rate > 5.0)
{
throw new Exception("Рейтинг продукта должен быть в пределах от 0.0 до 5.0");
}
_logger.LogInformation("Product. ProductName:{Name}.Price:{ Price}. Id: { Id}", model.Name, model.Price, model.Id);
var element = _productStorage.GetElement(new ProductSearchModel
{
Name = model.Name
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Продукт с таким названием уже есть");
}
}
}
}

View File

@ -0,0 +1,114 @@
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 SupplierLogic : ISupplierLogic
{
private readonly ISupplierStorage _supplierStorage;
private readonly ILogger _logger;
public SupplierLogic(ISupplierStorage supplierStorage, ILogger<SupplierLogic> logger)
{
_supplierStorage = supplierStorage;
_logger = logger;
}
public bool Create(SupplierBindingModel model)
{
CheckModel(model);
if (_supplierStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(SupplierBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_supplierStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public SupplierViewModel? ReadElement(SupplierSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Name:{Name}.Id:{ Id}", model.Name, model.Id);
var element = _supplierStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
public List<SupplierViewModel>? ReadList(SupplierSearchModel? model)
{
_logger.LogInformation("ReadList. Name:{Name}.Id:{ Id}", model?.Name, model?.Id);
var list = model == null ? _supplierStorage.GetFullList() : _supplierStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public bool Update(SupplierBindingModel model)
{
CheckModel(model);
if (_supplierStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(SupplierBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Name))
{
throw new ArgumentNullException("Нет названия поставщика", nameof(model.Name));
}
if (model.Deals < 0)
{
throw new Exception("Кол-во сделок не может быть меньше нуля");
}
foreach(var item in model.AvailibleProducts.Values)
{
if (item.Item2 < 0)
{
throw new Exception("Кол-во доступных продуктов не должно быть отрицательным");
}
}
}
}
}

View File

@ -0,0 +1,105 @@
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 SupplyLogic : ISupplyLogic
{
private readonly ISupplyStorage _supplyStorage;
private readonly ILogger _logger;
public SupplyLogic(ISupplyStorage supplyStorage, ILogger<SupplyLogic> logger)
{
_supplyStorage = supplyStorage;
_logger = logger;
}
public bool Create(SupplyBindingModel model)
{
CheckModel(model);
if (_supplyStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(SupplyBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_supplyStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public SupplyViewModel? ReadElement(SupplySearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Name:{Name}.Id:{ Id}", model.Name, model.Id);
var element = _supplyStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
public List<SupplyViewModel>? ReadList(SupplySearchModel? model)
{
_logger.LogInformation("ReadList. Name:{Name}.Id:{ Id}", model?.Name, model?.Id);
var list = model == null ? _supplyStorage.GetFullList() : _supplyStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public bool Update(SupplyBindingModel model)
{
CheckModel(model);
if (_supplyStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(SupplyBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Name))
{
throw new ArgumentNullException("Нет названия поставки", nameof(model.Name));
}
}
}
}

View File

@ -0,0 +1,17 @@
using DataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Contracts.BindingModels
{
public class MediaFileBindingModel : IMediaFile
{
public Guid Id { get; set; }
public Guid ProductId { get; set; }
public string Name { get; set; } = string.Empty;
public string Location { get; set; } = string.Empty;
}
}

View File

@ -0,0 +1,19 @@
using DataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Contracts.BindingModels
{
public class ProductBindingModel : IProduct
{
public Guid Id { get; set; }
public string Name { get; set; } = string.Empty;
public double Price { get; set; }
public int Amount { get; set; }
public bool IsBeingSold { get; set; }
public double Rate { get; set; }
}
}

View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Contracts.BindingModels
{
public class PurchaseBindingModel
{
public Guid Id { get; set; }
public DateTime DatePurchase { get; set; }
}
}

View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Contracts.BindingModels
{
public class SellBindingModel
{
public Guid Id { get; set; }
public DateTime DateSell { get; set; }
}
}

View File

@ -0,0 +1,17 @@
using DataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Contracts.BindingModels
{
public class SupplierBindingModel : ISupplier
{
public Guid Id { get; set; }
public string Name { get; set; } = string.Empty;
public Dictionary<Guid, (IProduct, int)> AvailibleProducts { get; set; } = new();
public int Deals { get; set; } = 0;
}
}

View File

@ -0,0 +1,21 @@
using DataModels.Enums;
using DataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Contracts.BindingModels
{
public class SupplyBindingModel : ISupply
{
public Guid Id { get; set; }
public string Name { get; set; } = string.Empty;
public Guid SupplierId { get; set; }
public DateTime Date { get; set; }
public double Price { get; set; }
public SupplyStatus Status { get; set; }
public Dictionary<Guid, (IProduct, int)> SupplyProducts { get; set; } = new();
}
}

View File

@ -0,0 +1,17 @@
using DataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Contracts.BindingModels
{
public class SupplyDocBindingModel : ISupplyDoc
{
public Guid Id { get; set; }
public string Name { get; set; } = string.Empty;
public Guid SupplyId { get; set; }
}
}

View File

@ -0,0 +1,24 @@
using Contracts.BindingModels;
using Contracts.SearchModels;
using Contracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Contracts.BusinessLogicContracts
{
public interface IEmployeeProductLogic
{
EmployeeProductViewModel Create(ProductBindingModel model);
EmployeeProductViewModel Update(ProductBindingModel model);
EmployeeProductViewModel ReadElement(ProductSearchModel model);
IEnumerable<EmployeeProductViewModel> ReadElements(ProductSearchModel? model);
EmployeeProductViewModel Delete(ProductSearchModel model);
}
}

View File

@ -0,0 +1,20 @@
using Contracts.BindingModels;
using Contracts.SearchModels;
using Contracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Contracts.BusinessLogicContracts
{
public interface IProductLogic
{
bool Create(ProductBindingModel model);
bool Update(ProductBindingModel model);
bool Delete(ProductBindingModel model);
List<ProductViewModel>? ReadList(ProductSearchModel? model);
ProductViewModel? ReadElement(ProductSearchModel model);
}
}

View File

@ -0,0 +1,24 @@
using Contracts.BindingModels;
using Contracts.SearchModels;
using Contracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Contracts.BusinessLogicContracts
{
public interface IPurchaseLogic
{
PurchaseViewModel Create(PurchaseBindingModel model);
PurchaseViewModel Update(PurchaseBindingModel model);
PurchaseViewModel ReadElement(PurchaseSearchModel model);
IEnumerable<PurchaseViewModel> ReadElements(PurchaseSearchModel? model);
PurchaseViewModel Delete(PurchaseSearchModel model);
}
}

View File

@ -0,0 +1,24 @@
using Contracts.BindingModels;
using Contracts.SearchModels;
using Contracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Contracts.BusinessLogicContracts
{
public interface ISellLogic
{
SellViewModel Create(SellBindingModel model);
SellViewModel Update(SellBindingModel model);
SellViewModel ReadElement(SellSearchModel model);
IEnumerable<SellViewModel> ReadElements(SellSearchModel? model);
SellViewModel Delete(SellSearchModel model);
}
}

View File

@ -0,0 +1,20 @@
using Contracts.BindingModels;
using Contracts.SearchModels;
using Contracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Contracts.BusinessLogicContracts
{
public interface ISupplierLogic
{
List<SupplierViewModel>? ReadList(SupplierSearchModel? model);
SupplierViewModel? ReadElement(SupplierSearchModel model);
bool Create(SupplierBindingModel model);
bool Update(SupplierBindingModel model);
bool Delete(SupplierBindingModel model);
}
}

View File

@ -0,0 +1,20 @@
using Contracts.BindingModels;
using Contracts.SearchModels;
using Contracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Contracts.BusinessLogicContracts
{
public interface ISupplyDocLogic
{
List<SupplyDocViewModel>? ReadList(SupplyDocSearchModel? model);
SupplyDocViewModel? ReadElement(SupplyDocSearchModel model);
bool Create(SupplyDocBindingModel model);
bool Update(SupplyDocBindingModel model);
bool Delete(SupplyDocBindingModel model);
}
}

View File

@ -0,0 +1,20 @@
using Contracts.BindingModels;
using Contracts.SearchModels;
using Contracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Contracts.BusinessLogicContracts
{
public interface ISupplyLogic
{
List<SupplyViewModel>? ReadList(SupplySearchModel? model);
SupplyViewModel? ReadElement(SupplySearchModel model);
bool Create(SupplyBindingModel model);
bool Update(SupplyBindingModel model);
bool Delete(SupplyBindingModel model);
}
}

View File

@ -0,0 +1,21 @@
using Contracts.BindingModels;
using Contracts.SearchModels;
using Contracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Contracts.BusinessLogicContracts
{
public interface IUserProductLogic
{
UserProductViewModel ReadElement(ProductSearchModel model);
IEnumerable<UserProductViewModel> ReadElements(ProductSearchModel? model);
UserProductViewModel Delete(ProductSearchModel model);
}
}

View File

@ -6,4 +6,8 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\DataModels\DataModels.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Contracts.SearchModels
{
public class MediaFileSearchModel
{
public Guid? Id { get; set; }
public Guid? ProductId { get; set; }
}
}

View File

@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Contracts.SearchModels
{
public class ProductSearchModel
{
public Guid? Id { get; set; }
public string? Name { get; set; }
public double? Price { get; set; }
public double? Rate { get; set; }
public int? Amount { get; set; }
public bool? IsBeingSold { get; set; }
}
}

View File

@ -0,0 +1,20 @@
using DataModels.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Contracts.SearchModels
{
public class PurchaseSearchModel
{
public Guid? Id { get; set; }
public DateTime? DateFrom { get; set; }
public DateTime? DateTo { get; set; }
public double? PriceFrom { get; set; }
public double? PriceTo { get; set; }
public PurchaseStatus? Status { get; set; }
}
}

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Contracts.SearchModels
{
public class SellSearchModel
{
public Guid? Id { get; set; }
public DateTime? DateFrom { get; set; }
public DateTime? DateTo { get; set; }
public Guid? SupplyDocId { get; set; }
}
}

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Contracts.SearchModels
{
public class SupplierSearchModel
{
public Guid? Id { get; set; }
public string? Name { get; set; }
//public List<ProductSearchModel>? AvailibleProducts { get; set; }
public int? Deals { get; set; }
}
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Contracts.SearchModels
{
public class SupplyDocSearchModel
{
public Guid? Id { get; set; }
public string? Name { get; set; }
public Guid? SupplyId { get; set; }
}
}

View File

@ -0,0 +1,18 @@
using DataModels.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Contracts.SearchModels
{
public class SupplySearchModel
{
public Guid? Id { get; set; }
public string Name { get; set; } = string.Empty;
public SupplyStatus? Status { get; set; }
public DateTime? DateStart { get; set; }
public DateTime? DateEnd { get; set; }
}
}

View File

@ -0,0 +1,21 @@
using Contracts.BindingModels;
using Contracts.SearchModels;
using Contracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Contracts.StorageContracts
{
public interface IProductStorage
{
List<ProductViewModel> GetFullList();
List<ProductViewModel> GetFilteredList(ProductSearchModel model);
ProductViewModel? GetElement(ProductSearchModel model);
ProductViewModel? Insert(ProductBindingModel model);
ProductViewModel? Update(ProductBindingModel model);
ProductViewModel? Delete(ProductBindingModel model);
}
}

View File

@ -0,0 +1,23 @@
using Contracts.BindingModels;
using Contracts.SearchModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Contracts.StorageContracts
{
public interface IPurchaseStorage
{
PurchaseBindingModel? Insert(PurchaseBindingModel model);
IEnumerable<PurchaseBindingModel> GetList(PurchaseSearchModel? model);
PurchaseBindingModel? GetElement(PurchaseSearchModel model);
PurchaseBindingModel? Update(PurchaseBindingModel model);
PurchaseBindingModel? Delete(PurchaseSearchModel model);
}
}

View File

@ -0,0 +1,23 @@
using Contracts.BindingModels;
using Contracts.SearchModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Contracts.StorageContracts
{
public interface ISellStorage
{
SellBindingModel? Insert(SellBindingModel model);
IEnumerable<SellBindingModel> GetList(SellSearchModel? model);
SellBindingModel? GetElement(SellSearchModel model);
SellBindingModel? Update(SellBindingModel model);
SellBindingModel? Delete(SellSearchModel model);
}
}

View File

@ -0,0 +1,21 @@
using Contracts.BindingModels;
using Contracts.SearchModels;
using Contracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Contracts.StorageContracts
{
public interface ISupplierStorage
{
List<SupplierViewModel> GetFullList();
List<SupplierViewModel> GetFilteredList(SupplierSearchModel model);
SupplierViewModel? GetElement(SupplierSearchModel model);
SupplierViewModel? Insert(SupplierBindingModel model);
SupplierViewModel? Update(SupplierBindingModel model);
SupplierViewModel? Delete(SupplierBindingModel model);
}
}

View File

@ -0,0 +1,21 @@
using Contracts.BindingModels;
using Contracts.SearchModels;
using Contracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Contracts.StorageContracts
{
public interface ISupplyDocStorage
{
List<SupplyDocViewModel> GetFullList();
List<SupplyDocViewModel> GetFilteredList(SupplyDocSearchModel model);
SupplyDocViewModel? GetElement(SupplyDocSearchModel model);
SupplyDocViewModel? Insert(SupplyDocBindingModel model);
SupplyDocViewModel? Update(SupplyDocBindingModel model);
SupplyDocViewModel? Delete(SupplyDocBindingModel model);
}
}

View File

@ -0,0 +1,21 @@
using Contracts.BindingModels;
using Contracts.SearchModels;
using Contracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Contracts.StorageContracts
{
public interface ISupplyStorage
{
List<SupplyViewModel> GetFullList();
List<SupplyViewModel> GetFilteredList(SupplySearchModel model);
SupplyViewModel? GetElement(SupplySearchModel model);
SupplyViewModel? Insert(SupplyBindingModel model);
SupplyViewModel? Update(SupplyBindingModel model);
SupplyViewModel? Delete(SupplyBindingModel model);
}
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Contracts.ViewModels
{
public class EmployeeProductViewModel
{
public Guid Guid { get; set; }
public string Name { get; set; } = string.Empty;
public double Price { get; set; }
}
}

View File

@ -0,0 +1,29 @@
using Contracts.BindingModels;
using DataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Contracts.ViewModels
{
public class MediaFileViewModel
{
public Guid Id { get; set; }
public string Location { get; set; } = string.Empty;
public string Name { get; set; } = string.Empty;
public Guid ProductId { get; set; }
public MediaFileBindingModel GetBindingModel()
{
return new MediaFileBindingModel
{
Id = Id,
Name = Name,
Location = Location,
ProductId = ProductId
};
}
}
}

View File

@ -0,0 +1,20 @@
using DataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Contracts.ViewModels
{
public class ProductViewModel
{
public Guid Id { get; set; }
public string Name { get; set; } = string.Empty;
public double Price { get; set; }
public double Rating { get; set; }
public bool IsBeingSold { get; set; }
public int Amount { get; set; }
public List<MediaFileViewModel> MediaFiles { get; set; } = new();
}
}

View File

@ -0,0 +1,19 @@
using DataModels.Enums;
using DataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Contracts.ViewModels
{
public class PurchaseViewModel
{
public Guid Id { get; set; }
public DateTime DatePurchase { get; set; }
public required IUser User { get; set; }
public required List<IProduct> Products { get; set; }
public PurchaseStatus Status { get; set; }
}
}

View File

@ -0,0 +1,16 @@
using DataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Contracts.ViewModels
{
public class SellViewModel
{
public Guid Id { get; set; }
public DateTime DateSell { get; set; }
public ISupplyDoc? SupplyDoc { get; set; }
}
}

View File

@ -0,0 +1,16 @@
using DataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Contracts.ViewModels
{
public class SupplierViewModel
{
public Guid Id { get; set; }
public string Name { get; set; } = string.Empty;
public Dictionary<Guid, (IProduct, int)> AvailibleProducts = new();
}
}

View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Contracts.ViewModels
{
public class SupplyDocViewModel
{
public Guid Id { get; set; }
public string Name { get; set; } = string.Empty;
}
}

View File

@ -0,0 +1,23 @@
using DataModels.Enums;
using DataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Contracts.ViewModels
{
public class SupplyViewModel
{
public Guid Id { get; set; }
public string Name { get; set; } = string.Empty;
public Guid SupplierId { get; set; }
public string SupplierName { get; set; } = string.Empty;
public double Price { get; set; }
public Dictionary<Guid, (IProduct, int)> Products { get; set; } = new();
public DateTime Date { get; set; }
public SupplyStatus Status { get; set; }
}
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Contracts.ViewModels
{
public class UserProductViewModel
{
public string Name { get; set; } = string.Empty;
public double Price { get; set; }
public double Rate { get; set; }
}
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataModels.Enums
{
public enum PurchaseStatus
{
Unknown = -1,
Processing = 0,
Complited = 1
}
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataModels.Enums
{
public enum SupplyStatus
{
Pending = -1,
Arriving = 0,
Completed = 1
}
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataModels.Models
{
public interface IMediaFile : IId
{
string Name { get; }
string Location { get; }
Guid ProductId { get; }
}
}

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataModels.Models
{
public interface IProduct : IId
{
string Name { get; }
double Price { get; }
bool IsBeingSold { get; }
public double Rate { get; }
int Amount { get; }
// будут браться через mediafilestorage так что скорее всего это тут не надо
// List<IMediaFile> MediaFiles { get; }
}
}

View File

@ -0,0 +1,15 @@
using DataModels.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataModels.Models
{
public interface IPurchase : IId
{
DateTime DatePurchase { get; }
PurchaseStatus Status { get; }
}
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataModels.Models
{
public interface ISell : IId
{
DateTime DateSell { get; }
}
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataModels.Models
{
public interface ISupplier : IId
{
string Name { get; }
Dictionary<Guid, (IProduct, int)> AvailibleProducts { get; }
int Deals { get; }
}
}

View File

@ -0,0 +1,19 @@
using DataModels.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataModels.Models
{
public interface ISupply: IId
{
string Name { get; }
double Price { get; }
Guid SupplierId { get; }
DateTime Date { get; }
SupplyStatus Status { get; }
Dictionary<Guid, (IProduct, int)> SupplyProducts { get; }
}
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataModels.Models
{
public interface ISupplyDoc : IId
{
string Name { get; }
Guid SupplyId { get; }
}
}

View File

@ -21,5 +21,14 @@ namespace DatabaseImplement
public virtual DbSet<Role> Roles { get; set; } = null!;
public virtual DbSet<User> Users { get; set; } = null!;
public virtual DbSet<Sell> Sells { get; set; } = null!;
public virtual DbSet<Purchase> Purchases { 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<Supplier> Suppliers { get; set; } = null!;
public virtual DbSet<SupplierProduct> SupplierProducts { get; set; } = null!;
public virtual DbSet<MediaFile> MediaFiles { get; set; } = null!;
}
}

View File

@ -0,0 +1,161 @@
using Contracts.BindingModels;
using Contracts.SearchModels;
using Contracts.StorageContracts;
using Contracts.ViewModels;
using DatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DatabaseImplement.Implements
{
public class ProductStorage : IProductStorage
{
public ProductViewModel? Delete(ProductBindingModel model)
{
using var context = new Database();
var element = context.Products
.Include(x => x.Name)
.Include(x => x.Price)
.Include(x => x.IsBeingSold)
.Include(x => x.Rate)
.Include(x => x.Amount)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Products.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public ProductViewModel? GetElement(ProductSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new Database();
return context.Products
.Include(x => x.Name)
.Include(x => x.Price)
.Include(x => x.IsBeingSold)
.Include(x => x.Rate)
.Include(x => x.Amount)
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public List<ProductViewModel> GetFilteredList(ProductSearchModel model)
{
if (!model.IsBeingSold.HasValue && !model.Price.HasValue && !model.Rate.HasValue && !model.Amount.HasValue && string.IsNullOrEmpty(model.Name))
{
return new();
}
using var context = new Database();
if (model.Price.HasValue)
{
return context.Products
.Include(x => x.Name)
.Include(x => x.Price)
.Include(x => x.IsBeingSold)
.Include(x => x.Rate)
.Include(x => x.Amount)
.Where(x => x.Price <= model.Price)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
if (model.Rate.HasValue)
{
return context.Products
.Include(x => x.Name)
.Include(x => x.Price)
.Include(x => x.IsBeingSold)
.Include(x => x.Rate)
.Include(x => x.Amount)
.Where(x => x.Rate <= model.Rate)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
if (model.Amount.HasValue && model.IsBeingSold.HasValue)
{
return context.Products
.Include(x => x.Name)
.Include(x => x.Price)
.Include(x => x.IsBeingSold)
.Include(x => x.Rate)
.Include(x => x.Amount)
.Where(x => x.IsBeingSold == model.IsBeingSold && x.Amount >= model.Amount)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
return context.Products
.Include(x => x.Name)
.Include(x => x.Price)
.Include(x => x.IsBeingSold)
.Include(x => x.Rate)
.Include(x => x.Amount)
.Where(x => x.Name == model.Name)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<ProductViewModel> GetFullList()
{
using var context = new Database();
return context.Products
.Include(x => x.Name)
.Include(x => x.Price)
.Include(x => x.IsBeingSold)
.Include(x => x.Rate)
.Include(x => x.Amount)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public ProductViewModel? Insert(ProductBindingModel model)
{
using var context = new Database();
var newProduct = Product.Create(context, model);
if (newProduct == null)
{
return null;
}
context.Products.Add(newProduct);
context.SaveChanges();
return newProduct.GetViewModel;
}
public ProductViewModel? Update(ProductBindingModel model)
{
using var context = new Database();
using var transaction = context.Database.BeginTransaction();
try
{
var product = context.Products.FirstOrDefault(rec =>
rec.Id == model.Id);
if (product == null)
{
return null;
}
product.Update(model);
context.SaveChanges();
transaction.Commit();
return product.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
}
}

View File

@ -0,0 +1,121 @@
using Contracts.BindingModels;
using Contracts.SearchModels;
using Contracts.StorageContracts;
using Contracts.ViewModels;
using DatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DatabaseImplement.Implements
{
public class SupplierStorage : ISupplierStorage
{
public SupplierViewModel? Delete(SupplierBindingModel model)
{
using var context = new Database();
var element = context.Suppliers
.Include(x => x.Products)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Suppliers.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public SupplierViewModel? GetElement(SupplierSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new Database();
return context.Suppliers
.Include(x => x.Name)
.Include(x => x.Products)
.ThenInclude(x => x.Product)
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public List<SupplierViewModel> GetFilteredList(SupplierSearchModel model)
{
if (string.IsNullOrEmpty(model.Name) && !model.Deals.HasValue)
{
return new();
}
using var context = new Database();
if (!string.IsNullOrEmpty(model.Name))
{
return context.Suppliers
.Include(x => x.Products)
.ThenInclude(x => x.Product)
.Where(x => model.Name == x.Name)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
return context.Suppliers
.Include(x => x.Products)
.ThenInclude(x => x.Product)
.Where(x => model.Deals <= x.Deals)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<SupplierViewModel> GetFullList()
{
using var context = new Database();
return context.Suppliers
.Include(x => x.Products)
.ThenInclude(x => x.Product)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public SupplierViewModel? Insert(SupplierBindingModel model)
{
using var context = new Database();
var newProduct = Supplier.Create(context, model);
if (newProduct == null)
{
return null;
}
context.Suppliers.Add(newProduct);
context.SaveChanges();
return newProduct.GetViewModel;
}
public SupplierViewModel? Update(SupplierBindingModel model)
{
using var context = new Database();
using var transaction = context.Database.BeginTransaction();
try
{
var product = context.Suppliers.FirstOrDefault(rec =>
rec.Id == model.Id);
if (product == null)
{
return null;
}
product.Update(context, model);
context.SaveChanges();
product.UpdateProducts(context, model);
transaction.Commit();
return product.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
}
}

View File

@ -0,0 +1,147 @@
using Contracts.BindingModels;
using Contracts.SearchModels;
using Contracts.StorageContracts;
using Contracts.ViewModels;
using DatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DatabaseImplement.Implements
{
public class SupplyStorage : ISupplyStorage
{
public SupplyViewModel? Delete(SupplyBindingModel model)
{
using var context = new Database();
var element = context.Supplies
.Include(x => x.Products)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Supplies.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public SupplyViewModel? GetElement(SupplySearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new Database();
return context.Supplies
.Include(x => x.Supplier)
.Include(x => x.Products)
.ThenInclude(x => x.Product)
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public List<SupplyViewModel> GetFilteredList(SupplySearchModel model)
{
if (!model.DateStart.HasValue && !model.DateEnd.HasValue && model.Status == null)
{
return new();
}
using var context = new Database();
if (model.DateStart.HasValue && model.DateEnd.HasValue)
{
return context.Supplies
.Include(x => x.Supplier)
.Include(x => x.Products)
.ThenInclude(x => x.Product)
.Where(x => x.Id == model.Id || model.DateStart <= x.Date && x.Date <= model.DateEnd)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
if (model.DateEnd.HasValue)
{
return context.Supplies
.Include(x => x.Supplier)
.Include(x => x.Products)
.ThenInclude(x => x.Product)
.Where(x => x.Id == model.Id || x.Date <= model.DateEnd)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
if (model.DateStart.HasValue)
{
return context.Supplies
.Include(x => x.Supplier)
.Include(x => x.Products)
.ThenInclude(x => x.Product)
.Where(x => x.Id == model.Id || model.DateStart <= x.Date)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
return context.Supplies
.Include(x => x.Supplier)
.Include(x => x.Products)
.ThenInclude(x => x.Product)
.Where(x => model.Status.Equals(x.Status))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<SupplyViewModel> GetFullList()
{
using var context = new Database();
return context.Supplies
.Include(x => x.Supplier)
.Include(x => x.Products)
.ThenInclude(x => x.Product)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public SupplyViewModel? Insert(SupplyBindingModel model)
{
using var context = new Database();
var newProduct = Supply.Create(context, model);
if (newProduct == null)
{
return null;
}
context.Supplies.Add(newProduct);
context.SaveChanges();
return newProduct.GetViewModel;
}
public SupplyViewModel? Update(SupplyBindingModel model)
{
using var context = new Database();
using var transaction = context.Database.BeginTransaction();
try
{
var product = context.Supplies.FirstOrDefault(rec =>
rec.Id == model.Id);
if (product == null)
{
return null;
}
product.Update(model);
context.SaveChanges();
product.UpdateProducts(context, model);
transaction.Commit();
return product.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
}
}

View File

@ -0,0 +1,70 @@
using Contracts.BindingModels;
using Contracts.ViewModels;
using DataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DatabaseImplement.Models
{
public class MediaFile : IMediaFile
{
[Required]
public Guid Id { get; set; }
[Required]
public string Name { get; set; } = string.Empty;
[Required]
public string Location { get; set; } = string.Empty;
[Required]
public Guid ProductId { get; set; }
public MediaFileBindingModel GetBindingModel()
{
return new MediaFileBindingModel
{
Id = Id,
Name = Name,
Location = Location,
ProductId = ProductId
};
}
public static MediaFile ToMediaFileFromView(MediaFileViewModel model, MediaFile mediaFile)
{
return new MediaFile
{
Id = model.Id,
Name = model.Name,
Location = model.Location,
ProductId = model.ProductId
};
}
public static MediaFile ToMediaFileFromBinding(MediaFileBindingModel model, MediaFile mediaFile)
{
return new MediaFile
{
Id = model.Id,
Name = model.Name,
Location = model.Location,
ProductId = model.ProductId
};
}
public void Update(MediaFileBindingModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
// Обновление свойств на основе модели привязки
Name = model.Name;
Location = model.Location;
ProductId = model.ProductId;
}
}
}

View File

@ -0,0 +1,104 @@
using Contracts.BindingModels;
using Contracts.ViewModels;
using DataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType;
namespace DatabaseImplement.Models
{
public class Product : IProduct
{
[Required]
public Guid Id { get; set; }
[Required]
public string Name { get; set; } = string.Empty;
[Required]
public double Price { get; set; }
[Required]
public double Rate { get; set; }
[Required]
public bool IsBeingSold { get; set; }
[Required]
public int Amount { get; set; }
public ProductBindingModel GetBindingModel() => new()
{
Id = Id,
Name = Name,
Price = Price,
Rate = Rate,
IsBeingSold = IsBeingSold,
Amount = Amount
};
public static Product ToProductFromView(ProductViewModel model, Product product) => new()
{
Id = model.Id,
Name = model.Name,
Price = model.Price,
Rate = model.Rating,
IsBeingSold = model.IsBeingSold,
Amount = model.Amount
};
public static Product ToProductFromBinding(ProductBindingModel model, Product product) => new()
{
Id = model.Id,
Name = model.Name,
Price = model.Price,
Rate = model.Rate,
IsBeingSold = model.IsBeingSold,
Amount = model.Amount
};
public static Product Create(Database context, ProductBindingModel model)
{
return new Product()
{
Id = model.Id,
Name = model.Name,
Price = model.Price,
Rate = model.Rate,
IsBeingSold = model.IsBeingSold,
Amount = model.Amount
};
}
public void Update(ProductBindingModel model)
{
if (model is null)
{
throw new ArgumentNullException("Update product: binding model is null");
}
Name = model.Name;
Price = model.Price;
Rate = model.Rate;
IsBeingSold = model.IsBeingSold;
Amount = model.Amount;
}
public ProductViewModel GetViewModel
{
get
{
var context = new Database();
return new()
{
Id = Id,
Name = Name,
Price = Price,
IsBeingSold = IsBeingSold,
Rating = Rate,
Amount = Amount
};
}
}
}
}

View File

@ -0,0 +1,49 @@
using Contracts.BindingModels;
using Contracts.ViewModels;
using DataModels.Enums;
using DataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DatabaseImplement.Models
{
public class Purchase : IPurchase
{
public Guid Id { get; set; }
[Required]
public DateTime DatePurchase { get; set; }
[Required]
public PurchaseStatus Status { get; private set; } = PurchaseStatus.Unknown;
public PurchaseBindingModel GetBindingModel() => new()
{
Id = Id,
DatePurchase = DatePurchase
};
public static Purchase ToPurchaseFromView(PurchaseViewModel model, Purchase purchase) => new()
{
Id = model.Id,
DatePurchase = model.DatePurchase
};
public static Purchase ToPurchaseFromBinding(PurchaseBindingModel model, Purchase purchase) => new()
{
Id = model.Id,
DatePurchase = model.DatePurchase,
};
public void Update(PurchaseBindingModel model, Purchase purchase)
{
if (model is null)
{
throw new ArgumentNullException("Update purchase: binding model is null");
}
DatePurchase = purchase.DatePurchase;
}
}
}

View File

@ -0,0 +1,44 @@
using Contracts.BindingModels;
using Contracts.ViewModels;
using DataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DatabaseImplement.Models
{
public class Sell : ISell
{
public Guid Id { get; set; }
public DateTime DateSell { get; set; }
public SellBindingModel GetBindingModel() => new()
{
Id = Id,
DateSell = DateSell
};
public static Sell ToSellFromView(SellViewModel model, Sell sell) => new()
{
Id = model.Id,
DateSell = model.DateSell
};
public static Sell ToSellFromBinding(SellBindingModel model, Sell sell) => new()
{
Id = model.Id,
DateSell = model.DateSell,
};
public void Update(SellBindingModel model, Sell sell)
{
if (model is null)
{
throw new ArgumentNullException("Update user: binding model is null");
}
DateSell = sell.DateSell;
}
}
}

View File

@ -0,0 +1,109 @@
using Contracts.BindingModels;
using Contracts.ViewModels;
using DataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType;
namespace DatabaseImplement.Models
{
public class Supplier : ISupplier
{
public Guid Id { get; set; }
[Required]
public string Name { get; set; } = string.Empty;
[Required]
public int Deals { get; set; }
private Dictionary<Guid, (IProduct, int)>? _availibleProducts = null;
[NotMapped]
public Dictionary<Guid, (IProduct, int)> AvailibleProducts
{
get
{
if (_availibleProducts == null)
{
_availibleProducts = Products
.ToDictionary(recPC => recPC.Id, recPC =>
(recPC.Product as IProduct, recPC.Count));
}
return _availibleProducts;
}
}
[ForeignKey("SupplierId")]
public virtual List<SupplierProduct> Products { get; set; } = new();
public static Supplier Create(Database context, SupplierBindingModel model)
{
return new Supplier()
{
Id = model.Id,
Name = model.Name,
Products = model.AvailibleProducts.Select(x => new
SupplierProduct
{
Product = context.Products.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(Database context, SupplierBindingModel model)
{
Name = model.Name;
Products = model.AvailibleProducts.Select(x => new
SupplierProduct
{
Product = context.Products.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList();
}
public SupplierViewModel GetViewModel
{
get
{
var context = new Database();
return new()
{
Id = Id,
Name = Name,
AvailibleProducts = AvailibleProducts
};
}
}
public void UpdateProducts(Database context, SupplierBindingModel model)
{
var supplierProducts = context.SupplierProducts.Where(rec =>
rec.Id == model.Id).ToList();
if (supplierProducts != null && supplierProducts.Count > 0)
{ // удалили те, которых нет в модели
context.SupplierProducts.RemoveRange(supplierProducts.Where(rec
=> !model.AvailibleProducts.ContainsKey(rec.ProductId)));
context.SaveChanges();
// обновили количество у существующих записей
foreach (var updateProduct in supplierProducts)
{
updateProduct.Count = model.AvailibleProducts[updateProduct.ProductId].Item2;
model.AvailibleProducts.Remove(updateProduct.ProductId);
}
context.SaveChanges();
}
var supplier = context.Suppliers.First(x => x.Id == Id);
foreach (var pc in model.AvailibleProducts)
{
context.SupplierProducts.Add(new SupplierProduct
{
Supplier = supplier,
Product = context.Products.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();
}
_availibleProducts = null;
}
}
}

View File

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

View File

@ -0,0 +1,118 @@
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
{
public Guid Id { get; set; }
[Required]
public string Name { get; set; } = string.Empty;
[Required]
public double Price { get; set; }
[Required]
public Guid SupplierId { 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 virtual Supplier Supplier { get; set; }
public static Supply Create(Database context, SupplyBindingModel model)
{
return new Supply()
{
Id = model.Id,
Name = model.Name,
Price = model.Price,
Date = model.Date,
SupplierId = model.SupplierId,
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
{
get
{
var context = new Database();
return new()
{
Id = Id,
Name = Name,
Price = Price,
Products = SupplyProducts,
Date = Date,
Status = Status,
SupplierName = context.Suppliers.FirstOrDefault(x => x.Id == Id)?.Name ?? string.Empty,
};
}
}
public void UpdateProducts(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();
}
}