часть круда готова

This commit is contained in:
the 2024-06-22 15:10:46 +04:00
parent 43aef44d98
commit 5f76bf2750
14 changed files with 438 additions and 20 deletions

View File

@ -11,7 +11,7 @@ namespace Contracts.BindingModels
{ {
public Guid Id { get; set; } public Guid Id { get; set; }
public string Name { get; set; } = string.Empty; public string Name { get; set; } = string.Empty;
public Dictionary<int, (IProduct, int)> AvailibleProducts { get; set; } = new(); public Dictionary<Guid, (IProduct, int)> AvailibleProducts { get; set; } = new();
public int Deals { get; set; } = 0; public int Deals { get; set; } = 0;
} }
} }

View File

@ -12,6 +12,7 @@ namespace Contracts.BindingModels
{ {
public Guid Id { get; set; } public Guid Id { get; set; }
public string Name { get; set; } = string.Empty; public string Name { get; set; } = string.Empty;
public Guid SupplierId { get; set; }
public DateTime Date { get; set; } public DateTime Date { get; set; }
public double Price { get; set; } public double Price { get; set; }
public SupplyStatus Status { get; set; } public SupplyStatus Status { get; set; }

View File

@ -10,7 +10,7 @@ namespace Contracts.SearchModels
{ {
public Guid? Id { get; set; } public Guid? Id { get; set; }
public string? Name { get; set; } public string? Name { get; set; }
public List<ProductSearchModel>? AvailibleProducts { get; set; } //public List<ProductSearchModel>? AvailibleProducts { get; set; }
public int? Deals { get; set; } public int? Deals { get; set; }
} }
} }

View File

@ -1,4 +1,5 @@
using System; using DataModels.Enums;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
@ -9,7 +10,7 @@ namespace Contracts.SearchModels
public class SupplySearchModel public class SupplySearchModel
{ {
public Guid? Id { get; set; } public Guid? Id { get; set; }
public List<ProductSearchModel>? Products { get; set; } public SupplyStatus? Status { get; set; }
public DateTime? DateStart { get; set; } public DateTime? DateStart { get; set; }
public DateTime? DateEnd { get; set; } public DateTime? DateEnd { get; set; }
} }

View File

@ -1,4 +1,5 @@
using System; using DataModels.Models;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
@ -10,6 +11,6 @@ namespace Contracts.ViewModels
{ {
public Guid Id { get; set; } public Guid Id { get; set; }
public string Name { get; set; } = string.Empty; public string Name { get; set; } = string.Empty;
public Dictionary<string, int> AvailibleProducts = new(); public Dictionary<Guid, (IProduct, int)> AvailibleProducts = new();
} }
} }

View File

@ -2,6 +2,7 @@
using DataModels.Models; using DataModels.Models;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -12,6 +13,7 @@ namespace Contracts.ViewModels
{ {
public Guid Id { get; set; } public Guid Id { get; set; }
public string Name { get; set; } = string.Empty; public string Name { get; set; } = string.Empty;
public Guid SupplierId { get; set; }
public string SupplierName { get; set; } = string.Empty; public string SupplierName { get; set; } = string.Empty;
public double Price { get; set; } public double Price { get; set; }
public Dictionary<Guid, (IProduct, int)> Products { get; set; } = new(); public Dictionary<Guid, (IProduct, int)> Products { get; set; } = new();

View File

@ -9,7 +9,7 @@ namespace DataModels.Models
public interface ISupplier : IId public interface ISupplier : IId
{ {
string Name { get; } string Name { get; }
Dictionary<int, (IProduct, int)> AvailibleProducts { get; } Dictionary<Guid, (IProduct, int)> AvailibleProducts { get; }
int Deals { get; } int Deals { get; }
} }
} }

View File

@ -11,6 +11,7 @@ namespace DataModels.Models
{ {
string Name { get; } string Name { get; }
double Price { get; } double Price { get; }
Guid SupplierId { get; }
DateTime Date { get; } DateTime Date { get; }
SupplyStatus Status { get; } SupplyStatus Status { get; }
Dictionary<Guid, (IProduct, int)> SupplyProducts { get; } Dictionary<Guid, (IProduct, int)> SupplyProducts { get; }

View File

@ -26,6 +26,9 @@ namespace DatabaseImplement
public virtual DbSet<Product> Products { get; set; } = null!; public virtual DbSet<Product> Products { get; set; } = null!;
public virtual DbSet<Supply> Supplies { get; set; } = null!; public virtual DbSet<Supply> Supplies { get; set; } = null!;
public virtual DbSet<SupplyProduct> SupplyProducts { get; set; } = null!; public virtual DbSet<SupplyProduct> SupplyProducts { get; set; } = null!;
public virtual DbSet<MediaFile> MediaFiles { get; set; } = null!; public virtual DbSet<Supplier> Suppliers { get; set; } = null!;
public virtual DbSet<SupplierProduct> SupplierProducts { get; set; } = null!;
public virtual DbSet<MediaFile> MediaFiles { get; set; } = null!;
} }
} }

View File

@ -0,0 +1,121 @@
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 SupplierStorage : ISupplierStorage
{
public SupplierViewModel? Delete(SupplierBindingModel model)
{
using var context = new Database();
var element = context.Suppliers
.Include(x => x.Products)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Suppliers.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public SupplierViewModel? GetElement(SupplierSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new Database();
return context.Suppliers
.Include(x => x.Name)
.Include(x => x.Products)
.ThenInclude(x => x.Product)
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public List<SupplierViewModel> GetFilteredList(SupplierSearchModel model)
{
if (string.IsNullOrEmpty(model.Name) && !model.Deals.HasValue)
{
return new();
}
using var context = new Database();
if (!string.IsNullOrEmpty(model.Name))
{
return context.Suppliers
.Include(x => x.Products)
.ThenInclude(x => x.Product)
.Where(x => model.Name == x.Name)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
return context.Suppliers
.Include(x => x.Products)
.ThenInclude(x => x.Product)
.Where(x => model.Deals <= x.Deals)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<SupplierViewModel> GetFullList()
{
using var context = new Database();
return context.Suppliers
.Include(x => x.Products)
.ThenInclude(x => x.Product)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public SupplierViewModel? Insert(SupplierBindingModel model)
{
using var context = new Database();
var newProduct = Supplier.Create(context, model);
if (newProduct == null)
{
return null;
}
context.Suppliers.Add(newProduct);
context.SaveChanges();
return newProduct.GetViewModel;
}
public SupplierViewModel? Update(SupplierBindingModel model)
{
using var context = new Database();
using var transaction = context.Database.BeginTransaction();
try
{
var product = context.Suppliers.FirstOrDefault(rec =>
rec.Id == model.Id);
if (product == null)
{
return null;
}
product.Update(context, model);
context.SaveChanges();
product.UpdateProducts(context, model);
transaction.Commit();
return product.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
}
}

View File

@ -0,0 +1,147 @@
using Contracts.BindingModels;
using Contracts.SearchModels;
using Contracts.StorageContracts;
using Contracts.ViewModels;
using DatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DatabaseImplement.Implements
{
public class SupplyStorage : ISupplyStorage
{
public SupplyViewModel? Delete(SupplyBindingModel model)
{
using var context = new Database();
var element = context.Supplies
.Include(x => x.Products)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Supplies.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public SupplyViewModel? GetElement(SupplySearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new Database();
return context.Supplies
.Include(x => x.Supplier)
.Include(x => x.Products)
.ThenInclude(x => x.Product)
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public List<SupplyViewModel> GetFilteredList(SupplySearchModel model)
{
if (!model.DateStart.HasValue && !model.DateEnd.HasValue && model.Status == null)
{
return new();
}
using var context = new Database();
if (model.DateStart.HasValue && model.DateEnd.HasValue)
{
return context.Supplies
.Include(x => x.Supplier)
.Include(x => x.Products)
.ThenInclude(x => x.Product)
.Where(x => x.Id == model.Id || model.DateStart <= x.Date && x.Date <= model.DateEnd)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
if (model.DateEnd.HasValue)
{
return context.Supplies
.Include(x => x.Supplier)
.Include(x => x.Products)
.ThenInclude(x => x.Product)
.Where(x => x.Id == model.Id || x.Date <= model.DateEnd)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
if (model.DateStart.HasValue)
{
return context.Supplies
.Include(x => x.Supplier)
.Include(x => x.Products)
.ThenInclude(x => x.Product)
.Where(x => x.Id == model.Id || model.DateStart <= x.Date)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
return context.Supplies
.Include(x => x.Supplier)
.Include(x => x.Products)
.ThenInclude(x => x.Product)
.Where(x => model.Status.Equals(x.Status))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<SupplyViewModel> GetFullList()
{
using var context = new Database();
return context.Supplies
.Include(x => x.Supplier)
.Include(x => x.Products)
.ThenInclude(x => x.Product)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public SupplyViewModel? Insert(SupplyBindingModel model)
{
using var context = new Database();
var newProduct = Supply.Create(context, model);
if (newProduct == null)
{
return null;
}
context.Supplies.Add(newProduct);
context.SaveChanges();
return newProduct.GetViewModel;
}
public SupplyViewModel? Update(SupplyBindingModel model)
{
using var context = new Database();
using var transaction = context.Database.BeginTransaction();
try
{
var product = context.Supplies.FirstOrDefault(rec =>
rec.Id == model.Id);
if (product == null)
{
return null;
}
product.Update(model);
context.SaveChanges();
product.UpdateProducts(context, model);
transaction.Commit();
return product.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
}
}

View File

@ -0,0 +1,109 @@
using Contracts.BindingModels;
using Contracts.ViewModels;
using DataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType;
namespace DatabaseImplement.Models
{
public class Supplier : ISupplier
{
public Guid Id { get; set; }
[Required]
public string Name { get; set; } = string.Empty;
[Required]
public int Deals { get; set; }
private Dictionary<Guid, (IProduct, int)>? _availibleProducts = null;
[NotMapped]
public Dictionary<Guid, (IProduct, int)> AvailibleProducts
{
get
{
if (_availibleProducts == null)
{
_availibleProducts = Products
.ToDictionary(recPC => recPC.Id, recPC =>
(recPC.Product as IProduct, recPC.Count));
}
return _availibleProducts;
}
}
[ForeignKey("SupplierId")]
public virtual List<SupplierProduct> Products { get; set; } = new();
public static Supplier Create(Database context, SupplierBindingModel model)
{
return new Supplier()
{
Id = model.Id,
Name = model.Name,
Products = model.AvailibleProducts.Select(x => new
SupplierProduct
{
Product = context.Products.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(Database context, SupplierBindingModel model)
{
Name = model.Name;
Products = model.AvailibleProducts.Select(x => new
SupplierProduct
{
Product = context.Products.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList();
}
public SupplierViewModel GetViewModel
{
get
{
var context = new Database();
return new()
{
Id = Id,
Name = Name,
AvailibleProducts = AvailibleProducts
};
}
}
public void UpdateProducts(Database context, SupplierBindingModel model)
{
var supplierProducts = context.SupplierProducts.Where(rec =>
rec.Id == model.Id).ToList();
if (supplierProducts != null && supplierProducts.Count > 0)
{ // удалили те, которых нет в модели
context.SupplierProducts.RemoveRange(supplierProducts.Where(rec
=> !model.AvailibleProducts.ContainsKey(rec.ProductId)));
context.SaveChanges();
// обновили количество у существующих записей
foreach (var updateProduct in supplierProducts)
{
updateProduct.Count = model.AvailibleProducts[updateProduct.ProductId].Item2;
model.AvailibleProducts.Remove(updateProduct.ProductId);
}
context.SaveChanges();
}
var supplier = context.Suppliers.First(x => x.Id == Id);
foreach (var pc in model.AvailibleProducts)
{
context.SupplierProducts.Add(new SupplierProduct
{
Supplier = supplier,
Product = context.Products.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();
}
_availibleProducts = null;
}
}
}

View File

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DatabaseImplement.Models
{
public class SupplierProduct
{
public Guid Id { get; set; }
[Required]
public Guid SupplierId { get; set; }
[Required]
public Guid ProductId { get; set; }
[Required]
public int Count { get; set; }
public virtual Supplier Supplier { get; set; } = new();
public virtual Product Product { get; set; } = new();
}
}

View File

@ -16,13 +16,14 @@ namespace DatabaseImplement.Models
{ {
public class Supply : ISupply public class Supply : ISupply
{ {
[Required]
public Guid Id { get; set; } public Guid Id { get; set; }
[Required] [Required]
public string Name { get; set; } = string.Empty; public string Name { get; set; } = string.Empty;
[Required] [Required]
public double Price { get; set; } public double Price { get; set; }
[Required] [Required]
public Guid SupplierId { get; set; }
[Required]
public DateTime Date { get; set; } public DateTime Date { get; set; }
[Required] [Required]
public SupplyStatus Status { get; set; } = SupplyStatus.Pending; public SupplyStatus Status { get; set; } = SupplyStatus.Pending;
@ -43,7 +44,7 @@ namespace DatabaseImplement.Models
} }
[ForeignKey("SupplyId")] [ForeignKey("SupplyId")]
public virtual List<SupplyProduct> Products { get; set; } = new(); public virtual List<SupplyProduct> Products { get; set; } = new();
public virtual Supplier Supplier { get; set; }
public static Supply Create(Database context, SupplyBindingModel model) public static Supply Create(Database context, SupplyBindingModel model)
{ {
return new Supply() return new Supply()
@ -51,6 +52,8 @@ namespace DatabaseImplement.Models
Id = model.Id, Id = model.Id,
Name = model.Name, Name = model.Name,
Price = model.Price, Price = model.Price,
Date = model.Date,
SupplierId = model.SupplierId,
Products = model.SupplyProducts.Select(x => new Products = model.SupplyProducts.Select(x => new
SupplyProduct SupplyProduct
{ {
@ -64,17 +67,24 @@ namespace DatabaseImplement.Models
Name = model.Name; Name = model.Name;
Price = model.Price; Price = model.Price;
} }
public SupplyViewModel GetViewModel => new() public SupplyViewModel GetViewModel
{ {
Id = Id, get
Name = Name, {
Price = Price, var context = new Database();
Products = SupplyProducts, return new()
//supplierName сделать {
Date = Date, Id = Id,
Status = Status Name = Name,
}; Price = Price,
public void UpdateComponents(Database context, SupplyBindingModel model) Products = SupplyProducts,
Date = Date,
Status = Status,
SupplierName = context.Suppliers.FirstOrDefault(x => x.Id == Id)?.Name ?? string.Empty,
};
}
}
public void UpdateProducts(Database context, SupplyBindingModel model)
{ {
var supplyProducts = context.SupplyProducts.Where(rec => var supplyProducts = context.SupplyProducts.Where(rec =>
rec.Id == model.Id).ToList(); rec.Id == model.Id).ToList();