139 lines
4.8 KiB
C#
139 lines
4.8 KiB
C#
using AbstractLawFirmContracts.BindingModels;
|
|
using AbstractLawFirmContracts.SearchModels;
|
|
using AbstractLawFirmContracts.StoragesContracts;
|
|
using AbstractLawFirmContracts.ViewModels;
|
|
using AbstractLawFirmDataModels.Models;
|
|
using AbstractLawFirmDatabaseImplement.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace AbstractLawFirmDatabaseImplement.Implements
|
|
{
|
|
public class ShopStorage : IShopStorage
|
|
{
|
|
public ShopViewModel? GetElement(ShopSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue)
|
|
{
|
|
return new();
|
|
}
|
|
using var context = new AbstractLawFirmDatabase();
|
|
return context.Shops.Include(x => x.Documents).ThenInclude(x => x.Document).FirstOrDefault(x =>
|
|
(!string.IsNullOrEmpty(model.ShopName) && x.ShopName == model.ShopName) ||
|
|
(model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
|
|
}
|
|
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.ShopName))
|
|
{
|
|
return new();
|
|
}
|
|
using var context = new AbstractLawFirmDatabase();
|
|
return context.Shops.Include(x => x.Documents).ThenInclude(x => x.Document).Where(x => x.ShopName.Contains(model.ShopName)).ToList().Select(x => x.GetViewModel).ToList();
|
|
}
|
|
public List<ShopViewModel> GetFullList()
|
|
{
|
|
using var context = new AbstractLawFirmDatabase();
|
|
return context.Shops.Include(x => x.Documents).ThenInclude(x => x.Document).ToList().Select(x => x.GetViewModel).ToList();
|
|
}
|
|
public ShopViewModel? Insert(ShopBindingModel model)
|
|
{
|
|
using var context = new AbstractLawFirmDatabase();
|
|
using var transaction = context.Database.BeginTransaction();
|
|
try
|
|
{
|
|
var newShop = Shop.Create(context, model);
|
|
if (newShop == null)
|
|
{
|
|
return null;
|
|
}
|
|
if (context.Shops.Any(x => x.ShopName == newShop.ShopName))
|
|
{
|
|
throw new Exception("Название магазина уже занято");
|
|
}
|
|
|
|
context.Shops.Add(newShop);
|
|
context.SaveChanges();
|
|
transaction.Commit();
|
|
return newShop.GetViewModel;
|
|
}
|
|
catch
|
|
{
|
|
transaction.Rollback();
|
|
throw;
|
|
}
|
|
}
|
|
public ShopViewModel? Update(ShopBindingModel model)
|
|
{
|
|
using var context = new AbstractLawFirmDatabase();
|
|
using var transaction = context.Database.BeginTransaction();
|
|
try
|
|
{
|
|
var shop = context.Shops.Include(x => x.Documents).FirstOrDefault(x => x.Id == model.Id);
|
|
if (shop == null)
|
|
{
|
|
return null;
|
|
}
|
|
shop.Update(model);
|
|
context.SaveChanges();
|
|
if (model.ShopDocuments.Count > 0)
|
|
{
|
|
shop.UpdateDocuments(context, model);
|
|
}
|
|
transaction.Commit();
|
|
return shop.GetViewModel;
|
|
}
|
|
catch
|
|
{
|
|
transaction.Rollback();
|
|
throw;
|
|
}
|
|
}
|
|
public ShopViewModel? Delete(ShopBindingModel model)
|
|
{
|
|
using var context = new AbstractLawFirmDatabase();
|
|
var shop = context.Shops.Include(x => x.Documents).FirstOrDefault(x => x.Id == model.Id);
|
|
if (shop != null)
|
|
{
|
|
context.Shops.Remove(shop);
|
|
context.SaveChanges();
|
|
return shop.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
public bool SellDocument(IDocumentModel model, int count)
|
|
{
|
|
using var context = new AbstractLawFirmDatabase();
|
|
using var transaction = context.Database.BeginTransaction();
|
|
|
|
foreach (var shopDocuments in context.ShopDocuments.Where(x => x.DocumentId == model.Id))
|
|
{
|
|
var min = Math.Min(count, shopDocuments.Count);
|
|
shopDocuments.Count -= min;
|
|
count -= min;
|
|
if (count <= 0)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (count == 0)
|
|
{
|
|
context.SaveChanges();
|
|
transaction.Commit();
|
|
}
|
|
else
|
|
transaction.Rollback();
|
|
|
|
if (count > 0)
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
}
|
|
}
|