часть круда готова
This commit is contained in:
parent
43aef44d98
commit
5f76bf2750
@ -11,7 +11,7 @@ namespace Contracts.BindingModels
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ namespace Contracts.BindingModels
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public string Name { get; set; } = string.Empty;
|
||||
public Guid SupplierId { get; set; }
|
||||
public DateTime Date { get; set; }
|
||||
public double Price { get; set; }
|
||||
public SupplyStatus Status { get; set; }
|
||||
|
@ -10,7 +10,7 @@ namespace Contracts.SearchModels
|
||||
{
|
||||
public Guid? Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public List<ProductSearchModel>? AvailibleProducts { get; set; }
|
||||
//public List<ProductSearchModel>? AvailibleProducts { get; set; }
|
||||
public int? Deals { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using DataModels.Enums;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
@ -9,7 +10,7 @@ namespace Contracts.SearchModels
|
||||
public class SupplySearchModel
|
||||
{
|
||||
public Guid? Id { get; set; }
|
||||
public List<ProductSearchModel>? Products { get; set; }
|
||||
public SupplyStatus? Status { get; set; }
|
||||
public DateTime? DateStart { get; set; }
|
||||
public DateTime? DateEnd { get; set; }
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using DataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
@ -10,6 +11,6 @@ namespace Contracts.ViewModels
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public string Name { get; set; } = string.Empty;
|
||||
public Dictionary<string, int> AvailibleProducts = new();
|
||||
public Dictionary<Guid, (IProduct, int)> AvailibleProducts = new();
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
using DataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
@ -12,6 +13,7 @@ namespace Contracts.ViewModels
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public string Name { get; set; } = string.Empty;
|
||||
public Guid SupplierId { get; set; }
|
||||
public string SupplierName { get; set; } = string.Empty;
|
||||
public double Price { get; set; }
|
||||
public Dictionary<Guid, (IProduct, int)> Products { get; set; } = new();
|
||||
|
@ -9,7 +9,7 @@ namespace DataModels.Models
|
||||
public interface ISupplier : IId
|
||||
{
|
||||
string Name { get; }
|
||||
Dictionary<int, (IProduct, int)> AvailibleProducts { get; }
|
||||
Dictionary<Guid, (IProduct, int)> AvailibleProducts { get; }
|
||||
int Deals { get; }
|
||||
}
|
||||
}
|
||||
|
@ -11,6 +11,7 @@ namespace DataModels.Models
|
||||
{
|
||||
string Name { get; }
|
||||
double Price { get; }
|
||||
Guid SupplierId { get; }
|
||||
DateTime Date { get; }
|
||||
SupplyStatus Status { get; }
|
||||
Dictionary<Guid, (IProduct, int)> SupplyProducts { get; }
|
||||
|
@ -26,6 +26,9 @@ namespace DatabaseImplement
|
||||
public virtual DbSet<Product> Products { get; set; } = null!;
|
||||
public virtual DbSet<Supply> Supplies { 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!;
|
||||
}
|
||||
}
|
121
DatabaseImplement/Implements/SupplierStorage.cs
Normal file
121
DatabaseImplement/Implements/SupplierStorage.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
147
DatabaseImplement/Implements/SupplyStorage.cs
Normal file
147
DatabaseImplement/Implements/SupplyStorage.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
109
DatabaseImplement/Models/Supplier.cs
Normal file
109
DatabaseImplement/Models/Supplier.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
22
DatabaseImplement/Models/SupplierProduct.cs
Normal file
22
DatabaseImplement/Models/SupplierProduct.cs
Normal 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();
|
||||
}
|
||||
}
|
@ -16,13 +16,14 @@ namespace DatabaseImplement.Models
|
||||
{
|
||||
public class Supply : ISupply
|
||||
{
|
||||
[Required]
|
||||
public Guid Id { get; set; }
|
||||
[Required]
|
||||
public string Name { get; set; } = string.Empty;
|
||||
[Required]
|
||||
public double Price { get; set; }
|
||||
[Required]
|
||||
public Guid SupplierId { get; set; }
|
||||
[Required]
|
||||
public DateTime Date { get; set; }
|
||||
[Required]
|
||||
public SupplyStatus Status { get; set; } = SupplyStatus.Pending;
|
||||
@ -43,7 +44,7 @@ namespace DatabaseImplement.Models
|
||||
}
|
||||
[ForeignKey("SupplyId")]
|
||||
public virtual List<SupplyProduct> Products { get; set; } = new();
|
||||
|
||||
public virtual Supplier Supplier { get; set; }
|
||||
public static Supply Create(Database context, SupplyBindingModel model)
|
||||
{
|
||||
return new Supply()
|
||||
@ -51,6 +52,8 @@ namespace DatabaseImplement.Models
|
||||
Id = model.Id,
|
||||
Name = model.Name,
|
||||
Price = model.Price,
|
||||
Date = model.Date,
|
||||
SupplierId = model.SupplierId,
|
||||
Products = model.SupplyProducts.Select(x => new
|
||||
SupplyProduct
|
||||
{
|
||||
@ -64,17 +67,24 @@ namespace DatabaseImplement.Models
|
||||
Name = model.Name;
|
||||
Price = model.Price;
|
||||
}
|
||||
public SupplyViewModel GetViewModel => new()
|
||||
public SupplyViewModel GetViewModel
|
||||
{
|
||||
Id = Id,
|
||||
Name = Name,
|
||||
Price = Price,
|
||||
Products = SupplyProducts,
|
||||
//supplierName сделать
|
||||
Date = Date,
|
||||
Status = Status
|
||||
};
|
||||
public void UpdateComponents(Database context, SupplyBindingModel model)
|
||||
get
|
||||
{
|
||||
var context = new Database();
|
||||
return new()
|
||||
{
|
||||
Id = Id,
|
||||
Name = Name,
|
||||
Price = Price,
|
||||
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 =>
|
||||
rec.Id == model.Id).ToList();
|
||||
|
Loading…
Reference in New Issue
Block a user