From 647b797a19267ad44d803edaf1376ab6ac1c4881 Mon Sep 17 00:00:00 2001 From: Yourdax Date: Tue, 30 Apr 2024 00:28:56 +0400 Subject: [PATCH] Fix orderstorage --- .../StorageContracts/IUserStorage.cs | 1 - .../Implements/OrderStorage.cs | 96 +++++++------------ .../Implements/UserStorage.cs | 17 +--- 3 files changed, 38 insertions(+), 76 deletions(-) diff --git a/DiningRoom/DiningRoomContracts/StorageContracts/IUserStorage.cs b/DiningRoom/DiningRoomContracts/StorageContracts/IUserStorage.cs index dca9103..d2d0b5c 100644 --- a/DiningRoom/DiningRoomContracts/StorageContracts/IUserStorage.cs +++ b/DiningRoom/DiningRoomContracts/StorageContracts/IUserStorage.cs @@ -12,7 +12,6 @@ namespace DiningRoomContracts.StorageContracts public interface IUserStorage { List GetFullList(); - List GetFilteredList(UserSearchModel model); UserViewModel? GetElement(UserSearchModel model); UserViewModel? Insert(UserBindingModel model); UserViewModel? Update(UserBindingModel model); diff --git a/DiningRoom/DiningRoomDatabaseImplement/Implements/OrderStorage.cs b/DiningRoom/DiningRoomDatabaseImplement/Implements/OrderStorage.cs index f1c92a7..74ad27c 100644 --- a/DiningRoom/DiningRoomDatabaseImplement/Implements/OrderStorage.cs +++ b/DiningRoom/DiningRoomDatabaseImplement/Implements/OrderStorage.cs @@ -45,65 +45,51 @@ namespace DiningRoomDatabaseImplement.Implements using var context = new DiningRoomDatabase(); return context.Orders - .Include(x => x.Client) - .Include(x => x.Implementer) - .FirstOrDefault(x => - (model.Statuses == null || model.Statuses != null && model.Statuses.Contains(x.Status)) && - model.ImplementerId.HasValue && x.ImplementerId == model.ImplementerId || - model.Id.HasValue && x.Id == model.Id - ) - ?.GetViewModel; + .Include(x=> x.Product) + .FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; } public List GetFilteredList(OrderSearchModel model) { - if (model.Id.HasValue) - { - var result = GetElement(model); - return result != null ? new() { result } : new(); - } - - using var context = new CarpentryWorkshopDatabase(); - IQueryable? queryWhere = null; - + using var context = new DiningRoomDatabase(); + if (model.DateFrom.HasValue && model.DateTo.HasValue) { - queryWhere = context.Orders - .Where(x => model.DateFrom <= x.DateCreate.Date && - x.DateCreate.Date <= model.DateTo); + return context.Orders + .Where(x => (x.UserId == model.UserId) && (x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo)) + .Include(x => x.Product) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); } - - else if (model.Statuses != null) + + else if (model.Status.HasValue) { - queryWhere = context.Orders.Where(x => model.Statuses.Contains(x.Status)); + return context.Orders + .Where(x => (x.UserId == model.UserId) && (x.Status == model.Status)) + .Include(x => x.Product) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); } - - else if (model.ClientId.HasValue) - { - queryWhere = context.Orders.Where(x => x.ClientId == model.ClientId); - } - - else - { - return new(); - } - - return queryWhere - .Include(x => x.Client) - .Include(x => x.Implementer) + + return context.Orders + .Where(x => x.UserId == model.UserId) + .Include(x => x.Product) + .ToList() .Select(x => x.GetViewModel) .ToList(); } public List GetFullList() { - using var context = new CarpentryWorkshopDatabase(); + using var context = new DiningRoomDatabase(); return context.Orders - .Include(x => x.Wood) - .Include(x => x.Client) - .Include(x => x.Implementer) - .Select(x => x.GetViewModel).ToList(); + .Include(x => x.Product) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); } public OrderViewModel? Insert(OrderBindingModel model) @@ -115,37 +101,27 @@ namespace DiningRoomDatabaseImplement.Implements return null; } - using var context = new CarpentryWorkshopDatabase(); - + using var context = new DiningRoomDatabase(); + if (newOrder == null) + { + return null; + } context.Orders.Add(newOrder); context.SaveChanges(); - - return context.Orders - .Include(x => x.Wood) - .Include(x => x.Client) - .Include(x => x.Implementer) - .FirstOrDefault(x => x.Id == newOrder.Id) - ?.GetViewModel; + return newOrder.GetViewModel; } public OrderViewModel? Update(OrderBindingModel model) { - using var context = new CarpentryWorkshopDatabase(); - - var order = context.Orders - .Include(x => x.Wood) - .Include(x => x.Client) - .Include(x => x.Implementer) - .FirstOrDefault(x => x.Id == model.Id); + using var context = new DiningRoomDatabase(); + var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); if (order == null) { return null; } - order.Update(model); context.SaveChanges(); - return order.GetViewModel; } } diff --git a/DiningRoom/DiningRoomDatabaseImplement/Implements/UserStorage.cs b/DiningRoom/DiningRoomDatabaseImplement/Implements/UserStorage.cs index 36b11bb..43efb3f 100644 --- a/DiningRoom/DiningRoomDatabaseImplement/Implements/UserStorage.cs +++ b/DiningRoom/DiningRoomDatabaseImplement/Implements/UserStorage.cs @@ -12,7 +12,7 @@ using System.Threading.Tasks; namespace DiningRoomDatabaseImplement.Implements { - //!!!МБ У USER ХРАНИТЬ СПИСКИ ВСЕХ СОЗДАННЫХ СУШНОСТЕЙ И ТОГДА ПРИ СОЗДАНИИ, УДАЛЕНИИ СУЩНОСТЕЙ ЕЩЁ СОЗДАВАТЬ И УДАЛЯТЬ ИЗ СПИСКА У ПОЛЬЗОВАТЕЛЯ + public class UserStorage : IUserStorage { public List GetFullList() @@ -21,19 +21,6 @@ namespace DiningRoomDatabaseImplement.Implements return context.Users.Select(x => x.GetViewModel).ToList(); } - //Фильтрация пользователей логически нужна только для роли (по остальным параметрам будет 1 или 0 пользователей) - public List GetFilteredList(UserSearchModel model) - { - if (model.Role == null || model.Role == UserRole.Неизвестная) - { - return new(); - } - using var context = new DiningRoomDatabase(); - return context.Users.Where(x => x.Role == model.Role).Select(x => x.GetViewModel).ToList(); - } - - //!!!ПРОВЕРИТЬ - //id, почта и логин уникальны, можно получать по ним public UserViewModel? GetElement(UserSearchModel model) { //!!!МБ ЭТУ ПРОВЕРКУ УБРАТЬ @@ -46,7 +33,7 @@ namespace DiningRoomDatabaseImplement.Implements //Поиск пользователя при входе в систему по логину, паролю и его роли (чтобы не могли войти в аккаунты другой роли) if (!string.IsNullOrEmpty(model.Login) && !string.IsNullOrEmpty(model.Password) && model.Role.HasValue) { - return context.Users.FirstOrDefault(x => x.Login == model.Login && x.Password == model.Password && x.Role == model.Role)?.GetViewModel; + return context.Users.FirstOrDefault(x => x.Login == model.Login && x.Password == model.Password)?.GetViewModel; } //!!!НИЖЕ МБ НЕ НАДО //Получение по логину (пользователей с таким логином будет 1 или 0)