diff --git a/ComputerShopContracts/BindingModels/RequestBindingModel.cs b/ComputerShopContracts/BindingModels/RequestBindingModel.cs index fe1af9e..a279ebe 100644 --- a/ComputerShopContracts/BindingModels/RequestBindingModel.cs +++ b/ComputerShopContracts/BindingModels/RequestBindingModel.cs @@ -15,7 +15,7 @@ namespace ComputerShopContracts.BindingModels public int? AssemblyId { get; set; } - public DateTime DateMake { get; set; } = DateTime.Now; + public DateTime DateRequest { get; set; } = DateTime.Now; //!!!МБ НЕ НАДО string.Empty public string ClientFIO { get; set; } = string.Empty; diff --git a/ComputerShopContracts/ViewModels/RequestViewModel.cs b/ComputerShopContracts/ViewModels/RequestViewModel.cs index 1a3ca35..0a23b2c 100644 --- a/ComputerShopContracts/ViewModels/RequestViewModel.cs +++ b/ComputerShopContracts/ViewModels/RequestViewModel.cs @@ -31,7 +31,7 @@ namespace ComputerShopContracts.ViewModels //!!!МБ НЕ НУЖНО DateTime.Now [DisplayName("Дата оформления")] - public DateTime DateMake { get; set; } = DateTime.Now; + public DateTime DateRequest { get; set; } = DateTime.Now; //!!!МБ НЕ НУЖЕН string.Empty [DisplayName("ФИО клиента")] diff --git a/ComputerShopDataModels/Models/IRequestModel.cs b/ComputerShopDataModels/Models/IRequestModel.cs index 5b57b40..6afda86 100644 --- a/ComputerShopDataModels/Models/IRequestModel.cs +++ b/ComputerShopDataModels/Models/IRequestModel.cs @@ -24,7 +24,7 @@ namespace ComputerShopDataModels.Models /// /// Дата создания заявки /// - DateTime DateMake { get; } + DateTime DateRequest { get; } /// /// ФИО клиента, для которого создана заявка diff --git a/ComputerShopDatabaseImplement/Implements/OrderStorage.cs b/ComputerShopDatabaseImplement/Implements/OrderStorage.cs index 8111f16..45aad83 100644 --- a/ComputerShopDatabaseImplement/Implements/OrderStorage.cs +++ b/ComputerShopDatabaseImplement/Implements/OrderStorage.cs @@ -1,4 +1,7 @@ -using System; +using ComputerShopContracts.StorageContracts; +using ComputerShopContracts.ViewModels; +using Microsoft.EntityFrameworkCore; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,7 +9,20 @@ using System.Threading.Tasks; namespace ComputerShopDatabaseImplement.Implements { - internal class OrderStorage + //!!!ПОДОБИЕ component + public class OrderStorage : IOrderStorage { + //!!!ТУТ НАДО ПОЛУЧАТЬ СПИСОК ТОЛЬКО ПОЛЬЗОВАТЕЛЯ, КАК-ТО ДОБАВИТЬ (мб убрать этот метод) + //!!!ТУТ СВЯЗЬ С НЕСКОЛЬКИМИ СУЩНОСТЯМИ + public List GetFullList() + { + //using var context = new ComputerShopDatabase(); + //return context.Orders + // .Include(x => x.Shi) + // .ThenInclude(x => x.Order) + // .ToList() + // .Select(x => x.GetViewModel) + // .ToList(); + } } } diff --git a/ComputerShopDatabaseImplement/Implements/RequestStorage.cs b/ComputerShopDatabaseImplement/Implements/RequestStorage.cs index 88f569b..6929916 100644 --- a/ComputerShopDatabaseImplement/Implements/RequestStorage.cs +++ b/ComputerShopDatabaseImplement/Implements/RequestStorage.cs @@ -1,4 +1,10 @@ -using System; +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.SearchModels; +using ComputerShopContracts.StorageContracts; +using ComputerShopContracts.ViewModels; +using ComputerShopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,7 +12,136 @@ using System.Threading.Tasks; namespace ComputerShopDatabaseImplement.Implements { - internal class RequestStorage + //!!!ПОДОБИЕ textile + public class RequestStorage : IRequestStorage { + //!!!ТУТ НАДО ПОЛУЧАТЬ СПИСОК ТОЛЬКО ПОЛЬЗОВАТЕЛЯ, КАК-ТО ДОБАВИТЬ (мб убрать этот метод) + public List GetFullList() + { + using var context = new ComputerShopDatabase(); + return context.Requests + .Include(x => x.Orders) + .ThenInclude(x => x.Order) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(RequestSearchModel model) { + using var context = new ComputerShopDatabase(); + if (model.DateFrom.HasValue && model.DateTo.HasValue) + { + return context.Requests + .Where(x => x.UserId == model.UserId) + .Include(x => x.Orders) + .ThenInclude(x => x.Order) + .Where(x => x.DateRequest >= model.DateFrom && x.DateRequest <= model.DateTo) + .Select(x => x.GetViewModel) + .ToList(); + } + //Получение заявок по сборкам + else if (model.AssemblyId.HasValue) + { + return context.Requests + .Where(x => x.UserId == model.UserId) + .Include(x => x.Orders) + .ThenInclude(x => x.Order) + .Where(x => x.AssemblyId == model.AssemblyId) + .Select(x => x.GetViewModel) + .ToList(); + } + else if (!string.IsNullOrEmpty(model.ClientFIO)) + { + return context.Requests + .Where(x => x.UserId == model.UserId) + .Include(x => x.Orders) + .ThenInclude(x => x.Order) + .Where(x => x.ClientFIO == model.ClientFIO) + .Select(x => x.GetViewModel) + .ToList(); + } + return new(); + } + + //!!!мб тут надо будет в FirstOrDefault добавить другие параметры + public RequestViewModel? GetElement(RequestSearchModel model) + { + if (!model.Id.HasValue && !model.DateFrom.HasValue && !model.DateTo.HasValue && !model.AssemblyId.HasValue && string.IsNullOrEmpty(model.ClientFIO)) + { + return null; + } + using var context = new ComputerShopDatabase(); + return context.Requests + .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)) + ?.GetViewModel; + } + + public RequestViewModel? Insert(RequestBindingModel model) + { + using var context = new ComputerShopDatabase(); + var newRequest = Request.Create(context, model); + if (newRequest == null) + { + return null; + } + context.Requests.Add(newRequest); + context.SaveChanges(); + + //!!!мб тут вернуть другое значение + //!!!МБ ТУТ НЕЛЬЗЯ 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; + } + + public RequestViewModel? Update(RequestBindingModel model) + { + using var context = new ComputerShopDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var request = context.Requests.FirstOrDefault(x => x.Id == model.Id); + if (request == null) + { + return null; + } + request.Update(model); + 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; + } + catch + { + transaction.Rollback(); + throw; + } + } + + //!!!мб возвращать не просто request.getviewmodel + public RequestViewModel? Delete(RequestBindingModel model) + { + using var context = new ComputerShopDatabase(); + var request = context.Requests + .Include(x => x.Orders) + .FirstOrDefault(y => y.Id == model.Id); + if (request != null) + { + context.Requests.Remove(request); + context.SaveChanges(); + return request.GetViewModel; + } + return null; + } } } diff --git a/ComputerShopDatabaseImplement/Implements/ShipmentStorage.cs b/ComputerShopDatabaseImplement/Implements/ShipmentStorage.cs index 0737245..ce61c14 100644 --- a/ComputerShopDatabaseImplement/Implements/ShipmentStorage.cs +++ b/ComputerShopDatabaseImplement/Implements/ShipmentStorage.cs @@ -1,4 +1,10 @@ -using System; +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.SearchModels; +using ComputerShopContracts.StorageContracts; +using ComputerShopContracts.ViewModels; +using ComputerShopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,7 +12,141 @@ using System.Threading.Tasks; namespace ComputerShopDatabaseImplement.Implements { - internal class ShipmentStorage + //!!!ПОДОБИЕ textile + public class ShipmentStorage : IShipmentStorage { + //!!!ПРОВЕРИТЬ, ЧТО ПРАВИЛЬНО ПОЛУЧАЮ СПИСОК + //!!!ТУТ НАДО ПОЛУЧАТЬ СПИСОК ТОЛЬКО ПОЛЬЗОВАТЕЛЯ, КАК-ТО ДОБАВИТЬ (мб убрать этот метод) + public List GetFullList() + { + using var context = new ComputerShopDatabase(); + + return context.Shipments + .Include(x => x.Orders) + .ThenInclude(x => x.Order) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + //!!!МБ ТУТ ВСЁ ВРЕМЯ НАДО БУДЕТ УЧИТЫВАТЬ UserID + public List GetFilteredList(ShipmentSearchModel model) + { + using var context = new ComputerShopDatabase(); + //!!!мб добавить if (model.Id.HasValue) + //!!!МБ WHERE НЕЛЬЗЯ СТАВИТЬ ПЕРВЫМ + if (model.DateFrom.HasValue && model.DateTo.HasValue) + { + return context.Shipments + .Where(x => x.UserId == model.UserId) + .Include(x => x.Orders) + .ThenInclude(x => x.Order) + .Where(x => x.DateShipment >= model.DateFrom && x.DateShipment <= model.DateTo) + .Select(x => x.GetViewModel) + .ToList(); + } + //!!!ВРЯД ЛИ ПРАВИЛЬНО (мб не надо include user) + //else if (model.UserId.HasValue) + //{ + // return context.Shipments + // .Include(x => x.User) + // .Where(x => x.UserId == model.UserId) + // .Include(x => x.Orders) + // .ThenInclude(x => x.Order) + // .Select (x => x.GetViewModel) + // .ToList(); + //} + else if (!string.IsNullOrEmpty(model.ProviderName)) + { + return context.Shipments + .Where(x => x.ProviderName == model.ProviderName && x.UserId == model.UserId) + .Include(x => x.Orders) + .ThenInclude(x => x.Order) + .Select(x => x.GetViewModel) + .ToList(); + } + return new(); + } + + public ShipmentViewModel? GetElement(ShipmentSearchModel model) + { + if (!model.Id.HasValue && !model.DateFrom.HasValue && !model.DateTo.HasValue && string.IsNullOrEmpty(model.ProviderName)) + { + return null; + } + using var context = new ComputerShopDatabase(); + return context.Shipments + .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.ProviderName) && x.ProviderName == model.ProviderName)) + ?.GetViewModel; + } + + public ShipmentViewModel? Insert(ShipmentBindingModel model) + { + using var context = new ComputerShopDatabase(); + var newShipment = Shipment.Create(context, model); + + if (newShipment == null) + { + return null; + } + context.Shipments.Add(newShipment); + context.SaveChanges(); + + //!!!мб тут вернуть другое значение + //!!!МБ ТУТ НЕЛЬЗЯ WHERE первым + return context.Shipments + .Where(x => x.UserId == model.UserId) + .Include(x => x.Orders) + .ThenInclude(x => x.Order) + .FirstOrDefault(x => x.Id == newShipment.Id)?.GetViewModel; + } + + //!!!МБ возвращать что-то другое + public ShipmentViewModel? Update(ShipmentBindingModel model) + { + using var context = new ComputerShopDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var shipment = context.Shipments.FirstOrDefault(x => x.Id == model.Id); + if (shipment == null) + { + return null; + } + shipment.Update(model); + context.SaveChanges(); + shipment.UpdateOrders(context, model); + transaction.Commit(); + return context.Shipments + .Include(x => x.Orders) + .ThenInclude(x => x.Order) + .FirstOrDefault(x => x.Id == model.Id)? + .GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + //!!!мб по-другому присваивать значение shipment + //!!!мб возвращать не просто shipment.getviewmodel + public ShipmentViewModel? Delete(ShipmentBindingModel model) + { + using var context = new ComputerShopDatabase(); + var shipment = context.Shipments.Include(x => x.Orders).FirstOrDefault(y => y.Id == model.Id); + if (shipment != null) + { + context.Shipments.Remove(shipment); + context.SaveChanges(); + return shipment.GetViewModel; + } + return null; + } } } diff --git a/ComputerShopDatabaseImplement/Models/Request.cs b/ComputerShopDatabaseImplement/Models/Request.cs index 64fd3ee..9868507 100644 --- a/ComputerShopDatabaseImplement/Models/Request.cs +++ b/ComputerShopDatabaseImplement/Models/Request.cs @@ -24,7 +24,7 @@ namespace ComputerShopDatabaseImplement.Models public int? AssemblyId { get; set; } [Required] - public DateTime DateMake { get; set; } + public DateTime DateRequest { get; set; } [Required] public string ClientFIO { get; set; } = string.Empty; @@ -58,7 +58,7 @@ namespace ComputerShopDatabaseImplement.Models Id = model.Id, UserId = model.UserId, AssemblyId = model.AssemblyId, - DateMake = model.DateMake, + DateRequest = model.DateRequest, ClientFIO = model.ClientFIO, Orders = model.RequestOrders.Select(x => new RequestOrder { @@ -69,7 +69,7 @@ namespace ComputerShopDatabaseImplement.Models //!!!МБ ТУТ ЕЩЁ ЧТО-ТО ОБНОВИТЬ public void Update(RequestBindingModel model) { - DateMake = model.DateMake; + DateRequest = model.DateRequest; ClientFIO = model.ClientFIO; } //!!!МБ ТУТ НЕ ВСЁ НАДО ПРИСВАИВАТЬ @@ -79,10 +79,38 @@ namespace ComputerShopDatabaseImplement.Models UserId = UserId, UserLogin = User.Login, AssemblyId = AssemblyId, - DateMake = DateMake, + DateRequest = DateRequest, ClientFIO = ClientFIO }; //!!!ДОПИСАТЬ UpdateOrders + //!!!МБ ЭТОТ МЕТОД БУДЕТ НЕ НУЖЕН, ПОТОМУ ЧТО ПО ЗАДАНИЮ НЕ НАПИСАНО, ЧТО МОЖНО ОБНОВЛЯТЬ + //!!!МБ НАЗВАТЬ КАК-ТО ПО-ДРУГОМУ (мб метод вынести в Implement) + //!!!МБ ПЕРЕДАВАТЬ ЧТО-ТО ДРУГОЕ + //!!!ПРОВЕРИТЬ, ЧТО ВСЁ ПРАВИЛЬНО ИЗВЛЕКАЮ + //!!!делаю как в Textile + public void UpdateOrders(ComputerShopDatabase context, RequestBindingModel model) + { + var requestOrders = context.RequestOrders.Where(x => x.RequestId == model.Id).ToList(); + //удаление тех заказов, которых нет в модели + if (requestOrders != null && requestOrders.Count > 0) + { + //!!!ТУТ МБ НЕ x.OrderId, а x.RequestId, но не факт (вряд ли) + context.RequestOrders.RemoveRange(requestOrders.Where(x => !model.RequestOrders.ContainsKey(x.OrderId))); + context.SaveChanges(); + } + //добавление новых заказов + var currentRequest = context.Requests.First(x => x.Id == Id); + foreach (var request_order in model.RequestOrders) + { + context.RequestOrders.Add(new RequestOrder + { + Request = currentRequest, + Order = context.Orders.First(x => x.Id == request_order.Key) + }); + context.SaveChanges(); + } + _requestOrders = null; + } } }