Добавил бизнес-логику (ПРОВЕРИТЬ) и подправил слой базы данных (ПРОВЕРИТЬ)
This commit is contained in:
parent
8bca6105f2
commit
2d89e0973b
174
ComputerShopBusinessLogic/BusinessLogics/OrderLogic.cs
Normal file
174
ComputerShopBusinessLogic/BusinessLogics/OrderLogic.cs
Normal file
@ -0,0 +1,174 @@
|
||||
using ComputerShopContracts.BindingModels;
|
||||
using ComputerShopContracts.BusinessLogicContracts;
|
||||
using ComputerShopContracts.SearchModels;
|
||||
using ComputerShopContracts.StorageContracts;
|
||||
using ComputerShopContracts.ViewModels;
|
||||
using ComputerShopDataModels.Enums;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ComputerShopBusinessLogic.BusinessLogics
|
||||
{
|
||||
public class OrderLogic : IOrderLogic
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
//Хранение всех заказов
|
||||
private readonly IOrderStorage _orderStorage;
|
||||
|
||||
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage)
|
||||
{
|
||||
_logger = logger;
|
||||
_orderStorage = orderStorage;
|
||||
}
|
||||
|
||||
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
|
||||
{
|
||||
var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model);
|
||||
if (list == null)
|
||||
{
|
||||
_logger.LogWarning("ReadList return null list");
|
||||
return null;
|
||||
}
|
||||
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
public OrderViewModel? ReadElement(OrderSearchModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(model));
|
||||
}
|
||||
var element = _orderStorage.GetElement(model);
|
||||
if (element == null)
|
||||
{
|
||||
_logger.LogWarning("ReadElement order not found");
|
||||
return null;
|
||||
}
|
||||
_logger.LogInformation("ReadElement order found. Id:{Id}", element.Id);
|
||||
return element;
|
||||
}
|
||||
|
||||
private void CheckModel(OrderBindingModel model, bool withParams = true)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(model));
|
||||
}
|
||||
if (!withParams)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (model.DateCreate > DateTime.Now)
|
||||
{
|
||||
throw new ArgumentException($"Дата создания заказа {model.DateCreate} не может быть в будущем");
|
||||
}
|
||||
}
|
||||
|
||||
public bool Create(OrderBindingModel model)
|
||||
{
|
||||
CheckModel(model);
|
||||
if (model.Status != OrderStatus.Неизвестен)
|
||||
{
|
||||
_logger.LogWarning("Invalid order status");
|
||||
return false;
|
||||
}
|
||||
model.Status = OrderStatus.Принят;
|
||||
if (_orderStorage.Insert(model) == null)
|
||||
{
|
||||
_logger.LogWarning("Insert operation failed");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
//!!!ПРОВЕРИТЬ
|
||||
public bool Update(OrderBindingModel model)
|
||||
{
|
||||
CheckModel(model, false);
|
||||
var order = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id });
|
||||
if (order == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(order));
|
||||
}
|
||||
if (_orderStorage.Update(model) == null)
|
||||
{
|
||||
_logger.LogWarning("Update operation failed");
|
||||
return false;
|
||||
}
|
||||
//!!!ПРОВЕРИТЬ
|
||||
//if (model.Status != null & ChangeStatus(model, order.Status))
|
||||
//{
|
||||
// ordee.DateCreate[model.DateCreate;
|
||||
// return true;
|
||||
//}
|
||||
//model.Status = order.Status;
|
||||
//model.Sum = order.Sum;
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Delete(OrderBindingModel model)
|
||||
{
|
||||
CheckModel(model, false);
|
||||
_logger.LogInformation("Delete. Id:{Id}", model.Id);
|
||||
if (_orderStorage.Delete(model) == null)
|
||||
{
|
||||
_logger.LogWarning("Delete order operation failed");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
//!!!ПРОВЕРИТЬ
|
||||
private bool ChangeStatus(OrderBindingModel model, OrderStatus newStatus)
|
||||
{
|
||||
CheckModel(model, false);
|
||||
var order = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id });
|
||||
if (order == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(order));
|
||||
}
|
||||
if (newStatus - model.Status == 1)
|
||||
{
|
||||
model.Status = newStatus;
|
||||
|
||||
if (_orderStorage.Update(model) == null)
|
||||
{
|
||||
_logger.LogWarning("Update order operation failed");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
if (order.Status + 1 != newStatus)
|
||||
{
|
||||
_logger.LogWarning("Change status operation failed. Incorrect new status: {newStatus}. Current status: {currStatus}", newStatus, order.Status);
|
||||
return false;
|
||||
}
|
||||
_logger.LogWarning("Changing status operation faled: current:{Status}: required:{newStatus}.", model.Status, newStatus);
|
||||
throw new ArgumentException($"Невозможно присвоить статус {newStatus} заказу с текущим статусом {model.Status}");
|
||||
}
|
||||
|
||||
//Перевод заказа в состояние выполнения
|
||||
public bool TakeOrderInWork(OrderBindingModel model)
|
||||
{
|
||||
return ChangeStatus(model, OrderStatus.Выполняется);
|
||||
}
|
||||
|
||||
//Перевод заказа в состояние готовности
|
||||
public bool FinishOrder(OrderBindingModel model)
|
||||
{
|
||||
return ChangeStatus(model, OrderStatus.Готов);
|
||||
}
|
||||
//Перевод заказа в состояние выдачи (окончательное завершение)
|
||||
public bool DeliveryOrder(OrderBindingModel model)
|
||||
{
|
||||
return ChangeStatus(model, OrderStatus.Выдан);
|
||||
}
|
||||
}
|
||||
}
|
108
ComputerShopBusinessLogic/BusinessLogics/RequestLogic.cs
Normal file
108
ComputerShopBusinessLogic/BusinessLogics/RequestLogic.cs
Normal file
@ -0,0 +1,108 @@
|
||||
using ComputerShopContracts.BindingModels;
|
||||
using ComputerShopContracts.BusinessLogicContracts;
|
||||
using ComputerShopContracts.SearchModels;
|
||||
using ComputerShopContracts.StorageContracts;
|
||||
using ComputerShopContracts.ViewModels;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ComputerShopBusinessLogic.BusinessLogics
|
||||
{
|
||||
public class RequestLogic : IRequestLogic
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly IRequestStorage _requestStorage;
|
||||
public RequestLogic(ILogger<RequestLogic> logger, IRequestStorage requestStorage)
|
||||
{
|
||||
_logger = logger;
|
||||
_requestStorage = requestStorage;
|
||||
}
|
||||
|
||||
public List<RequestViewModel>? ReadList(RequestSearchModel? model)
|
||||
{
|
||||
var list = model == null ? _requestStorage.GetFullList() : _requestStorage.GetFilteredList(model);
|
||||
if (list == null)
|
||||
{
|
||||
_logger.LogWarning("ReadList requests return null list");
|
||||
return null;
|
||||
}
|
||||
_logger.LogInformation("ReadList requests.Count:{Count}", list.Count);
|
||||
return list;
|
||||
}
|
||||
|
||||
public RequestViewModel? ReadElement(RequestSearchModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(model));
|
||||
}
|
||||
|
||||
var element = _requestStorage.GetElement(model);
|
||||
if (element == null)
|
||||
{
|
||||
_logger.LogWarning("ReadElement request not found");
|
||||
return null;
|
||||
}
|
||||
_logger.LogInformation("ReadElement reguest found Id:{Id}", element.Id);
|
||||
return element;
|
||||
}
|
||||
|
||||
public bool Create(RequestBindingModel model)
|
||||
{
|
||||
CheckModel(model);
|
||||
if (_requestStorage.Insert(model) == null)
|
||||
{
|
||||
_logger.LogWarning("Insert operation failed");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Update(RequestBindingModel model)
|
||||
{
|
||||
CheckModel(model);
|
||||
if (_requestStorage.Update(model) == null)
|
||||
{
|
||||
_logger.LogWarning("Update operation failed");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Delete(RequestBindingModel model)
|
||||
{
|
||||
CheckModel(model, false);
|
||||
_logger.LogInformation("Delete. Id:{Id}", model.Id);
|
||||
if (_requestStorage.Delete(model) == null)
|
||||
{
|
||||
_logger.LogWarning("Delete operation failed");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
//!!!мб проверять, что есть userID
|
||||
private void CheckModel(RequestBindingModel model, bool withParams = true)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(model));
|
||||
}
|
||||
if (!withParams)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (string.IsNullOrEmpty(model.ClientFIO))
|
||||
{
|
||||
throw new ArgumentNullException("Нет ФИО клиента, оформившего заявку", nameof(model.ClientFIO));
|
||||
}
|
||||
if (model.DateRequest > DateTime.Now)
|
||||
{
|
||||
throw new ArgumentException($"Дата оформления заявки {model.DateRequest} не может быть в будущем");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
112
ComputerShopBusinessLogic/BusinessLogics/ShipmentLogic.cs
Normal file
112
ComputerShopBusinessLogic/BusinessLogics/ShipmentLogic.cs
Normal file
@ -0,0 +1,112 @@
|
||||
using ComputerShopContracts.BindingModels;
|
||||
using ComputerShopContracts.BusinessLogicContracts;
|
||||
using ComputerShopContracts.SearchModels;
|
||||
using ComputerShopContracts.StorageContracts;
|
||||
using ComputerShopContracts.ViewModels;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace ComputerShopBusinessLogic.BusinessLogics
|
||||
{
|
||||
public class ShipmentLogic : IShipmentLogic
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly IShipmentStorage _shipmentStorage;
|
||||
|
||||
public ShipmentLogic(ILogger<ShipmentLogic> logger, IShipmentStorage shipmentStorage)
|
||||
{
|
||||
_logger = logger;
|
||||
_shipmentStorage = shipmentStorage;
|
||||
}
|
||||
|
||||
//!!!ТУТ МБ ПО-ДРУГОМУ ПРИСВАИВАТЬ ЗНАЧЕНИЕ ДЛЯ list (через fulllist)
|
||||
//!!!ИЛИ СОЗДАТЬ ОТДЕЛЬНЫЙ МЕТОД ReadListUser
|
||||
public List<ShipmentViewModel> ReadList(ShipmentSearchModel? model)
|
||||
{
|
||||
_logger.LogInformation("ReadList. ShipmentId:{Id}", model?.Id);
|
||||
var list = model == null ? _shipmentStorage.GetFullList() : _shipmentStorage.GetFilteredList(model);
|
||||
if (list == null)
|
||||
{
|
||||
_logger.LogWarning("ReadList return null list");
|
||||
return null;
|
||||
}
|
||||
_logger.LogInformation("ReadList Count:{Count}", list.Count);
|
||||
return list;
|
||||
}
|
||||
|
||||
public ShipmentViewModel? ReadElement(ShipmentSearchModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(model));
|
||||
}
|
||||
var shipment = _shipmentStorage.GetElement(model);
|
||||
if (shipment == null)
|
||||
{
|
||||
_logger.LogWarning("ReadElement shipment not found");
|
||||
return null;
|
||||
}
|
||||
_logger.LogInformation("ReadElement found. Id:{Id}", shipment.Id);
|
||||
return shipment;
|
||||
}
|
||||
|
||||
public bool Create(ShipmentBindingModel model)
|
||||
{
|
||||
CheckModel(model);
|
||||
if (_shipmentStorage.Insert(model) == null)
|
||||
{
|
||||
_logger.LogWarning("Insert operation failed");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Update(ShipmentBindingModel model)
|
||||
{
|
||||
CheckModel(model);
|
||||
if (_shipmentStorage.Update(model) == null)
|
||||
{
|
||||
_logger.LogWarning("Update operation failed");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Delete(ShipmentBindingModel model)
|
||||
{
|
||||
CheckModel(model, false);
|
||||
_logger.LogInformation("Delete. Id:{Id}", model.Id);
|
||||
if (_shipmentStorage.Delete(model) == null)
|
||||
{
|
||||
_logger.LogWarning("Delete operation failed");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private void CheckModel(ShipmentBindingModel model, bool withParams = true)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(model));
|
||||
}
|
||||
if (!withParams)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (string.IsNullOrEmpty(model.ProviderName))
|
||||
{
|
||||
throw new ArgumentNullException("Нет имени поставщика", nameof(model.ProviderName));
|
||||
}
|
||||
if (model.DateShipment > DateTime.Now)
|
||||
{
|
||||
throw new ArgumentException($"Дата поступления {model.DateShipment} партии товаров не может быть в будущем");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -88,6 +88,7 @@ namespace ComputerShopBusinessLogic.BusinessLogics
|
||||
return true;
|
||||
}
|
||||
|
||||
//!!!мб проверять, что есть userID
|
||||
private void CheckModel(UserBindingModel model, bool withParams = true)
|
||||
{
|
||||
if (model == null)
|
||||
|
@ -14,7 +14,9 @@ namespace ComputerShopContracts.BusinessLogicContracts
|
||||
{
|
||||
List<OrderViewModel>? ReadList(OrderSearchModel? model);
|
||||
OrderViewModel? ReadElement(OrderSearchModel model);
|
||||
bool CreateOrder(OrderBindingModel model);
|
||||
bool Create(OrderBindingModel model);
|
||||
bool Update(OrderBindingModel model);
|
||||
bool Delete(OrderBindingModel model);
|
||||
bool TakeOrderInWork(OrderBindingModel model);
|
||||
bool FinishOrder(OrderBindingModel model);
|
||||
bool DeliveryOrder(OrderBindingModel model);
|
||||
|
@ -16,9 +16,6 @@ namespace ComputerShopContracts.ViewModels
|
||||
|
||||
public int UserId { get; set; }
|
||||
|
||||
//!!!МБ НЕ НУЖЕН string.Empty
|
||||
[DisplayName("Ник пользователя")]
|
||||
public string UserLogin { get; set; } = string.Empty;
|
||||
|
||||
//!!!ТУТ МБ НАДО DisplayName (НО ВЯРД ЛИ)
|
||||
|
||||
|
@ -18,8 +18,6 @@ namespace ComputerShopContracts.ViewModels
|
||||
public int UserId { get; set; }
|
||||
|
||||
//!!!МБ ДОБАВИТЬ НИК ПОЛЬЗОВАТЕЛЯ, СОЗДАВШЕГО ЗАЯВКУ
|
||||
[DisplayName("Ник пользователя")]
|
||||
public string UserLogin { get; set; } = string.Empty;
|
||||
|
||||
//id сборки
|
||||
public int? AssemblyId { get; set; }
|
||||
|
@ -17,10 +17,6 @@ namespace ComputerShopContracts.ViewModels
|
||||
//!!!ТУТ МБ НЕ НУЖЕН DisplayName
|
||||
public int UserId { get; set; }
|
||||
|
||||
//!!!МБ НЕ НУЖЕН string.Empty
|
||||
[DisplayName("Ник пользователя")]
|
||||
public string UserLogin { get; set; } = string.Empty;
|
||||
|
||||
public Dictionary<int, IOrderModel> ShipmentOrders { get; set; } = new();
|
||||
|
||||
//!!!МБ НЕ НУЖЕН string.Empty
|
||||
|
@ -1,6 +1,10 @@
|
||||
using ComputerShopContracts.StorageContracts;
|
||||
using ComputerShopContracts.BindingModels;
|
||||
using ComputerShopContracts.SearchModels;
|
||||
using ComputerShopContracts.StorageContracts;
|
||||
using ComputerShopContracts.ViewModels;
|
||||
using ComputerShopDatabaseImplement.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Internal;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@ -10,19 +14,116 @@ using System.Threading.Tasks;
|
||||
namespace ComputerShopDatabaseImplement.Implements
|
||||
{
|
||||
//!!!ПОДОБИЕ component
|
||||
//!!! ДОПИСАТЬ
|
||||
public class OrderStorage : IOrderStorage
|
||||
{
|
||||
//!!!ТУТ НАДО ПОЛУЧАТЬ СПИСОК ТОЛЬКО ПОЛЬЗОВАТЕЛЯ, КАК-ТО ДОБАВИТЬ (мб убрать этот метод)
|
||||
//!!!ТУТ СВЯЗЬ С НЕСКОЛЬКИМИ СУЩНОСТЯМИ
|
||||
public List<RequestViewModel> GetFullList()
|
||||
//!!!НЕ ФАКТ, ЧТО ПРАВИЛЬНО
|
||||
//!!!мб присоединять user
|
||||
public List<OrderViewModel> GetFullList()
|
||||
{
|
||||
//using var context = new ComputerShopDatabase();
|
||||
//return context.Orders
|
||||
// .Include(x => x.Shi)
|
||||
// .ThenInclude(x => x.Order)
|
||||
// .ToList()
|
||||
// .Select(x => x.GetViewModel)
|
||||
// .ToList();
|
||||
using var context = new ComputerShopDatabase();
|
||||
return context.Orders
|
||||
.Include(x => x.ShipmentOrders)
|
||||
.ThenInclude(x => x.Shipment)
|
||||
.Include(x => x.RequestOrders)
|
||||
.ThenInclude(x => x.Request)
|
||||
.ToList()
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
//!!!ПРОВЕРИТЬ
|
||||
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
|
||||
{
|
||||
using var context = new ComputerShopDatabase();
|
||||
if (model.DateFrom.HasValue && model.DateTo.HasValue)
|
||||
{
|
||||
return context.Orders
|
||||
.Where(x => (x.UserId == model.UserId) && (x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo))
|
||||
.Include(x => x.ShipmentOrders)
|
||||
.ThenInclude(x => x.Shipment)
|
||||
.Include(x => x.RequestOrders)
|
||||
.ThenInclude(x => x.Request)
|
||||
.ToList()
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
else if (model.Status.HasValue)
|
||||
{
|
||||
return context.Orders
|
||||
.Where(x => (x.UserId == model.UserId) && (x.Status == model.Status))
|
||||
.Include(x => x.ShipmentOrders)
|
||||
.ThenInclude(x => x.Shipment)
|
||||
.Include(x => x.RequestOrders)
|
||||
.ThenInclude(x => x.Request)
|
||||
.ToList()
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
//возвращение просто всех заказов пользователя (замена GetFullList)
|
||||
return context.Orders
|
||||
.Where(x => x.UserId == model.UserId)
|
||||
.Include(x => x.ShipmentOrders)
|
||||
.ThenInclude(x => x.Shipment)
|
||||
.Include(x => x.RequestOrders)
|
||||
.ThenInclude(x => x.Request)
|
||||
.ToList()
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
//!!!ПРОВЕРИТЬ
|
||||
public OrderViewModel? GetElement(OrderSearchModel model)
|
||||
{
|
||||
if (!model.Id.HasValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new ComputerShopDatabase();
|
||||
return context.Orders
|
||||
.Include(x => x.ShipmentOrders)
|
||||
.ThenInclude(x => x.Shipment)
|
||||
.Include(x => x.RequestOrders)
|
||||
.ThenInclude(x => x.Request)
|
||||
.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
|
||||
}
|
||||
|
||||
public OrderViewModel? Insert(OrderBindingModel model)
|
||||
{
|
||||
using var context = new ComputerShopDatabase();
|
||||
var newOrder = Order.Create(model);
|
||||
if (newOrder == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
context.Orders.Add(newOrder);
|
||||
context.SaveChanges();
|
||||
return newOrder.GetViewModel;
|
||||
}
|
||||
|
||||
public OrderViewModel? Update(OrderBindingModel model)
|
||||
{
|
||||
using var context = new ComputerShopDatabase();
|
||||
var order = context.Orders.FirstOrDefault(x => x.Id == model.Id);
|
||||
if (order == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
order.Update(model);
|
||||
context.SaveChanges();
|
||||
return order.GetViewModel;
|
||||
}
|
||||
|
||||
public OrderViewModel? Delete(OrderBindingModel model)
|
||||
{
|
||||
using var context = new ComputerShopDatabase();
|
||||
var order = context.Orders.FirstOrDefault(x => x.Id == model.Id);
|
||||
if (order != null)
|
||||
{
|
||||
context.Orders.Remove(order);
|
||||
context.SaveChanges();
|
||||
return order.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -15,52 +15,66 @@ namespace ComputerShopDatabaseImplement.Implements
|
||||
//!!!ПОДОБИЕ textile
|
||||
public class RequestStorage : IRequestStorage
|
||||
{
|
||||
//!!!ТУТ НАДО ПОЛУЧАТЬ СПИСОК ТОЛЬКО ПОЛЬЗОВАТЕЛЯ, КАК-ТО ДОБАВИТЬ (мб убрать этот метод)
|
||||
//!!!КАК ПРИСОЕДИНИТЬ СБОРКУ И НАДО ЛИ?
|
||||
public List<RequestViewModel> GetFullList()
|
||||
{
|
||||
using var context = new ComputerShopDatabase();
|
||||
return context.Requests
|
||||
.Include(x => x.Orders)
|
||||
.ThenInclude(x => x.Order)
|
||||
.Include(x => x.Assembly)
|
||||
.ToList()
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
//!!!тут добавил .Include(x => x.Assembly)
|
||||
public List<RequestViewModel> GetFilteredList(RequestSearchModel model) {
|
||||
using var context = new ComputerShopDatabase();
|
||||
|
||||
if (model.DateFrom.HasValue && model.DateTo.HasValue)
|
||||
{
|
||||
return context.Requests
|
||||
.Where(x => x.UserId == model.UserId)
|
||||
.Where(x => x.UserId == model.UserId && x.DateRequest >= model.DateFrom && x.DateRequest <= model.DateTo)
|
||||
.Include(x => x.Orders)
|
||||
.ThenInclude(x => x.Order)
|
||||
.Where(x => x.DateRequest >= model.DateFrom && x.DateRequest <= model.DateTo)
|
||||
.Include(x => x.Assembly)
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
//Получение заявок по сборкам
|
||||
else if (model.AssemblyId.HasValue)
|
||||
{
|
||||
return context.Requests
|
||||
.Where(x => x.UserId == model.UserId)
|
||||
.Where(x => x.UserId == model.UserId && x.AssemblyId == model.AssemblyId)
|
||||
.Include(x => x.Orders)
|
||||
.ThenInclude(x => x.Order)
|
||||
.Where(x => x.AssemblyId == model.AssemblyId)
|
||||
.Include(x => x.Assembly)
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
else if (!string.IsNullOrEmpty(model.ClientFIO))
|
||||
{
|
||||
return context.Requests
|
||||
.Where(x => x.UserId == model.UserId && x.ClientFIO == model.ClientFIO)
|
||||
.Include(x => x.Orders)
|
||||
.ThenInclude(x => x.Order)
|
||||
.Include (x => x.Assembly)
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
//возвращение просто всех заявок пользователя (замена GetFullList)
|
||||
return context.Requests
|
||||
.Where(x => x.UserId == model.UserId)
|
||||
.Include(x => x.Orders)
|
||||
.ThenInclude(x => x.Order)
|
||||
.Where(x => x.ClientFIO == model.ClientFIO)
|
||||
.Include(x => x.Assembly)
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
return new();
|
||||
}
|
||||
|
||||
//!!!мб тут надо будет в FirstOrDefault добавить другие параметры
|
||||
public RequestViewModel? GetElement(RequestSearchModel model)
|
||||
@ -74,8 +88,7 @@ namespace ComputerShopDatabaseImplement.Implements
|
||||
.Where(x => x.UserId == model.UserId)
|
||||
.Include(x => x.Orders)
|
||||
.ThenInclude(x => x.Order)
|
||||
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id)
|
||||
|| (!string.IsNullOrEmpty(model.ClientFIO) && x.ClientFIO == model.ClientFIO))
|
||||
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))
|
||||
?.GetViewModel;
|
||||
}
|
||||
|
||||
@ -93,11 +106,7 @@ namespace ComputerShopDatabaseImplement.Implements
|
||||
//!!!мб тут вернуть другое значение
|
||||
//!!!МБ ТУТ НЕЛЬЗЯ WHERE первым
|
||||
//!!!мб тут и не надо where по пользователю (попробовать потом без него)
|
||||
return context.Requests
|
||||
.Where(x => x.UserId == model.UserId)
|
||||
.Include(x => x.Orders)
|
||||
.ThenInclude(x => x.Order)
|
||||
.FirstOrDefault(x => x.Id == newRequest.Id)?.GetViewModel;
|
||||
return newRequest.GetViewModel;
|
||||
}
|
||||
|
||||
public RequestViewModel? Update(RequestBindingModel model)
|
||||
@ -115,11 +124,7 @@ namespace ComputerShopDatabaseImplement.Implements
|
||||
context.SaveChanges();
|
||||
request.UpdateOrders(context, model);
|
||||
transaction.Commit();
|
||||
return context.Requests
|
||||
.Include(x => x.Orders)
|
||||
.ThenInclude(x => x.Order)
|
||||
.FirstOrDefault(x => x.Id == model.Id)?
|
||||
.GetViewModel;
|
||||
return request.GetViewModel;
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
@ -33,9 +33,18 @@ namespace ComputerShopDatabaseImplement.Implements
|
||||
public List<ShipmentViewModel> GetFilteredList(ShipmentSearchModel model)
|
||||
{
|
||||
using var context = new ComputerShopDatabase();
|
||||
if (model.Id.HasValue && !model.DateFrom.HasValue && !model.DateTo.HasValue && string.IsNullOrEmpty(model.ProviderName))
|
||||
{
|
||||
return context.Shipments
|
||||
.Where(x => x.UserId == model.Id)
|
||||
.Include(x => x.Orders)
|
||||
.ThenInclude(x => x.Order)
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
//!!!мб добавить if (model.Id.HasValue)
|
||||
//!!!МБ WHERE НЕЛЬЗЯ СТАВИТЬ ПЕРВЫМ
|
||||
if (model.DateFrom.HasValue && model.DateTo.HasValue)
|
||||
else if (model.DateFrom.HasValue && model.DateTo.HasValue)
|
||||
{
|
||||
return context.Shipments
|
||||
.Where(x => x.UserId == model.UserId)
|
||||
|
@ -3,6 +3,7 @@ using ComputerShopContracts.SearchModels;
|
||||
using ComputerShopContracts.StorageContracts;
|
||||
using ComputerShopContracts.ViewModels;
|
||||
using ComputerShopDatabaseImplement.Models;
|
||||
using ComputerShopDataModels.Enums;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@ -14,14 +15,16 @@ namespace ComputerShopDatabaseImplement.Implements
|
||||
public class UserStorage : IUserStorage
|
||||
{
|
||||
//!!!ТУТ МБ РАЗДЕЛИТЬ НА 2 МЕТОДА: исполнителя и поручителя и добавить where
|
||||
public List<UserViewModel> GetFullList()
|
||||
public List<UserViewModel> GetFullList(UserRole role)
|
||||
{
|
||||
using var context = new ComputerShopDatabase();
|
||||
return context.Users.Select(x => x.GetViewModel).ToList();
|
||||
return context.Users.Where(x => x.Role == role).Select(x => x.GetViewModel).ToList();
|
||||
}
|
||||
|
||||
//!!!ТУТ МБ РАЗДЕЛИТЬ НА 2 МЕТОДА: исполнителя и поручителя и добавить where
|
||||
//!!!ДОПИСАТЬ
|
||||
//!!!мб не надо, т.к. есть getelement для получения 1 пользователя и получение всех пользователей по роли.
|
||||
//при фильтрации всегда (?) будет либо 1, либо все пользователи роли
|
||||
public List<UserViewModel> GetFilteredList(UserSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.Login))
|
||||
@ -29,14 +32,25 @@ namespace ComputerShopDatabaseImplement.Implements
|
||||
return new();
|
||||
}
|
||||
using var context = new ComputerShopDatabase();
|
||||
|
||||
}
|
||||
|
||||
//!!!ДОПИСАТЬ
|
||||
public UserViewModel? GetElement(UserSearchModel model)
|
||||
if (!string.IsNullOrEmpty(model.Login))
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
//!!!ПРОВЕРИТЬ
|
||||
public UserViewModel? GetElement(UserSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.Login) && string.IsNullOrEmpty(model.Email) && !model.Id.HasValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new ComputerShopDatabase();
|
||||
return context.Users.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id)
|
||||
|| (!string.IsNullOrEmpty(model.Login) && x.Login == model.Login)
|
||||
|| (!string.IsNullOrEmpty(model.Email) && x.Email == model.Email))
|
||||
?.GetViewModel;
|
||||
}
|
||||
|
||||
public UserViewModel? Insert(UserBindingModel model)
|
||||
{
|
||||
|
@ -73,7 +73,6 @@ namespace ComputerShopDatabaseImplement.Models
|
||||
{
|
||||
Id = Id,
|
||||
UserId = UserId,
|
||||
UserLogin = User.Login,
|
||||
DateCreate = DateCreate,
|
||||
Status = Status,
|
||||
Sum = Sum
|
||||
|
@ -23,6 +23,9 @@ namespace ComputerShopDatabaseImplement.Models
|
||||
|
||||
public int? AssemblyId { get; set; }
|
||||
|
||||
//!!!МБ ТУТ НЕ НАДО, НО НА ПОДОБИЕ С ЗАКАЗОМ И СВЯЗЬЮ С implmenter В ЛАБАХ НАДО
|
||||
public virtual Assembly? Assembly { get; set; } = new();
|
||||
|
||||
[Required]
|
||||
public DateTime DateRequest { get; set; }
|
||||
|
||||
@ -52,12 +55,15 @@ namespace ComputerShopDatabaseImplement.Models
|
||||
[ForeignKey("RequestId")]
|
||||
public virtual List<RequestOrder> Orders { get; set; } = new();
|
||||
|
||||
|
||||
public static Request Create(ComputerShopDatabase context, RequestBindingModel model)
|
||||
{
|
||||
return new Request() {
|
||||
Id = model.Id,
|
||||
UserId = model.UserId,
|
||||
User = context.Users.First(x => x.Id == model.UserId),
|
||||
AssemblyId = model.AssemblyId,
|
||||
Assembly = model.AssemblyId.HasValue ? context.Assemblies.First(x => x.Id == model.AssemblyId) : null,
|
||||
DateRequest = model.DateRequest,
|
||||
ClientFIO = model.ClientFIO,
|
||||
Orders = model.RequestOrders.Select(x => new RequestOrder
|
||||
@ -69,23 +75,28 @@ namespace ComputerShopDatabaseImplement.Models
|
||||
//!!!МБ ТУТ ЕЩЁ ЧТО-ТО ОБНОВИТЬ
|
||||
public void Update(RequestBindingModel model)
|
||||
{
|
||||
if (model == null) {
|
||||
return;
|
||||
}
|
||||
using var context = new ComputerShopDatabase();
|
||||
DateRequest = model.DateRequest;
|
||||
AssemblyId = model.AssemblyId.HasValue ? model.AssemblyId : AssemblyId;
|
||||
Assembly = model.AssemblyId.HasValue ? context.Assemblies.First(x => x.Id == model.AssemblyId) : Assembly;
|
||||
ClientFIO = model.ClientFIO;
|
||||
context.SaveChanges();
|
||||
}
|
||||
//!!!МБ ТУТ НЕ ВСЁ НАДО ПРИСВАИВАТЬ
|
||||
public RequestViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
UserId = UserId,
|
||||
UserLogin = User.Login,
|
||||
AssemblyId = AssemblyId,
|
||||
DateRequest = DateRequest,
|
||||
ClientFIO = ClientFIO
|
||||
};
|
||||
|
||||
//!!!ДОПИСАТЬ UpdateOrders
|
||||
|
||||
//!!!МБ ЭТОТ МЕТОД БУДЕТ НЕ НУЖЕН, ПОТОМУ ЧТО ПО ЗАДАНИЮ НЕ НАПИСАНО, ЧТО МОЖНО ОБНОВЛЯТЬ
|
||||
//!!!МБ НАЗВАТЬ КАК-ТО ПО-ДРУГОМУ (мб метод вынести в Implement)
|
||||
//!!!МБ ПЕРЕДАВАТЬ ЧТО-ТО ДРУГОЕ
|
||||
//!!!ПРОВЕРИТЬ, ЧТО ВСЁ ПРАВИЛЬНО ИЗВЛЕКАЮ
|
||||
//!!!делаю как в Textile
|
||||
|
@ -77,7 +77,6 @@ namespace ComputerShopDatabaseImplement.Models
|
||||
{
|
||||
Id = Id,
|
||||
UserId = UserId,
|
||||
UserLogin = User.Login,
|
||||
ProviderName = ProviderName,
|
||||
DateShipment = DateShipment,
|
||||
ShipmentOrders = ShipmentOrders
|
||||
|
Loading…
Reference in New Issue
Block a user