ура работает!

This commit is contained in:
Галина Федоренко 2024-05-15 11:26:56 +04:00
parent 4786396747
commit a10bd7a5ad
4 changed files with 17 additions and 3 deletions

View File

@ -4,6 +4,8 @@ using StorehouseContracts.SearchModels;
using StorehouseContracts.StoragesContracts;
using StorehouseContracts.ViewModels;
using StorehouseDatabaseImplement.Models;
using StorehouseDataModels.Models;
using System.ComponentModel.DataAnnotations;
namespace StorehouseDatabaseImplement.Implements
{
@ -41,12 +43,18 @@ namespace StorehouseDatabaseImplement.Implements
public SaleViewModel? Insert(SaleBindingModel model)
{
using var context = new StorehouseDatabase();
var product = context.Products.FirstOrDefault(p => p.Id == model.ProductId);
if (product == null || model.SaleNum > product.ProductNum)
{
return null;
}
var newSale = Sale.Create(model);
if (newSale == null)
{
return null;
}
using var context = new StorehouseDatabase();
product.ProductNum -= model.SaleNum;
context.Sales.Add(newSale);
context.SaveChanges();
return context.Sales.Include(x => x.Product).Include(x => x.Client).FirstOrDefault(x => x.Id == newSale.Id)?.GetViewModel;

View File

@ -41,12 +41,18 @@ namespace StorehouseDatabaseImplement.Implements
public SupplyViewModel? Insert(SupplyBindingModel model)
{
using var context = new StorehouseDatabase();
var product = context.Products.FirstOrDefault(p => p.Id == model.ProductId);
if (product == null)
{
return null;
}
var newSupply = Supply.Create(model);
if (newSupply == null)
{
return null;
}
using var context = new StorehouseDatabase();
product.ProductNum += model.SupplyNum;
context.Supplys.Add(newSupply);
context.SaveChanges();
return context.Supplys.Include(x => x.Product).Include(x => x.Provider).FirstOrDefault(x => x.Id == newSupply.Id)?.GetViewModel;

View File

@ -11,7 +11,7 @@ using StorehouseDatabaseImplement;
namespace StorehouseDatabaseImplement.Migrations
{
[DbContext(typeof(StorehouseDatabase))]
[Migration("20240515061146_FirstMigration")]
[Migration("20240515071016_FirstMigration")]
partial class FirstMigration
{
/// <inheritdoc />