2024-06-23 00:33:26 +04:00
|
|
|
|
using Contracts.BindingModels;
|
2024-06-23 20:39:28 +04:00
|
|
|
|
using Contracts.Converters;
|
2024-06-23 00:33:26 +04:00
|
|
|
|
using Contracts.SearchModels;
|
|
|
|
|
using Contracts.StorageContracts;
|
2024-06-23 20:39:28 +04:00
|
|
|
|
using Contracts.ViewModels;
|
|
|
|
|
using DatabaseImplement.Models;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2024-06-23 00:33:26 +04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace DatabaseImplement.Implements
|
|
|
|
|
{
|
|
|
|
|
public class SellStorage : ISellStorage
|
|
|
|
|
{
|
2024-06-23 20:39:28 +04:00
|
|
|
|
public SellViewModel? Delete(SellSearchModel model)
|
2024-06-23 00:33:26 +04:00
|
|
|
|
{
|
2024-06-24 01:07:53 +04:00
|
|
|
|
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;
|
2024-06-23 16:10:45 +04:00
|
|
|
|
}
|
2024-06-23 00:33:26 +04:00
|
|
|
|
|
2024-06-23 20:39:28 +04:00
|
|
|
|
public SellViewModel? GetElement(SellSearchModel model)
|
2024-06-23 00:33:26 +04:00
|
|
|
|
{
|
2024-06-24 01:07:53 +04:00
|
|
|
|
using var context = new Database();
|
|
|
|
|
return context.Sells
|
|
|
|
|
.Include(x => x.SupplyDoc)
|
|
|
|
|
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
|
|
|
|
|
}
|
2024-06-23 00:33:26 +04:00
|
|
|
|
|
2024-06-23 20:39:28 +04:00
|
|
|
|
public IEnumerable<SellViewModel> GetFilteredList(SellSearchModel? model)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<SellViewModel> GetFullList(SellSearchModel? model)
|
2024-06-23 00:33:26 +04:00
|
|
|
|
{
|
2024-06-23 20:39:28 +04:00
|
|
|
|
using var context = new Database();
|
|
|
|
|
return context.Sells
|
|
|
|
|
.Include(x => x.SupplyDoc)
|
|
|
|
|
.ToList()
|
2024-06-24 01:07:53 +04:00
|
|
|
|
.Select((Sell sell, int index) => sell.GetViewModel)
|
2024-06-23 20:39:28 +04:00
|
|
|
|
.ToList();
|
|
|
|
|
}
|
2024-06-23 00:33:26 +04:00
|
|
|
|
|
2024-06-23 20:39:28 +04:00
|
|
|
|
public SellViewModel? Insert(SellBindingModel model)
|
2024-06-23 00:33:26 +04:00
|
|
|
|
{
|
2024-06-23 20:39:28 +04:00
|
|
|
|
using var context = new Database();
|
|
|
|
|
var sell = Sell.Create(context, model);
|
|
|
|
|
if (sell == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
context.Sells.Add(sell);
|
|
|
|
|
context.SaveChanges();
|
2024-06-24 01:07:53 +04:00
|
|
|
|
return sell.GetViewModel;
|
2024-06-23 00:33:26 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-23 20:39:28 +04:00
|
|
|
|
public SellViewModel? Update(SellBindingModel model)
|
2024-06-23 00:33:26 +04:00
|
|
|
|
{
|
2024-06-23 20:39:28 +04:00
|
|
|
|
using var context = new Database();
|
|
|
|
|
using var transaction = context.Database.BeginTransaction();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var sell = context.Sells.FirstOrDefault(rec => rec.Id == model.Id);
|
|
|
|
|
if (sell == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
sell.Update(model);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
transaction.Commit();
|
2024-06-24 01:07:53 +04:00
|
|
|
|
return sell.GetViewModel;
|
2024-06-23 20:39:28 +04:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
transaction.Rollback();
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-23 00:33:26 +04:00
|
|
|
|
}
|
|
|
|
|
}
|