133 lines
4.3 KiB
C#
133 lines
4.3 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using System.ComponentModel;
|
|
using VeterinaryContracts.BindingModels;
|
|
using VeterinaryContracts.SearchModels;
|
|
using VeterinaryContracts.StorageContracts;
|
|
using VeterinaryContracts.ViewModels;
|
|
using VeterinaryDatabaseImplement.Models;
|
|
|
|
namespace VeterinaryDatabaseImplement.Implements
|
|
{
|
|
public class PetStorage : IPetStorage
|
|
{
|
|
public List<PetViewModel> GetFullList()
|
|
{
|
|
using var context = new VeterinaryDatabase();
|
|
return context.Pets
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
public List<PetViewModel> GetFilteredList(PetSearchModel model)
|
|
{
|
|
|
|
using var context = new VeterinaryDatabase();
|
|
return context.Pets.Where(x => x.OwnerId == model.OwnerId)
|
|
.Where(x => String.IsNullOrEmpty(model.PetName) || x.PetName.Contains(model.PetName))
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
public PetViewModel? GetElement(PetSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.PetName) && !model.Id.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new VeterinaryDatabase();
|
|
return context.Pets
|
|
.FirstOrDefault(x =>
|
|
(!string.IsNullOrEmpty(model.PetName) && x.PetName ==
|
|
model.PetName) ||
|
|
(model.Id.HasValue && x.Id == model.Id))
|
|
?.GetViewModel;
|
|
}
|
|
public List<ReportServicesViewModel> GetReportServices(ReportServicesSearchModel model)
|
|
{
|
|
if (model.petsIds == null)
|
|
{
|
|
return new();
|
|
}
|
|
using var context = new VeterinaryDatabase();
|
|
return context.Pets
|
|
.Where(pet => model.petsIds == null || model.petsIds.Contains(pet.Id))
|
|
.Select(pet => new ReportServicesViewModel()
|
|
{
|
|
PetName = pet.PetName,
|
|
Services = context.Services.Include(service => service.Visit).ThenInclude(visit => visit.Pets)
|
|
.Where(service => service.Visit!=null&&service.Visit.Pets.Any(p => p.PetId == pet.Id))
|
|
.Select(service => service.GetViewModel).ToList()
|
|
}).ToList();
|
|
}
|
|
public List<ReportVisitsDrugsViewModel> GetReportVisitsDrugs(ReportVisitsDrugsSearchModel model)
|
|
{
|
|
using var context = new VeterinaryDatabase();
|
|
//visit medicamen drug
|
|
return context.Visits
|
|
.Select(vis => new ReportVisitsDrugsViewModel()
|
|
{
|
|
PetName = vis.VisitName,
|
|
Medicaments = context.Medications
|
|
.Where(med => med.ServiceMedications
|
|
.Where(m => context.Services.Where(service => service.VisitId == vis.Id).Select(a => a.Id).Contains(m.ServiceId))
|
|
.Any(p => p.MedicationId == med.Id)
|
|
)
|
|
.Select(visit => visit.GetViewModel)
|
|
.ToList(),
|
|
Purchases = context.Purchases
|
|
//.Include(purchase => purchase.Pets)
|
|
.Where(purchase => purchase.DateCreate <= model.DateTo && purchase.DateCreate >= model.DateFrom
|
|
&& context.PurchasePets
|
|
.Where(m => context.VisitPets.Where(visit => visit.VisitId == vis.Id).Select(a => a.PetId).Contains(m.PetId))
|
|
/*.Where(med => med.
|
|
.Where(m => context.Services.Where(service => service.VisitId == vis.Id).Select(a => a.Id).Contains(m.ServiceId))
|
|
.Any(p => p.MedicationId == purchase.Id)
|
|
)*/
|
|
.Any(p => p.PurchaseId == purchase.Id)
|
|
//.Where(m => context.VisitPets.Where(service => service.VisitId == vis.Id).Select(a => a.Id).Contains(m.ServiceId))
|
|
//.Any(an => an.PurchaseId == m.Id )
|
|
)
|
|
//purchase.Pets.Any(p => context.VisitPets.Any(an => an.VisitId == pet.Id) p.PetId == pet.Id))
|
|
.Select(purchase => purchase.GetViewModel)
|
|
.ToList(),
|
|
})
|
|
.ToList();
|
|
}
|
|
public PetViewModel? Insert(PetBindingModel model)
|
|
{
|
|
var newPet = Pet.Create(model);
|
|
if (newPet == null)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new VeterinaryDatabase();
|
|
context.Pets.Add(newPet);
|
|
context.SaveChanges();
|
|
return newPet.GetViewModel;
|
|
}
|
|
public PetViewModel? Update(PetBindingModel model)
|
|
{
|
|
using var context = new VeterinaryDatabase();
|
|
var pet = context.Pets.FirstOrDefault(x => x.Id ==model.Id);
|
|
if (pet == null)
|
|
{
|
|
return null;
|
|
}
|
|
pet.Update(model);
|
|
context.SaveChanges();
|
|
return pet.GetViewModel;
|
|
}
|
|
public PetViewModel? Delete(PetBindingModel model)
|
|
{
|
|
using var context = new VeterinaryDatabase();
|
|
var element = context.Pets.FirstOrDefault(rec => rec.Id ==
|
|
model.Id);
|
|
if (element != null)
|
|
{
|
|
context.Pets.Remove(element);
|
|
context.SaveChanges();
|
|
return element.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
}
|