fix implements & logic

This commit is contained in:
Илья Федотов 2024-05-26 17:47:24 +04:00
parent 7f9720200f
commit 1a56a5f1d4
17 changed files with 291 additions and 249 deletions

View File

@ -28,7 +28,7 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic
CheckModel(model);
if (_storage.Insert(model) == null)
{
_logger.LogWarning("Add operation failed");
_logger.LogWarning("Create operation failed");
return false;
}
return true;
@ -91,10 +91,6 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic
private void CheckModel(ClientBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;

View File

@ -13,12 +13,17 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic
private readonly ILogger _logger;
private readonly ICostItemStorage _storage;
public CostItemLogic(ILogger<CostItemLogic> logger, ICostItemStorage storage) {
_logger = logger;
_storage = storage;
}
public bool Create(CostItemBindingModel model)
{
CheckModel(model);
if (_storage.Insert(model) == null)
{
_logger.LogWarning("Add operation failed");
_logger.LogWarning("Create operation failed");
return false;
}
return true;
@ -79,23 +84,22 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic
private void CheckModel(CostItemBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (model.Price <= 0)
{
throw new ArgumentNullException("Цена продукта должна быть больше 0", nameof(model.Price));
throw new ArgumentNullException("Затраты должны быть больше 0", nameof(model.Price));
}
if (string.IsNullOrEmpty(model.Name)) {
throw new ArgumentNullException("Отсутствует название статьи затрат", nameof(model.Price));
}
_logger.LogInformation($"CostItem. ID:{model.ID}.EmployeeID:{model.EmployeeID}.Name:{model.Name}.Price:{model.Price}");
var element = _storage.GetElement(new CostItemSearchModel { ID = model.ID });
if (element != null && element.ID != model.EmployeeID)
{
throw new InvalidOperationException("Продукт с таким названием уже есть");
throw new InvalidOperationException("Такая статья затрат уде есть");
}
}
}

View File

@ -10,46 +10,42 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ElectronicsShopBusinessLogic.BusinessLogic
{
public class EmployeeLogic : IEmployeeLogic
{
namespace ElectronicsShopBusinessLogic.BusinessLogic {
public class EmployeeLogic : IEmployeeLogic {
private readonly ILogger _logger;
private readonly IEmployeeStorage _storage;
public bool Add(EmployeeBindingModel model)
{
public EmployeeLogic(ILogger<EmployeeLogic> loger, IEmployeeStorage storage) {
_logger = loger;
_storage = storage;
}
public bool Create(EmployeeBindingModel model) {
CheckModel(model);
if (_storage.Insert(model) == null)
{
_logger.LogWarning("Add operation failed");
if (_storage.Insert(model) == null) {
_logger.LogWarning("Create operation failed");
return false;
}
return true;
}
public bool Delete(EmployeeBindingModel model)
{
public bool Delete(EmployeeBindingModel model) {
CheckModel(model, false);
_logger.LogInformation($"Delete.ID:{model.ID}");
if (_storage.Delete(model) == null)
{
if (_storage.Delete(model) == null) {
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public EmployeeViewModel? ReadElemet(EmployeeSearchModel model)
{
if (model == null)
{
public EmployeeViewModel? ReadElemet(EmployeeSearchModel model) {
if (model == null) {
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation($"ReadElement.Login:{model.Login}.ID:{model.ID}");
var element = _storage.GetElement(model);
if (element == null)
{
if (element == null) {
_logger.LogWarning("ReadElement. element not fount");
return null;
}
@ -57,12 +53,10 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic
return element;
}
public List<EmployeeViewModel>? ReadList(EmployeeSearchModel model)
{
public List<EmployeeViewModel>? ReadList(EmployeeSearchModel model) {
_logger.LogInformation($"ReadList. ClientID:{model?.ID}");
var list = model == null ? _storage.GetFullList() : _storage.GetFilteredList(model); ;
if (list == null)
{
if (list == null) {
_logger.LogWarning("ReadList. return null list");
return null;
}
@ -70,48 +64,39 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic
return list;
}
public bool Update(EmployeeBindingModel model)
{
public bool Update(EmployeeBindingModel model) {
CheckModel(model);
if (_storage.Update(model) == null)
{
if (_storage.Update(model) == null) {
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(EmployeeBindingModel model, bool withParams = true)
{
if (model == null)
{
private void CheckModel(EmployeeBindingModel model, bool withParams = true) {
if (model == null) {
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
if (!withParams) {
return;
}
if (string.IsNullOrEmpty(model.Login))
{
throw new ArgumentNullException("Нет логина пользователя", nameof(model.Login));
if (string.IsNullOrEmpty(model.Login)) {
throw new ArgumentNullException("Нет логина сотрудника", nameof(model.Login));
}
if (string.IsNullOrEmpty(model.EmployeeFIO))
{
throw new ArgumentNullException("Нет имени пользователя", nameof(model.EmployeeFIO));
if (string.IsNullOrEmpty(model.EmployeeFIO)) {
throw new ArgumentNullException("Нет имени сотрудника", nameof(model.EmployeeFIO));
}
if (string.IsNullOrEmpty(model.Password))
{
throw new ArgumentNullException("Нет пароля пользователя", nameof(model.Password));
if (string.IsNullOrEmpty(model.Password)) {
throw new ArgumentNullException("Нет пароля сотрудника", nameof(model.Password));
}
_logger.LogInformation($"Client. ID:{model.ID}.ClientFIO:{model.EmployeeFIO}.Password:{model.Password}.Email:{model.Login}.");
var element = _storage.GetElement(new EmployeeSearchModel
{
var element = _storage.GetElement(new EmployeeSearchModel {
Login = model.Login
});
if (element != null && element.Login != model.Login)
{
throw new InvalidOperationException("Клиент с такой почтой уже есть");
if (element != null && element.Login != model.Login) {
throw new InvalidOperationException("Сотрудник с таким логином уже есть");
}
}
}
}
}

View File

@ -44,9 +44,6 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic
}
private void CheckModel(OrderBindingModel model, bool withParams = true) {
if (model == null) {
throw new ArgumentNullException(nameof(model));
}
if (!withParams) {
return;
}
@ -56,7 +53,10 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic
if (model.Sum <= 0) {
throw new ArgumentNullException("Цена зака должна быть больше 0", nameof(model.Sum));
}
_logger.LogInformation($"Order. ID:{model.ID}.Sum:{model.Sum}.DateCreate:{model.DateCreate}.");
if (model.ClientID < 0) {
throw new ArgumentNullException("Некорректный идентификатор у клиента", nameof(model.ClientID));
}
_logger.LogInformation($"Order. ID:{model.ID}.Sum:{model.Sum}.DateCreate:{model.DateCreate}.ClientID:{model.ClientID}");
}
public OrderViewModel? ReadElement(OrderSearchModel model)

View File

@ -8,11 +8,16 @@ using Microsoft.Extensions.Logging;
namespace ElectronicsShopBusinessLogic.BusinessLogic
{
public class PayLogic : IPaymentLogic
public class PaymentLogic : IPaymentLogic
{
private readonly ILogger _logger;
private readonly IPaymentStorage _storage;
public PaymentLogic(ILogger<PaymentLogic> logger, IPaymentStorage storage) {
_logger = logger;
_storage = storage;
}
public bool CreatePay(PaymentBindingModel model)
{
CheckModel(model);
@ -44,37 +49,29 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic
public List<PaymentViewModel>? ReadList(PaymentSearchModel? model)
{
_logger.LogInformation($"ReadList. ClientID:{model?.ID}");
/*var list = model == null ? _storage.GetFullList() : _storage.GetFillteredList(model); ;
var list = model == null ? _storage.GetFullList() : _storage.GetFillteredList(model); ;
if (list == null)
{
_logger.LogWarning("ReadList. return null list");
return null;
}
_logger.LogInformation($"ReadList.Count:{list.Count}");*/
_logger.LogInformation($"ReadList.Count:{list.Count}");
return null;
}
private void CheckModel(PaymentBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (model.SumPayment <= 0)
{
throw new ArgumentNullException("Цена продукта должна быть больше 0", nameof(model.SumPayment));
}
_logger.LogInformation($"Payment. ID:{model.ID}.ProductID:{model.ProductID}.Sum:{model.SumPayment}.OrderID:{model.OrderID}.PayOption{model.PayOption}");
var element = _storage.GetElement(new PaymentSearchModel { ID = model.ID });
if (element != null && element.PayOption != model.PayOption)
{
throw new InvalidOperationException("Продукт с таким названием уже есть");
throw new ArgumentNullException("Сумма оплаты должна быть больше 0", nameof(model.SumPayment));
}
_logger.LogInformation($"Payment. ID:{model.ID}.ProductID:{model.ProductID}.Sum:{model.SumPayment}.OrderID:{model.OrderID}" +
$".PayOption{model.PayOption}");
}
}
}

View File

@ -84,9 +84,6 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic
private void CheckModel(ProductBindingModel model, bool withParams = true)
{
if (model == null) {
throw new ArgumentNullException(nameof(model));
}
if (!withParams) {
return;
}

View File

@ -12,7 +12,7 @@ namespace ElectronicsShopContracts.BusinessLogicContracts {
List<EmployeeViewModel>? ReadList(EmployeeSearchModel model);
EmployeeViewModel? ReadElemet(EmployeeSearchModel model);
bool Add(EmployeeBindingModel model);
bool Create(EmployeeBindingModel model);
bool Update(EmployeeBindingModel model);
bool Delete(EmployeeBindingModel model);
}

View File

@ -10,5 +10,6 @@ namespace ElectronicsShopContracts.SearchModels
{
public int? ID { get; set; }
public string? Email { get; set; }
public string? Passwrod { get; set; }
}
}

View File

@ -12,7 +12,7 @@ namespace ElectronicsShopContracts.StorageContracts {
List<CostItemViewModel> GetFullList();
List<CostItemViewModel> GetFillteredList(CostItemSearchModel model);
CostItemViewModel GetElement(CostItemSearchModel model);
CostItemViewModel? GetElement(CostItemSearchModel model);
CostItemViewModel? Insert(CostItemBindingModel model);
CostItemViewModel? Update(CostItemBindingModel model);
CostItemViewModel? Delete(CostItemBindingModel model);

View File

@ -11,8 +11,10 @@ namespace ElectronicsShopContracts.StorageContracts
{
public interface IPaymentStorage
{
List<PaymentViewModel> GetFullList();
List<PaymentViewModel>? GetFullList();
List<PaymentViewModel>? GetFillteredList(PaymentSearchModel model);
PaymentViewModel? GetElement(PaymentSearchModel model);
PaymentViewModel? Insert(PaymentBindingModel model);
}
}

View File

@ -20,6 +20,6 @@ namespace ElectronicsShopDataBaseImplement
public virtual DbSet<OrderProduct> OrderProducts { set; get; }
public virtual DbSet<Employee> Employees { set; get; }
public virtual DbSet<CostItem> CostItems { set; get; }
public virtual DbSet<Payment> Payments { get; set; }
}
}

View File

@ -3,6 +3,8 @@ using ElectronicsShopContracts.SearchModels;
using ElectronicsShopContracts.StorageContracts;
using ElectronicsShopContracts.ViewModels;
using ElectronicsShopDataBaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Query.Internal;
using System;
using System.Collections.Generic;
using System.Linq;
@ -13,7 +15,29 @@ namespace ElectronicsShopDataBaseImplement.Implements
{
public class ClientStorage : IClientStorage
{
public ClientViewModel? Delete(ClientBindingModel model)
public ClientViewModel? Insert(ClientBindingModel model) {
var newComponent = Client.Create(model);
if (newComponent == null) {
return null;
}
using var context = new Database();
context.Clients.Add(newComponent);
context.SaveChanges();
return newComponent.GetViewModel;
}
public ClientViewModel? Update(ClientBindingModel model) {
using var context = new Database();
var component = context.Clients.FirstOrDefault(x => x.ID == model.ID);
if (component == null) {
return null;
}
component.Update(model);
context.SaveChanges();
return component.GetViewModel;
}
public ClientViewModel? Delete(ClientBindingModel model)
{
using var context = new Database();
var element = context.Clients.FirstOrDefault(rec => rec.ID == model.ID);
@ -28,14 +52,21 @@ namespace ElectronicsShopDataBaseImplement.Implements
public ClientViewModel? GetElement(ClientSearchModel model)
{
if (string.IsNullOrEmpty(model.Email) && !model.ID.HasValue)
{
return null;
}
using var context = new Database();
return context.Clients.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.Email) && x.Email == model.Email ||
(model.ID.HasValue && x.ID == model.ID)))?.GetViewModel;
if (model.ID.HasValue) {
return context.Clients
.Include(x => x.Orders)
.FirstOrDefault(x => model.ID.HasValue &&
x.ID == model.ID)
?.GetViewModel;
}
else if (!string.IsNullOrEmpty(model.Email) && !string.IsNullOrEmpty(model.Passwrod)) {
return context.Clients
.Include (x => x.Orders)
.FirstOrDefault(x => (x.Email == model.Email && x.Password == model.Passwrod))
?.GetViewModel;
}
return new();
}
public List<ClientViewModel> GetFilteredList(ClientSearchModel model)
@ -45,39 +76,20 @@ namespace ElectronicsShopDataBaseImplement.Implements
return new();
}
using var context = new Database();
return context.Clients.Where(x => x.Email.Contains(model.Email)).Select(x => x.GetViewModel).ToList();
return context.Clients
.Include(x => x.Orders)
.Where(x => x.Email
.Contains(model.Email))
.Select(x => x.GetViewModel).ToList();
}
public List<ClientViewModel> GetFullList()
{
using var context = new Database();
return context.Clients.Select(x => x.GetViewModel).ToList();
}
public ClientViewModel? Insert(ClientBindingModel model)
{
var newComponent = Client.Create(model);
if (newComponent == null)
{
return null;
}
using var context = new Database();
context.Clients.Add(newComponent);
context.SaveChanges();
return newComponent.GetViewModel;
}
public ClientViewModel? Update(ClientBindingModel model)
{
using var context = new Database();
var component = context.Clients.FirstOrDefault(x => x.ID == model.ID);
if (component == null)
{
return null;
}
component.Update(model);
context.SaveChanges();
return component.GetViewModel;
return context.Clients
.Include(x => x.Orders)
.Select(x => x.GetViewModel)
.ToList();
}
}
}

View File

@ -13,7 +13,29 @@ namespace ElectronicsShopDataBaseImplement.Implements
{
public class CostItemStorage : ICostItemStorage
{
public CostItemViewModel? Delete(CostItemBindingModel model)
public CostItemViewModel? Insert(CostItemBindingModel model) {
var newComponent = CostItem.Create(model);
if (newComponent == null) {
return null;
}
using var context = new Database();
context.CostItems.Add(newComponent);
context.SaveChanges();
return newComponent.GetViewModel;
}
public CostItemViewModel? Update(CostItemBindingModel model) {
using var context = new Database();
var component = context.CostItems.FirstOrDefault(x => x.ID == model.ID);
if (component == null) {
return null;
}
component.Update(model);
context.SaveChanges();
return component.GetViewModel;
}
public CostItemViewModel? Delete(CostItemBindingModel model)
{
using var context = new Database();
var element = context.CostItems.FirstOrDefault(rec => rec.ID == model.ID);
@ -26,16 +48,17 @@ namespace ElectronicsShopDataBaseImplement.Implements
return null;
}
public CostItemViewModel GetElement(CostItemSearchModel model)
public CostItemViewModel? GetElement(CostItemSearchModel model)
{
if (string.IsNullOrEmpty(model.Name) && !model.ID.HasValue)
{
return null;
}
using var context = new Database();
return context.CostItems.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.Name) && x.Name == model.Name ||
(model.ID.HasValue && x.ID == model.ID)))?.GetViewModel;
return context.CostItems
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name)
&& x.Name == model.Name) ||
(model.ID.HasValue && x.ID == model.ID))?.GetViewModel;
}
public List<CostItemViewModel> GetFillteredList(CostItemSearchModel model)
@ -45,7 +68,8 @@ namespace ElectronicsShopDataBaseImplement.Implements
return new();
}
using var context = new Database();
return context.CostItems.Where(x => x.Name.Contains(model.Name)).Select(x => x.GetViewModel).ToList();
return context.CostItems.Where(x => x.Name.Contains(model.Name))
.Select(x => x.GetViewModel).ToList();
}
public List<CostItemViewModel> GetFullList()
@ -53,31 +77,5 @@ namespace ElectronicsShopDataBaseImplement.Implements
using var context = new Database();
return context.CostItems.Select(x => x.GetViewModel).ToList();
}
public CostItemViewModel? Insert(CostItemBindingModel model)
{
var newComponent = CostItem.Create(model);
if (newComponent == null)
{
return null;
}
using var context = new Database();
context.CostItems.Add(newComponent);
context.SaveChanges();
return newComponent.GetViewModel;
}
public CostItemViewModel? Update(CostItemBindingModel model)
{
using var context = new Database();
var component = context.CostItems.FirstOrDefault(x => x.ID == model.ID);
if (component == null)
{
return null;
}
component.Update(model);
context.SaveChanges();
return component.GetViewModel;
}
}
}

View File

@ -9,7 +9,29 @@ namespace ElectronicsShopDataBaseImplement.Implements
{
public class EmployeeStorage : IEmployeeStorage
{
public EmployeeViewModel? Delete(EmployeeBindingModel model)
public EmployeeViewModel? Insert(EmployeeBindingModel model) {
var newComponent = Employee.Create(model);
if (newComponent == null) {
return null;
}
using var context = new Database();
context.Employees.Add(newComponent);
context.SaveChanges();
return newComponent.GetViewModel;
}
public EmployeeViewModel? Update(EmployeeBindingModel model) {
using var context = new Database();
var component = context.Employees.FirstOrDefault(x => x.ID == model.ID);
if (component == null) {
return null;
}
component.Update(model);
context.SaveChanges();
return component.GetViewModel;
}
public EmployeeViewModel? Delete(EmployeeBindingModel model)
{
using var context = new Database();
var element = context.Employees.FirstOrDefault(rec => rec.ID == model.ID);
@ -29,15 +51,12 @@ namespace ElectronicsShopDataBaseImplement.Implements
return null;
}
using var context = new Database();
return context.Employees.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.Login) && x.EmployeeFIO == model.EmployeFIO ||
(model.ID.HasValue && x.ID == model.ID)))?.GetViewModel;
return context.Employees
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Login) &&
x.EmployeeFIO == model.EmployeFIO ||
(model.ID.HasValue && x.ID == model.ID)))?.GetViewModel;
}
public object GetElement(CostItemSearchModel costItemSearchModel) {
throw new NotImplementedException();
}
public List<EmployeeViewModel> GetFilteredList(EmployeeSearchModel model)
{
if (string.IsNullOrEmpty(model.Login))
@ -45,7 +64,9 @@ namespace ElectronicsShopDataBaseImplement.Implements
return new();
}
using var context = new Database();
return context.Employees.Where(x => x.Login.Contains(model.Login)).Select(x => x.GetViewModel).ToList();
return context.Employees.Where(x => x.Login
.Contains(model.Login))
.Select(x => x.GetViewModel).ToList();
}
public List<EmployeeViewModel> GetFullList()
@ -53,31 +74,5 @@ namespace ElectronicsShopDataBaseImplement.Implements
using var context = new Database();
return context.Employees.Select(x => x.GetViewModel).ToList();
}
public EmployeeViewModel? Insert(EmployeeBindingModel model)
{
var newComponent = Employee.Create(model);
if (newComponent == null)
{
return null;
}
using var context = new Database();
context.Employees.Add(newComponent);
context.SaveChanges();
return newComponent.GetViewModel;
}
public EmployeeViewModel? Update(EmployeeBindingModel model)
{
using var context = new Database();
var component = context.Employees.FirstOrDefault(x => x.ID == model.ID);
if (component == null)
{
return null;
}
component.Update(model);
context.SaveChanges();
return component.GetViewModel;
}
}
}

View File

@ -10,7 +10,38 @@ namespace ElectronicsShopDataBaseImplement.Implements
{
public class OrderStorage : IOrderStorage
{
public OrderViewModel? Delete(OrderBindingModel model)
public OrderViewModel? Insert(OrderBindingModel model)
{
using var context = new Database();
var newComponent = Order.Create(context,model);
if (newComponent == null)
{
return null;
}
context.Orders.Add(newComponent);
context.SaveChanges();
return newComponent.GetViewModel;
}
public OrderViewModel? Update(OrderBindingModel model) {
using var context = new Database();
using var transcation = context.Database.BeginTransaction();
try {
var product = context.Orders.FirstOrDefault(rec => rec.ID == model.ID);
if (product == null) {
return null;
}
product.UpdateProducts(context, model);
transcation.Commit();
return product.GetViewModel;
}
catch {
transcation.Rollback();
throw;
}
}
public OrderViewModel? Delete(OrderBindingModel model)
{
using var context = new Database();
var element = context.Orders.FirstOrDefault(rec => rec.ID == model.ID);
@ -29,8 +60,11 @@ namespace ElectronicsShopDataBaseImplement.Implements
return null;
}
using var context = new Database();
return context.Orders.FirstOrDefault(x =>
(model.ID.HasValue && x.ID == model.ID))?.GetViewModel;
return context.Orders
.Include(x => x.Payments)
.Include(x => x.Products)
.ThenInclude(x => x._product)
.FirstOrDefault(x => (model.ID.HasValue && x.ID == model.ID))?.GetViewModel;
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
@ -51,33 +85,11 @@ namespace ElectronicsShopDataBaseImplement.Implements
public List<OrderViewModel> GetFullList()
{
using var context = new Database();
return context.Orders.Select(x => x.GetViewModel).ToList();
return context.Orders
.Include(x => x.Products)
.ThenInclude(x => x._product).ToList()
.Select(x => x.GetViewModel).ToList();
}
public OrderViewModel? Insert(OrderBindingModel model)
{
using var context = new Database();
var newComponent = Order.Create(context,model);
if (newComponent == null)
{
return null;
}
context.Orders.Add(newComponent);
context.SaveChanges();
return newComponent.GetViewModel;
}
public OrderViewModel? Update(OrderBindingModel model)
{
using var context = new Database();
var component = context.Orders.FirstOrDefault(x => x.ID == model.ID);
if (component == null)
{
return null;
}
component.Update(context ,model);
context.SaveChanges();
return component.GetViewModel;
}
}
}
}

View File

@ -0,0 +1,49 @@
using ElectronicsShopContracts.BindingModels;
using ElectronicsShopContracts.SearchModels;
using ElectronicsShopContracts.StorageContracts;
using ElectronicsShopContracts.ViewModels;
using ElectronicsShopDataBaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Security;
using System.Text;
using System.Threading.Tasks;
namespace ElectronicsShopDataBaseImplement.Implements {
public class PaymentStorage : IPaymentStorage {
public PaymentViewModel? Insert(PaymentBindingModel model) {
var newPayment = Payment.Create(model);
if (newPayment == null) {
return null;
}
using var context = new Database();
context.Payments.Add(newPayment);
context.SaveChanges();
return newPayment.GetViewModel;
}
public PaymentViewModel? GetElement(PaymentSearchModel model) {
if (model.ProductID.HasValue || model.OrderID.HasValue) {
return null;
}
using var context = new Database();
return context.Payments.FirstOrDefault(x => (model.ID.HasValue && x.ID == model.ID))?.GetViewModel;
}
public List<PaymentViewModel>? GetFillteredList(PaymentSearchModel model) {
if (model.ProductID.HasValue || model.OrderID.HasValue) {
return new();
}
using var context = new Database();
return context.Payments
.Where(x => x.ID == model.ID)
.Select(x => x.GetViewModel).ToList();
}
public List<PaymentViewModel>? GetFullList() {
using var context = new Database();
return context.Payments.Select(x => x.GetViewModel).ToList();
}
}
}

View File

@ -13,7 +13,29 @@ namespace ElectronicsShopDataBaseImplement.Implements
{
public class ProductStorage : IProductStorage
{
public ProductViewModel? Delete(ProductBindingModel model)
public ProductViewModel? Insert(ProductBindingModel model) {
var newComponent = Product.Create(model);
if (newComponent == null) {
return null;
}
using var context = new Database();
context.Products.Add(newComponent);
context.SaveChanges();
return newComponent.GetViewModel;
}
public ProductViewModel? Update(ProductBindingModel model) {
using var context = new Database();
var component = context.Products.FirstOrDefault(x => x.ID == model.ID);
if (component == null) {
return null;
}
component.Update(model);
context.SaveChanges();
return component.GetViewModel;
}
public ProductViewModel? Delete(ProductBindingModel model)
{
using var context = new Database();
var element = context.Products.FirstOrDefault(rec => rec.ID == model.ID);
@ -33,8 +55,8 @@ namespace ElectronicsShopDataBaseImplement.Implements
return null;
}
using var context = new Database();
return context.Products.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.ProductName) && (model.ID.HasValue && x.ID == model.ID)))?.GetViewModel;
return context.Products.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ProductName) &&
(model.ID.HasValue && x.ID == model.ID)))?.GetViewModel;
}
public List<ProductViewModel> GetFilteredList(ProductSearchModel model)
@ -47,38 +69,10 @@ namespace ElectronicsShopDataBaseImplement.Implements
return context.Products.Where(x => x.ProductName.Contains(model.ProductName)).Select(x => x.GetViewModel).ToList();
}
public List<ProductViewModel> GetFullList()
{
using var context = new Database();
return context.Products.Select(x => x.GetViewModel).ToList();
}
public ProductViewModel? Insert(ProductBindingModel model)
{
var newComponent = Product.Create(model);
if (newComponent == null)
{
return null;
}
using var context = new Database();
context.Products.Add(newComponent);
context.SaveChanges();
return newComponent.GetViewModel;
}
public ProductViewModel? Update(ProductBindingModel model)
{
using var context = new Database();
var component = context.Products.FirstOrDefault(x => x.ID == model.ID);
if (component == null)
{
return null;
}
component.Update(model);
context.SaveChanges();
return component.GetViewModel;
}
}
}