Добавил животных

This commit is contained in:
goblinrf 2024-04-23 19:11:39 +04:00
parent 8b960b6d81
commit cff2dbca9e
16 changed files with 439 additions and 16 deletions

View File

@ -20,7 +20,7 @@ namespace VetClinicDataBaseImplement.Implements
{
var deletedElement = context.Admins
//.Include(x => x.Visits)
//.Include(x => x.Animals)
.Include(x => x.Animals)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
@ -39,7 +39,7 @@ namespace VetClinicDataBaseImplement.Implements
{
return context.Admins
//.Include(x => x.Visits)
//.Include(x => x.Animals)
.Include(x => x.Animals)
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)
?.GetViewModel;
}
@ -47,7 +47,7 @@ namespace VetClinicDataBaseImplement.Implements
{
return context.Admins
//.Include(x => x.Visits)
//.Include(x => x.Animals)
.Include(x => x.Animals)
.FirstOrDefault(x => (x.Email == model.Email && x.Password == model.Password))
?.GetViewModel;
}
@ -64,7 +64,7 @@ namespace VetClinicDataBaseImplement.Implements
return context.Admins
//.Include(x => x.Visits)
//.Include(x => x.Animals)
.Include(x => x.Animals)
.Where(x => x.AdminFIO.Contains(model.AdminFIO))
.Select(x => x.GetViewModel)
.ToList();
@ -76,7 +76,7 @@ namespace VetClinicDataBaseImplement.Implements
return context.Admins
//.Include(x => x.Visits)
//.Include(x => x.Animals)
.Include(x => x.Animals)
.Select(x => x.GetViewModel)
.ToList();
}
@ -94,7 +94,7 @@ namespace VetClinicDataBaseImplement.Implements
return context.Admins
//.Include(x => x.Visits)
//.Include(x => x.Animals)
.Include(x => x.Animals)
.FirstOrDefault(x => x.Id == newAdmin.Id)
?.GetViewModel;
}
@ -104,7 +104,7 @@ namespace VetClinicDataBaseImplement.Implements
using var context = new VetClinicDatabase();
var order = context.Admins
//.Include(x => x.Visits)
//.Include(x => x.Animals)
.Include(x => x.Animals)
.FirstOrDefault(x => x.Id == model.Id);
if (order == null)
{
@ -115,7 +115,7 @@ namespace VetClinicDataBaseImplement.Implements
return context.Admins
//.Include(x => x.Visits)
//.Include(x => x.Animals)
.Include(x => x.Animals)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
}

View File

@ -0,0 +1,129 @@
using VetClinicContracts.BindingModels;
using VetClinicContracts.SearchModels;
using VetClinicContracts.StoragesContracts;
using VetClinicContracts.ViewModels;
using VetClinicDataBaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using VetClinicDataBaseImplement;
namespace DinerDataBaseImplement.Implements
{
public class AnimalStorage
{
public List<AnimalViewModel> GetFullList()
{
using var context = new VetClinicDatabase();
return context.Animals
//.Include(x => x.Visit)
.Include(x => x.Admin)
//.Include(x => x.Vaccination)
.Select(x => x.GetViewModel)
.ToList();
}
public List<AnimalViewModel> GetFilteredList(AnimalSearchModel model)
{
using var context = new VetClinicDatabase();
if (model.Id.HasValue)
{
return context.Animals
//.Include(x => x.Visit)
.Include(x => x.Admin)
//.Include(x => x.Vaccination)
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
}
else if (model.AdminId.HasValue)
{
return context.Animals
//.Include(x => x.Visit)
.Include(x => x.Admin)
//.Include(x => x.Vaccination)
.Where(x => x.AdminId == model.AdminId)
.Select(x => x.GetViewModel)
.ToList();
}
else if (model.VisitId.HasValue)
{
return context.Animals
//.Include(x => x.Visit)
.Include(x => x.Admin)
//.Include(x => x.Vaccination)
.Where(x => x.AdminId == model.AdminId)
.Select(x => x.GetViewModel)
.ToList();
}
return new();
}
public AnimalViewModel? GetElement(AnimalSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new VetClinicDatabase();
return context.Animals//.Include(x => x.Visit)
.Include(x => x.Admin)
//.Include(x => x.Vaccination)
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel;
}
public AnimalViewModel? Insert(AnimalBindingModel model)
{
using var context = new VetClinicDatabase();
var newAnimal = Animal.Create(model);
if (newAnimal == null)
{
return null;
}
context.Animals.Add(newAnimal);
context.SaveChanges();
return context.Animals//.Include(x => x.Visit)
.Include(x => x.Admin)
//.Include(x => x.Vaccination)
.FirstOrDefault(x => x.Id == newAnimal.Id)?.GetViewModel; ;
}
public AnimalViewModel? Update(AnimalBindingModel model)
{
using var context = new VetClinicDatabase();
var animal = context.Animals.FirstOrDefault(rec => rec.Id == model.Id);
if (animal == null)
{
return null;
}
animal.Update(model);
context.SaveChanges();
return context.Animals
//.Include(x => x.Visit)
.Include(x => x.Admin)
//.Include(x => x.Vaccination)
.FirstOrDefault(x => x.Id == model.Id)?
.GetViewModel;
}
public AnimalViewModel? Delete(AnimalBindingModel model)
{
using var context = new VetClinicDatabase();
var element = context.Animals
.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
var deletedElement = context.Animals
//.Include(x => x.Visit)
.Include(x => x.Admin)
//.Include(x => x.Vaccination)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
context.Animals.Remove(element);
context.SaveChanges();
return deletedElement;
}
return null;
}
}
}

View File

@ -26,8 +26,8 @@ namespace VetClinicDataBaseImplement.Models
// [ForeignKey("AdminId")]
//public virtual List<Visit> Visits { get; set; } = new();
// [ForeignKey("AdminId")]
//public virtual List<Animal> Animals { get; set; } = new();
[ForeignKey("AdminId")]
public virtual List<Animal> Animals { get; set; } = new();
public static Admin? Create(AdminBindingModel model)
{
if (model == null)
@ -36,7 +36,7 @@ namespace VetClinicDataBaseImplement.Models
}
return new Admin()
{
Id = model.Id,
Id = model.AdminId,
AdminFIO = model.AdminFIO,
Email = model.Email,
Password = model.Password

View File

@ -0,0 +1,77 @@
using VetClinicDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VetClinicContracts.BindingModels;
using VetClinicContracts.ViewModels;
using System.Collections;
namespace VetClinicDataBaseImplement.Models
{
public class Animal:IAnimalModel
{
public int Id { get; set; }
[Required]
public int AdminId { get; set; }
[Required]
public int VisitId { get; set; }
[Required]
public string AnimalName { get; set; }= string.Empty;
[Required]
public string Family { get; set;} = string.Empty;
//public virtual Visit Visit { get; set; }
public virtual Admin Admin { get; set; }
//public virtual Visut Visit { get; set; }
//public virtual Vaccination Vaccination { get; set; }
// [ForeignKey("AnimalId")]
//public virtual List<Visit> Visits { get; set; } = new();
//[ForeignKey("AnimalId")]
//public virtual List<Vaccination> Vaccinations { get; set; } = new();
public static Animal? Create(AnimalBindingModel? model)
{
if (model == null)
{
return null;
}
return new Animal()
{
Id = model.Id,
VisitId = model.VisitId,
AdminId = model.AdminId,
//VaccinationId = model.VaccinationId
AnimalName = model.AnimalName,
Family = model.Family
};
}
public void Update(AnimalBindingModel? model)
{
if (model == null)
{
return;
}
AnimalName = model.AnimalName;
Family = model.Family;
}
public AnimalViewModel GetViewModel => new()
{
Id = Id,
VisitId = VisitId,
AdminId = AdminId,
//VaccinationId = VaccinationId
AnimalName = AnimalName,
Family = Family
};
}
}

View File

@ -13,7 +13,7 @@ namespace VetClinicDataBaseImplement
}
base.OnConfiguring(optionsBuilder);
}
//public virtual DbSet<Animal> Animals { set; get; }
public virtual DbSet<Animal> Animals { set; get; }
public virtual DbSet<Admin> Admins { set; get; }
// public virtual DbSet<Visit> Visits { set; get; }
// public virtual DbSet<Vaccination> Vaccinations { set; get; }

View File

@ -52,7 +52,7 @@ namespace VetClinicAdminApp.Controllers
APIAdmin.PostRequest("api/admin/updatedata", new AdminBindingModel
{
Id = APIAdmin.Admin.Id,
AdminId = APIAdmin.Admin.Id,
AdminFIO = fio,
Email = login,
Password = password

View File

@ -44,7 +44,7 @@
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.VisitId)
@Html.DisplayFor(modelItem => item.Id)
</td>
<td>
@Html.DisplayFor(modelItem => item.AnimalName)

View File

@ -0,0 +1,113 @@
using VetClinicContracts.BindingModels;
using VetClinicContracts.BusinessLogicsContracts;
using VetClinicContracts.SearchModels;
using VetClinicContracts.StoragesContracts;
using VetClinicContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace VetClinicBusinessLogic.BusinessLogics
{
public class AnimalLogic : IAnimalLogic
{
private readonly ILogger _logger;
private readonly IAnimalStorage _animalStorage;
public AnimalLogic(ILogger<AnimalLogic> logger, IAnimalStorage orderStorage)
{
_logger = logger;
_animalStorage = orderStorage;
}
public List<AnimalViewModel>? ReadList(AnimalSearchModel? model)
{
_logger.LogInformation("ReadList. Id:{ Id}", model?.Id);
var list = model == null ? _animalStorage.GetFullList() : _animalStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
return list;
}
public AnimalViewModel? ReadElement(AnimalSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadList. AnimalName:{AnimalName}. Family {Family}. Id:{Id}", model.AnimalName, model?.Family, model?.Id);
var element = _animalStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
public bool CreateAnimal(AnimalBindingModel model)
{
CheckModel(model);
if (_animalStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool UpdateAnimal(AnimalBindingModel model)
{
CheckModel(model);
if (_animalStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool DeleteAnimal(AnimalBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_animalStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(AnimalBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
//if (model.VaccitinationId < 0)
//{
// throw new ArgumentNullException("Неверный идентификатор прививки", nameof(model.VaccitinationId));
//}
if (model.AnimalName == "")
{
throw new ArgumentNullException("Имя не должно быть пустым", nameof(model.AnimalName));
}
if (model.AdminId < 0)
{
throw new ArgumentNullException("Некорректный идентификатор у клиента", nameof(model.AdminId));
}
//_logger.LogInformation("Animal. VaccinationId: {VaccinationId}. AnimalName: {AnimalName}. Family: {Family}. Id: {Id}. AdminId: {AdminId}. VisitId: {VisitId}", model.VaccinationId, model.AnimalName, model.Family, model.Id, model.AdminId, model.VisitId);
var element = _animalStorage.GetElement(new AnimalSearchModel
{
Id = model.Id
});
}
}
}

View File

@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace VetClinicContracts.BindingModels
{
public class AnimalBindingModel
{
public int Id { get; set; }
public int AdminId { get; set; }
public int VisitId { get; set; }
public string AnimalName { get; set; } = string.Empty;
public string? Family { get; set; } = string.Empty;
}
}

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 IAnimalLogic
{
List<AnimalViewModel>? ReadList(AnimalSearchModel? model);
AnimalViewModel? ReadElement(AnimalSearchModel model);
bool CreateAnimal(AnimalBindingModel model);
bool UpdateAnimal(AnimalBindingModel model);
bool DeleteAnimal(AnimalBindingModel 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 AnimalSearchModel
{
public int? Id { get; set; }
public int? AdminId { get; set; }
public int? VisitId { get; set; }
public string? AnimalName { get; set; }
public string? Family { 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 IAnimalStorage
{
List<AnimalViewModel> GetFullList();
List<AnimalViewModel> GetFilteredList(AnimalSearchModel model);
AnimalViewModel? GetElement(AnimalSearchModel model);
AnimalViewModel? Insert(AnimalBindingModel model);
AnimalViewModel? Update(AnimalBindingModel model);
AnimalViewModel? Delete(AnimalBindingModel model);
}
}

View File

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VetClinicDataModels.Models;
namespace VetClinicContracts.ViewModels
{
public class AnimalViewModel: IAnimalModel
{
public int Id { get; set; }
public int AdminId { get; set; }
public int VisitId { get; set; }
[DisplayName("Имя животного")]
public string AnimalName { get; set; } = string.Empty;
[DisplayName("Семейство")]
public string? Family { get; set; } = string.Empty;
}
}

View File

@ -7,7 +7,7 @@ namespace VetClinicContracts.ViewModels
public class VisitViewModel : IVisitModel
{
[DisplayName("Номер")]
public int VisitId { get; set; }
public int Id { get; set; }
public int ServiceId { get; set; }
public int AdminId { get; set; }
public int AnimalId { get; set; }

View File

@ -9,6 +9,7 @@ namespace VetClinicDataModels.Models
public interface IAnimalModel
{
int AdminId { get; }
int VisitId { get; }
string AnimalName { get; }
string? Family { get; }
}

View File

@ -8,7 +8,7 @@ namespace VetClinicDataModels.Models
{
public interface IVisitModel
{
int VisitId { get;}
int Id { get;}
int ServiceId { get; }
int AdminId { get; }