PIAPS_CW/DatabaseImplement/Implements/ProductStorage.cs

171 lines
5.6 KiB
C#
Raw Normal View History

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();
return context.Products.Include(x => x.Sale)
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel;
}
public List<ProductViewModel> GetFilteredList(ProductSearchModel model)
{
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))
{
return new();
}
using var context = new Database();
if (model.Price.HasValue)
{
return context.Products.Include(x => x.Sale)
.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)
{
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)
{
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)
{
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();
}
if (model.Rate.HasValue)
{
return context.Products.Include(x => x.Sale)
.Where(x => x.Rate <= model.Rate)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
if (model.Amount.HasValue && model.IsBeingSold.HasValue)
{
return context.Products.Include(x => x.Sale)
.Where(x => x.IsBeingSold == model.IsBeingSold && x.Amount >= model.Amount)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
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))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<ProductViewModel> GetFullList()
{
using var context = new Database();
return context.Products.Include(x => x.Sale)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public ProductViewModel? Insert(ProductBindingModel model)
{
using var context = new Database();
var newProduct = Product.Create(context, model);
foreach (var sale in context.Sales)
{
if (sale.Category == newProduct.Category)
{
newProduct.Sale = sale;
newProduct.SaleId = sale.Id;
}
}
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
{
var product = context.Products.Include(x => x.Sale).FirstOrDefault(rec =>
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();
}
catch
{
transaction.Rollback();
throw;
}
}
}
}