PurchaseStorage

This commit is contained in:
bekodeg 2024-05-01 11:55:41 +04:00
parent 326b2b2b87
commit 96c186a590
5 changed files with 122 additions and 1 deletions

View File

@ -8,6 +8,7 @@ namespace ComputerHardwareStoreContracts.BindingModels
public int VendorId { get; set; } public int VendorId { get; set; }
public double Cost { get; set; } public double Cost { get; set; }
public DateTime DateCreate { get; set; } public DateTime DateCreate { get; set; }
public DateTime? DateImplement { get; set; }
public Dictionary<int, (IBuildModel, int)> PurchaseBuilds { get; set; } = new(); public Dictionary<int, (IBuildModel, int)> PurchaseBuilds { get; set; } = new();
public Dictionary<int, (IProductModel, int)> PurchaseProducts { get; set; } = new(); public Dictionary<int, (IProductModel, int)> PurchaseProducts { get; set; } = new();
} }

View File

@ -10,11 +10,12 @@ namespace ComputerHardwareStoreContracts.ViewModels
public double Cost { get; set; } public double Cost { get; set; }
[DisplayName("Дата создания")] [DisplayName("Дата создания")]
public DateTime DateCreate { get; set; } public DateTime DateCreate { get; set; }
public DateTime? DateImplement { get; set; }
public int VendorId { get; set; } public int VendorId { get; set; }
[DisplayName("Сумма")] [DisplayName("Сумма")]
public double Sum { get; set; } public double Sum { get; set; }
public Dictionary<int, (IBuildModel, int)> PurchaseBuilds { get; set; } = new(); public Dictionary<int, (IBuildModel, int)> PurchaseBuilds { get; set; } = new();
public Dictionary<int, (IProductModel, int)> PurchaseProducts { get; set; } = new(); public Dictionary<int, (IProductModel, int)> PurchaseProducts { get; set; } = new();
} }
} }

View File

@ -5,6 +5,7 @@
int VendorId { get; } int VendorId { get; }
double Cost { get; } double Cost { get; }
DateTime DateCreate { get; } DateTime DateCreate { get; }
DateTime? DateImplement { get; }
public Dictionary<int, (IBuildModel, int)> PurchaseBuilds { get; } public Dictionary<int, (IBuildModel, int)> PurchaseBuilds { get; }
public Dictionary<int, (IProductModel, int)> PurchaseProducts { get; } public Dictionary<int, (IProductModel, int)> PurchaseProducts { get; }
} }

View File

@ -0,0 +1,117 @@
using ComputerHardwareStoreContracts.BindingModels;
using ComputerHardwareStoreContracts.SearchModels;
using ComputerHardwareStoreContracts.StorageContracts;
using ComputerHardwareStoreContracts.ViewModels;
using ComputerHardwareStoreDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace ComputerHardwareStoreDatabaseImplement.Implements
{
public class PurchaseStorage : IPurchaseStorage
{
public List<PurchaseViewModel> GetFullList()
{
using var context = new ComputerHardwareStoreDBContext();
return context.Purchases
.Include(p => p.Builds)
.ThenInclude(p => p.Build)
.Include(p => p.Products)
.ThenInclude(p => p.Product)
.Select(p => p.GetViewModel)
.ToList();
}
public List<PurchaseViewModel> GetFilteredList(PurchaseSearchModel model)
{
using var context = new ComputerHardwareStoreDBContext();
return context.Purchases
.Include(p => p.Builds)
.ThenInclude(p => p.Build)
.Include(p => p.Products)
.ThenInclude(p => p.Product)
.Where(p =>
(model.Id.HasValue && p.Id == model.Id) ||
((model.DateFrom.HasValue && model.DateTo.HasValue) &&
((model.DateFrom <= p.DateCreate && p.DateCreate <= model.DateTo) ||
(p.DateImplement.HasValue && p.DateCreate < model.DateFrom && model.DateFrom < p.DateImplement)))
)
.Select(p => p.GetViewModel)
.ToList();
}
public PurchaseViewModel? GetElement(PurchaseSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new ComputerHardwareStoreDBContext();
return context.Purchases
.Include(p => p.Builds)
.ThenInclude(p => p.Build)
.Include(p => p.Products)
.ThenInclude(p => p.Product)
.FirstOrDefault(p => p.Id == model.Id)?
.GetViewModel;
}
public PurchaseViewModel? Insert(PurchaseBindingModel model)
{
using var context = new ComputerHardwareStoreDBContext();
var newPurchase = Purchase.Create(context, model);
if (newPurchase == null)
{
return null;
}
context.Purchases.Add(newPurchase);
context.SaveChanges();
return newPurchase.GetViewModel;
}
public PurchaseViewModel? Update(PurchaseBindingModel model)
{
using var context = new ComputerHardwareStoreDBContext();
using var transaction = context.Database.BeginTransaction();
try
{
var purchase = context.Purchases
.Include(p => p.Builds)
.ThenInclude(p => p.Build)
.Include(p => p.Products)
.ThenInclude(p => p.Product)
.FirstOrDefault(p => p.Id == model.Id);
if (purchase == null)
{
return null;
}
purchase.Update(model);
context.SaveChanges();
transaction.Commit();
return purchase.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public PurchaseViewModel? Delete(PurchaseBindingModel model)
{
using var context = new ComputerHardwareStoreDBContext();
var element = context.Purchases
.Include(p => p.Builds)
.ThenInclude(p => p.Build)
.Include(p => p.Products)
.ThenInclude(p => p.Product)
.FirstOrDefault(o => o.Id == model.Id);
if (element != null)
{
context.Purchases.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -17,6 +17,7 @@ namespace ComputerHardwareStoreDatabaseImplement.Models
public double Cost { get; set; } public double Cost { get; set; }
[Required] [Required]
public DateTime DateCreate { get; set; } = DateTime.Now; public DateTime DateCreate { get; set; } = DateTime.Now;
public DateTime? DateImplement { get; set; } = null;
private Dictionary<int, (IBuildModel, int)>? _purchaseBuilds = null; private Dictionary<int, (IBuildModel, int)>? _purchaseBuilds = null;
[NotMapped] [NotMapped]
public Dictionary<int, (IBuildModel, int)> PurchaseBuilds public Dictionary<int, (IBuildModel, int)> PurchaseBuilds