Main controller additions

This commit is contained in:
Yuee Shiness 2023-05-17 17:57:25 +04:00
parent 247ffd3b74
commit 23449b08ce
11 changed files with 93 additions and 22 deletions

View File

@ -1,4 +1,5 @@
using ComputerStoreDataModels.Models; using ComputerStoreDataModels.Models;
using Newtonsoft.Json;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -19,5 +20,12 @@ namespace ComputerStoreContracts.BindingModels
public int RequestID { get; set; } public int RequestID { get; set; }
public Dictionary<int, (IComponentModel, int)> PCComponents { get; set; } = new(); public Dictionary<int, (IComponentModel, int)> PCComponents { get; set; } = new();
[JsonConstructor]
public PCBindingModel(Dictionary<int, (ComponentBindingModel Component, int Quantity)> PCComponents)
{
this.PCComponents = PCComponents.ToDictionary(x => x.Key, x => (x.Value.Component as IComponentModel, x.Value.Quantity));
}
public PCBindingModel() { }
} }
} }

View File

@ -1,4 +1,6 @@
using ComputerStoreDataModels.Models; using ComputerStoreContracts.BindingModels;
using ComputerStoreDataModels.Models;
using Newtonsoft.Json;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
@ -25,8 +27,13 @@ namespace ComputerStoreContracts.ViewModels
[DisplayName("Request ID")] [DisplayName("Request ID")]
public int RequestID { get; set; } public int RequestID { get; set; }
public Dictionary<int, (IComponentModel, int)> PCComponents { get; set; } = new(); public Dictionary<int, (IComponentModel Component, int Quantity)> PCComponents { get; set; } = new();
[JsonConstructor]
public PCViewModel(Dictionary<int, (ComponentBindingModel Component, int Quantity)> PCComponents)
{
this.PCComponents = PCComponents.ToDictionary(x => x.Key, x => (x.Value.Component as IComponentModel, x.Value.Quantity));
}
public PCViewModel() { }
} }
} }

View File

@ -21,6 +21,6 @@ namespace ComputerStoreContracts.ViewModels
public int? PCID { get; set; } public int? PCID { get; set; }
[DisplayName("PC's name")] [DisplayName("PC's name")]
public string PCName { get; set; } = string.Empty; public string? PCName { get; set; } = string.Empty;
} }
} }

View File

@ -14,6 +14,6 @@ namespace ComputerStoreDataModels.Models
public double Price { get; } public double Price { get; }
public int EmployeeID { get; } public int EmployeeID { get; }
public int RequestID { get; } public int RequestID { get; }
Dictionary<int,(IComponentModel,int)> PCComponents { get; } Dictionary<int,(IComponentModel Component,int Quantity)> PCComponents { get; }
} }
} }

View File

@ -9,6 +9,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Transactions;
namespace ComputerStoreDatabaseImplement.Implements namespace ComputerStoreDatabaseImplement.Implements
{ {
@ -53,6 +54,10 @@ namespace ComputerStoreDatabaseImplement.Implements
context.PCs.Add(newPC); context.PCs.Add(newPC);
context.SaveChanges(); context.SaveChanges();
var pc = context.PCs.FirstOrDefault(x => x.Name.Equals(model.Name));
PC.EnterPCID(context, pc);
return newPC.GetViewModel; return newPC.GetViewModel;
} }

View File

@ -274,7 +274,7 @@ namespace ComputerStoreDatabaseImplement.Migrations
b.Property<int>("Count") b.Property<int>("Count")
.HasColumnType("int"); .HasColumnType("int");
b.Property<int>("PCID") b.Property<int?>("PCID")
.HasColumnType("int"); .HasColumnType("int");
b.Property<int>("RequestID") b.Property<int>("RequestID")
@ -428,9 +428,7 @@ namespace ComputerStoreDatabaseImplement.Migrations
b.HasOne("ComputerStoreDatabaseImplement.Models.PC", "PC") b.HasOne("ComputerStoreDatabaseImplement.Models.PC", "PC")
.WithMany("Components") .WithMany("Components")
.HasForeignKey("PCID") .HasForeignKey("PCID");
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("ComputerStoreDatabaseImplement.Models.Request", "Request") b.HasOne("ComputerStoreDatabaseImplement.Models.Request", "Request")
.WithMany("PCs") .WithMany("PCs")

View File

@ -49,7 +49,7 @@ namespace ComputerStoreDatabaseImplement.Models
public virtual Employee Employee { get; set; } public virtual Employee Employee { get; set; }
public static PC Create(ComputerStoreDatabase context, PCBindingModel model) public static PC Create(ComputerStoreDatabase context, PCBindingModel model)
{ {
return new PC() return new PC()
{ {
ID = model.ID, 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) public void Update(PCBindingModel model)
{ {
Name = model.Name; 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(); var pcComponents = context.RequestComponents.Where(rec => rec.PCID == model.ID && rec.RequestID == model.RequestID).ToList();
if(pcComponents != null && pcComponents.Count > 0) if(pcComponents != null && pcComponents.Count > 0)
{ {
context.RequestComponents.RemoveRange(pcComponents.Where(rec => !model.PCComponents.ContainsKey(rec.ComponentID) && rec.RequestID == model.RequestID)); if(pcComponents.Where(rec => !model.PCComponents.ContainsKey(rec.ComponentID) && rec.RequestID == model.RequestID).Any())
context.SaveChanges();
foreach(var updateComponent in pcComponents)
{ {
updateComponent.Count = model.PCComponents[updateComponent.PCID].Item2; context.RequestComponents.RemoveRange(pcComponents.Where(rec => !model.PCComponents.ContainsKey(rec.ComponentID) && rec.RequestID == model.RequestID));
model.PCComponents.Remove(updateComponent.PCID); 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(); context.SaveChanges();
} }

View File

@ -71,7 +71,7 @@ namespace ComputerStoreDatabaseImplement.Models
OrderID = OrderID, OrderID = OrderID,
Price = Price, Price = Price,
PCID = PCID, PCID = PCID,
PCName = context.PCs.First(rec => rec.ID == PCID).Name PCName = context.PCs.FirstOrDefault(rec => rec.ID == PCID)?.Name
}; };
} }
} }

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -16,14 +17,13 @@ namespace ComputerStoreDatabaseImplement.Models
[Required] [Required]
public int ComponentID { get; set; } public int ComponentID { get; set; }
[Required] public int? PCID { get; set; }
public int PCID { get; set; }
[Required] [Required]
public int Count { get; set; } 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 Component Component { get; set; } = new();
public virtual Request Request { get; set; } = new(); public virtual Request Request { get; set; } = new();
} }

View File

@ -14,15 +14,19 @@ namespace ComputerStoreRestAPI.Controllers
private readonly IComponentLogic _componentLogic; private readonly IComponentLogic _componentLogic;
private readonly IPCLogic _pcLogic; private readonly IPCLogic _pcLogic;
private readonly IProductLogic _productLogic; private readonly IProductLogic _productLogic;
private readonly IRequestComponentLogic _requestComponentLogic;
private readonly IEmployeeReportLogic _employeeReportLogic; private readonly IEmployeeReportLogic _employeeReportLogic;
private readonly IRequestLogic _requestLogic;
public MainController(ILogger<MainController> logger, IComponentLogic componentLogic, IPCLogic pcLogic, IProductLogic productLogic, IEmployeeReportLogic employeeReportLogic) public MainController(ILogger<MainController> logger, IComponentLogic componentLogic, IPCLogic pcLogic, IProductLogic productLogic, IEmployeeReportLogic employeeReportLogic, IRequestComponentLogic requestComponentLogic, IRequestLogic requestLogic)
{ {
_logger = logger; _logger = logger;
_componentLogic = componentLogic; _componentLogic = componentLogic;
_pcLogic = pcLogic; _pcLogic = pcLogic;
_productLogic = productLogic; _productLogic = productLogic;
_employeeReportLogic = employeeReportLogic; _employeeReportLogic = employeeReportLogic;
_requestComponentLogic = requestComponentLogic;
_requestLogic = requestLogic;
} }
[HttpGet] [HttpGet]
@ -206,5 +210,33 @@ namespace ComputerStoreRestAPI.Controllers
throw; throw;
} }
} }
[HttpGet]
public List<RequestComponentViewModel>? 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<RequestViewModel>? GetRequestList()
{
try
{
return _requestLogic.ReadList(null);
}
catch (Exception ex)
{
_logger.LogError(ex, "Receiving list of requestcomponent error.");
throw;
}
}
} }
} }

View File

@ -14,14 +14,21 @@ builder.Services.AddTransient<IEmployeeStorage,EmployeeStorage>();
builder.Services.AddTransient<IComponentStorage, ComponentStorage>(); builder.Services.AddTransient<IComponentStorage, ComponentStorage>();
builder.Services.AddTransient<IPCStorage,PCStorage>(); builder.Services.AddTransient<IPCStorage,PCStorage>();
builder.Services.AddTransient<IProductStorage, ProductStorage>(); builder.Services.AddTransient<IProductStorage, ProductStorage>();
builder.Services.AddTransient<IConsignmentStorage, ConsignmentStorage>();
builder.Services.AddTransient<IRequestComponentStorage, RequestComponentStorage>();
builder.Services.AddTransient<IRequestStorage, RequestStorage>();
builder.Services.AddTransient<IEmployeeLogic,EmployeeLogic>(); builder.Services.AddTransient<IEmployeeLogic,EmployeeLogic>();
builder.Services.AddTransient<IComponentLogic, ComponentLogic>(); builder.Services.AddTransient<IComponentLogic, ComponentLogic>();
builder.Services.AddTransient<IEmployeeReportLogic, EmployeeReportLogic>(); builder.Services.AddTransient<IEmployeeReportLogic, EmployeeReportLogic>();
builder.Services.AddTransient<IPCLogic, PCLogic>(); builder.Services.AddTransient<IPCLogic, PCLogic>();
builder.Services.AddTransient<IProductLogic, ProductLogic>(); builder.Services.AddTransient<IProductLogic, ProductLogic>();
builder.Services.AddTransient<IConsignmentLogic,ConsignmentLogic>();
builder.Services.AddTransient<IRequestComponentLogic, RequestComponentLogic>();
builder.Services.AddTransient<IRequestLogic, RequestLogic>();
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 // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(); builder.Services.AddSwaggerGen();