2024-06-23 00:33:26 +04:00
|
|
|
|
using Contracts.BindingModels;
|
|
|
|
|
using Contracts.SearchModels;
|
|
|
|
|
using Contracts.StorageContracts;
|
2024-06-24 01:07:53 +04:00
|
|
|
|
using Contracts.ViewModels;
|
|
|
|
|
using DatabaseImplement.Models;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2024-06-23 00:33:26 +04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace DatabaseImplement.Implements
|
|
|
|
|
{
|
|
|
|
|
public class PurchaseStorage : IPurchaseStorage
|
|
|
|
|
{
|
2024-06-24 01:07:53 +04:00
|
|
|
|
public PurchaseViewModel? Delete(PurchaseSearchModel model)
|
2024-06-23 00:33:26 +04:00
|
|
|
|
{
|
2024-06-24 01:07:53 +04:00
|
|
|
|
using var context = new Database();
|
|
|
|
|
var element = context.Purchases.FirstOrDefault(rec => rec.Id == model.Id);
|
|
|
|
|
if (element != null)
|
|
|
|
|
{
|
|
|
|
|
context.Purchases.Remove(element);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return element.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2024-06-23 00:33:26 +04:00
|
|
|
|
|
2024-06-24 01:07:53 +04:00
|
|
|
|
public PurchaseViewModel? GetElement(PurchaseSearchModel model)
|
2024-06-23 00:33:26 +04:00
|
|
|
|
{
|
2024-06-24 01:07:53 +04:00
|
|
|
|
if (!model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
using var context = new Database();
|
|
|
|
|
return context.Purchases
|
|
|
|
|
.Include(x => x.Products)
|
|
|
|
|
.ThenInclude(x => x.Product)
|
|
|
|
|
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
|
|
|
|
|
}
|
2024-06-23 00:33:26 +04:00
|
|
|
|
|
2024-06-24 01:07:53 +04:00
|
|
|
|
public List<PurchaseViewModel> GetFullList(PurchaseSearchModel? model)
|
2024-06-23 00:33:26 +04:00
|
|
|
|
{
|
2024-06-24 01:07:53 +04:00
|
|
|
|
using var context = new Database();
|
|
|
|
|
return context.Purchases
|
|
|
|
|
.Include(x => x.Products)
|
|
|
|
|
.ThenInclude(x => x.Product)
|
|
|
|
|
.ToList()
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
public List<PurchaseViewModel> GetFilteredList(PurchaseSearchModel? model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new Database();
|
2024-06-26 08:04:07 +04:00
|
|
|
|
if (!model.CostFrom.HasValue && !model.CostTo.HasValue && !model.DateTo.HasValue && !model.DateFrom.HasValue && !model.Status.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return new();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (model.CostFrom.HasValue && model.CostTo.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return context.Purchases
|
|
|
|
|
.Where(x => x.Cost <= model.CostTo && x.Cost >= model.CostFrom)
|
|
|
|
|
.ToList()
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
if (model.CostFrom.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return context.Purchases
|
|
|
|
|
.Where(x => x.Cost >= model.CostFrom)
|
|
|
|
|
.ToList()
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
if (model.CostTo.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return context.Purchases
|
|
|
|
|
.Where(x => x.Cost <= model.CostTo)
|
|
|
|
|
.ToList()
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
if (model.DateFrom.HasValue && model.DateTo.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return context.Purchases
|
|
|
|
|
.Where(x => x.DatePurchase <= model.DateTo && x.DatePurchase >= model.DateFrom)
|
|
|
|
|
.ToList()
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
if (model.DateFrom.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return context.Purchases
|
|
|
|
|
.Where(x => x.DatePurchase >= model.DateFrom)
|
|
|
|
|
.ToList()
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
if (model.DateTo.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return context.Purchases
|
|
|
|
|
.Where(x => x.DatePurchase <= model.DateTo)
|
|
|
|
|
.ToList()
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
2024-06-24 01:07:53 +04:00
|
|
|
|
return context.Purchases
|
|
|
|
|
.Include(x => x.Products)
|
|
|
|
|
.ThenInclude(x => x.Product)
|
|
|
|
|
.ToList()
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
2024-06-23 00:33:26 +04:00
|
|
|
|
|
2024-06-24 01:07:53 +04:00
|
|
|
|
public PurchaseViewModel? Insert(PurchaseBindingModel model)
|
2024-06-23 00:33:26 +04:00
|
|
|
|
{
|
2024-06-24 01:07:53 +04:00
|
|
|
|
using var context = new Database();
|
|
|
|
|
var purchase = Purchase.Create(context, model);
|
|
|
|
|
if (purchase == null)
|
|
|
|
|
return null;
|
|
|
|
|
context.Purchases.Add(purchase);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return purchase.GetViewModel;
|
|
|
|
|
}
|
2024-06-23 00:33:26 +04:00
|
|
|
|
|
2024-06-24 01:07:53 +04:00
|
|
|
|
public PurchaseViewModel? Update(PurchaseBindingModel model)
|
2024-06-23 00:33:26 +04:00
|
|
|
|
{
|
2024-06-24 01:07:53 +04:00
|
|
|
|
using var context = new Database();
|
|
|
|
|
using var transaction = context.Database.BeginTransaction();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var purchase = context.Purchases.FirstOrDefault(rec =>
|
|
|
|
|
rec.Id == model.Id);
|
|
|
|
|
if (purchase == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
purchase.Update(model);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
purchase.UpdateProducts(context, model);
|
|
|
|
|
transaction.Commit();
|
|
|
|
|
return purchase.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
transaction.Rollback();
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-26 08:04:07 +04:00
|
|
|
|
public PurchaseViewModel? AddProducts(PurchaseSearchModel purchaseModel, ProductSearchModel productModel, int count)
|
|
|
|
|
{
|
|
|
|
|
using var context = new Database();
|
|
|
|
|
using var transaction = context.Database.BeginTransaction();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var purchase = context.Purchases.FirstOrDefault(rec =>
|
|
|
|
|
rec.Id == purchaseModel.Id);
|
|
|
|
|
|
|
|
|
|
var product = context.Products.FirstOrDefault(rec =>
|
|
|
|
|
rec.Id == productModel.Id);
|
|
|
|
|
|
|
|
|
|
if (purchase == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
if (product == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
if (count <= 0)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
context.PurchaseProducts.Add(new PurchaseProducts
|
|
|
|
|
{
|
|
|
|
|
Purchase = purchase,
|
|
|
|
|
Product = product,
|
|
|
|
|
Count = count
|
|
|
|
|
});
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return purchase.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
catch { transaction.Rollback(); throw; }
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Dictionary<Guid, int> GetProducts(PurchaseSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
Dictionary<Guid, int> productsDict = new();
|
|
|
|
|
using var context = new Database();
|
|
|
|
|
|
|
|
|
|
var purchase = context.Purchases.FirstOrDefault(rec =>
|
|
|
|
|
rec.Id == model.Id);
|
|
|
|
|
|
|
|
|
|
if (purchase == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var purchaseProducts = context.PurchaseProducts
|
|
|
|
|
.Where(x => x.PurchaseId == model.Id)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
foreach (var purchaseProduct in purchaseProducts)
|
|
|
|
|
{
|
|
|
|
|
productsDict.Add(purchaseProduct.ProductId, purchaseProduct.Count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return productsDict;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2024-06-23 00:33:26 +04:00
|
|
|
|
}
|