Почти доделал

This commit is contained in:
2025-05-27 17:31:03 +04:00
parent da946e07ce
commit 491df32c85
279 changed files with 172737 additions and 1121 deletions

View File

@@ -0,0 +1,201 @@
using ComputerStoreContracts.BindingModels;
using ComputerStoreContracts.StoragesContracts;
using ComputerStoreContracts.ViewModels;
using ComputerStoreDatabase.Models;
using Microsoft.EntityFrameworkCore;
namespace ComputerStoreDatabase.Implementations;
public class AssemblyStorageImplementation : IAssemblyStorageContract
{
private readonly ComputerStoreDbContext _context;
public AssemblyStorageImplementation(ComputerStoreDbContext context)
{
_context = context;
}
public List<AssemblyViewModel> GetFullList() => _context.Assemblies
.Select(a => new AssemblyViewModel
{
Id = a.Id,
Name = a.Name,
Description = a.Description,
UserId = a.UserId
}).ToList();
public AssemblyViewModel? GetElement(int id) => _context.Assemblies
.Where(a => a.Id == id)
.Select(a => new AssemblyViewModel
{
Id = a.Id,
Name = a.Name,
Description = a.Description,
UserId = a.UserId
}).FirstOrDefault();
public void Create(AssemblyCreateBindingModel model)
{
var assembly = new Assembly
{
Name = model.Name,
Description = model.Description,
UserId = model.UserId
};
// Добавляем связь с компонентами
foreach (var componentId in model.ComponentIds)
{
var component = _context.Components.Find(componentId);
if (component != null)
{
assembly.ComponentsLink.Add(new ComponentAssembly
{
ComponentId = componentId
});
}
}
_context.Assemblies.Add(assembly);
_context.SaveChanges();
}
public void Update(AssemblyCreateBindingModel model)
{
var assembly = _context.Assemblies
.Include(a => a.ComponentsLink)
.FirstOrDefault(a => a.Id == model.Id);
if (assembly == null) throw new Exception("Сборка не найдена");
assembly.Name = model.Name;
assembly.Description = model.Description;
// Удаляем старые связи
var oldLinks = assembly.ComponentsLink.ToList();
foreach (var link in oldLinks)
{
_context.ComponentAssemblies.Remove(link);
}
// Добавляем новые
foreach (var componentId in model.ComponentIds)
{
_context.ComponentAssemblies.Add(new ComponentAssembly
{
AssemblyId = model.Id,
ComponentId = componentId
});
}
_context.Assemblies.Update(assembly);
_context.SaveChanges();
}
public void Delete(int id)
{
var assembly = _context.Assemblies.Find(id);
if (assembly != null)
{
_context.Assemblies.Remove(assembly);
_context.SaveChanges();
}
}
public List<ComponentViewModel> GetComponentsByAssembly(int assemblyId)
{
return _context.ComponentAssemblies
.Where(ca => ca.AssemblyId == assemblyId)
.Select(ca => new ComponentViewModel
{
Id = ca.Component.Id,
Name = ca.Component.Name,
Manufacturer = ca.Component.Manufacturer,
Price = ca.Component.Price,
UserId = ca.Component.UserID
}).ToList();
}
public List<AssemblyViewModel> GetAssembliesWithoutRequest()
{
var requestAssemblyIds = _context.Requests
.Where(r => r.AssemblyId.HasValue)
.Select(r => r.AssemblyId.Value)
.ToList();
return _context.Assemblies
.Where(a => !requestAssemblyIds.Contains(a.Id))
.Select(a => new AssemblyViewModel
{
Id = a.Id,
Name = a.Name,
Description = a.Description,
UserId = a.UserId
}).ToList();
}
public List<AssemblyViewModel> GetAssembliesByOrders(List<int> orderIds)
{
var requestIds = _context.OrderRequests
.Where(or => orderIds.Contains(or.OrderId))
.Select(or => or.RequestId)
.ToList();
var assemblyIds = _context.Assemblies
.Where(a => a.RequestId.HasValue && requestIds.Contains(a.RequestId.Value))
.Select(a => a.Id)
.Distinct()
.ToList();
return _context.Assemblies
.Where(a => assemblyIds.Contains(a.Id))
.Select(a => new AssemblyViewModel
{
Id = a.Id,
Name = a.Name,
Description = a.Description,
UserId = a.UserId
}).ToList();
}
public List<AssemblyViewModel> GetUnlinkedAssemblies(int userId) => _context.Assemblies
.Where(a => !a.RequestId.HasValue)
.Select(a => new AssemblyViewModel
{
Id = a.Id,
Name = a.Name,
Description = a.Description,
UserId = a.UserId
}).ToList();
public List<AssemblyViewModel> GetAssembliesByRequest(int requestId) => _context.Assemblies
.Where(a => a.RequestId == requestId)
.Select(a => new AssemblyViewModel
{
Id = a.Id,
Name = a.Name,
Description = a.Description,
UserId = a.UserId
}).ToList();
public void AttachRequestToAssembly(int assemblyId, int requestId)
{
var assembly = _context.Assemblies.Find(assemblyId);
if (assembly == null) throw new Exception("Сборка не найдена");
assembly.RequestId = requestId;
_context.Assemblies.Update(assembly);
_context.SaveChanges();
}
public void DetachRequestFromAssembly(int assemblyId)
{
var assembly = _context.Assemblies.Find(assemblyId);
if (assembly == null || !assembly.RequestId.HasValue) return;
assembly.RequestId = null;
_context.Assemblies.Update(assembly);
_context.SaveChanges();
}
}

View File

@@ -0,0 +1,115 @@
using ComputerStoreContracts.BindingModels;
using ComputerStoreContracts.StoragesContracts;
using ComputerStoreContracts.ViewModels;
using ComputerStoreDatabase.Models;
namespace ComputerStoreDatabase.Implementations;
public class ComponentStorageImplementation : IComponentStorageContract
{
private readonly ComputerStoreDbContext _context;
public ComponentStorageImplementation(ComputerStoreDbContext context)
{
_context = context;
}
public List<ComponentViewModel> GetFullList() => _context.Components
.Select(c => new ComponentViewModel
{
Id = c.Id,
Name = c.Name,
Manufacturer = c.Manufacturer,
Price = c.Price,
UserId = c.UserID
}).ToList();
public ComponentViewModel? GetElement(int id) => _context.Components
.Where(c => c.Id == id)
.Select(c => new ComponentViewModel
{
Id = c.Id,
Name = c.Name,
Manufacturer = c.Manufacturer,
Price = c.Price,
UserId = c.UserID
}).FirstOrDefault();
public void Create(ComponentCreateBindingModel model)
{
var component = new Component
{
Name = model.Name,
Manufacturer = model.Manufacturer,
Price = model.Price,
UserID = model.UserId
};
_context.Components.Add(component);
_context.SaveChanges();
}
public void Update(ComponentCreateBindingModel model)
{
var component = _context.Components.Find(model.Id);
if (component == null) throw new Exception("Комплектующее не найдено");
component.Name = model.Name;
component.Manufacturer = model.Manufacturer;
component.Price = model.Price;
_context.Components.Update(component);
_context.SaveChanges();
}
public void Delete(int id)
{
var component = _context.Components.Find(id);
if (component != null)
{
_context.Components.Remove(component);
_context.SaveChanges();
}
}
public List<ProductViewModel> GetProductsByComponents(List<int> componentIds)
{
return _context.ProductComponents
.Where(pc => componentIds.Contains(pc.ComponentId))
.Select(pc => pc.Product)
.Distinct()
.Select(p => new ProductViewModel
{
Id = p.Id,
Name = p.Name,
Price = p.Price,
UserId = p.UserID
}).ToList();
}
public List<OrderBatchViewModel> GetUnlinkedBatches(int productId)
{
var linkedBatchIds = _context.ProductComponents
.Where(pc => pc.ProductId == productId)
.Select(pc => pc.ComponentId)
.ToList();
var componentIds = _context.Components
.Where(c => c.UserID == productId)
.Select(c => c.Id)
.ToList();
var batchIdsWithComponent = _context.OrderOrderBatches
.Where(ob => componentIds.Contains(ob.OrderBatchId))
.Select(ob => ob.OrderBatchId)
.ToList();
return _context.OrderBatches
.Where(b => !batchIdsWithComponent.Contains(b.Id))
.Select(b => new OrderBatchViewModel
{
Id = b.Id,
Name = b.Name,
UserId = b.UserID
}).ToList();
}
}

View File

@@ -0,0 +1,188 @@
using ComputerStoreContracts.BindingModels;
using ComputerStoreContracts.StoragesContracts;
using ComputerStoreContracts.ViewModels;
using ComputerStoreDatabase.Models;
namespace ComputerStoreDatabase.Implementations;
public class OrderBatchStorageImplementation : IOrderBatchStorageContract
{
private readonly ComputerStoreDbContext _context;
public OrderBatchStorageImplementation(ComputerStoreDbContext context)
{
_context = context;
}
public List<OrderBatchViewModel> GetFullList() => _context.OrderBatches
.Select(b => new OrderBatchViewModel
{
Id = b.Id,
Name = b.Name,
UserId = b.UserID,
ProductId = b.ProductId,
CreatedAt = b.CreatedAt,
}).ToList();
public OrderBatchViewModel? GetElement(int id) => _context.OrderBatches
.Where(b => b.Id == id)
.Select(b => new OrderBatchViewModel
{
Id = b.Id,
Name = b.Name,
UserId = b.UserID,
CreatedAt = b.CreatedAt,
}).FirstOrDefault();
public void Create(OrderBatchCreateBindingModel model)
{
var batch = new OrderBatch
{
Name = model.Name,
UserID = model.UserId,
CreatedAt = DateTime.UtcNow,
};
_context.OrderBatches.Add(batch);
_context.SaveChanges();
}
public void Update(OrderBatchCreateBindingModel model)
{
var batch = _context.OrderBatches.Find(model.Id);
if (batch == null) throw new Exception("Партия не найдена");
batch.Name = model.Name;
_context.OrderBatches.Update(batch);
_context.SaveChanges();
}
public List<OrderBatchViewModel> GetBatchesByProduct(int userId) => _context.OrderBatches
.Where(b => b.UserID == userId && b.ProductId.HasValue)
.Select(b => new OrderBatchViewModel
{
Id = b.Id,
Name = b.Name,
UserId = b.UserID
}).ToList();
public List<OrderBatchViewModel> GetBatchesWithoutProduct(int userId, int productId) => _context.OrderBatches
.Where(b => !b.ProductId.HasValue || b.ProductId == productId )
.Select(b => new OrderBatchViewModel
{
Id = b.Id,
Name = b.Name,
UserId = b.UserID
}).ToList();
public void AssignProductToBatches(List<int> batchIds, int productId)
{
var batches = _context.OrderBatches
.Where(b => batchIds.Contains(b.Id))
.ToList();
foreach (var batch in batches)
{
batch.ProductId = productId;
}
_context.SaveChanges();
}
public void Delete(int id)
{
var batch = _context.OrderBatches.Find(id);
if (batch != null)
{
_context.OrderBatches.Remove(batch);
_context.SaveChanges();
}
}
public List<AssemblyViewModel> GetUnlinkedAssemblies(int orderId)
{
var requestIds = _context.OrderRequests
.Where(or => or.OrderId == orderId)
.Select(or => or.RequestId)
.ToList();
var assemblyIds = _context.Requests
.Where(r => requestIds.Contains(r.Id))
.Select(r => r.AssemblyId)
.Where(a => a.HasValue)
.Select(a => a.Value)
.ToList();
return _context.Assemblies
.Where(a => !assemblyIds.Contains(a.Id))
.Select(a => new AssemblyViewModel
{
Id = a.Id,
Name = a.Name,
Description = a.Description,
UserId = a.UserId
}).ToList();
}
public List<AssemblyViewModel> GetAssembliesByOrders(List<int> orderIds)
{
var requestIds = _context.OrderRequests
.Where(or => orderIds.Contains(or.OrderId))
.Select(or => or.RequestId)
.ToList();
var assemblyIds = _context.Requests
.Where(r => requestIds.Contains(r.Id) && r.AssemblyId.HasValue)
.Select(r => r.AssemblyId.Value)
.ToList();
return _context.Assemblies
.Where(a => assemblyIds.Contains(a.Id))
.Select(a => new AssemblyViewModel
{
Id = a.Id,
Name = a.Name,
Description = a.Description,
UserId = a.UserId
}).ToList();
}
public List<OrderViewModel> GetOrdersByBatch(int batchId)
{
return _context.OrderOrderBatches
.Where(ob => ob.OrderBatchId == batchId)
.Select(ob => ob.Order)
.Select(o => new OrderViewModel
{
Id = o.Id,
Status = o.Status,
UserId = o.UserID,
CreatedAt = o.CreatedAt,
}).ToList();
}
public void AddOrdersToBatch(int batchId, List<int> orderIds)
{
foreach (var orderId in orderIds)
{
if (!_context.OrderOrderBatches.Any(ob => ob.OrderId == orderId && ob.OrderBatchId == batchId))
{
_context.OrderOrderBatches.Add(new OrderOrderBatch
{
OrderId = orderId,
OrderBatchId = batchId
});
}
}
_context.SaveChanges();
}
public void RemoveOrdersFromBatch(int batchId, List<int> orderIds)
{
var links = _context.OrderOrderBatches
.Where(ob => ob.OrderBatchId == batchId && orderIds.Contains(ob.OrderId));
_context.OrderOrderBatches.RemoveRange(links);
_context.SaveChanges();
}
}

View File

@@ -0,0 +1,157 @@
using ComputerStoreContracts.BindingModels;
using ComputerStoreContracts.StoragesContracts;
using ComputerStoreContracts.ViewModels;
using ComputerStoreDatabase.Models;
using Microsoft.EntityFrameworkCore;
namespace ComputerStoreDatabase.Implementations;
public class OrderStorageImplementation : IOrderStorageContract
{
private readonly ComputerStoreDbContext _context;
public OrderStorageImplementation(ComputerStoreDbContext context)
{
_context = context;
}
public List<OrderViewModel> GetFullList() => _context.Orders
.Select(o => new OrderViewModel
{
Id = o.Id,
CreatedAt = o.CreatedAt,
Status = o.Status,
UserId = o.UserID,
RequestIds = o.OrderRequests.Select(or => or.RequestId).ToList(),
BatchIds = o.OrderBatchesLink.Select(ob => ob.OrderBatchId).ToList()
}).ToList();
public OrderViewModel? GetElement(int id) => _context.Orders
.Where(o => o.Id == id)
.Select(o => new OrderViewModel
{
Id = o.Id,
CreatedAt = o.CreatedAt,
Status = o.Status,
UserId = o.UserID,
RequestIds = o.OrderRequests.Select(or => or.RequestId).ToList(),
BatchIds = o.OrderBatchesLink.Select(ob => ob.OrderBatchId).ToList()
}).FirstOrDefault();
public void Create(OrderCreateBindingModel model)
{
var order = new Order
{
Status = model.Status,
UserID = model.UserId,
OrderRequests = model.RequestIds.Select(r => new OrderRequest
{
OrderId = model.UserId, // будет обновлено после SaveChanges
RequestId = r
}).ToList(),
OrderBatchesLink = model.BatchIds.Select(b => new OrderOrderBatch
{
OrderId = model.UserId, // будет обновлено после SaveChanges
OrderBatchId = b
}).ToList(),
CreatedAt = DateTime.UtcNow,
};
_context.Orders.Add(order);
_context.SaveChanges();
}
public void Update(OrderCreateBindingModel model)
{
var order = _context.Orders
.Include(o => o.OrderRequests)
.Include(o => o.OrderBatchesLink)
.FirstOrDefault(o => o.Id == model.Id);
if (order == null) throw new Exception("Order not found");
order.Status = model.Status;
order.UserID = model.UserId;
// Обновление связей
var newRequests = model.RequestIds.Except(order.OrderRequests.Select(r => r.RequestId)).ToList();
var removedRequests = order.OrderRequests.Where(r => !model.RequestIds.Contains(r.RequestId)).ToList();
foreach (var id in newRequests)
order.OrderRequests.Add(new OrderRequest { OrderId = model.Id, RequestId = id });
foreach (var req in removedRequests)
_context.OrderRequests.Remove(req);
var newBatches = model.BatchIds.Except(order.OrderBatchesLink.Select(b => b.OrderBatchId)).ToList();
var removedBatches = order.OrderBatchesLink.Where(b => !model.BatchIds.Contains(b.OrderBatchId)).ToList();
foreach (var id in newBatches)
order.OrderBatchesLink.Add(new OrderOrderBatch { OrderId = model.Id, OrderBatchId = id });
foreach (var batch in removedBatches)
_context.OrderOrderBatches.Remove(batch);
_context.SaveChanges();
}
public void Delete(int id)
{
var order = _context.Orders.Find(id);
if (order != null)
{
_context.Orders.Remove(order);
_context.SaveChanges();
}
}
public List<AssemblyViewModel> GetAssembliesByOrders(List<int> orderIds) => _context.Requests
.Where(r => orderIds.Contains(r.Id) && r.AssemblyId.HasValue)
.Select(r => new AssemblyViewModel
{
Id = r.Assembly!.Id,
Name = r.Assembly.Name,
Description = r.Assembly.Description,
UserId = r.Assembly.UserId
}).ToList();
public List<OrderReportViewModel> GetOrdersWithDetailsByDateRange(
int userId,
DateTime start,
DateTime end)
{
return _context.Orders
.Where(o => o.UserID == userId && o.CreatedAt >= start.ToUniversalTime() && o.CreatedAt <= end.ToUniversalTime())
.SelectMany(o => o.OrderRequests.Select(or => new OrderReportViewModel
{
OrderId = o.Id,
OrderStatus = o.Status,
CreatedAt = o.CreatedAt,
RequestId = or.RequestId,
RequestDescription = or.Request.Description,
ProductId = o.OrderBatchesLink!= null
? _context.OrderOrderBatches
.Where(ca => ca.OrderId == o.Id)
.Select(ca => ca.OrderBatch)
.Select(c => c.Product)
.Select(pc => pc.Id)
.FirstOrDefault()
: 0,
ProductName = o.OrderBatchesLink != null
? _context.OrderOrderBatches
.Where(ca => ca.OrderId == o.Id)
.Select(ca => ca.OrderBatch)
.Select(c => c.Product)
.Select(pc => pc.Name)
.FirstOrDefault() ?? "N/A"
: "N/A",
UserId = o.UserID,
UserFIO = o.User.FIO
}))
.ToList();
}
}

View File

@@ -0,0 +1,156 @@
using ComputerStoreContracts.BindingModels;
using ComputerStoreContracts.StoragesContracts;
using ComputerStoreContracts.ViewModels;
using ComputerStoreDatabase.Models;
using Microsoft.EntityFrameworkCore;
namespace ComputerStoreDatabase.Implementations;
public class ProductStorageImplementation : IProductStorageContract
{
private readonly ComputerStoreDbContext _context;
public ProductStorageImplementation(ComputerStoreDbContext context)
{
_context = context;
}
public List<ProductViewModel> GetFullList() => _context.Products
.Select(p => new ProductViewModel
{
Id = p.Id,
Name = p.Name,
Price = p.Price,
UserId = p.UserID
}).ToList();
public ProductViewModel? GetElement(int id) => _context.Products
.Where(p => p.Id == id)
.Select(p => new ProductViewModel
{
Id = p.Id,
Name = p.Name,
Price = p.Price,
UserId = p.UserID
}).FirstOrDefault();
public void Create(ProductCreateBindingModel model)
{
var product = new Product
{
Name = model.Name,
Price = model.Price,
UserID = model.UserId
};
foreach (var componentId in model.ComponentIds)
{
var component = _context.Components.Find(componentId);
if (component != null)
{
product.ComponentsLink.Add(new ProductComponent
{
ComponentId = componentId
});
}
}
_context.Products.Add(product);
_context.SaveChanges();
}
public void Update(ProductCreateBindingModel model)
{
var product = _context.Products
.Include(p => p.ComponentsLink)
.FirstOrDefault(p => p.Id == model.Id);
if (product == null) throw new Exception("Товар не найден");
product.Name = model.Name;
product.Price = model.Price;
// Удаляем старые связи
var oldLinks = product.ComponentsLink.ToList();
foreach (var link in oldLinks)
{
_context.ProductComponents.Remove(link);
}
// Добавляем новые
foreach (var componentId in model.ComponentIds)
{
_context.ProductComponents.Add(new ProductComponent
{
ProductId = model.Id,
ComponentId = componentId
});
}
_context.Products.Update(product);
_context.SaveChanges();
}
public void Delete(int id)
{
var product = _context.Products.Find(id);
if (product != null)
{
_context.Products.Remove(product);
_context.SaveChanges();
}
}
public List<OrderBatchViewModel> GetUnlinkedBatches(int productId)
{
var linkedBatchIds = _context.OrderBatches
.Join(_context.ProductComponents,
b => b.Id,
pc => pc.ComponentId,
(b, pc) => new { Batch = b, ProductComponent = pc })
.Where(j => j.ProductComponent.ProductId == productId)
.Select(j => j.Batch.Id)
.ToList();
return _context.OrderBatches
.Where(b => !linkedBatchIds.Contains(b.Id))
.Select(b => new OrderBatchViewModel
{
Id = b.Id,
Name = b.Name,
UserId = b.UserID
}).ToList();
}
public List<OrderBatchViewModel> GetBatchesByComponents(List<int> componentIds)
{
var productIds = _context.ProductComponents
.Where(pc => componentIds.Contains(pc.ComponentId))
.Select(pc => pc.ProductId)
.Distinct()
.ToList();
return _context.OrderBatches
.Where(b => productIds.Contains(b.ProductId ?? 0))
.Select(b => new OrderBatchViewModel
{
Id = b.Id,
Name = b.Name,
UserId = b.UserID
}).ToList();
}
public List<ComponentViewModel> GetAllByComponent(int componentId)
{
return _context.ProductComponents
.Where(pc => pc.ProductId == componentId)
.Select(pc => pc.Component)
.Distinct()
.Select(p => new ComponentViewModel
{
Id = p.Id,
Name = p.Name,
Price = p.Price,
Manufacturer = p.Manufacturer,
UserId = p.UserID
}).ToList();
}
}

View File

@@ -0,0 +1,144 @@
using ComputerStoreContracts.BindingModels;
using ComputerStoreContracts.StoragesContracts;
using ComputerStoreContracts.ViewModels;
using ComputerStoreDatabase.Models;
namespace ComputerStoreDatabase.Implementations;
public class RequestStorageImplementation : IRequestStorageContract
{
private readonly ComputerStoreDbContext _context;
public RequestStorageImplementation(ComputerStoreDbContext context)
{
_context = context;
}
public List<RequestViewModel> GetFullList() => _context.Requests
.Select(r => new RequestViewModel
{
Id = r.Id,
Description = r.Description,
UserId = r.UserID,
AssemblyId = r.AssemblyId,
CreatedAt = r.CreatedAt,
}).ToList();
public RequestViewModel? GetElement(int id) => _context.Requests
.Where(r => r.Id == id)
.Select(r => new RequestViewModel
{
Id = r.Id,
Description = r.Description,
UserId = r.UserID,
AssemblyId = r.AssemblyId,
CreatedAt = r.CreatedAt,
}).FirstOrDefault();
public void Create(RequestCreateBindingModel model)
{
var request = new Request
{
Description = model.Description,
UserID = model.UserId,
AssemblyId = model.AssemblyId,
CreatedAt = DateTime.UtcNow,
};
_context.Requests.Add(request);
_context.SaveChanges();
}
public void Update(RequestCreateBindingModel model)
{
var request = _context.Requests.Find(model.Id);
if (request == null) throw new Exception("Заявка не найдена");
request.Description = model.Description;
request.UserID = model.UserId;
request.AssemblyId = model.AssemblyId;
_context.Requests.Update(request);
_context.SaveChanges();
}
public void Delete(int id)
{
var request = _context.Requests.Find(id);
if (request != null)
{
_context.Requests.Remove(request);
_context.SaveChanges();
}
}
public List<AssemblyViewModel> GetUnlinkedAssemblies(int requestId)
{
var linkedAssemblyIds = _context.Requests
.Where(r => r.Id == requestId)
.Select(r => r.AssemblyId)
.Where(a => a.HasValue)
.Select(a => a.Value)
.ToList();
return _context.Assemblies
.Where(a => !linkedAssemblyIds.Contains(a.Id))
.Select(a => new AssemblyViewModel
{
Id = a.Id,
Name = a.Name,
Description = a.Description,
UserId = a.UserId
}).ToList();
}
public void AttachAssemblyToRequest(int requestId, int assemblyId)
{
var assembly = _context.Assemblies.Find(assemblyId);
if (assembly == null) throw new Exception("Сборка не найдена");
assembly.RequestId = requestId;
_context.Assemblies.Update(assembly);
_context.SaveChanges();
}
public List<OrderViewModel> GetOrdersByRequest(int requestId)
{
return _context.OrderRequests
.Where(or => or.RequestId == requestId)
.Select(or => or.Order)
.Select(o => new OrderViewModel
{
Id = o.Id,
Status = o.Status,
UserId = o.UserID,
CreatedAt = o.CreatedAt,
}).ToList();
}
public void AddOrdersToRequest(int requestId, List<int> orderIds)
{
foreach (var orderId in orderIds)
{
if (!_context.OrderRequests.Any(or => or.RequestId == requestId && or.OrderId == orderId))
{
_context.OrderRequests.Add(new OrderRequest
{
RequestId = requestId,
OrderId = orderId
});
}
}
_context.SaveChanges();
}
public void RemoveOrdersFromRequest(int requestId, List<int> orderIds)
{
var links = _context.OrderRequests
.Where(or => or.RequestId == requestId && orderIds.Contains(or.OrderId));
_context.OrderRequests.RemoveRange(links);
_context.SaveChanges();
}
}

View File

@@ -0,0 +1,78 @@
using ComputerStoreContracts.BindingModels;
using ComputerStoreContracts.StoragesContracts;
using ComputerStoreContracts.ViewModels;
using ComputerStoreDatabase.Models;
namespace ComputerStoreDatabase.Implementations;
public class UserStorageImplementation : IUserStorageContract
{
private readonly ComputerStoreDbContext _context;
public UserStorageImplementation(ComputerStoreDbContext context)
{
_context = context;
}
public List<UserViewModel> GetFullList() => _context.Users
.Select(u => new UserViewModel
{
ID = u.ID,
Login = u.Login,
FIO = u.FIO,
Email = u.Email,
RoleType = u.UserType,
Password = u.Password
}).ToList();
public UserViewModel? GetElement(int id) => _context.Users
.Where(u => u.ID == id)
.Select(u => new UserViewModel
{
ID = u.ID,
Login = u.Login,
FIO = u.FIO,
Email = u.Email,
RoleType = u.UserType,
Password = u.Password
}).FirstOrDefault();
public void Create(UserBindingModel model)
{
var user = new User
{
Login = model.Login,
Password = model.Password!,
FIO = model.FIO,
Email = model.Email,
UserType = model.RoleType
};
_context.Users.Add(user);
_context.SaveChanges();
}
public void Update(UserBindingModel model)
{
var user = _context.Users.Find(model.ID);
if (user == null) throw new Exception("User not found");
user.Login = model.Login;
user.FIO = model.FIO;
user.Email = model.Email;
user.UserType = model.RoleType;
if (!string.IsNullOrEmpty(model.Password)) user.Password = model.Password!;
_context.Users.Update(user);
_context.SaveChanges();
}
public void Delete(int id)
{
var user = _context.Users.Find(id);
if (user != null)
{
_context.Users.Remove(user);
_context.SaveChanges();
}
}
}