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 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<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.Collections.Generic;
using System.ComponentModel;
@ -25,8 +27,13 @@ namespace ComputerStoreContracts.ViewModels
[DisplayName("Request ID")]
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; }
[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 int EmployeeID { 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.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;
}

View File

@ -274,7 +274,7 @@ namespace ComputerStoreDatabaseImplement.Migrations
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("PCID")
b.Property<int?>("PCID")
.HasColumnType("int");
b.Property<int>("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")

View File

@ -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;
@ -86,13 +96,17 @@ namespace ComputerStoreDatabaseImplement.Models
{
var pcComponents = context.RequestComponents.Where(rec => rec.PCID == model.ID && rec.RequestID == model.RequestID).ToList();
if(pcComponents != null && pcComponents.Count > 0)
{
if(pcComponents.Where(rec => !model.PCComponents.ContainsKey(rec.ComponentID) && rec.RequestID == model.RequestID).Any())
{
context.RequestComponents.RemoveRange(pcComponents.Where(rec => !model.PCComponents.ContainsKey(rec.ComponentID) && rec.RequestID == model.RequestID));
context.SaveChanges();
foreach(var updateComponent in pcComponents)
}
foreach(var updateComponent in pcComponents.Where(x => model.PCComponents.ContainsKey(x.ComponentID)))
{
updateComponent.Count = model.PCComponents[updateComponent.PCID].Item2;
model.PCComponents.Remove(updateComponent.PCID);
updateComponent.Count = model.PCComponents[updateComponent.ComponentID].Item2;
model.PCComponents.Remove(updateComponent.ComponentID);
}
context.SaveChanges();
}

View File

@ -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
};
}
}

View File

@ -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;
@ -17,13 +18,12 @@ 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();
}

View File

@ -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<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;
_componentLogic = componentLogic;
_pcLogic = pcLogic;
_productLogic = productLogic;
_employeeReportLogic = employeeReportLogic;
_requestComponentLogic = requestComponentLogic;
_requestLogic = requestLogic;
}
[HttpGet]
@ -206,5 +210,33 @@ namespace ComputerStoreRestAPI.Controllers
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<IPCStorage,PCStorage>();
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<IComponentLogic, ComponentLogic>();
builder.Services.AddTransient<IEmployeeReportLogic, EmployeeReportLogic>();
builder.Services.AddTransient<IPCLogic, PCLogic>();
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
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();