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 GetFullList() { using var context = new CarServiceDbContext(); return context.RepairRequests .Include(x => x.Vehicle) .ThenInclude(x => x.Customer) .Select(x => x.GetViewModel) .ToList(); } public List 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; } } }