From 23449b08ce95cba6ecc5f150e7521bad170f9748 Mon Sep 17 00:00:00 2001 From: Yuee Shiness Date: Wed, 17 May 2023 17:57:25 +0400 Subject: [PATCH] Main controller additions --- .../BindingModels/PCBindingModel.cs | 8 +++++ .../ViewModels/PCViewModel.cs | 13 +++++-- .../ViewModels/RequestViewModel.cs | 2 +- ComputerStoreDataModels/Models/IPCModel.cs | 2 +- .../Implements/PCStorage.cs | 5 +++ .../ComputerStoreDatabaseModelSnapshot.cs | 6 ++-- ComputerStoreDatabaseImplement/Models/PC.cs | 26 ++++++++++---- .../Models/Request.cs | 2 +- .../Models/RequestComponent.cs | 8 ++--- .../Controllers/MainController.cs | 34 ++++++++++++++++++- ComputerStoreRestAPI/Program.cs | 9 ++++- 11 files changed, 93 insertions(+), 22 deletions(-) diff --git a/ComputerStoreContracts/BindingModels/PCBindingModel.cs b/ComputerStoreContracts/BindingModels/PCBindingModel.cs index 0be4156..71600eb 100644 --- a/ComputerStoreContracts/BindingModels/PCBindingModel.cs +++ b/ComputerStoreContracts/BindingModels/PCBindingModel.cs @@ -1,4 +1,5 @@ using ComputerStoreDataModels.Models; +using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; @@ -19,5 +20,12 @@ namespace ComputerStoreContracts.BindingModels public int RequestID { get; set; } public Dictionary PCComponents { get; set; } = new(); + + [JsonConstructor] + public PCBindingModel(Dictionary PCComponents) + { + this.PCComponents = PCComponents.ToDictionary(x => x.Key, x => (x.Value.Component as IComponentModel, x.Value.Quantity)); + } + public PCBindingModel() { } } } diff --git a/ComputerStoreContracts/ViewModels/PCViewModel.cs b/ComputerStoreContracts/ViewModels/PCViewModel.cs index 17f350b..9f4b8df 100644 --- a/ComputerStoreContracts/ViewModels/PCViewModel.cs +++ b/ComputerStoreContracts/ViewModels/PCViewModel.cs @@ -1,4 +1,6 @@ -using ComputerStoreDataModels.Models; +using ComputerStoreContracts.BindingModels; +using ComputerStoreDataModels.Models; +using Newtonsoft.Json; using System; using System.Collections.Generic; using System.ComponentModel; @@ -25,8 +27,13 @@ namespace ComputerStoreContracts.ViewModels [DisplayName("Request ID")] public int RequestID { get; set; } - public Dictionary PCComponents { get; set; } = new(); - + public Dictionary PCComponents { get; set; } = new(); + [JsonConstructor] + public PCViewModel(Dictionary PCComponents) + { + this.PCComponents = PCComponents.ToDictionary(x => x.Key, x => (x.Value.Component as IComponentModel, x.Value.Quantity)); + } + public PCViewModel() { } } } diff --git a/ComputerStoreContracts/ViewModels/RequestViewModel.cs b/ComputerStoreContracts/ViewModels/RequestViewModel.cs index 93c274b..c339340 100644 --- a/ComputerStoreContracts/ViewModels/RequestViewModel.cs +++ b/ComputerStoreContracts/ViewModels/RequestViewModel.cs @@ -21,6 +21,6 @@ namespace ComputerStoreContracts.ViewModels public int? PCID { get; set; } [DisplayName("PC's name")] - public string PCName { get; set; } = string.Empty; + public string? PCName { get; set; } = string.Empty; } } diff --git a/ComputerStoreDataModels/Models/IPCModel.cs b/ComputerStoreDataModels/Models/IPCModel.cs index 1542d14..d599ec6 100644 --- a/ComputerStoreDataModels/Models/IPCModel.cs +++ b/ComputerStoreDataModels/Models/IPCModel.cs @@ -14,6 +14,6 @@ namespace ComputerStoreDataModels.Models public double Price { get; } public int EmployeeID { get; } public int RequestID { get; } - Dictionary PCComponents { get; } + Dictionary PCComponents { get; } } } diff --git a/ComputerStoreDatabaseImplement/Implements/PCStorage.cs b/ComputerStoreDatabaseImplement/Implements/PCStorage.cs index 16c1ec9..6fb30ba 100644 --- a/ComputerStoreDatabaseImplement/Implements/PCStorage.cs +++ b/ComputerStoreDatabaseImplement/Implements/PCStorage.cs @@ -9,6 +9,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Transactions; namespace ComputerStoreDatabaseImplement.Implements { @@ -53,6 +54,10 @@ namespace ComputerStoreDatabaseImplement.Implements context.PCs.Add(newPC); context.SaveChanges(); + + var pc = context.PCs.FirstOrDefault(x => x.Name.Equals(model.Name)); + PC.EnterPCID(context, pc); + return newPC.GetViewModel; } diff --git a/ComputerStoreDatabaseImplement/Migrations/ComputerStoreDatabaseModelSnapshot.cs b/ComputerStoreDatabaseImplement/Migrations/ComputerStoreDatabaseModelSnapshot.cs index 09a4479..ff46027 100644 --- a/ComputerStoreDatabaseImplement/Migrations/ComputerStoreDatabaseModelSnapshot.cs +++ b/ComputerStoreDatabaseImplement/Migrations/ComputerStoreDatabaseModelSnapshot.cs @@ -274,7 +274,7 @@ namespace ComputerStoreDatabaseImplement.Migrations b.Property("Count") .HasColumnType("int"); - b.Property("PCID") + b.Property("PCID") .HasColumnType("int"); b.Property("RequestID") @@ -428,9 +428,7 @@ namespace ComputerStoreDatabaseImplement.Migrations b.HasOne("ComputerStoreDatabaseImplement.Models.PC", "PC") .WithMany("Components") - .HasForeignKey("PCID") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); + .HasForeignKey("PCID"); b.HasOne("ComputerStoreDatabaseImplement.Models.Request", "Request") .WithMany("PCs") diff --git a/ComputerStoreDatabaseImplement/Models/PC.cs b/ComputerStoreDatabaseImplement/Models/PC.cs index 56d6eea..be43c94 100644 --- a/ComputerStoreDatabaseImplement/Models/PC.cs +++ b/ComputerStoreDatabaseImplement/Models/PC.cs @@ -49,7 +49,7 @@ namespace ComputerStoreDatabaseImplement.Models public virtual Employee Employee { get; set; } public static PC Create(ComputerStoreDatabase context, PCBindingModel model) - { + { return new PC() { ID = model.ID, @@ -65,6 +65,16 @@ namespace ComputerStoreDatabaseImplement.Models }; } + public static void EnterPCID(ComputerStoreDatabase context, PC model) + { + foreach(var rc in context.RequestComponents.Where(x => x.RequestID == model.RequestID)) + { + rc.PCID = model.ID; + } + + context.SaveChanges(); + } + public void Update(PCBindingModel model) { Name = model.Name; @@ -87,12 +97,16 @@ namespace ComputerStoreDatabaseImplement.Models var pcComponents = context.RequestComponents.Where(rec => rec.PCID == model.ID && rec.RequestID == model.RequestID).ToList(); if(pcComponents != null && pcComponents.Count > 0) { - context.RequestComponents.RemoveRange(pcComponents.Where(rec => !model.PCComponents.ContainsKey(rec.ComponentID) && rec.RequestID == model.RequestID)); - context.SaveChanges(); - foreach(var updateComponent in pcComponents) + if(pcComponents.Where(rec => !model.PCComponents.ContainsKey(rec.ComponentID) && rec.RequestID == model.RequestID).Any()) { - updateComponent.Count = model.PCComponents[updateComponent.PCID].Item2; - model.PCComponents.Remove(updateComponent.PCID); + context.RequestComponents.RemoveRange(pcComponents.Where(rec => !model.PCComponents.ContainsKey(rec.ComponentID) && rec.RequestID == model.RequestID)); + context.SaveChanges(); + } + + foreach(var updateComponent in pcComponents.Where(x => model.PCComponents.ContainsKey(x.ComponentID))) + { + updateComponent.Count = model.PCComponents[updateComponent.ComponentID].Item2; + model.PCComponents.Remove(updateComponent.ComponentID); } context.SaveChanges(); } diff --git a/ComputerStoreDatabaseImplement/Models/Request.cs b/ComputerStoreDatabaseImplement/Models/Request.cs index 7db9bc3..ba7a768 100644 --- a/ComputerStoreDatabaseImplement/Models/Request.cs +++ b/ComputerStoreDatabaseImplement/Models/Request.cs @@ -71,7 +71,7 @@ namespace ComputerStoreDatabaseImplement.Models OrderID = OrderID, Price = Price, PCID = PCID, - PCName = context.PCs.First(rec => rec.ID == PCID).Name + PCName = context.PCs.FirstOrDefault(rec => rec.ID == PCID)?.Name }; } } diff --git a/ComputerStoreDatabaseImplement/Models/RequestComponent.cs b/ComputerStoreDatabaseImplement/Models/RequestComponent.cs index 3157632..1d8a7d6 100644 --- a/ComputerStoreDatabaseImplement/Models/RequestComponent.cs +++ b/ComputerStoreDatabaseImplement/Models/RequestComponent.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -16,14 +17,13 @@ namespace ComputerStoreDatabaseImplement.Models [Required] public int ComponentID { get; set; } - - [Required] - public int PCID { get; set; } + + public int? PCID { get; set; } [Required] public int Count { get; set; } - public virtual PC PC { get; set; } = new(); + public virtual PC? PC { get; set; } = new(); public virtual Component Component { get; set; } = new(); public virtual Request Request { get; set; } = new(); } diff --git a/ComputerStoreRestAPI/Controllers/MainController.cs b/ComputerStoreRestAPI/Controllers/MainController.cs index 07770b7..a31701e 100644 --- a/ComputerStoreRestAPI/Controllers/MainController.cs +++ b/ComputerStoreRestAPI/Controllers/MainController.cs @@ -14,15 +14,19 @@ namespace ComputerStoreRestAPI.Controllers private readonly IComponentLogic _componentLogic; private readonly IPCLogic _pcLogic; private readonly IProductLogic _productLogic; + private readonly IRequestComponentLogic _requestComponentLogic; private readonly IEmployeeReportLogic _employeeReportLogic; + private readonly IRequestLogic _requestLogic; - public MainController(ILogger logger, IComponentLogic componentLogic, IPCLogic pcLogic, IProductLogic productLogic, IEmployeeReportLogic employeeReportLogic) + public MainController(ILogger logger, IComponentLogic componentLogic, IPCLogic pcLogic, IProductLogic productLogic, IEmployeeReportLogic employeeReportLogic, IRequestComponentLogic requestComponentLogic, IRequestLogic requestLogic) { _logger = logger; _componentLogic = componentLogic; _pcLogic = pcLogic; _productLogic = productLogic; _employeeReportLogic = employeeReportLogic; + _requestComponentLogic = requestComponentLogic; + _requestLogic = requestLogic; } [HttpGet] @@ -206,5 +210,33 @@ namespace ComputerStoreRestAPI.Controllers throw; } } + + [HttpGet] + public List? GetRequestComponentList(int? id) + { + try + { + return _requestComponentLogic.ReadList(new RequestSearchModel { ID = id}); + } + catch (Exception ex) + { + _logger.LogError(ex, "Receiving list of requestcomponent error."); + throw; + } + } + + [HttpGet] + public List? GetRequestList() + { + try + { + return _requestLogic.ReadList(null); + } + catch (Exception ex) + { + _logger.LogError(ex, "Receiving list of requestcomponent error."); + throw; + } + } } } diff --git a/ComputerStoreRestAPI/Program.cs b/ComputerStoreRestAPI/Program.cs index 6f67cc8..ea66968 100644 --- a/ComputerStoreRestAPI/Program.cs +++ b/ComputerStoreRestAPI/Program.cs @@ -14,14 +14,21 @@ builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); + +builder.Services.AddControllers().AddNewtonsoftJson(x => x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore); -builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen();