diff --git a/VetClinic/VetClinicBusinessLogic/BusinessLogics/VaccinationLogic.cs b/VetClinic/VetClinicBusinessLogic/BusinessLogics/VaccinationLogic.cs new file mode 100644 index 0000000..95af9e0 --- /dev/null +++ b/VetClinic/VetClinicBusinessLogic/BusinessLogics/VaccinationLogic.cs @@ -0,0 +1,112 @@ +using Microsoft.Extensions.Logging; +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; + +namespace VetClinicBusinessLogic.BusinessLogics +{ + public class VaccinationLogic + { + private readonly ILogger _logger; + private readonly IVaccinationStorage _vaccinationStorage; + + public VaccinationLogic(ILogger logger, IVaccinationStorage vaccinationStorage) + { + _logger = logger; + _vaccinationStorage = vaccinationStorage; + } + + public VaccinationViewModel? ReadElement(VaccinationSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. Id:{ Id}", model.Id); + var element = _vaccinationStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); + return element; + } + + public List? ReadList(VaccinationSearchModel? model) + { + _logger.LogInformation("ReadList. Id:{ Id}", model?.Id); + var list = model == null ? _vaccinationStorage.GetFullList() : + _vaccinationStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + + return list; + } + + public bool Create(VaccinationBindingModel model) + { + CheckModel(model); + if (_vaccinationStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + public bool Update(VaccinationBindingModel model) + { + CheckModel(model); + if (_vaccinationStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool Delete(VaccinationBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_vaccinationStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + private void CheckModel(VaccinationBindingModel model, bool withParams = + true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.NameVaccination)) + { + throw new ArgumentNullException("Нет текста рекомендации", + nameof(model.NameVaccination)); + } + if (model.CostVaccination <= 0) + { + throw new ArgumentNullException("Цена привики должна быть больше 0", nameof(model.CostVaccination)); + } + + _logger.LogInformation("Guidance. NameVaccination:{NameVaccination}. CostVaccination:{CostVaccination}", model.NameVaccination, model.CostVaccination); + } + } +} + diff --git a/VetClinic/VetClinicContracts/BindingModels/VaccinationBindingModel.cs b/VetClinic/VetClinicContracts/BindingModels/VaccinationBindingModel.cs index 0f4d8e7..3742cf8 100644 --- a/VetClinic/VetClinicContracts/BindingModels/VaccinationBindingModel.cs +++ b/VetClinic/VetClinicContracts/BindingModels/VaccinationBindingModel.cs @@ -8,6 +8,11 @@ namespace VetClinicContracts.BindingModels { public class VaccinationBindingModel { + public int Id { get; set; } + public int AnimalId { get; set; } + public string NameVaccination { get; set; } = string.Empty; + public double CostVaccination { get; set; } + public DateTime? DateStamp { get; set; } } } diff --git a/VetClinic/VetClinicContracts/BusinessLogicsContracts/IVaccinationLogic.cs b/VetClinic/VetClinicContracts/BusinessLogicsContracts/IVaccinationLogic.cs new file mode 100644 index 0000000..7f760b1 --- /dev/null +++ b/VetClinic/VetClinicContracts/BusinessLogicsContracts/IVaccinationLogic.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using VetClinicContracts.BindingModels; +using VetClinicContracts.SearchModels; +using VetClinicContracts.ViewModels; + +namespace VetClinicContracts.BusinessLogicsContracts +{ + public interface IVaccinationLogic + { + List? ReadList(VaccinationSearchModel? model); + VaccinationViewModel? ReadElement(VaccinationSearchModel model); + bool Create(VaccinationBindingModel model); + bool Update(VaccinationBindingModel model); + bool Delete(VaccinationBindingModel model); + } +} diff --git a/VetClinic/VetClinicContracts/SearchModels/VaccinationSearchModel.cs b/VetClinic/VetClinicContracts/SearchModels/VaccinationSearchModel.cs new file mode 100644 index 0000000..abb24fd --- /dev/null +++ b/VetClinic/VetClinicContracts/SearchModels/VaccinationSearchModel.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace VetClinicContracts.SearchModels +{ + public class VaccinationSearchModel + { + public int? Id { get; set; } + public string? NameVaccination { get; set; } + public double? CostVaccination { get; set; } + + public DateTime? DateFrom { get; set; } + public DateTime? DateTo { get; set; } + + public int? AnimalId { get; set; } + } +} diff --git a/VetClinic/VetClinicContracts/StoragesContracts/IVaccinationStorage.cs b/VetClinic/VetClinicContracts/StoragesContracts/IVaccinationStorage.cs new file mode 100644 index 0000000..ccb7798 --- /dev/null +++ b/VetClinic/VetClinicContracts/StoragesContracts/IVaccinationStorage.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using VetClinicContracts.BindingModels; +using VetClinicContracts.SearchModels; +using VetClinicContracts.ViewModels; + +namespace VetClinicContracts.StoragesContracts +{ + public interface IVaccinationStorage + { + List GetFullList(); + List GetFilteredList(VaccinationSearchModel model); + VaccinationViewModel? GetElement(VaccinationSearchModel model); + VaccinationViewModel? Insert(VaccinationBindingModel model); + VaccinationViewModel? Update(VaccinationBindingModel model); + VaccinationViewModel? Delete(VaccinationBindingModel model); + } +} diff --git a/VetClinic/VetClinicContracts/ViewModels/VaccinationViewModel.cs b/VetClinic/VetClinicContracts/ViewModels/VaccinationViewModel.cs new file mode 100644 index 0000000..f26704a --- /dev/null +++ b/VetClinic/VetClinicContracts/ViewModels/VaccinationViewModel.cs @@ -0,0 +1,23 @@ +using VetClinicDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +namespace VetClinicContracts.ViewModels +{ + public class VaccinationViewModel : IVaccinationModel + { + public int Id { get; set; } + [DisplayName("Название прививки")] + public string NameVaccination { get; set; } = string.Empty; + [DisplayName("Цена")] + public double CostVaccination { get; set; } + [DisplayName("Дата")] + public DateTime? DateStamp { get; set; } + [DisplayName("Животное")] + public string AnimalName { get; set; } = string.Empty; + public int AnimalId { get; set; } + } +} diff --git a/VetClinic/VetClinicContracts/ViewModels/VisitViewModel.cs b/VetClinic/VetClinicContracts/ViewModels/VisitViewModel.cs index ffda9cc..ca0d817 100644 --- a/VetClinic/VetClinicContracts/ViewModels/VisitViewModel.cs +++ b/VetClinic/VetClinicContracts/ViewModels/VisitViewModel.cs @@ -1,6 +1,10 @@ -using System.ComponentModel; -using VetClinicDataModels.Models; - +using VetClinicDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; namespace VetClinicContracts.ViewModels { diff --git a/VetClinic/VetClinicDataBaseImplement/Implements/AnimalStorage.cs b/VetClinic/VetClinicDataBaseImplement/Implements/AnimalStorage.cs index fe0f1c7..e6c7cd0 100644 --- a/VetClinic/VetClinicDataBaseImplement/Implements/AnimalStorage.cs +++ b/VetClinic/VetClinicDataBaseImplement/Implements/AnimalStorage.cs @@ -6,7 +6,7 @@ using VetClinicDataBaseImplement.Models; using Microsoft.EntityFrameworkCore; using VetClinicDataBaseImplement; -namespace DinerDataBaseImplement.Implements +namespace VetClinicBaseImplement.Implements { public class AnimalStorage { diff --git a/VetClinic/VetClinicDataBaseImplement/Implements/VaccinationStorage.cs b/VetClinic/VetClinicDataBaseImplement/Implements/VaccinationStorage.cs new file mode 100644 index 0000000..cf0f619 --- /dev/null +++ b/VetClinic/VetClinicDataBaseImplement/Implements/VaccinationStorage.cs @@ -0,0 +1,78 @@ +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.ViewModels; +using VetClinicDataBaseImplement.Models; + +namespace VetClinicDataBaseImplement.Implements +{ + public class VaccinationStorage + { + public List GetFullList() + { + using var context = new VetClinicDatabase(); + return context.Vaccinations.Include(x => x.Animal).Select(x => x.GetViewModel).ToList(); + } + public List GetFilteredList(VaccinationSearchModel model) + { + + using var context = new VetClinicDatabase(); + return context.Vaccinations.Include(x => x.Animal).Where(x => (!model.Id.HasValue || model.Id == x.Id) + && (!model.AnimalId.HasValue || model.AnimalId == x.AnimalId) && (!model.DateFrom.HasValue || model.DateFrom <= x.DateStamp) + && (!model.DateTo.HasValue || model.DateTo >= x.DateStamp)) + .Select(x => x.GetViewModel) + .ToList(); + } + public VaccinationViewModel? GetElement(VaccinationSearchModel model) + { + using var context = new VetClinicDatabase(); + return context.Vaccinations.Include(x => x.Animal).FirstOrDefault(x => (!model.Id.HasValue || model.Id == x.Id) + && (!model.AnimalId.HasValue || model.AnimalId == x.AnimalId) && (!model.DateFrom.HasValue || model.DateFrom <= x.DateStamp) + && (!model.DateTo.HasValue || model.DateTo >= x.DateStamp))?.GetViewModel; + } + public VaccinationViewModel? Insert(VaccinationBindingModel model) + { + using var context = new VetClinicDatabase(); + var newVaccination = Vaccination.Create(context, model); + if (newVaccination == null) + { + return null; + } + context.Vaccinations.Add(newVaccination); + context.SaveChanges(); + return newVaccination.GetViewModel; + } + public VaccinationViewModel? Update(VaccinationBindingModel model) + { + using var context = new VetClinicDatabase(); + var element = context.Vaccinations + .Include(x => x.Animal) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Vaccinations.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + public VaccinationViewModel? Delete(VaccinationBindingModel model) + { + using var context = new VetClinicDatabase(); + var element = context.Vaccinations.FirstOrDefault(rec => rec.Id == + model.Id); + if (element != null) + { + context.Vaccinations.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/VetClinic/VetClinicDataBaseImplement/Models/Vaccination.cs b/VetClinic/VetClinicDataBaseImplement/Models/Vaccination.cs new file mode 100644 index 0000000..3b2a14a --- /dev/null +++ b/VetClinic/VetClinicDataBaseImplement/Models/Vaccination.cs @@ -0,0 +1,64 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using static System.Net.Mime.MediaTypeNames; +using static System.Runtime.InteropServices.JavaScript.JSType; +using VetClinicContracts.BindingModels; +using VetClinicContracts.ViewModels; + +namespace VetClinicDataBaseImplement.Models +{ + public class Vaccination + { + public int Id { get; private set; } + [Required] + public int AnimalId { get; private set; } + public virtual Animal Animal { get; private set; } + [Required] + public string NameVaccination { get; private set; } = string.Empty; + [Required] + public double CostVaccination { get; private set; } + [Required] + public DateTime? DateStamp { get; private set; } + public static Vaccination? Create(VetClinicDatabase context, VaccinationBindingModel model) + { + if (model == null) + { + return null; + } + return new Vaccination() + { + Id = model.Id, + AnimalId = model.AnimalId, + NameVaccination = model.NameVaccination, + CostVaccination = model.CostVaccination, + DateStamp = model.DateStamp, + Animal = context.Animals.FirstOrDefault(x => x.Id == model.AnimalId), + }; + } + + public void Update(VaccinationBindingModel? model) + { + if (model == null) + { + return; + } + NameVaccination = model.NameVaccination; + CostVaccination = model.CostVaccination; + DateStamp = model.DateStamp; + + } + + public VaccinationViewModel GetViewModel => new() + { + AnimalId = AnimalId, + NameVaccination = NameVaccination, + DateStamp = DateStamp, + Id = Id, + AnimalName = Animal.AnimalName + }; + } +} diff --git a/VetClinic/VetClinicDataBaseImplement/VetClinicDatabase .cs b/VetClinic/VetClinicDataBaseImplement/VetClinicDatabase .cs index 2266641..f0a0b08 100644 --- a/VetClinic/VetClinicDataBaseImplement/VetClinicDatabase .cs +++ b/VetClinic/VetClinicDataBaseImplement/VetClinicDatabase .cs @@ -19,6 +19,6 @@ namespace VetClinicDataBaseImplement public virtual DbSet Admins { set; get; } public virtual DbSet VisitAnimals { set; get; } public virtual DbSet Visits { set; get; } - // public virtual DbSet Vaccinations { set; get; } + public virtual DbSet Vaccinations { set; get; } } }