Сделал хранилища

This commit is contained in:
gg12 darfren 2024-04-24 14:04:29 +04:00
parent 0f72fb4e2d
commit 14a03218c0
7 changed files with 387 additions and 3 deletions

View File

@ -0,0 +1,79 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VetClinicContracts.BindingModels;
using VetClinicContracts.SearchModels;
using VetClinicContracts.StoragesContracts;
using VetClinicContracts.ViewModels;
using VetClinicDataBaseImplement.Models;
namespace VetClinicDataBaseImplement.Implements
{
public class GuidanceStorage : IGuidanceStorage
{
public List<GuidanceViewModel> GetFullList()
{
using var context = new VetClinicDatabase();
return context.Guidances.Include(x => x.Service).Select(x => x.GetViewModel).ToList();
}
public List<GuidanceViewModel> GetFilteredList(GuidanceSearchModel model)
{
using var context = new VetClinicDatabase();
return context.Guidances.Include(x => x.Service).Where(x => (!model.Id.HasValue || model.Id == x.Id)
&& (!model.ServiceId.HasValue || model.ServiceId == x.ServiceId) && (!model.DateFrom.HasValue || model.DateFrom <= x.Date)
&& (!model.DateTo.HasValue || model.DateTo >= x.Date))
.Select(x => x.GetViewModel)
.ToList();
}
public GuidanceViewModel? GetElement(GuidanceSearchModel model)
{
using var context = new VetClinicDatabase();
return context.Guidances.Include(x => x.Service).FirstOrDefault(x => (!model.Id.HasValue || model.Id == x.Id)
&& (!model.ServiceId.HasValue || model.ServiceId == x.ServiceId) && (!model.DateFrom.HasValue || model.DateFrom <= x.Date)
&& (!model.DateTo.HasValue || model.DateTo >= x.Date))?.GetViewModel;
}
public GuidanceViewModel? Insert(GuidanceBindingModel model)
{
using var context = new VetClinicDatabase();
var newGuidance = Guidance.Create(context, model);
if (newGuidance == null)
{
return null;
}
context.Guidances.Add(newGuidance);
context.SaveChanges();
return newGuidance.GetViewModel;
}
public GuidanceViewModel? Update(GuidanceBindingModel model)
{
using var context = new VetClinicDatabase();
var element = context.Guidances
.Include(x => x.Service)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Guidances.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public GuidanceViewModel? Delete(GuidanceBindingModel model)
{
using var context = new VetClinicDatabase();
var element = context.Guidances.FirstOrDefault(rec => rec.Id ==
model.Id);
if (element != null)
{
context.Guidances.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,105 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VetClinicContracts.BindingModels;
using VetClinicContracts.SearchModels;
using VetClinicContracts.StoragesContracts;
using VetClinicContracts.ViewModels;
using VetClinicDataBaseImplement.Models;
namespace VetClinicDataBaseImplement.Implements
{
public class MedicineStorage : IMedicineStorage
{
public List<MedicineViewModel> GetFullList()
{
using var context = new VetClinicDatabase();
return context.Medicines.Include(x => x.Pharmacist).Include(x => x.Animals)
.ThenInclude(x => x.Animal)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<MedicineViewModel> GetFilteredList(MedicineSearchModel model)
{
using var context = new VetClinicDatabase();
return context.Medicines.Include(x => x.Pharmacist).Include(x => x.Animals)
.ThenInclude(x => x.Animal)
.Where(x => (string.IsNullOrEmpty(model.MedicineName) || x.MedicineName.Contains(model.MedicineName)
&& (!model.PharmacistId.HasValue || x.PharmacistId == model.PharmacistId)))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public MedicineViewModel? GetElement(MedicineSearchModel model)
{
if (string.IsNullOrEmpty(model.MedicineName) &&
!model.Id.HasValue)
{
return null;
}
using var context = new VetClinicDatabase();
return context.Medicines.Include(x => x.Pharmacist)
.Include(x => x.Animals)
.ThenInclude(x => x.Animal)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.MedicineName) &&
x.MedicineName == model.MedicineName) ||
(model.Id.HasValue && x.Id ==
model.Id))
?.GetViewModel;
}
public MedicineViewModel? Insert(MedicineBindingModel model)
{
using var context = new VetClinicDatabase();
var newMedicine = Medicine.Create(context, model);
if (newMedicine == null)
{
return null;
}
context.Medicines.Add(newMedicine);
context.SaveChanges();
return newMedicine.GetViewModel;
}
public MedicineViewModel? Update(MedicineBindingModel model)
{
using var context = new VetClinicDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var medicine = context.Medicines.FirstOrDefault(rec =>
rec.Id == model.Id);
if (medicine == null)
{
return null;
}
medicine.Update(model);
context.SaveChanges();
medicine.UpdateAnimals(context, model);
transaction.Commit();
return medicine.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public MedicineViewModel? Delete(MedicineBindingModel model)
{
using var context = new VetClinicDatabase();
var element = context.Medicines
.Include(x => x.Animals).ThenInclude(x => x.Animal)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Medicines.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,89 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VetClinicContracts.BindingModels;
using VetClinicContracts.SearchModels;
using VetClinicContracts.StoragesContracts;
using VetClinicContracts.ViewModels;
using VetClinicDataBaseImplement.Models;
namespace VetClinicDataBaseImplement.Implements
{
public class PharmacistStorage : IPharmacistStorage
{
public List<PharmacistViewModel> GetFullList()
{
using var context = new VetClinicDatabase();
return context.Pharmacists
.Select(x => x.GetViewModel)
.ToList();
}
public List<PharmacistViewModel> GetFilteredList(PharmacistSearchModel model)
{
if (string.IsNullOrEmpty(model.PharmacistFIO) && string.IsNullOrEmpty(model.Email) &&
string.IsNullOrEmpty(model.Password))
{
return new();
}
using var context = new VetClinicDatabase();
return context.Pharmacists
.Where(x => (string.IsNullOrEmpty(model.PharmacistFIO) || x.PharmacistFIO.Contains(model.PharmacistFIO) &&
string.IsNullOrEmpty(model.Email) || x.Email.Contains(model.Email) &&
string.IsNullOrEmpty(model.Password) || x.Password.Contains(model.Password)))
.Select(x => x.GetViewModel)
.ToList();
}
public PharmacistViewModel? GetElement(PharmacistSearchModel model)
{
if (string.IsNullOrEmpty(model.PharmacistFIO) && string.IsNullOrEmpty(model.Email) &&
!model.Id.HasValue)
{
return null;
}
using var context = new VetClinicDatabase();
return context.Pharmacists
.FirstOrDefault(x => (string.IsNullOrEmpty(model.PharmacistFIO) || x.PharmacistFIO == model.PharmacistFIO) &&
(!model.Id.HasValue || x.Id == model.Id) && (string.IsNullOrEmpty(model.Email) || x.Email == model.Email) &&
(string.IsNullOrEmpty(model.Password) || x.Password == model.Password))
?.GetViewModel;
}
public PharmacistViewModel? Insert(PharmacistBindingModel model)
{
var newPharmacist = Pharmacist.Create(model);
if (newPharmacist == null)
{
return null;
}
using var context = new VetClinicDatabase();
context.Pharmacists.Add(newPharmacist);
context.SaveChanges();
return newPharmacist.GetViewModel;
}
public PharmacistViewModel? Update(PharmacistBindingModel model)
{
using var context = new VetClinicDatabase();
var pharmacist = context.Pharmacists.FirstOrDefault(x => x.Id == model.Id);
if (pharmacist == null)
{
return null;
}
pharmacist.Update(model);
context.SaveChanges();
return pharmacist.GetViewModel;
}
public PharmacistViewModel? Delete(PharmacistBindingModel model)
{
using var context = new VetClinicDatabase();
var element = context.Pharmacists.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Pharmacists.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,110 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VetClinicContracts.BindingModels;
using VetClinicContracts.SearchModels;
using VetClinicContracts.StoragesContracts;
using VetClinicContracts.ViewModels;
using VetClinicDataBaseImplement.Models;
namespace VetClinicDataBaseImplement.Implements
{
public class ServiceStorage : IServiceStorage
{
public List<ServiceViewModel> GetFullList()
{
using var context = new VetClinicDatabase();
return context.Services.Include(x => x.Pharmacist)
.Include(x => x.Medicines)
.ThenInclude(x => x.Medicine)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<ServiceViewModel> GetFilteredList(ServiceSearchModel model)
{
using var context = new VetClinicDatabase();
return context.Services.Include(x => x.Pharmacist)
.Include(x => x.Medicines)
.ThenInclude(x => x.Medicine)
.Where(x => (string.IsNullOrEmpty(model.ServiceName) || x.ServiceName.Contains(model.ServiceName)
&& (!model.PharmacistId.HasValue || x.PharmacistId == model.PharmacistId)))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public ServiceViewModel? GetElement(ServiceSearchModel model)
{
if (string.IsNullOrEmpty(model.ServiceName) &&
!model.Id.HasValue)
{
return null;
}
using var context = new VetClinicDatabase();
return context.Services.Include(x => x.Pharmacist)
.Include(x => x.Medicines)
.ThenInclude(x => x.Medicine)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ServiceName) &&
x.ServiceName == model.ServiceName) ||
(model.Id.HasValue && x.Id ==
model.Id))
?.GetViewModel;
}
public ServiceViewModel? Insert(ServiceBindingModel model)
{
using var context = new VetClinicDatabase();
var newService = Service.Create(context, model);
if (newService == null)
{
return null;
}
context.Services.Add(newService);
context.SaveChanges();
return newService.GetViewModel;
}
public ServiceViewModel? Update(ServiceBindingModel model)
{
using var context = new VetClinicDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var iceCream = context.Services.FirstOrDefault(rec =>
rec.Id == model.Id);
if (iceCream == null)
{
return null;
}
iceCream.Update(model);
context.SaveChanges();
iceCream.UpdateMedicines(context, model);
transaction.Commit();
return iceCream.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public ServiceViewModel? Delete(ServiceBindingModel model)
{
using var context = new VetClinicDatabase();
var element = context.Services
.Include(x => x.Medicines)
.ThenInclude(x => x.Medicine)
.ThenInclude(x => x.Animals).ThenInclude(x => x.Animal)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Services.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -70,7 +70,7 @@ namespace VetClinicDataBaseImplement.Models
MedicineAnimals = MedicineAnimals,
PharmacistFIO = Pharmacist.PharmacistFIO
};
public void UpdateComponents(VetClinicDatabase context,
public void UpdateAnimals(VetClinicDatabase context,
MedicineBindingModel model)
{
var medicineAnimals = context.MedicineAnimals.Where(rec =>

View File

@ -62,7 +62,7 @@ namespace VetClinicDataBaseImplement.Models
Email = model.Email;
Password = model.Password;
}
public PharmacistBindingModel GetViewModel => new()
public PharmacistViewModel GetViewModel => new()
{
Id = Id,
PharmacistFIO = PharmacistFIO,

View File

@ -8,6 +8,7 @@ using System.Threading.Tasks;
using VetClinicDataModels.Models;
using VetClinicContracts.BindingModels;
using VetClinicContracts.ViewModels;
using Microsoft.EntityFrameworkCore;
namespace VetClinicDataBaseImplement.Models
{
@ -96,7 +97,7 @@ namespace VetClinicDataBaseImplement.Models
context.ServiceMedicines.Add(new ServiceMedicine
{
Service = service,
Medicine = context.Medicines.First(x => x.Id == pc.Key),
Medicine = context.Medicines.Include(x => x.Animals).ThenInclude(x => x.Animal).First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();