diff --git a/BusinessLogic/BusinessLogic/SellLogic.cs b/BusinessLogic/BusinessLogic/SellLogic.cs index 9561f4f..aacd8e5 100644 --- a/BusinessLogic/BusinessLogic/SellLogic.cs +++ b/BusinessLogic/BusinessLogic/SellLogic.cs @@ -1,4 +1,11 @@ -using System; +using Contracts.BindingModels; +using Contracts.Converters; +using Contracts.SearchModels; +using Contracts.StorageContracts; +using Contracts.ViewModels; +using DatabaseImplement.Models; +using Microsoft.Extensions.Logging; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,7 +13,65 @@ using System.Threading.Tasks; namespace BusinessLogic.BusinessLogic { - internal class SellLogic + public class SellLogic { + private readonly ISellStorage _sellStorage; + private readonly ILogger _logger; + + public SellLogic(ISellStorage sellStorage, ILogger logger) + { + _sellStorage = sellStorage; + _logger = logger; + } + public SellViewModel Create(SellBindingModel model) + { + ArgumentNullException.ThrowIfNull(model); + var sell = _sellStorage.Insert(model); + if (sell is null) + { + throw new Exception("Insert operation failed."); + } + return sell; + } + public SellViewModel GetElement(SellSearchModel model) + { + ArgumentNullException.ThrowIfNull(model); + var sell = _sellStorage.GetElement(model); + if (sell is null) + { + throw new Exception("Get element operation failed."); + } + return sell; + } + public IEnumerable GetElements(SellSearchModel? model) + { + var sell_list = _sellStorage.GetList(model); + if (sell_list is null || sell_list.Count() == 0) + { + _logger.LogWarning("ReadList return null list"); + return []; + } + return sell_list; + } + public SellViewModel Update(SellSearchModel model) + { + ArgumentNullException.ThrowIfNull(model); + var sell = _sellStorage.GetElement(model); + if (sell is null) + { + throw new Exception("Update operation failed."); + } + return sell; + } + public SellViewModel Delete(SellSearchModel model) + { + ArgumentNullException.ThrowIfNull(model); + var sell = _sellStorage.Delete(model); + if (sell is null) + { + throw new Exception("Update operation failed."); + } + return sell; + } } } diff --git a/Contracts/BindingModels/SellBindingModel.cs b/Contracts/BindingModels/SellBindingModel.cs index e0c32ed..0d93bcb 100644 --- a/Contracts/BindingModels/SellBindingModel.cs +++ b/Contracts/BindingModels/SellBindingModel.cs @@ -10,5 +10,6 @@ namespace Contracts.BindingModels { public Guid Id { get; set; } public DateTime DateSell { get; set; } - } + public Guid? SupplyDocId { get; set; } + } } diff --git a/Contracts/Converters/SellConverter.cs b/Contracts/Converters/SellConverter.cs index 556c9b1..0acd7fe 100644 --- a/Contracts/Converters/SellConverter.cs +++ b/Contracts/Converters/SellConverter.cs @@ -1,4 +1,7 @@ -using System; +using Contracts.BindingModels; +using Contracts.ViewModels; +using DataModels.Models; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -8,6 +11,18 @@ namespace Contracts.Converters { public class SellConverter { + public static SellViewModel ToView(SellBindingModel model) => new() + { + Id = model.Id, + DateSell = model.DateSell, + SupplyDocId = model.SupplyDocId + }; + public static SellBindingModel ToBinding(SellViewModel model) => new() + { + Id = model.Id, + DateSell = model.DateSell, + SupplyDocId = model.SupplyDocId + }; } } diff --git a/Contracts/StorageContracts/ISellStorage.cs b/Contracts/StorageContracts/ISellStorage.cs index ba31175..acf47f1 100644 --- a/Contracts/StorageContracts/ISellStorage.cs +++ b/Contracts/StorageContracts/ISellStorage.cs @@ -1,5 +1,6 @@ using Contracts.BindingModels; using Contracts.SearchModels; +using Contracts.ViewModels; using System; using System.Collections.Generic; using System.Linq; @@ -10,14 +11,15 @@ namespace Contracts.StorageContracts { public interface ISellStorage { - SellBindingModel? Insert(SellBindingModel model); + SellViewModel? Insert(SellBindingModel model); - IEnumerable GetList(SellSearchModel? model); + IEnumerable GetFullList(SellSearchModel? model); + IEnumerable GetFilteredList(SellSearchModel? model); - SellBindingModel? GetElement(SellSearchModel model); + SellViewModel? GetElement(SellSearchModel model); - SellBindingModel? Update(SellBindingModel model); + SellViewModel? Update(SellBindingModel model); - SellBindingModel? Delete(SellSearchModel model); + SellViewModel? Delete(SellSearchModel model); } } diff --git a/Contracts/ViewModels/SellViewModel.cs b/Contracts/ViewModels/SellViewModel.cs index 285d63f..3d09e5a 100644 --- a/Contracts/ViewModels/SellViewModel.cs +++ b/Contracts/ViewModels/SellViewModel.cs @@ -11,6 +11,6 @@ namespace Contracts.ViewModels { public Guid Id { get; set; } public DateTime DateSell { get; set; } - public ISupplyDoc? SupplyDoc { get; set; } + public Guid? SupplyDocId { get; set; } } } diff --git a/DatabaseImplement/Database.cs b/DatabaseImplement/Database.cs index 193acaf..c3284c4 100644 --- a/DatabaseImplement/Database.cs +++ b/DatabaseImplement/Database.cs @@ -30,5 +30,6 @@ namespace DatabaseImplement public virtual DbSet SupplierProducts { get; set; } = null!; public virtual DbSet MediaFiles { get; set; } = null!; public virtual DbSet PurchaseProducts { get; set; } = null!; - } + public virtual DbSet SupplyDocs { get; set; } = null!; + } } \ No newline at end of file diff --git a/DatabaseImplement/Implements/SellStorage.cs b/DatabaseImplement/Implements/SellStorage.cs index 533486d..090188f 100644 --- a/DatabaseImplement/Implements/SellStorage.cs +++ b/DatabaseImplement/Implements/SellStorage.cs @@ -1,6 +1,10 @@ using Contracts.BindingModels; +using Contracts.Converters; using Contracts.SearchModels; using Contracts.StorageContracts; +using Contracts.ViewModels; +using DatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; @@ -11,29 +15,65 @@ namespace DatabaseImplement.Implements { public class SellStorage : ISellStorage { - public SellBindingModel? Delete(SellSearchModel model) + public SellViewModel? Delete(SellSearchModel model) { throw new NotImplementedException(); } - public SellBindingModel? GetElement(SellSearchModel model) + public SellViewModel? GetElement(SellSearchModel model) { throw new NotImplementedException(); } - public IEnumerable GetList(SellSearchModel? model) + public IEnumerable GetFilteredList(SellSearchModel? model) + { + throw new NotImplementedException(); + } + + public IEnumerable GetFullList(SellSearchModel? model) { - throw new NotImplementedException(); + using var context = new Database(); + return context.Sells + .Include(x => x.SupplyDoc) + .ToList() + .Select((Sell sell, int index) => sell.GetViewModel()) + .ToList(); + } + + public SellViewModel? Insert(SellBindingModel model) + { + using var context = new Database(); + var sell = Sell.Create(context, model); + if (sell == null) + { + return null; + } + context.Sells.Add(sell); + context.SaveChanges(); + return sell.GetViewModel(); } - public SellBindingModel? Insert(SellBindingModel model) + public SellViewModel? Update(SellBindingModel model) { - throw new NotImplementedException(); - } - - public SellBindingModel? Update(SellBindingModel model) - { - throw new NotImplementedException(); - } + using var context = new Database(); + using var transaction = context.Database.BeginTransaction(); + try + { + var sell = context.Sells.FirstOrDefault(rec => rec.Id == model.Id); + if (sell == null) + { + return null; + } + sell.Update(model); + context.SaveChanges(); + transaction.Commit(); + return sell.GetViewModel(); + } + catch + { + transaction.Rollback(); + throw; + } + } } } diff --git a/DatabaseImplement/Migrations/DatabaseModelSnapshot.cs b/DatabaseImplement/Migrations/DatabaseModelSnapshot.cs index 0368a1c..b430445 100644 --- a/DatabaseImplement/Migrations/DatabaseModelSnapshot.cs +++ b/DatabaseImplement/Migrations/DatabaseModelSnapshot.cs @@ -41,7 +41,7 @@ namespace DatabaseImplement.Migrations b.HasKey("Id"); - b.ToTable("MediaFiles"); + b.ToTable("MediaFiles", (string)null); }); modelBuilder.Entity("DatabaseImplement.Models.Product", b => @@ -68,7 +68,7 @@ namespace DatabaseImplement.Migrations b.HasKey("Id"); - b.ToTable("Products"); + b.ToTable("Products", (string)null); }); modelBuilder.Entity("DatabaseImplement.Models.Purchase", b => @@ -90,7 +90,7 @@ namespace DatabaseImplement.Migrations b.HasIndex("UserId"); - b.ToTable("Purchases"); + b.ToTable("Purchases", (string)null); }); modelBuilder.Entity("DatabaseImplement.Models.PurchaseProducts", b => @@ -114,7 +114,7 @@ namespace DatabaseImplement.Migrations b.HasIndex("PurchaseId"); - b.ToTable("PurchaseProducts"); + b.ToTable("PurchaseProducts", (string)null); }); modelBuilder.Entity("DatabaseImplement.Models.Role", b => @@ -129,7 +129,7 @@ namespace DatabaseImplement.Migrations b.HasKey("Id"); - b.ToTable("Roles"); + b.ToTable("Roles", (string)null); }); modelBuilder.Entity("DatabaseImplement.Models.Sell", b => @@ -143,7 +143,7 @@ namespace DatabaseImplement.Migrations b.HasKey("Id"); - b.ToTable("Sells"); + b.ToTable("Sells", (string)null); }); modelBuilder.Entity("DatabaseImplement.Models.Supplier", b => @@ -161,7 +161,7 @@ namespace DatabaseImplement.Migrations b.HasKey("Id"); - b.ToTable("Suppliers"); + b.ToTable("Suppliers", (string)null); }); modelBuilder.Entity("DatabaseImplement.Models.SupplierProduct", b => @@ -185,7 +185,7 @@ namespace DatabaseImplement.Migrations b.HasIndex("SupplierId"); - b.ToTable("SupplierProducts"); + b.ToTable("SupplierProducts", (string)null); }); modelBuilder.Entity("DatabaseImplement.Models.Supply", b => @@ -214,7 +214,7 @@ namespace DatabaseImplement.Migrations b.HasIndex("SupplierId"); - b.ToTable("Supplies"); + b.ToTable("Supplies", (string)null); }); modelBuilder.Entity("DatabaseImplement.Models.SupplyProduct", b => @@ -238,7 +238,7 @@ namespace DatabaseImplement.Migrations b.HasIndex("SupplyId"); - b.ToTable("SupplyProducts"); + b.ToTable("SupplyProducts", (string)null); }); modelBuilder.Entity("DatabaseImplement.Models.User", b => @@ -276,7 +276,7 @@ namespace DatabaseImplement.Migrations b.HasIndex("RoleId"); - b.ToTable("Users"); + b.ToTable("Users", (string)null); }); modelBuilder.Entity("DatabaseImplement.Models.Purchase", b => diff --git a/DatabaseImplement/Models/Product.cs b/DatabaseImplement/Models/Product.cs index 70da3ba..fd8053e 100644 --- a/DatabaseImplement/Models/Product.cs +++ b/DatabaseImplement/Models/Product.cs @@ -43,7 +43,7 @@ namespace DatabaseImplement.Models Id = model.Id, Name = model.Name, Price = model.Price, - Rate = model.Rate, + Rate = model.Rating, IsBeingSold = model.IsBeingSold, Amount = model.Amount }; @@ -96,7 +96,7 @@ namespace DatabaseImplement.Models Name = Name, Price = Price, IsBeingSold = IsBeingSold, - Rate = Rate, + Rating = Rate, Amount = Amount }; } diff --git a/DatabaseImplement/Models/Purchase.cs b/DatabaseImplement/Models/Purchase.cs index 35e9041..a3e999e 100644 --- a/DatabaseImplement/Models/Purchase.cs +++ b/DatabaseImplement/Models/Purchase.cs @@ -41,6 +41,16 @@ namespace DatabaseImplement.Models } [ForeignKey("PurchaseId")] public virtual List Products { get; set; } = new(); + public static Purchase Create(Database context, SellBindingModel model) + { + return new Purchase() + { + DatePurchase = DateTime.Now, + UserId = Guid.NewGuid(), + Status = PurchaseStatus.Unknown, + + }; + } public PurchaseBindingModel GetBindingModel() => new() { Id = Id, diff --git a/DatabaseImplement/Models/Sell.cs b/DatabaseImplement/Models/Sell.cs index 03a7f26..74a441e 100644 --- a/DatabaseImplement/Models/Sell.cs +++ b/DatabaseImplement/Models/Sell.cs @@ -1,4 +1,5 @@ using Contracts.BindingModels; +using Contracts.Converters; using Contracts.ViewModels; using DataModels.Models; using System; @@ -13,13 +14,33 @@ namespace DatabaseImplement.Models { public Guid Id { get; set; } public DateTime DateSell { get; set; } + public Guid? SupplyDocId { get; set; } + + public virtual SupplyDoc? SupplyDoc { get; set; } + public static Sell Create(Database context, SellBindingModel model) + { + return new Sell() + { + Id = model.Id, + DateSell = model.DateSell, + SupplyDocId = model.SupplyDocId, + }; + } public SellBindingModel GetBindingModel() => new() { Id = Id, - DateSell = DateSell + DateSell = DateSell, + SupplyDocId = SupplyDocId, }; - public static Sell ToSellFromView(SellViewModel model, Sell sell) => new() + public SellViewModel GetViewModel() => new() + { + Id = Id, + DateSell = DateSell, + SupplyDocId = SupplyDocId, + + }; + public static Sell ToSellFromView(SellViewModel model, Sell sell) => new() { Id = model.Id, DateSell = model.DateSell @@ -31,14 +52,15 @@ namespace DatabaseImplement.Models DateSell = model.DateSell, }; - public void Update(SellBindingModel model, Sell sell) + public void Update(SellBindingModel model) { if (model is null) { throw new ArgumentNullException("Update user: binding model is null"); } - DateSell = sell.DateSell; + DateSell = model.DateSell; + SupplyDocId = model.SupplyDocId; } } } diff --git a/DatabaseImplement/Models/SupplyDoc.cs b/DatabaseImplement/Models/SupplyDoc.cs new file mode 100644 index 0000000..b4aed5a --- /dev/null +++ b/DatabaseImplement/Models/SupplyDoc.cs @@ -0,0 +1,18 @@ +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 SupplyDoc : ISupplyDoc + { + public Guid Id { get; set; } + [Required] + public string Name { get; set; } = string.Empty; + public Guid SupplyId { get; set; } + } +} diff --git a/WebApp/WebApp.csproj b/WebApp/WebApp.csproj index 33e3dc7..8cc73e5 100644 --- a/WebApp/WebApp.csproj +++ b/WebApp/WebApp.csproj @@ -15,4 +15,11 @@ + + + true + PreserveNewest + + + diff --git a/WebApp/wwwroot/favicon.ico b/WebApp/wwwroot/favicon.ico index 63e859b..f5a7905 100644 Binary files a/WebApp/wwwroot/favicon.ico and b/WebApp/wwwroot/favicon.ico differ