diff --git a/VeterinaryClinic/VeterinaryClinicDatabaseImplement/Implements/VisitStorage.cs b/VeterinaryClinic/VeterinaryClinicDatabaseImplement/Implements/VisitStorage.cs index 13565c7..976d744 100644 --- a/VeterinaryClinic/VeterinaryClinicDatabaseImplement/Implements/VisitStorage.cs +++ b/VeterinaryClinic/VeterinaryClinicDatabaseImplement/Implements/VisitStorage.cs @@ -23,7 +23,6 @@ namespace VeterinaryClinicDatabaseImplement.Implements .ThenInclude(x => x.Animal) .Include(x => x.Services) .ThenInclude(x => x.Service) - .ToList() .Select(x => x.GetViewModel) .ToList(); } @@ -31,20 +30,23 @@ namespace VeterinaryClinicDatabaseImplement.Implements { using var context = new VeterinaryClinicDatabase(); - if (model.DateFrom.HasValue && model.DateTo.HasValue) - { - return context.Visits + var filtered = context.Visits .Include(x => x.User) .Include(x => x.Animals) .ThenInclude(x => x.Animal) .Include(x => x.Services) - .ThenInclude(x => x.Service) - .Where(x => x.Date >= model.DateFrom && x.Date <= model.DateTo) + .ThenInclude (x => x.Service) .Select(x => x.GetViewModel) .ToList(); + + if (model.UserId.HasValue) + { + filtered = filtered + .Where(x => x.UserId.Equals(model.UserId)) + .ToList(); } - return new(); + return filtered ?? new(); } public VisitViewModel? GetElement(VisitSearchModel model) @@ -96,7 +98,7 @@ namespace VeterinaryClinicDatabaseImplement.Implements visit.Update(model); context.SaveChanges(); visit.UpdateAnimals(context, model); - visit.UpdateMedication(context, model); + visit.UpdateServices(context, model); transaction.Commit(); return visit.GetViewModel; } diff --git a/VeterinaryClinic/VeterinaryClinicDatabaseImplement/Models/Animal.cs b/VeterinaryClinic/VeterinaryClinicDatabaseImplement/Models/Animal.cs index 5c446cf..9abb055 100644 --- a/VeterinaryClinic/VeterinaryClinicDatabaseImplement/Models/Animal.cs +++ b/VeterinaryClinic/VeterinaryClinicDatabaseImplement/Models/Animal.cs @@ -94,11 +94,8 @@ namespace VeterinaryClinicDatabaseImplement.Models public void UpdateMedications(VeterinaryClinicDatabase context, AnimalBindingModel model) { var animalMedications = context.AnimalMedications.Where(rec => rec.AnimalId == model.Id).ToList(); - if (animalMedications != null && animalMedications.Count > 0) - { - context.AnimalMedications.RemoveRange(animalMedications.Where(rec => !model.AnimalMedications.ContainsKey(rec.MedicationId))); - context.SaveChanges(); - } + context.AnimalMedications.RemoveRange(animalMedications); + context.SaveChanges(); var animal = context.Animals.First(x => x.Id == Id); foreach (var pr in model.AnimalMedications) diff --git a/VeterinaryClinic/VeterinaryClinicDatabaseImplement/Models/Visit.cs b/VeterinaryClinic/VeterinaryClinicDatabaseImplement/Models/Visit.cs index c165f9b..53aa091 100644 --- a/VeterinaryClinic/VeterinaryClinicDatabaseImplement/Models/Visit.cs +++ b/VeterinaryClinic/VeterinaryClinicDatabaseImplement/Models/Visit.cs @@ -105,11 +105,8 @@ namespace VeterinaryClinicDatabaseImplement.Models public void UpdateAnimals(VeterinaryClinicDatabase context, VisitBindingModel model) { var visitAnimals = context.VisitAnimals.Where(rec => rec.VisitId == model.Id).ToList(); - if (visitAnimals != null && visitAnimals.Count > 0) - { - context.VisitAnimals.RemoveRange(visitAnimals.Where(rec => !model.VisitAnimals.ContainsKey(rec.AnimalId))); - context.SaveChanges(); - } + context.VisitAnimals.RemoveRange(visitAnimals); + context.SaveChanges(); var visit = context.Visits.First(x => x.Id == Id); foreach (var rm in model.VisitAnimals) @@ -124,14 +121,11 @@ namespace VeterinaryClinicDatabaseImplement.Models _visitAnimals = null; } - public void UpdateMedication(VeterinaryClinicDatabase context, VisitBindingModel model) + public void UpdateServices(VeterinaryClinicDatabase context, VisitBindingModel model) { var visitServices = context.VisitServices.Where(rec => rec.VisitId == model.Id).ToList(); - if (visitServices != null && visitServices.Count > 0) - { - context.VisitServices.RemoveRange(visitServices.Where(rec => !model.VisitServices.ContainsKey(rec.ServiceId))); - context.SaveChanges(); - } + context.VisitServices.RemoveRange(visitServices); + context.SaveChanges(); var visit = context.Visits.First(x => x.Id == Id); foreach (var rm in model.VisitServices) diff --git a/VeterinaryClinic/VeterinaryClinicWebApp/Controllers/VisitController.cs b/VeterinaryClinic/VeterinaryClinicWebApp/Controllers/VisitController.cs index edda017..f1e4386 100644 --- a/VeterinaryClinic/VeterinaryClinicWebApp/Controllers/VisitController.cs +++ b/VeterinaryClinic/VeterinaryClinicWebApp/Controllers/VisitController.cs @@ -1,6 +1,11 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using System.Diagnostics; +using VeterinaryClinicBusinessLogics.BusinessLogics; +using VeterinaryClinicContracts.BindingModels; +using VeterinaryClinicContracts.BusinessLogicsContracts; +using VeterinaryClinicContracts.SearchModels; +using VeterinaryClinicDataModels.Models; using VeterinaryClinicWebApp.Models; namespace VeterinaryClinicWebApp.Controllers @@ -8,20 +13,156 @@ namespace VeterinaryClinicWebApp.Controllers public class VisitController : Controller { private readonly ILogger _logger; - public VisitController(ILogger logger) + + private readonly IVisitLogic _visitLogic; + + private readonly IAnimalLogic _animalLogic; + + private readonly IServiceLogic _serviceLogic; + public VisitController(ILogger logger, IVisitLogic visitLogic, IAnimalLogic animalLogic, IServiceLogic serviceLogic) { _logger = logger; + _visitLogic = visitLogic; + _animalLogic = animalLogic; + _serviceLogic = serviceLogic; } - public IActionResult Visits() + /// + /// Вывести список животных + /// + public IActionResult Visits() { - return View(); - } + if (APIClient.User == null) + { + return Redirect("~/Home/Enter"); + } - [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] - public IActionResult Error() - { - return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); - } - } + return View(_visitLogic.ReadList(new VisitSearchModel + { + UserId = APIClient.User.Id, + })); + } + + /// + /// Создать животного + /// + [HttpGet] + public IActionResult CreateVisit() + { + if (APIClient.User == null) + { + return Redirect("~/Home/Enter"); + } + + ViewBag.Animals = _animalLogic.ReadList(new AnimalSearchModel + { + UserId = APIClient.User.Id, + }); + + ViewBag.Services = _serviceLogic.ReadList(null); + + return View(); + } + + [HttpPost] + public void CreateVisit(DateTime dateVisit, List animal, List service) + { + if (APIClient.User == null) + { + throw new Exception("Необходимо авторизоваться!"); + } + + if (dateVisit == DateTime.MinValue || animal == null || service == null) + { + throw new Exception("Введены не все данные!"); + } + + Dictionary visitAnimals = new Dictionary(); + foreach (var animalId in animal) + { + visitAnimals.Add(animalId, _animalLogic.ReadElement(new AnimalSearchModel { Id = animalId })!); + } + + Dictionary visitServices = new Dictionary(); + foreach (var serviceId in service) + { + visitServices.Add(serviceId, _serviceLogic.ReadElement(new ServiceSearchModel { Id = serviceId })!); + } + + _visitLogic.Create(new VisitBindingModel + { + Date = dateVisit, + VisitAnimals = visitAnimals, + VisitServices = visitServices, + UserId = APIClient.User.Id + }); + + Response.Redirect("/Visit/Visits"); + } + + /// + /// Редактировать животного + /// + [HttpGet] + public IActionResult UpdateVisit(int id) + { + if (APIClient.User == null) + { + return Redirect("~/Home/Enter"); + } + + ViewBag.Animals = _animalLogic.ReadList(new AnimalSearchModel + { + UserId = APIClient.User.Id, + }); + + ViewBag.Services = _serviceLogic.ReadList(null); + + return View(_visitLogic.ReadElement(new VisitSearchModel + { + Id = id + })); + } + + [HttpPost] + public void UpdateVisit(int id, DateTime dateVisit, List animal, List service) + { + if (APIClient.User == null) + { + throw new Exception("Необходимо авторизоваться!"); + } + + if (dateVisit == DateTime.MinValue || animal == null || service == null) + { + throw new Exception("Введены не все данные!"); + } + + Dictionary visitAnimals = new Dictionary(); + foreach (var animalId in animal) + { + visitAnimals.Add(animalId, _animalLogic.ReadElement(new AnimalSearchModel { Id = animalId })!); + } + + Dictionary visitServices = new Dictionary(); + foreach (var serviceId in service) + { + visitServices.Add(serviceId, _serviceLogic.ReadElement(new ServiceSearchModel { Id = serviceId })!); + } + + _visitLogic.Update(new VisitBindingModel + { + Id = id, + Date = dateVisit, + VisitAnimals = visitAnimals, + VisitServices = visitServices, + UserId = APIClient.User.Id + }); + + Response.Redirect("/Visit/Visits"); + } + + /// + /// Удалить животного + /// + } } diff --git a/VeterinaryClinic/VeterinaryClinicWebApp/Views/Animal/CreateAnimal.cshtml b/VeterinaryClinic/VeterinaryClinicWebApp/Views/Animal/CreateAnimal.cshtml index 329386a..4c735fb 100644 --- a/VeterinaryClinic/VeterinaryClinicWebApp/Views/Animal/CreateAnimal.cshtml +++ b/VeterinaryClinic/VeterinaryClinicWebApp/Views/Animal/CreateAnimal.cshtml @@ -28,7 +28,7 @@
Медикаменты:
- @foreach (var medication in ViewBag.Medications) { diff --git a/VeterinaryClinic/VeterinaryClinicWebApp/Views/Animal/UpdateAnimal.cshtml b/VeterinaryClinic/VeterinaryClinicWebApp/Views/Animal/UpdateAnimal.cshtml index 5dd9552..ab17c28 100644 --- a/VeterinaryClinic/VeterinaryClinicWebApp/Views/Animal/UpdateAnimal.cshtml +++ b/VeterinaryClinic/VeterinaryClinicWebApp/Views/Animal/UpdateAnimal.cshtml @@ -34,7 +34,7 @@
Медикаменты:
- @foreach (var medication in ViewBag.Medications) { var isSelected = Model.AnimalMedications.Any(x => x.Key.Equals(medication.Id)); diff --git a/VeterinaryClinic/VeterinaryClinicWebApp/Views/Visit/CreateVisit.cshtml b/VeterinaryClinic/VeterinaryClinicWebApp/Views/Visit/CreateVisit.cshtml new file mode 100644 index 0000000..4579dcf --- /dev/null +++ b/VeterinaryClinic/VeterinaryClinicWebApp/Views/Visit/CreateVisit.cshtml @@ -0,0 +1,47 @@ +@{ + ViewData["Title"] = "Создание визита"; +} + +
+

Создание визита

+
+ +
+ +
+
Дата визита:
+
+
+ + +
+
Животные:
+
+ +
+
+ + +
+
Услуги:
+
+ +
+
+ + +
+
+
+
+
diff --git a/VeterinaryClinic/VeterinaryClinicWebApp/Views/Visit/UpdateVisit.cshtml b/VeterinaryClinic/VeterinaryClinicWebApp/Views/Visit/UpdateVisit.cshtml new file mode 100644 index 0000000..1e24aa4 --- /dev/null +++ b/VeterinaryClinic/VeterinaryClinicWebApp/Views/Visit/UpdateVisit.cshtml @@ -0,0 +1,54 @@ + +@using VeterinaryClinicContracts.ViewModels + +@model VisitViewModel + +@{ + ViewData["Title"] = "Редактирование визита"; +} + +
+

Редактирование визита

+
+ +
+ +
+
Дата визита:
+
+
+ + +
+
Животные:
+
+ +
+
+ + +
+
Услуги:
+
+ +
+
+ + +
+
+
+
+
diff --git a/VeterinaryClinic/VeterinaryClinicWebApp/Views/Visit/Visits.cshtml b/VeterinaryClinic/VeterinaryClinicWebApp/Views/Visit/Visits.cshtml index 3aa8fc5..e65598b 100644 --- a/VeterinaryClinic/VeterinaryClinicWebApp/Views/Visit/Visits.cshtml +++ b/VeterinaryClinic/VeterinaryClinicWebApp/Views/Visit/Visits.cshtml @@ -30,6 +30,7 @@ Пользователь Животные Услуги + Действия @@ -38,14 +39,40 @@ { @Html.DisplayFor(modelItem => visit.Id) - @Html.DisplayFor(modelItem => visit.Date.ToShortDateString()) + @Html.DisplayFor(modelItem => visit.Date) @Html.DisplayFor(modelItem => visit.UserFullName) - @Html.DisplayFor(modelItem => visit.VisitAnimals) - @Html.DisplayFor(modelItem => visit.VisitServices) + + @if (visit.VisitAnimals.Any()) + { +
    + @foreach (var animal in visit.VisitAnimals) + { +
  • @animal.Value.Breed
  • + } +
+ } + else + { +

Нет назначенных лекарств

+ } + + + @if (visit.VisitServices.Any()) + { +
    + @foreach (var service in visit.VisitServices) + { +
  • @service.Value.Name - @service.Value.Cost
  • + } +
+ } + else + { +

Нет назначенных лекарств

+ } +

- -