105 lines
3.3 KiB
C#
105 lines
3.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using LawFirmContracts.BindingModels;
|
|
using LawFirmContracts.Models;
|
|
using LawFirmContracts.SearchModels;
|
|
using LawFirmContracts.StorageContracts;
|
|
using LawFirmContracts.ViewModels;
|
|
using LawFirmDatabase.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace LawFirmDatabase.Implements
|
|
{
|
|
public class ServiceStorage : IServiceStorage
|
|
{
|
|
public ServiceViewModel? Delete(ServiceBindingModel model)
|
|
{
|
|
using var context = new LawFirmDBContext();
|
|
var service = context.Services.FirstOrDefault(x => x.Id == model.Id);
|
|
if (service == null)
|
|
{
|
|
return null;
|
|
}
|
|
context.Services.Remove(service);
|
|
context.SaveChanges();
|
|
return service.GetViewModel;
|
|
}
|
|
|
|
public ServiceViewModel? GetElement(ServiceSearchModel model)
|
|
{
|
|
if (model == null)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new LawFirmDBContext();
|
|
if (model.Id.HasValue)
|
|
{
|
|
return context.Services
|
|
.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public List<ServiceViewModel> GetFilteredList(ServiceSearchModel model)
|
|
{
|
|
using var context = new LawFirmDBContext();
|
|
return context.Services
|
|
.Where(x => x.Id == model.Id)
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
public List<ServiceViewModel> GetFullList()
|
|
{
|
|
using var context = new LawFirmDBContext();
|
|
return context.Services
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
public ServiceViewModel? Insert(ServiceBindingModel model)
|
|
{
|
|
using var context = new LawFirmDBContext();
|
|
var newService = Service.Create(context, model);
|
|
if (newService != null)
|
|
{
|
|
context.Services.Add(newService);
|
|
context.SaveChanges();
|
|
return newService.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public ServiceViewModel? Update(ServiceBindingModel model)
|
|
{
|
|
using var context = new LawFirmDBContext();
|
|
var service = context.Services.FirstOrDefault(x => x.Id == model.Id);
|
|
if (service == null)
|
|
{
|
|
return null;
|
|
}
|
|
service.Update(context, model);
|
|
context.SaveChanges();
|
|
return service.GetViewModel;
|
|
}
|
|
public List<ReportLawServicesViewModel> GetServicesWithRequest(ServiceSearchModel model)
|
|
{
|
|
if (model.SelectedServicesIds == null)
|
|
{
|
|
return new();
|
|
}
|
|
using var context = new LawFirmDBContext();
|
|
return context.Services.Select(x => x.GetViewModel)
|
|
.Where(w => model.SelectedServicesIds.Contains(w.Id))
|
|
.Select(w => new ReportLawServicesViewModel()
|
|
{
|
|
ServiceName = w.Name
|
|
})
|
|
.ToList();
|
|
}
|
|
}
|
|
}
|