ISEbd-21_Melnikov_I.O._CarS.../CarService/CarServiceDatabase/Implements/RepairRequestStorage.cs

83 lines
2.3 KiB
C#
Raw Normal View History

using CarServiceContracts.BindingModels;
using CarServiceContracts.SearchModels;
using CarServiceContracts.StorageContracts;
using CarServiceContracts.ViewModels;
using CarServiceDatabase.Models;
using Microsoft.EntityFrameworkCore;
namespace CarServiceDatabase.Implements
{
public class RepairRequestStorage : IRepairRequestStorage
{
public List<RepairRequestViewModel> GetFullList()
{
using var context = new CarServiceDbContext();
return context.RepairRequests
2023-05-25 13:31:55 +04:00
.Include(x => x.Vehicle)
.ThenInclude(x => x.Customer)
.Select(x => x.GetViewModel)
.ToList();
}
public List<RepairRequestViewModel> GetFilteredList(RepairRequestSearchModel model)
{
using var context = new CarServiceDbContext();
return context.RepairRequests
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
}
public RepairRequestViewModel? GetElement(RepairRequestSearchModel model)
{
if (model == null)
{
return null;
}
using var context = new CarServiceDbContext();
if (model.Id.HasValue)
{
return context.RepairRequests
.Include(x => x.Vehicle)
.ThenInclude(x => x.Customer)
.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
}
return null;
}
public RepairRequestViewModel? Insert(RepairRequestBindingModel model)
{
using var context = new CarServiceDbContext();
var newRepairRequest = RepairRequest.Create(context, model);
if (newRepairRequest != null)
{
context.RepairRequests.Add(newRepairRequest);
context.SaveChanges();
return newRepairRequest.GetViewModel;
}
return null;
}
public RepairRequestViewModel? Update(RepairRequestBindingModel model)
{
using var context = new CarServiceDbContext();
var repairRequest = context.RepairRequests.FirstOrDefault(x => x.Id == model.Id);
if (repairRequest == null)
{
return null;
}
repairRequest.Update(context, model);
context.SaveChanges();
return repairRequest.GetViewModel;
}
public RepairRequestViewModel? Delete(RepairRequestBindingModel model)
{
using var context = new CarServiceDbContext();
var repairRequest = context.RepairRequests.FirstOrDefault(x => x.Id == model.Id);
if (repairRequest == null)
{
return null;
}
context.RepairRequests.Remove(repairRequest);
context.SaveChanges();
return repairRequest.GetViewModel;
}
}
}