2024-03-25 21:16:58 +04:00
|
|
|
|
using CarRepairShopContracts.BindingModels;
|
|
|
|
|
using CarRepairShopContracts.SearchModels;
|
|
|
|
|
using CarRepairShopContracts.StoragesContracts;
|
|
|
|
|
using CarRepairShopContracts.ViewModels;
|
|
|
|
|
using CarRepairShopDatabaseImplement.Models;
|
|
|
|
|
using CarRepairShopDataModels.Models;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace CarRepairShopDatabaseImplement.Implements
|
|
|
|
|
{
|
|
|
|
|
public class ShopStorage : IShopStorage
|
|
|
|
|
{
|
|
|
|
|
public ShopViewModel? Delete(ShopBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new RepairsShopDatabase();
|
|
|
|
|
var element = context.Shops.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
if (element != null)
|
|
|
|
|
{
|
|
|
|
|
context.Shops.Remove(element);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return element.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ShopViewModel? GetElement(ShopSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
using var context = new RepairsShopDatabase();
|
|
|
|
|
return context.Shops
|
|
|
|
|
.Include(x => x.Repairs)
|
|
|
|
|
.ThenInclude(x => x.Repair)
|
|
|
|
|
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)
|
|
|
|
|
?.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(model.Name))
|
|
|
|
|
{
|
|
|
|
|
return new();
|
|
|
|
|
}
|
|
|
|
|
using var context = new RepairsShopDatabase();
|
|
|
|
|
return context.Shops
|
|
|
|
|
.Include(x => x.Repairs)
|
|
|
|
|
.ThenInclude(x => x.Repair)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.Where(x => x.ShopName.Contains(model.Name ?? string.Empty))
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<ShopViewModel> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
using var context = new RepairsShopDatabase();
|
|
|
|
|
return context.Shops
|
|
|
|
|
.Include(x => x.Repairs)
|
|
|
|
|
.ThenInclude(x => x.Repair)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ShopViewModel? Insert(ShopBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new RepairsShopDatabase();
|
|
|
|
|
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();
|
|
|
|
|
return newShop.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ShopViewModel? Update(ShopBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new RepairsShopDatabase();
|
|
|
|
|
var shop = context.Shops.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
if (shop == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (context.Shops.Any(x => (x.ShopName == model.ShopName && x.Id != model.Id)))
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Не должно быть два магазина с одним названием");
|
|
|
|
|
}
|
|
|
|
|
shop.Update(model);
|
|
|
|
|
shop.UpdateRepairs(context, model);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return shop.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public bool SellRepairs(IRepairModel model, int count)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
return false;
|
|
|
|
|
using var context = new RepairsShopDatabase();
|
|
|
|
|
using var transaction = context.Database.BeginTransaction();
|
|
|
|
|
List<ShopRepair> lst = new List<ShopRepair>();
|
|
|
|
|
foreach (var el in context.ShopRepairs.Where(x => x.RepairId == model.Id))
|
|
|
|
|
{
|
|
|
|
|
int dif = count;
|
2024-03-27 16:35:29 +04:00
|
|
|
|
if (el.Count < dif) dif = el.Count;
|
2024-03-25 21:16:58 +04:00
|
|
|
|
el.Count -= dif;
|
|
|
|
|
count -= dif;
|
|
|
|
|
if (el.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
lst.Add(el);
|
|
|
|
|
}
|
|
|
|
|
if (count == 0)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (count > 0)
|
|
|
|
|
{
|
|
|
|
|
transaction.Rollback();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
foreach (var el in lst)
|
|
|
|
|
{
|
|
|
|
|
context.ShopRepairs.Remove(el);
|
|
|
|
|
}
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
transaction.Commit();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|