106 lines
3.5 KiB
C#
106 lines
3.5 KiB
C#
|
using CarRepairShopContracts.BindingModels;
|
|||
|
using CarRepairShopContracts.SearchModels;
|
|||
|
using CarRepairShopContracts.StoragesContracts;
|
|||
|
using CarRepairShopContracts.ViewModels;
|
|||
|
using CarRepairShopDatabaseImplement.Models;
|
|||
|
using Microsoft.EntityFrameworkCore;
|
|||
|
|
|||
|
namespace CarRepairShopDatabaseImplement.Implements
|
|||
|
{
|
|||
|
public class RepairStorage : IRepairStorage
|
|||
|
{
|
|||
|
public List<RepairViewModel> GetFullList()
|
|||
|
{
|
|||
|
using var context = new RepairsShopDatabase();
|
|||
|
return context.Repairs
|
|||
|
.Include(x => x.Components)
|
|||
|
.ThenInclude(x => x.Component)
|
|||
|
.ToList()
|
|||
|
.Select(x => x.GetViewModel)
|
|||
|
.ToList();
|
|||
|
}
|
|||
|
public List<RepairViewModel> GetFilteredList(RepairSearchModel model)
|
|||
|
{
|
|||
|
if (string.IsNullOrEmpty(model.RepairName))
|
|||
|
{
|
|||
|
return new();
|
|||
|
}
|
|||
|
using var context = new RepairsShopDatabase();
|
|||
|
return context.Repairs
|
|||
|
.Include(x => x.Components)
|
|||
|
.ThenInclude(x => x.Component)
|
|||
|
.Where(x => x.RepairName.Contains(model.RepairName))
|
|||
|
.ToList()
|
|||
|
.Select(x => x.GetViewModel)
|
|||
|
.ToList();
|
|||
|
}
|
|||
|
public RepairViewModel? GetElement(RepairSearchModel model)
|
|||
|
{
|
|||
|
if (string.IsNullOrEmpty(model.RepairName) &&
|
|||
|
!model.Id.HasValue)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
using var context = new RepairsShopDatabase();
|
|||
|
return context.Repairs
|
|||
|
.Include(x => x.Components)
|
|||
|
.ThenInclude(x => x.Component)
|
|||
|
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.RepairName) &&
|
|||
|
x.RepairName == model.RepairName) ||
|
|||
|
(model.Id.HasValue && x.Id ==
|
|||
|
model.Id))
|
|||
|
?.GetViewModel;
|
|||
|
}
|
|||
|
public RepairViewModel? Insert(RepairBindingModel model)
|
|||
|
{
|
|||
|
using var context = new RepairsShopDatabase();
|
|||
|
var newRepair = Repair.Create(context, model);
|
|||
|
if (newRepair == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
context.Repairs.Add(newRepair);
|
|||
|
context.SaveChanges();
|
|||
|
return newRepair.GetViewModel;
|
|||
|
}
|
|||
|
public RepairViewModel? Update(RepairBindingModel model)
|
|||
|
{
|
|||
|
using var context = new RepairsShopDatabase();
|
|||
|
using var transaction = context.Database.BeginTransaction();
|
|||
|
try
|
|||
|
{
|
|||
|
var Repair = context.Repairs.FirstOrDefault(rec =>
|
|||
|
rec.Id == model.Id);
|
|||
|
if (Repair == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
Repair.Update(model);
|
|||
|
context.SaveChanges();
|
|||
|
Repair.UpdateComponents(context, model);
|
|||
|
transaction.Commit();
|
|||
|
return Repair.GetViewModel;
|
|||
|
}
|
|||
|
catch
|
|||
|
{
|
|||
|
transaction.Rollback();
|
|||
|
throw;
|
|||
|
}
|
|||
|
}
|
|||
|
public RepairViewModel? Delete(RepairBindingModel model)
|
|||
|
{
|
|||
|
using var context = new RepairsShopDatabase();
|
|||
|
var element = context.Repairs
|
|||
|
.Include(x => x.Components)
|
|||
|
.FirstOrDefault(rec => rec.Id == model.Id);
|
|||
|
if (element != null)
|
|||
|
{
|
|||
|
context.Repairs.Remove(element);
|
|||
|
context.SaveChanges();
|
|||
|
return element.GetViewModel;
|
|||
|
}
|
|||
|
return null;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|