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

79 lines
2.2 KiB
C#

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
.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
.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;
}
}
}