Всё только начинается друзья мои

This commit is contained in:
devil_1nc 2024-06-24 01:07:53 +04:00
parent b2b2922acf
commit cddde78b59
6 changed files with 145 additions and 35 deletions

View File

@ -45,7 +45,7 @@ namespace BusinessLogic.BusinessLogic
}
public IEnumerable<SellViewModel> GetElements(SellSearchModel? model)
{
var sell_list = _sellStorage.GetList(model);
var sell_list = model == null ? _sellStorage.GetFullList(model) : _sellStorage.GetFilteredList(model);
if (sell_list is null || sell_list.Count() == 0)
{
_logger.LogWarning("ReadList return null list");

View File

@ -1,5 +1,6 @@
using Contracts.BindingModels;
using Contracts.SearchModels;
using Contracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
@ -10,14 +11,15 @@ namespace Contracts.StorageContracts
{
public interface IPurchaseStorage
{
PurchaseBindingModel? Insert(PurchaseBindingModel model);
PurchaseViewModel? Insert(PurchaseBindingModel model);
IEnumerable<PurchaseBindingModel> GetList(PurchaseSearchModel? model);
List<PurchaseViewModel> GetFullList(PurchaseSearchModel? model);
List<PurchaseViewModel> GetFilteredList(PurchaseSearchModel? model);
PurchaseBindingModel? GetElement(PurchaseSearchModel model);
PurchaseViewModel? GetElement(PurchaseSearchModel model);
PurchaseBindingModel? Update(PurchaseBindingModel model);
PurchaseViewModel? Update(PurchaseBindingModel model);
PurchaseBindingModel? Delete(PurchaseSearchModel model);
PurchaseViewModel? Delete(PurchaseSearchModel model);
}
}

View File

@ -1,6 +1,9 @@
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;
@ -11,29 +14,87 @@ namespace DatabaseImplement.Implements
{
public class PurchaseStorage : IPurchaseStorage
{
public PurchaseBindingModel? Delete(PurchaseSearchModel model)
public PurchaseViewModel? Delete(PurchaseSearchModel model)
{
throw new NotImplementedException();
}
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;
}
public PurchaseBindingModel? GetElement(PurchaseSearchModel model)
public PurchaseViewModel? GetElement(PurchaseSearchModel model)
{
throw new NotImplementedException();
}
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;
}
public IEnumerable<PurchaseBindingModel> GetList(PurchaseSearchModel? model)
public List<PurchaseViewModel> GetFullList(PurchaseSearchModel? model)
{
throw new NotImplementedException();
}
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();
return context.Purchases
.Include(x => x.Products)
.ThenInclude(x => x.Product)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public PurchaseBindingModel? Insert(PurchaseBindingModel model)
public PurchaseViewModel? Insert(PurchaseBindingModel model)
{
throw new NotImplementedException();
}
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;
}
public PurchaseBindingModel? Update(PurchaseBindingModel model)
public PurchaseViewModel? Update(PurchaseBindingModel model)
{
throw new NotImplementedException();
}
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;
}
}
}
}

View File

@ -17,13 +17,24 @@ namespace DatabaseImplement.Implements
{
public SellViewModel? Delete(SellSearchModel model)
{
throw new NotImplementedException();
using var context = new Database();
var element = context.Sells.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Sells.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public SellViewModel? GetElement(SellSearchModel model)
{
throw new NotImplementedException();
}
using var context = new Database();
return context.Sells
.Include(x => x.SupplyDoc)
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public IEnumerable<SellViewModel> GetFilteredList(SellSearchModel? model)
{
@ -36,7 +47,7 @@ namespace DatabaseImplement.Implements
return context.Sells
.Include(x => x.SupplyDoc)
.ToList()
.Select((Sell sell, int index) => sell.GetViewModel())
.Select((Sell sell, int index) => sell.GetViewModel)
.ToList();
}
@ -50,7 +61,7 @@ namespace DatabaseImplement.Implements
}
context.Sells.Add(sell);
context.SaveChanges();
return sell.GetViewModel();
return sell.GetViewModel;
}
public SellViewModel? Update(SellBindingModel model)
@ -67,7 +78,7 @@ namespace DatabaseImplement.Implements
sell.Update(model);
context.SaveChanges();
transaction.Commit();
return sell.GetViewModel();
return sell.GetViewModel;
}
catch
{

View File

@ -1,4 +1,5 @@
using Contracts.BindingModels;
using Contracts.SearchModels;
using Contracts.ViewModels;
using DataModels.Enums;
using DataModels.Models;
@ -11,6 +12,7 @@ using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
namespace DatabaseImplement.Models
{
@ -41,7 +43,7 @@ namespace DatabaseImplement.Models
}
[ForeignKey("PurchaseId")]
public virtual List<PurchaseProducts> Products { get; set; } = new();
public static Purchase Create(Database context, SellBindingModel model)
public static Purchase Create(Database context, PurchaseBindingModel model)
{
return new Purchase()
{
@ -51,7 +53,7 @@ namespace DatabaseImplement.Models
};
}
public PurchaseBindingModel GetBindingModel() => new()
public PurchaseBindingModel GetBindingModel => new()
{
Id = Id,
DatePurchase = DatePurchase,
@ -60,7 +62,7 @@ namespace DatabaseImplement.Models
Status = Status
};
public PurchaseViewModel GetViewModel() => new()
public PurchaseViewModel GetViewModel => new()
{
Id = Id,
DatePurchase = DatePurchase,
@ -87,14 +89,48 @@ namespace DatabaseImplement.Models
Status = model.Status
};
public void Update(PurchaseBindingModel model, Purchase purchase)
public void Update(PurchaseBindingModel model)
{
if (model is null)
{
throw new ArgumentNullException("Update purchase: binding model is null");
}
DatePurchase = purchase.DatePurchase;
DatePurchase = model.DatePurchase;
UserId = model.UserId;
PurchaseProducts = model.PurchaseProducts;
Status = model.Status;
}
}
public void UpdateProducts(Database context, PurchaseBindingModel model)
{
var purchaseProducts = context.PurchaseProducts.Where(rec =>
rec.Id == model.Id).ToList();
if (purchaseProducts != null && purchaseProducts.Count > 0)
{ // удалили те, которых нет в модели
context.PurchaseProducts.RemoveRange(purchaseProducts.Where(rec
=> !model.PurchaseProducts.ContainsKey(rec.ProductId)));
context.SaveChanges();
// обновили количество у существующих записей
foreach (var updateProduct in purchaseProducts)
{
updateProduct.Count = model.PurchaseProducts[updateProduct.ProductId].Item2;
model.PurchaseProducts.Remove(updateProduct.ProductId);
}
context.SaveChanges();
}
var purchase = context.Purchases.First(x => x.Id == Id);
foreach (var pc in model.PurchaseProducts)
{
context.PurchaseProducts.Add(new PurchaseProducts
{
Purchase = purchase,
Product = context.Products.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();
}
_purchaseProducts = null;
}
}
}

View File

@ -27,13 +27,13 @@ namespace DatabaseImplement.Models
};
}
public SellBindingModel GetBindingModel() => new()
public SellBindingModel GetBindingModel => new()
{
Id = Id,
DateSell = DateSell,
SupplyDocId = SupplyDocId,
};
public SellViewModel GetViewModel() => new()
public SellViewModel GetViewModel => new()
{
Id = Id,
DateSell = DateSell,