Compare commits

...

3 Commits

Author SHA1 Message Date
5a3f0e218b рпьрп 2024-04-26 20:42:31 +04:00
915a947704 под новую бд 2024-04-26 20:38:41 +04:00
gg12 darfren
fbe3ab7eac cummit 2024-04-25 17:44:57 +04:00
8 changed files with 24 additions and 123 deletions

View File

@ -11,10 +11,7 @@ namespace VetClinicContracts.BindingModels
{
public int Id { get; set; }
public int AdminId { get; set; }
public int VaccinationId { get; set; }
public Dictionary<int, IVisitModel> VisitAnimals { get; set; } = new();
public Dictionary<int, IMedicineModel> MedicineAnimals { get; set; } = new();
public string AnimalName { get; set; } = string.Empty;
public string? Family { get; set; } = string.Empty;

View File

@ -11,10 +11,6 @@ namespace VetClinicContracts.SearchModels
{
public int? Id { get; set; }
public int? AdminId { get; set; }
public Dictionary<int, IVisitModel> Visits { get; set; } = new();
public Dictionary<int, IMedicineModel> Medicines { get; set; } = new();
public string? AnimalName { get; set; }
public string? Family { get; set; }

View File

@ -12,9 +12,7 @@ namespace VetClinicContracts.ViewModels
{
public int Id { get; set; }
public int AdminId { get; set; }
public int VaccinationId { get; set; }
public Dictionary<int, IVisitModel> VisitAnimals { get; set; } = new();
public Dictionary<int, IMedicineModel> MedicineAnimals { get; set; } = new();
[DisplayName("Имя животного")]
public string AnimalName { get; set; } = string.Empty;

View File

@ -8,17 +8,14 @@ using VetClinicDataBaseImplement;
namespace VetClinicBaseImplement.Implements
{
public class AnimalStorage
public class AnimalStorage : IAnimalStorage
{
public List<AnimalViewModel> GetFullList()
{
using var context = new VetClinicDatabase();
return context.Animals
.Include(x => x.Vaccinations)
.Include(x => x.Admin)
.Include(x => x.Visits)
.ThenInclude(x => x.Visit)
.Select(x => x.GetViewModel)
.ToList();
}
@ -32,9 +29,6 @@ namespace VetClinicBaseImplement.Implements
using var context = new VetClinicDatabase();
return context.Animals
.Include(x => x.Admin)
.Include(x => x.Vaccinations)
.Include(x => x.Visits)
.ThenInclude(x => x.Visit)
.Where(x => x.AnimalName.Contains(model.AnimalName))
.ToList()
.Select(x => x.GetViewModel).ToList();
@ -48,9 +42,6 @@ namespace VetClinicBaseImplement.Implements
using var context = new VetClinicDatabase();
return context.Animals
.Include(x => x.Admin)
.Include(x => x.Vaccinations)
.Include(x => x.Visits)
.ThenInclude(x => x.Visit)
.Include(x => x.Vaccinations)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.AnimalName) && x.AnimalName == model.AnimalName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
@ -80,7 +71,6 @@ namespace VetClinicBaseImplement.Implements
}
animal.Update(model);
context.SaveChanges();
animal.UpdateVisits(context, model);
transaction.Commit();
return animal.GetViewModel;
}
@ -94,14 +84,11 @@ namespace VetClinicBaseImplement.Implements
{
using var context = new VetClinicDatabase();
var element = context.Animals
.Include(x => x.Visits)
.ThenInclude(x => x.Visit)
.Include(x => x.Vaccinations)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Animals.Remove(element);
context.SaveChanges();
return element.GetViewModel;

View File

@ -18,13 +18,7 @@ namespace VetClinicDataBaseImplement.Models
public int Id { get; set; }
[Required]
public int AdminId { get; set; }
[Required]
public int VaccinationId { get; set; }
[Required]
public Dictionary<int, IVisitModel>? _visitAnimals = null;
[Required]
public Dictionary<int, IMedicineModel>? _medicineAnimals = null;
[Required]
public string AnimalName { get; set; } = string.Empty;
@ -43,32 +37,7 @@ namespace VetClinicDataBaseImplement.Models
[ForeignKey("AnimalId")]
public virtual List<MedicineAnimal> Medicines { get; set; } = new();
[NotMapped]
public Dictionary<int, IVisitModel> VisitAnimals
{
get
{
if (_visitAnimals == null)
{
_visitAnimals = Visits.ToDictionary(recPC => recPC.VisitId, recPC =>
recPC.Visit as IVisitModel);
}
return _visitAnimals;
}
}
[NotMapped]
public Dictionary<int, IMedicineModel> MedicineAnimals
{
get
{
if (_medicineAnimals == null)
{
_medicineAnimals = Medicines.ToDictionary(recPC => recPC.MedicineId, recPC =>
recPC.Medicine as IMedicineModel);
}
return _medicineAnimals;
}
}
public static Animal? Create(VetClinicDatabase context, AnimalBindingModel? model)
{
if (model == null)
@ -79,18 +48,9 @@ namespace VetClinicDataBaseImplement.Models
{
Id = model.Id,
AdminId = model.AdminId,
VaccinationId = model.VaccinationId,
AnimalName = model.AnimalName,
Family = model.Family,
Visits = model.VisitAnimals.Select(x => new VisitAnimal
{
Visit = context.Visits.First(y => y.Id == x.Key)
}).ToList(),
Medicines = model.MedicineAnimals.Select(x => new MedicineAnimal
{
Medicine = context.Medicines.First(y => y.Id == x.Key)
}).ToList(),
};
}
public void Update(AnimalBindingModel? model)
@ -105,57 +65,11 @@ namespace VetClinicDataBaseImplement.Models
public AnimalViewModel GetViewModel => new()
{
Id = Id,
VisitAnimals = VisitAnimals,
AdminId = AdminId,
VaccinationId = VaccinationId,
AnimalName = AnimalName,
MedicineAnimals = MedicineAnimals,
Family = Family
};
public void UpdateVisits(VetClinicDatabase context, AnimalBindingModel model)
{
var visitAnimals = context.VisitAnimals.Where(rec => rec.AnimalId == model.Id).ToList();
if (visitAnimals != null)
{ // удалили те, которых нет в модели
context.VisitAnimals.RemoveRange(visitAnimals.Where(rec => !model.VisitAnimals.ContainsKey(rec.VisitId)));
context.SaveChanges();
}
var animal = context.Animals.First(x => x.Id == Id);
foreach (var pc in model.VisitAnimals)
{
context.VisitAnimals.Add(new VisitAnimal
{
Animal = animal,
Visit = context.Visits.First(x => x.Id == pc.Key),
});
context.SaveChanges();
}
_visitAnimals = null;
}
public void UpdateMedicines(VetClinicDatabase context, AnimalBindingModel model)
{
var medicineAnimals = context.MedicineAnimals.Where(rec => rec.AnimalId == model.Id).ToList();
if (medicineAnimals != null)
{ // удалили те, которых нет в модели
context.MedicineAnimals.RemoveRange(medicineAnimals.Where(rec => !model.MedicineAnimals.ContainsKey(rec.MedicineId)));
context.SaveChanges();
}
var animal = context.Animals.First(x => x.Id == Id);
foreach (var pc in model.VisitAnimals)
{
context.MedicineAnimals.Add(new MedicineAnimal
{
Animal = animal,
Medicine = context.Medicines.First(x => x.Id == pc.Key),
});
context.SaveChanges();
}
_medicineAnimals = null;
}
}
}

View File

@ -37,7 +37,7 @@ namespace VetClinicDataBaseImplement.Models
NameVaccination = model.NameVaccination,
CostVaccination = model.CostVaccination,
DateStamp = model.DateStamp,
Animal = context.Animals.FirstOrDefault(x => x.Id == model.AnimalId),
};
}
@ -52,7 +52,7 @@ namespace VetClinicDataBaseImplement.Models
DateStamp = model.DateStamp;
}
public VaccinationViewModel GetViewModel => new()
{
AnimalId = AnimalId,

View File

@ -9,9 +9,6 @@ namespace VetClinicDataModels.Models
public interface IAnimalModel : IId
{
int AdminId { get; }
Dictionary<int, IVisitModel> VisitAnimals { get; }
Dictionary<int, IMedicineModel> MedicineAnimals { get; }
string AnimalName { get; }
string? Family { get; }
}

View File

@ -3,6 +3,7 @@ using VetClinicContracts.BusinessLogicsContracts;
using VetClinicContracts.StoragesContracts;
using VetClinicDataBaseImplement.Implements;
using Microsoft.OpenApi.Models;
using VetClinicBaseImplement.Implements;
var builder = WebApplication.CreateBuilder(args);
@ -12,12 +13,23 @@ builder.Logging.AddLog4Net("log4net.config");
// Add services to the container.
builder.Services.AddTransient<IAdminStorage, AdminStorage>();
//builder.Services.AddTransient<IAnimalStorage, AnimalStorage>();
//builder.Services.AddTransient<IAnimalStorage, AnimalStorage>();
builder.Services.AddTransient<IAnimalStorage, AnimalStorage>();
builder.Services.AddTransient<IVaccinationStorage, VaccinationStorage>();
builder.Services.AddTransient<IVisitStorage, VisitStorage>();
builder.Services.AddTransient<IPharmacistStorage, PharmacistStorage>();
builder.Services.AddTransient<IServiceStorage, ServiceStorage>();
builder.Services.AddTransient<IMedicineStorage, MedicineStorage>();
builder.Services.AddTransient<IGuidanceStorage, GuidanceStorage>();
builder.Services.AddTransient<IAdminLogic, AdminLogic>();
//builder.Services.AddTransient<IAnimalLogic, AnimalLogic>();
//builder.Services.AddTransient<IVisitLogic, VisitLogic>();
builder.Services.AddTransient<IAnimalLogic, AnimalLogic>();
builder.Services.AddTransient<IVaccinationLogic, VaccinationLogic>();
builder.Services.AddTransient<IVisitLogic, VisitLogic>();
builder.Services.AddTransient<IPharmacistLogic, PharmacistLogic>();
builder.Services.AddTransient<IServiceLogic, ServiceLogic>();
builder.Services.AddTransient<IMedicineLogic, MedicineLogic>();
builder.Services.AddTransient<IGuidanceLogic, GuidanceLogic>();
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
@ -33,7 +45,7 @@ var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "DinerRestApi v1"));
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "VetClinicRestApi v1"));
}
app.UseHttpsRedirection();