под новую бд

This commit is contained in:
goblinrf 2024-04-26 20:38:41 +04:00
parent fbe3ab7eac
commit 915a947704
9 changed files with 7 additions and 119 deletions

View File

@ -23,7 +23,7 @@ namespace VetClinicAdminApp.Controllers
return Redirect("~/Home/Enter");
}
return View(APIAdmin.GetRequest<List<VisitViewModel>>($"api/main/getorders?clientId={APIAdmin.Admin.Id}"));
return View(APIAdmin.GetRequest<List<VisitViewModel>>($"api/main/getorders?visitId={APIAdmin.Admin.Id}"));
}
[HttpGet]

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

@ -15,10 +15,7 @@ namespace VetClinicBaseImplement.Implements
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

@ -16,7 +16,6 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DinerDataBaseImplement\VetClinicDataBaseImplement.csproj" />
<ProjectReference Include="..\VetClinicBusinessLogic\VetClinicBusinessLogic.csproj" />
<ProjectReference Include="..\VetClinicContracts\VetClinicContracts.csproj" />
<ProjectReference Include="..\VetClinicDataBaseImplement\VetClinicDataBaseImplement.csproj" />