сделал прививку

This commit is contained in:
goblinrf 2024-04-24 15:53:22 +04:00
parent ca5e671616
commit a56127ae4f
11 changed files with 352 additions and 5 deletions

View File

@ -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<VaccinationLogic> 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<VaccinationViewModel>? 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);
}
}
}

View File

@ -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; }
}
}

View File

@ -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<VaccinationViewModel>? ReadList(VaccinationSearchModel? model);
VaccinationViewModel? ReadElement(VaccinationSearchModel model);
bool Create(VaccinationBindingModel model);
bool Update(VaccinationBindingModel model);
bool Delete(VaccinationBindingModel model);
}
}

View File

@ -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; }
}
}

View File

@ -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<VaccinationViewModel> GetFullList();
List<VaccinationViewModel> GetFilteredList(VaccinationSearchModel model);
VaccinationViewModel? GetElement(VaccinationSearchModel model);
VaccinationViewModel? Insert(VaccinationBindingModel model);
VaccinationViewModel? Update(VaccinationBindingModel model);
VaccinationViewModel? Delete(VaccinationBindingModel model);
}
}

View File

@ -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; }
}
}

View File

@ -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
{

View File

@ -6,7 +6,7 @@ using VetClinicDataBaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using VetClinicDataBaseImplement;
namespace DinerDataBaseImplement.Implements
namespace VetClinicBaseImplement.Implements
{
public class AnimalStorage
{

View File

@ -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<VaccinationViewModel> GetFullList()
{
using var context = new VetClinicDatabase();
return context.Vaccinations.Include(x => x.Animal).Select(x => x.GetViewModel).ToList();
}
public List<VaccinationViewModel> 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;
}
}
}

View File

@ -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
};
}
}

View File

@ -19,6 +19,6 @@ namespace VetClinicDataBaseImplement
public virtual DbSet<Admin> Admins { set; get; }
public virtual DbSet<VisitAnimal> VisitAnimals { set; get; }
public virtual DbSet<Visit> Visits { set; get; }
// public virtual DbSet<Vaccination> Vaccinations { set; get; }
public virtual DbSet<Vaccination> Vaccinations { set; get; }
}
}