2024-06-22 18:13:07 +04:00
|
|
|
|
using Contracts.BindingModels;
|
|
|
|
|
using Contracts.SearchModels;
|
|
|
|
|
using Contracts.StorageContracts;
|
|
|
|
|
using Contracts.ViewModels;
|
|
|
|
|
using DatabaseImplement.Models;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace DatabaseImplement.Implements
|
|
|
|
|
{
|
|
|
|
|
public class ProductStorage : IProductStorage
|
|
|
|
|
{
|
|
|
|
|
public ProductViewModel? Delete(ProductBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new Database();
|
|
|
|
|
var element = context.Products
|
|
|
|
|
.FirstOrDefault(rec => rec.Id == model.Id);
|
|
|
|
|
if (element != null)
|
|
|
|
|
{
|
|
|
|
|
context.Products.Remove(element);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return element.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ProductViewModel? GetElement(ProductSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (!model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
using var context = new Database();
|
2024-07-26 02:46:29 +04:00
|
|
|
|
return context.Products.Include(x => x.Sale)
|
|
|
|
|
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel;
|
2024-06-22 18:13:07 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<ProductViewModel> GetFilteredList(ProductSearchModel model)
|
|
|
|
|
{
|
2024-07-26 02:46:29 +04:00
|
|
|
|
if (!model.IsBeingSold.HasValue && !model.PriceFrom.HasValue && !model.PriceTo.HasValue && !model.Price.HasValue && !model.Rate.HasValue
|
|
|
|
|
&& !model.Amount.HasValue && string.IsNullOrEmpty(model.Name) && string.IsNullOrEmpty(model.Category))
|
2024-06-22 18:13:07 +04:00
|
|
|
|
{
|
|
|
|
|
return new();
|
|
|
|
|
}
|
|
|
|
|
using var context = new Database();
|
2024-07-26 02:46:29 +04:00
|
|
|
|
|
2024-06-22 18:13:07 +04:00
|
|
|
|
if (model.Price.HasValue)
|
|
|
|
|
{
|
2024-07-26 02:46:29 +04:00
|
|
|
|
return context.Products.Include(x => x.Sale)
|
2024-06-22 18:13:07 +04:00
|
|
|
|
.Where(x => x.Price <= model.Price)
|
|
|
|
|
.ToList()
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
2024-06-25 20:26:34 +04:00
|
|
|
|
if (model.PriceFrom.HasValue && model.PriceTo.HasValue)
|
|
|
|
|
{
|
2024-07-26 02:46:29 +04:00
|
|
|
|
return context.Products.Include(x => x.Sale)
|
2024-06-25 20:26:34 +04:00
|
|
|
|
.Where(x => x.Price <= model.PriceTo && x.Price >= model.PriceFrom)
|
|
|
|
|
.ToList()
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
if (model.PriceFrom.HasValue)
|
|
|
|
|
{
|
2024-07-26 02:46:29 +04:00
|
|
|
|
return context.Products.Include(x => x.Sale)
|
2024-06-25 20:26:34 +04:00
|
|
|
|
.Where(x => x.Price >= model.PriceFrom)
|
|
|
|
|
.ToList()
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
if (model.PriceTo.HasValue)
|
|
|
|
|
{
|
2024-07-26 02:46:29 +04:00
|
|
|
|
return context.Products.Include(x => x.Sale)
|
2024-06-25 20:26:34 +04:00
|
|
|
|
.Where(x => x.Price <= model.PriceTo)
|
|
|
|
|
.ToList()
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
2024-06-22 18:13:07 +04:00
|
|
|
|
if (model.Rate.HasValue)
|
|
|
|
|
{
|
2024-07-26 02:46:29 +04:00
|
|
|
|
return context.Products.Include(x => x.Sale)
|
2024-06-22 18:13:07 +04:00
|
|
|
|
.Where(x => x.Rate <= model.Rate)
|
|
|
|
|
.ToList()
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
if (model.Amount.HasValue && model.IsBeingSold.HasValue)
|
|
|
|
|
{
|
2024-07-26 02:46:29 +04:00
|
|
|
|
return context.Products.Include(x => x.Sale)
|
2024-06-22 18:13:07 +04:00
|
|
|
|
.Where(x => x.IsBeingSold == model.IsBeingSold && x.Amount >= model.Amount)
|
|
|
|
|
.ToList()
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
2024-07-26 02:46:29 +04:00
|
|
|
|
if (!string.IsNullOrEmpty(model.Category))
|
|
|
|
|
{
|
|
|
|
|
return context.Products.Include(x => x.Sale)
|
|
|
|
|
.Where(x => x.Category == model.Category)
|
|
|
|
|
.ToList()
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
return context.Products.Include(x => x.Sale)
|
2024-06-25 20:26:34 +04:00
|
|
|
|
.Where(x => x.Name.Contains(model.Name))
|
2024-06-22 18:13:07 +04:00
|
|
|
|
.ToList()
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<ProductViewModel> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
using var context = new Database();
|
2024-07-26 02:46:29 +04:00
|
|
|
|
return context.Products.Include(x => x.Sale)
|
2024-06-22 18:13:07 +04:00
|
|
|
|
.ToList()
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ProductViewModel? Insert(ProductBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new Database();
|
|
|
|
|
var newProduct = Product.Create(context, model);
|
2024-07-26 02:46:29 +04:00
|
|
|
|
|
|
|
|
|
foreach (var sale in context.Sales)
|
|
|
|
|
{
|
|
|
|
|
if (sale.Category == newProduct.Category)
|
|
|
|
|
{
|
|
|
|
|
newProduct.Sale = sale;
|
|
|
|
|
newProduct.SaleId = sale.Id;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-22 18:13:07 +04:00
|
|
|
|
if (newProduct == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
context.Products.Add(newProduct);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return newProduct.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ProductViewModel? Update(ProductBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new Database();
|
|
|
|
|
using var transaction = context.Database.BeginTransaction();
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-07-26 02:46:29 +04:00
|
|
|
|
var product = context.Products.Include(x => x.Sale).FirstOrDefault(rec =>
|
2024-06-22 18:13:07 +04:00
|
|
|
|
rec.Id == model.Id);
|
|
|
|
|
if (product == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
product.Update(model);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
transaction.Commit();
|
2024-06-25 16:21:15 +04:00
|
|
|
|
return new();
|
2024-06-22 18:13:07 +04:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
transaction.Rollback();
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|