108 lines
3.4 KiB
C#
108 lines
3.4 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using VetClinicBusinessLogic.BindingModels;
|
|||
|
using VetClinicBusinessLogic.Interfaces;
|
|||
|
using VetClinicBusinessLogic.ViewModels;
|
|||
|
using VetClinicDatabaseImplement.Models;
|
|||
|
using Microsoft.EntityFrameworkCore;
|
|||
|
|
|||
|
namespace VetClinicDatabaseImplement.Implement
|
|||
|
{
|
|||
|
public class ServiceStorage : IServiceStorage
|
|||
|
{
|
|||
|
public List<ServiceViewModel> GetFullList()
|
|||
|
{
|
|||
|
using var context = new VetClinicDatabase();
|
|||
|
return context.Services
|
|||
|
.Include(rec => rec.VisitServices)
|
|||
|
.ThenInclude(rec => rec.Visit)
|
|||
|
.Select(CreateModel)
|
|||
|
.ToList();
|
|||
|
}
|
|||
|
public List<ServiceViewModel> GetFilteredList(ServiceBindingModel model)
|
|||
|
{
|
|||
|
if (model == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
using var context = new VetClinicDatabase();
|
|||
|
return context.Services
|
|||
|
.Where(rec => rec.Name.Contains(model.Name))
|
|||
|
.Include(rec => rec.VisitServices)
|
|||
|
.ThenInclude(rec => rec.Visit)
|
|||
|
.Select(CreateModel)
|
|||
|
.ToList();
|
|||
|
}
|
|||
|
public ServiceViewModel GetElement(ServiceBindingModel model)
|
|||
|
{
|
|||
|
if (model == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
using var context = new VetClinicDatabase();
|
|||
|
var service = context.Services
|
|||
|
.FirstOrDefault(rec => rec.Id == model.Id);
|
|||
|
if (service == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
return CreateModel(service);
|
|||
|
}
|
|||
|
public void Insert(ServiceBindingModel model)
|
|||
|
{
|
|||
|
using var context = new VetClinicDatabase();
|
|||
|
|
|||
|
context.Services.Add(CreateModel(model, new Service()));
|
|||
|
context.SaveChanges();
|
|||
|
}
|
|||
|
public void Update(ServiceBindingModel model)
|
|||
|
{
|
|||
|
using var context = new VetClinicDatabase();
|
|||
|
var element = context.Services.FirstOrDefault(rec => rec.Id == model.Id);
|
|||
|
if (element == null)
|
|||
|
{
|
|||
|
throw new Exception("Элемент не найден");
|
|||
|
}
|
|||
|
CreateModel(model, element);
|
|||
|
context.SaveChanges();
|
|||
|
}
|
|||
|
public void Delete(ServiceBindingModel model)
|
|||
|
{
|
|||
|
using var context = new VetClinicDatabase();
|
|||
|
Service element = context.Services.FirstOrDefault(rec => rec.Id ==
|
|||
|
model.Id);
|
|||
|
if (element != null)
|
|||
|
{
|
|||
|
context.Services.Remove(element);
|
|||
|
context.SaveChanges();
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
throw new Exception("Элемент не найден");
|
|||
|
}
|
|||
|
}
|
|||
|
private static Service CreateModel(ServiceBindingModel model, Service
|
|||
|
service)
|
|||
|
{
|
|||
|
service.Cost = model.Cost;
|
|||
|
service.Name = model.Name;
|
|||
|
return service;
|
|||
|
}
|
|||
|
private static ServiceViewModel CreateModel(Service service)
|
|||
|
{
|
|||
|
return new ServiceViewModel
|
|||
|
{
|
|||
|
Id = service.Id,
|
|||
|
Cost = service.Cost,
|
|||
|
Name = service.Name,
|
|||
|
VisitServices = service.VisitServices
|
|||
|
.ToDictionary(recPC => recPC.VisitId,
|
|||
|
recPC => recPC.Visit.DateVisit)
|
|||
|
};
|
|||
|
}
|
|||
|
}
|
|||
|
}
|