поручитель реализации хранилищ

This commit is contained in:
antoc0der 2024-04-25 22:16:48 +04:00
parent 90a164c508
commit cfd9c7c406
8 changed files with 442 additions and 5 deletions

View File

@ -9,7 +9,7 @@ using VeterinaryContracts.SearchModels;
using VeterinaryContracts.StorageContracts;
using VeterinaryContracts.ViewModels;
using Microsoft.Extensions.Logging;
using VeterinaryDataModels;
namespace VeterinaryBusinessLogic.BusinessLogic
{
@ -113,5 +113,9 @@ namespace VeterinaryBusinessLogic.BusinessLogic
throw new InvalidOperationException("Лекарство с таким названием уже есть");
}
}
public bool MakeSell(IDrugModel drug, int count)
{
return _drugStorage.SellDrugs(drug, count);
}
}
}

View File

@ -6,6 +6,7 @@ using System.Threading.Tasks;
using VeterinaryContracts.BindingModels;
using VeterinaryContracts.ViewModels;
using VeterinaryContracts.SearchModels;
using VeterinaryDataModels;
namespace VeterinaryContracts.BusinessLogicContracts
{
@ -16,5 +17,6 @@ namespace VeterinaryContracts.BusinessLogicContracts
bool Create(DrugBindingModel model);
bool Update(DrugBindingModel model);
bool Delete(DrugBindingModel model);
bool MakeSell(IDrugModel drug, int count);
}
}

View File

@ -6,6 +6,7 @@ using System.Threading.Tasks;
using VeterinaryContracts.BindingModels;
using VeterinaryContracts.ViewModels;
using VeterinaryContracts.SearchModels;
using VeterinaryDataModels;
namespace VeterinaryContracts.StorageContracts
{
@ -17,5 +18,6 @@ namespace VeterinaryContracts.StorageContracts
DrugViewModel? Insert(DrugBindingModel model);
DrugViewModel? Update(DrugBindingModel model);
DrugViewModel? Delete(DrugBindingModel model);
public bool SellDrugs(IDrugModel model, int count);
}
}

View File

@ -0,0 +1,89 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VeterinaryContracts.StorageContracts;
using VeterinaryContracts.BindingModels;
using VeterinaryContracts.SearchModels;
using VeterinaryContracts.ViewModels;
using VeterinaryDatabaseImplement.Models;
namespace VeterinaryDatabaseImplement.Implements
{
public class DoctorStorage : IDoctorStorage
{
public List<DoctorViewModel> GetFullList()
{
using var context = new VeterinaryDatabase();
return context.Doctors
.Select(x => x.GetViewModel)
.ToList();
}
public List<DoctorViewModel> GetFilteredList(DoctorSearchModel model)
{
if (string.IsNullOrEmpty(model.DoctorFIO) && string.IsNullOrEmpty(model.Login) && string.IsNullOrEmpty(model.Password) &&
string.IsNullOrEmpty(model.DoctorFIO))
{
return new();
}
using var context = new VeterinaryDatabase();
return context.Doctors
.Where(x => (string.IsNullOrEmpty(model.DoctorFIO) || x.DoctorFIO.Contains(model.DoctorFIO) &&
(string.IsNullOrEmpty(model.Login) || x.DoctorFIO.Contains(model.Login)) &&
(string.IsNullOrEmpty(model.Password) || x.DoctorFIO.Contains(model.Password))))
.Select(x => x.GetViewModel)
.ToList();
}
public DoctorViewModel? GetElement(DoctorSearchModel model)
{
if (string.IsNullOrEmpty(model.DoctorFIO) && string.IsNullOrEmpty(model.Login) &&
!model.Id.HasValue)
{
return null;
}
using var context = new VeterinaryDatabase();
return context.Doctors
.FirstOrDefault(x => (string.IsNullOrEmpty(model.DoctorFIO) || x.DoctorFIO == model.DoctorFIO) &&
(!model.Id.HasValue || x.Id == model.Id) && (string.IsNullOrEmpty(model.Login) || x.Login == model.Login) &&
(string.IsNullOrEmpty(model.Password) || x.Password == model.Password))
?.GetViewModel;
}
public DoctorViewModel? Insert(DoctorBindingModel model)
{
var newDoctor = Doctor.Create(model);
if (newDoctor == null)
{
return null;
}
using var context = new VeterinaryDatabase();
context.Doctors.Add(newDoctor);
context.SaveChanges();
return newDoctor.GetViewModel;
}
public DoctorViewModel? Update(DoctorBindingModel model)
{
using var context = new VeterinaryDatabase();
var client = context.Doctors.FirstOrDefault(x => x.Id == model.Id);
if (client == null)
{
return null;
}
client.Update(model);
context.SaveChanges();
return client.GetViewModel;
}
public DoctorViewModel? Delete(DoctorBindingModel model)
{
using var context = new VeterinaryDatabase();
var element = context.Doctors.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Doctors.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,146 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VeterinaryContracts.StorageContracts;
using VeterinaryContracts.BindingModels;
using VeterinaryContracts.SearchModels;
using VeterinaryContracts.ViewModels;
using VeterinaryDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using VeterinaryDataModels;
namespace VeterinaryDatabaseImplement.Implements
{
public class DrugStorage : IDrugStorage
{
public List<DrugViewModel> GetFullList()
{
using var context = new VeterinaryDatabase();
return context.Drugs
.Include(x => x.Medications)
.ThenInclude(x => x.Medication)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<DrugViewModel> GetFilteredList(DrugSearchModel model)
{
if (string.IsNullOrEmpty(model.DrugName))
{
return new();
}
using var context = new VeterinaryDatabase();
return context.Drugs.Include(x => x.Medications)
.ThenInclude(x => x.Medication)
.Where(x => x.DrugName.Contains(model.DrugName))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public DrugViewModel? GetElement(DrugSearchModel model)
{
if (string.IsNullOrEmpty(model.DrugName) &&
!model.Id.HasValue)
{
return null;
}
using var context = new VeterinaryDatabase();
return context.Drugs
.Include(x => x.Medications)
.ThenInclude(x => x.Medication)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.DrugName) &&
x.DrugName == model.DrugName) ||
(model.Id.HasValue && x.Id ==
model.Id))
?.GetViewModel;
}
public DrugViewModel? Insert(DrugBindingModel model)
{
using var context = new VeterinaryDatabase();
var newDrug = Drug.Create(context, model);
if (newDrug == null)
{
return null;
}
context.Drugs.Add(newDrug);
context.SaveChanges();
return newDrug.GetViewModel;
}
public DrugViewModel? Update(DrugBindingModel model)
{
using var context = new VeterinaryDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var drug = context.Drugs.FirstOrDefault(rec =>
rec.Id == model.Id);
if (drug == null)
{
return null;
}
drug.Update(model);
context.SaveChanges();
drug.UpdateMedications(context, model);
transaction.Commit();
return drug.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public DrugViewModel? Delete(DrugBindingModel model)
{
using var context = new VeterinaryDatabase();
var element = context.Drugs
.Include(x => x.Medications)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Drugs.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public bool SellDrugs(IDrugModel model, int count)
{
if (model == null)
return false;
using var context = new VeterinaryDatabase();
using var transaction = context.Database.BeginTransaction();
//List<ShopDrug> lst = new List<ShopDrug>();
//foreach (var el in context.ShopDrugs.Where(x => x.DrugId == model.Id))
//{
int dif = count;
if (model.Count < dif)
dif = model.Count;
model.Count -= dif;
count -= dif;
if (el.Count == 0)
{
lst.Add(el);
}
if (count == 0)
break;
//}
if (count > 0)
{
transaction.Rollback();
return false;
}
// почистили лист
foreach (var el in lst)
{
context.ShopDrugs.Remove(el);
}
context.SaveChanges();
transaction.Commit();
return true;
}
}
}

View File

@ -0,0 +1,89 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VeterinaryContracts.StorageContracts;
using VeterinaryContracts.BindingModels;
using VeterinaryContracts.SearchModels;
using VeterinaryContracts.ViewModels;
using VeterinaryDatabaseImplement.Models;
using VeterinaryDatabaseImplement;
namespace VeterinaryDatabaseImplement.Implements
{
public class MedicationStorage : IMedicationStorage
{
public List<MedicationViewModel> GetFullList()
{
using var context = new VeterinaryDatabase();
return context.Medications
.Select(x => x.GetViewModel)
.ToList();
}
public List<MedicationViewModel> GetFilteredList(MedicationSearchModel model)
{
if (string.IsNullOrEmpty(model.MedicationName))
{
return new();
}
using var context = new VeterinaryDatabase();
return context.Medications
.Where(x => x.MedicationName.Contains(model.MedicationName))
.Select(x => x.GetViewModel)
.ToList();
}
public MedicationViewModel? GetElement(MedicationSearchModel model)
{
if (string.IsNullOrEmpty(model.MedicationName) && !model.Id.HasValue)
{
return null;
}
using var context = new VeterinaryDatabase();
return context.Medications
.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.MedicationName) && x.MedicationName ==
model.MedicationName) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public MedicationViewModel? Insert(MedicationBindingModel model)
{
var newMedication = Medication.Create(model);
if (newMedication == null)
{
return null;
}
using var context = new VeterinaryDatabase();
context.Medications.Add(newMedication);
context.SaveChanges();
return newMedication.GetViewModel;
}
public MedicationViewModel? Update(MedicationBindingModel model)
{
using var context = new VeterinaryDatabase();
var medication = context.Medications.FirstOrDefault(x => x.Id ==model.Id);
if (medication == null)
{
return null;
}
medication.Update(model);
context.SaveChanges();
return medication.GetViewModel;
}
public MedicationViewModel? Delete(MedicationBindingModel model)
{
using var context = new VeterinaryDatabase();
var element = context.Medications.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Medications.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,109 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VeterinaryContracts.BindingModels;
using VeterinaryContracts.SearchModels;
using VeterinaryContracts.StorageContracts;
using VeterinaryContracts.ViewModels;
using VeterinaryDatabaseImplement.Models;
namespace VeterinaryDatabaseImplement.Implements
{
public class ServiceStorage : IServiceStorage
{
public List<ServiceViewModel> GetFullList()
{
using var context = new VeterinaryDatabase();
return context.Services.Include(x=>x.Doctor)
.Include(x => x.Medications)
.ThenInclude(x => x.Medication)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<ServiceViewModel> GetFilteredList(ServiceSearchModel model)
{
if (string.IsNullOrEmpty(model.ServiceName))
{
return new();
}
using var context = new VeterinaryDatabase();
return context.Services.Include(x => x.Doctor).Include(x => x.Medications)
.ThenInclude(x => x.Medication)
.Where(x => x.ServiceName.Contains(model.ServiceName))
.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 VeterinaryDatabase();
return context.Services.Include(x => x.Doctor)
.Include(x => x.Medications)
.ThenInclude(x => x.Medication)
.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 VeterinaryDatabase();
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 VeterinaryDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var service = context.Services.FirstOrDefault(rec =>
rec.Id == model.Id);
if (service == null)
{
return null;
}
service.Update(model);
context.SaveChanges();
service.UpdateMedications(context, model);
transaction.Commit();
return service.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public ServiceViewModel? Delete(ServiceBindingModel model)
{
using var context = new VeterinaryDatabase();
var element = context.Services.Include(x => x.Doctor)
.Include(x => x.Medications)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Services.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -11,10 +11,6 @@
<ProjectReference Include="..\VeterinaryDataModels\VeterinaryDataModels.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Implements\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.16" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.16" />