Merge branch 'CourseworkRestAPI' into CourseworkEmployeeApp

This commit is contained in:
Yuee Shiness 2023-05-19 16:51:14 +04:00
commit 1a10fbed1d
14 changed files with 337 additions and 33 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

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

@ -33,17 +33,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,
};
}
@ -55,6 +60,7 @@ namespace ComputerStoreDatabaseImplement.Models
}
Price = model.Price;
PCID = model.PCID;
RequestComponents = model.RequestComponents;
}
public void UpdatePCID(ComputerStoreDatabase context)

View File

@ -4,6 +4,7 @@ using ComputerStoreContracts.BusinessLogicContracts;
using ComputerStoreContracts.SearchModels;
using ComputerStoreContracts.ViewModels;
using Microsoft.AspNetCore.Mvc;
using System.Windows;
namespace ComputerStoreRestAPI.Controllers
{
@ -19,7 +20,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;
@ -28,6 +44,9 @@ namespace ComputerStoreRestAPI.Controllers
_employeeReportLogic = employeeReportLogic;
_requestComponentLogic = requestComponentLogic;
_requestLogic = requestLogic;
_orderLogic = orderLogic;
_consignmentLogic = consignmentLogic;
_reportOrderLogic = reportOrderLogic;
}
[HttpGet]
@ -49,7 +68,7 @@ namespace ComputerStoreRestAPI.Controllers
{
try
{
return _componentLogic.ReadElement(new ComponentSearchModel { ID = id } );
return _componentLogic.ReadElement(new ComponentSearchModel { ID = id });
}
catch (Exception ex)
{
@ -65,7 +84,7 @@ namespace ComputerStoreRestAPI.Controllers
{
return _componentLogic.Delete(new ComponentBindingModel { ID = id });
}
catch(Exception ex)
catch (Exception ex)
{
_logger.LogError(ex, "Removing component error.");
throw;
@ -89,11 +108,11 @@ namespace ComputerStoreRestAPI.Controllers
[HttpPost]
public bool InsertComponent(ComponentBindingModel component)
{
try
try
{
return _componentLogic.Create(component);
}
catch(Exception ex)
catch (Exception ex)
{
_logger.LogError(ex, "Inserting component error.");
throw;
@ -177,7 +196,7 @@ namespace ComputerStoreRestAPI.Controllers
{
try
{
return _pcLogic.Delete(new PCBindingModel { ID = id});
return _pcLogic.Delete(new PCBindingModel { ID = id });
}
catch (Exception ex)
{
@ -219,7 +238,7 @@ namespace ComputerStoreRestAPI.Controllers
{
try
{
return _requestComponentLogic.ReadList(new RequestSearchModel { ID = id});
return _requestComponentLogic.ReadList(new RequestSearchModel { ID = id });
}
catch (Exception ex)
{
@ -228,6 +247,15 @@ namespace ComputerStoreRestAPI.Controllers
}
}
[HttpGet]
public List<ReportConsignmentsViewModel> GetReportConsignmentsList(ReportComponentsBindingModel model)
{
return _employeeReportLogic.GetConsignmentsByComponents(model);
}
#region Request
[HttpGet]
public List<RequestViewModel>? GetRequestList()
{
@ -241,5 +269,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

@ -24,6 +24,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>();