Contracktor: Fixes

This commit is contained in:
Safgerd 2023-05-19 16:11:31 +04:00
parent 6e2912f1c4
commit 92a8fefd66
16 changed files with 339 additions and 29 deletions

View File

@ -42,6 +42,29 @@ namespace ComputerStoreBusinessLogic.BusinessLogic
return true;
}
public bool DeleteOrder (OrderBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. ID:{ID}", model.ID);
if (_orderStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public bool UpdateOrder (OrderBindingModel model)
{
CheckModel(model);
if (_orderStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool StatusUpdate(OrderBindingModel rawModel, OrderStatus newStatus)
{
var viewModel = _orderStorage.GetElement(new OrderSearchModel
@ -99,7 +122,28 @@ namespace ComputerStoreBusinessLogic.BusinessLogic
return StatusUpdate(model, OrderStatus.Given);
}
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
public OrderViewModel? ReadElement(OrderSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Order ID:{ OrderID}", model.ID);
var element = _orderStorage.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<OrderViewModel>? ReadList(OrderSearchModel? model)
{
_logger.LogInformation("Order. OrderID:{Id}", model?.ID);
var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model);

View File

@ -13,5 +13,7 @@ namespace ComputerStoreContracts.BindingModels
public int OrderID { get; set; }
public double Price { get; set; }
public int? PCID { get; set; }
}
public List<(IComponentModel, int)> RequestComponents { get; set; } = new();
}
}

View File

@ -16,5 +16,9 @@ namespace ComputerStoreContracts.BusinessLogicContracts
bool TakeOrderInProcess(OrderBindingModel model);
bool TakeOrderReady(OrderBindingModel model);
bool TakeOrderGiven(OrderBindingModel model);
}
OrderViewModel? ReadElement(OrderSearchModel model);
bool UpdateOrder(OrderBindingModel model);
bool DeleteOrder(OrderBindingModel model);
}
}

View File

@ -11,5 +11,7 @@ namespace ComputerStoreContracts.BusinessLogicContracts
public interface IRequestComponentLogic
{
List<RequestComponentViewModel>? ReadList(RequestSearchModel? model);
}
}

View File

@ -1,12 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputerStoreContracts.BusinessLogicContracts
{
internal class ISellerReportLogic
{
}
}

View File

@ -6,6 +6,10 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ComputerStoreDataModels\ComputerStoreDataModels.csproj" />
</ItemGroup>

View File

@ -10,5 +10,6 @@ namespace ComputerStoreContracts.SearchModels
{
public int? ID { get; set; }
public string? Username { get; set; }
public string? Password { get; set; }
}
}

View File

@ -22,5 +22,7 @@ namespace ComputerStoreContracts.ViewModels
[DisplayName("PC's name")]
public string? PCName { get; set; } = string.Empty;
}
public List<(IComponentModel, int)> RequestComponents { get; set; } = new();
}
}

View File

@ -11,5 +11,7 @@ namespace ComputerStoreDataModels.Models
int OrderID { get; }
int? PCID { get; }
double Price { get; }
}
public List<(IComponentModel, int)> RequestComponents { get; }
}
}

View File

@ -7,6 +7,7 @@ using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
@ -55,13 +56,24 @@ namespace ComputerStoreDatabaseImplement.Implements
using var context = new ComputerStoreDatabase();
var newRequest = Request.Create(context, model);
if (newRequest == null) { return null; }
context.Requests.Add(newRequest);
context.SaveChanges();
var requestList = GetFullList();
var currentRequest = requestList.Last();
if (currentRequest == null) { return null; }
foreach (var reqcomp in newRequest.RequestComponents)
{
context.RequestComponents.Add(new RequestComponent { ComponentID = reqcomp.Item1.ID, RequestID = currentRequest.ID, Count = reqcomp.Item2 });
}
context.SaveChanges();
return context.Requests
.Include(x => x.Order)
.Include(y => y.PCs)
.FirstOrDefault(x => x.ID == model.ID)?.GetViewModel;
}
return newRequest.GetViewModel;
}
public RequestViewModel? Update(RequestBindingModel model)
{
@ -71,8 +83,19 @@ namespace ComputerStoreDatabaseImplement.Implements
{
return null;
}
if (request.PCID != null)
{
return null;
}
request.Update(model);
context.RequestComponents.RemoveRange(context.RequestComponents.Where(x => x.RequestID == request.ID));
foreach (var reqcomp in request.RequestComponents)
{
context.RequestComponents.Add(new RequestComponent { ComponentID = reqcomp.Item1.ID, RequestID = request.ID, Count = reqcomp.Item2 });
}
request.Update(model);
context.SaveChanges();
return context.Requests
@ -92,6 +115,7 @@ namespace ComputerStoreDatabaseImplement.Implements
if (request != null)
{
context.RequestComponents.RemoveRange(context.RequestComponents.Where(x => x.RequestID == request.ID));
context.Requests.Remove(request);
context.SaveChanges();

View File

@ -64,7 +64,7 @@ namespace ComputerStoreDatabaseImplement.Models
DateImplement = model.DateImplement,
_consignments = model.OrderConsignments.Select(x => context.Consignments.First(y => y.ID == x.ID)).ToList(),
_requests = model.OrderRequests.Select(x => context.Requests.First(y => y.ID == x.ID)).ToList(),
};
};
}
public void Update(OrderBindingModel? model)

View File

@ -34,17 +34,22 @@ namespace ComputerStoreDatabaseImplement.Models
[ForeignKey("RequestID")]
public virtual List<PC> PersonalComputers { get; set; } = new();
[NotMapped]
public List<(IComponentModel, int)> RequestComponents { get; set; } = new();
public static Request? Create(ComputerStoreDatabase context, RequestBindingModel model)
{
if (model == null)
{
return null;
}
return new Request()
{
ID = model.ID,
OrderID = model.OrderID,
Price = model.Price
Price = model.Price,
RequestComponents = model.RequestComponents,
};
}
@ -56,6 +61,7 @@ namespace ComputerStoreDatabaseImplement.Models
}
Price = model.Price;
PCID = model.PCID;
RequestComponents = model.RequestComponents;
}
public void UpdatePCID(ComputerStoreDatabase context)
@ -74,7 +80,7 @@ namespace ComputerStoreDatabaseImplement.Models
OrderID = OrderID,
Price = Price,
PCID = PCID,
PCName = context.PCs.FirstOrDefault(rec => rec.ID == PCID)?.Name
PCName = context.PCS.FirstOrDefault(rec => rec.ID == PCID)?.Name
};
}
}

View File

@ -7,6 +7,12 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Logging.Log4Net.AspNetCore" Version="6.1.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup>

View File

@ -18,7 +18,22 @@ namespace ComputerStoreRestAPI.Controllers
private readonly IEmployeeReportLogic _employeeReportLogic;
private readonly IRequestLogic _requestLogic;
public MainController(ILogger<MainController> logger, IComponentLogic componentLogic, IPCLogic pcLogic, IProductLogic productLogic, IEmployeeReportLogic employeeReportLogic, IRequestComponentLogic requestComponentLogic, IRequestLogic requestLogic)
private readonly IOrderLogic _orderLogic;
private readonly IConsignmentLogic _consignmentLogic;
private readonly IReportOrderLogic _reportOrderLogic;
public MainController(
ILogger<MainController> logger,
IComponentLogic componentLogic,
IPCLogic pcLogic,
IProductLogic productLogic,
IEmployeeReportLogic employeeReportLogic,
IRequestComponentLogic requestComponentLogic,
IRequestLogic requestLogic,
IOrderLogic orderLogic,
IConsignmentLogic consignmentLogic,
IReportOrderLogic reportOrderLogic)
{
_logger = logger;
_componentLogic = componentLogic;
@ -27,6 +42,9 @@ namespace ComputerStoreRestAPI.Controllers
_employeeReportLogic = employeeReportLogic;
_requestComponentLogic = requestComponentLogic;
_requestLogic = requestLogic;
_orderLogic = orderLogic;
_consignmentLogic = consignmentLogic;
_reportOrderLogic = reportOrderLogic;
}
[HttpGet]
@ -225,7 +243,13 @@ namespace ComputerStoreRestAPI.Controllers
}
}
[HttpGet]
#region Request
[HttpGet]
public List<RequestViewModel>? GetRequestList()
{
try
@ -238,5 +262,136 @@ namespace ComputerStoreRestAPI.Controllers
throw;
}
}
}
[HttpGet]
public RequestViewModel? GetRequest(int id)
{
try
{
return _requestLogic.ReadElement(new RequestSearchModel { ID = id });
}
catch (Exception ex)
{
_logger.LogError(ex, "Receiving request error.");
throw;
}
}
[HttpDelete("{id}")]
public bool DeleteRequest(int id)
{
try
{
return _requestLogic.Delete(new RequestBindingModel { ID = id });
}
catch (Exception ex)
{
_logger.LogError(ex, "Removing request error.");
throw;
}
}
[HttpPatch]
public bool UpdateRequest(RequestBindingModel request)
{
try
{
return _requestLogic.Update(request);
}
catch (Exception ex)
{
_logger.LogError(ex, "Updating request error.");
throw;
}
}
[HttpPost]
public bool InsertRequest(RequestBindingModel request)
{
try
{
return _requestLogic.Create(request);
}
catch (Exception ex)
{
_logger.LogError(ex, "Inserting request error.");
throw;
}
}
#endregion
#region Order
[HttpGet]
public List<OrderViewModel>? GetOrdersList()
{
try
{
return _orderLogic.ReadList(null);
}
catch (Exception ex)
{
_logger.LogError(ex, "Receiving list of orders error.");
throw;
}
}
[HttpPatch]
public bool UpdateOrderInProcess(OrderBindingModel order)
{
try
{
return _orderLogic.TakeOrderInProcess(order);
}
catch (Exception ex)
{
_logger.LogError(ex, "Updating order in process error.");
throw;
}
}
[HttpPatch]
public bool UpdateOrderReady(OrderBindingModel order)
{
try
{
return _orderLogic.TakeOrderReady(order);
}
catch (Exception ex)
{
_logger.LogError(ex, "Updating order ready error.");
throw;
}
}
[HttpPatch]
public bool UpdateOrderGiven(OrderBindingModel order)
{
try
{
return _orderLogic.TakeOrderGiven(order);
}
catch (Exception ex)
{
_logger.LogError(ex, "Updating order given error.");
throw;
}
}
[HttpPost]
public bool InsertOrder(OrderBindingModel order)
{
try
{
return _orderLogic.CreateOrder(order);
}
catch (Exception ex)
{
_logger.LogError(ex, "Inserting order error.");
throw;
}
}
#endregion
}
}

View File

@ -0,0 +1,63 @@
using ComputerStoreContracts.BindingModels;
using ComputerStoreContracts.BusinessLogicContracts;
using ComputerStoreContracts.SearchModels;
using ComputerStoreContracts.ViewModels;
using Microsoft.AspNetCore.Mvc;
namespace ComputerStoreRestAPI.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class SellerController : Controller
{
private readonly ILogger _logger;
private readonly ISellerLogic _logic;
public SellerController(ISellerLogic logic, ILogger<SellerController> logger)
{
_logger = logger;
_logic = logic;
}
[HttpGet]
public SellerViewModel? Login(string login, string password)
{
try
{
return _logic.ReadElement(new SellerSearchModel { Username = login, Password = password });
}
catch (Exception ex)
{
_logger.LogError(ex, "Seller logging in error"); throw;
}
}
[HttpPost]
public bool Register(SellerBindingModel seller)
{
try
{
return _logic.Create(seller);
}
catch (Exception ex)
{
_logger.LogError(ex, "Seller registration error ");
throw;
}
}
[HttpPatch]
public bool UpdateData(SellerBindingModel seller)
{
try
{
return _logic.Update(seller);
}
catch (Exception ex)
{
_logger.LogError(ex, "Update of data error ");
throw;
}
}
}
}

View File

@ -20,6 +20,13 @@ builder.Services.AddTransient<IConsignmentStorage, ConsignmentStorage>();
builder.Services.AddTransient<IRequestComponentStorage, RequestComponentStorage>();
builder.Services.AddTransient<IRequestStorage, RequestStorage>();
builder.Services.AddTransient<IOrderStorage, OrderStorage>();
builder.Services.AddTransient<ISellerStorage, SellerStorage>();
builder.Services.AddTransient<IOrderLogic, OrderLogic>();
builder.Services.AddTransient<IReportOrderLogic, ReportOrderLogic>();
builder.Services.AddTransient<ISellerLogic, SellerLogic>();
builder.Services.AddTransient<IEmployeeLogic,EmployeeLogic>();
builder.Services.AddTransient<IComponentLogic, ComponentLogic>();
builder.Services.AddTransient<IEmployeeReportLogic, EmployeeReportLogic>();