From 2406dbd831fd9f54182b4a572692dd54e1b86412 Mon Sep 17 00:00:00 2001 From: Yunusov_Niyaz Date: Tue, 30 Apr 2024 19:54:50 +0400 Subject: [PATCH] =?UTF-8?q?=D0=98=D1=81=D0=BF=D0=BE=D0=BB=D0=BD=D0=B8?= =?UTF-8?q?=D1=82=D0=B5=D0=BB=D1=8C:=20=D1=81=D0=B4=D0=B5=D0=BB=D0=B0?= =?UTF-8?q?=D0=BB=20=D0=BA=D0=BE=D0=BD=D1=82=D1=80=D0=BE=D0=BB=D0=BB=D0=B5?= =?UTF-8?q?=D1=80=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../VeterinaryDatabaseImplement/Models/Pet.cs | 82 ++++-- .../Controllers/PetController.cs | 26 +- .../Controllers/PurchaseController.cs | 63 +++++ .../Controllers/VisitController.cs | 64 +++++ .../Controllers/HomeController.cs | 249 +++++++++++++++++- .../VeterinaryShowOwnerApp.csproj | 5 + .../Views/Home/CreatePurchase.cshtml | 6 + .../Views/Home/CreateVisit.cshtml | 38 +++ 8 files changed, 486 insertions(+), 47 deletions(-) create mode 100644 VeterinaryView/VeterinaryRestApi/Controllers/PurchaseController.cs create mode 100644 VeterinaryView/VeterinaryRestApi/Controllers/VisitController.cs create mode 100644 VeterinaryView/VeterinaryShowOwnerApp/Views/Home/CreatePurchase.cshtml create mode 100644 VeterinaryView/VeterinaryShowOwnerApp/Views/Home/CreateVisit.cshtml diff --git a/VeterinaryView/VeterinaryDatabaseImplement/Models/Pet.cs b/VeterinaryView/VeterinaryDatabaseImplement/Models/Pet.cs index 12f1bee..1d1695e 100644 --- a/VeterinaryView/VeterinaryDatabaseImplement/Models/Pet.cs +++ b/VeterinaryView/VeterinaryDatabaseImplement/Models/Pet.cs @@ -3,6 +3,7 @@ using System.ComponentModel.DataAnnotations; using VeterinaryContracts.BindingModels; using VeterinaryContracts.ViewModels; using VeterinaryDataModels.Models; +using System.ComponentModel; namespace VeterinaryDatabaseImplement.Models { @@ -20,10 +21,38 @@ namespace VeterinaryDatabaseImplement.Models [Required] public string PetGender { get; private set; } = string.Empty; [ForeignKey("PetId")] - public virtual List VisitPets { get; set; } = new(); + public virtual List Visits { get; set; } = new(); [ForeignKey("PetId")] - public virtual List PurchasePets { get; set; } = new(); - public static Pet? Create(PetBindingModel model) + public virtual List Purchases { get; set; } = new(); + public Dictionary? _visitPets = null; + [NotMapped] + public Dictionary VisitPets + { + get + { + if (_visitPets == null) + { + _visitPets = Visits.ToDictionary(recPC => recPC.VisitId, recPC => + recPC.Visit as IVisitModel); + } + return _visitPets; + } + } + public Dictionary? _purchasePets = null; + [NotMapped] + public Dictionary PurchasePets + { + get + { + if (_purchasePets == null) + { + _purchasePets = Purchases.ToDictionary(recPC => recPC.PurchaseId, recPC => + recPC.Purchase as IPurchaseModel); + } + return _purchasePets; + } + } + public static Pet? Create(VeterinaryDatabase context, PetBindingModel model) { if (model == null) { @@ -35,18 +64,15 @@ namespace VeterinaryDatabaseImplement.Models PetName = model.PetName, PetType = model.PetType, PetBreed = model.PetBreed, - PetGender = model.PetGender - }; - } - public static Pet Create(PetViewModel model) - { - return new Pet - { - Id = model.Id, - PetName = model.PetName, - PetType = model.PetType, - PetBreed = model.PetBreed, - PetGender = model.PetGender + PetGender = model.PetGender, + Visits = model.VisitPets.Select(x => new VisitPet + { + Visit = context.Visits.First(y => y.Id == x.Key) + }).ToList(), + Purchases = model.PurchasePets.Select(x => new PurchasePet + { + Purchase = context.Purchases.First(y => y.Id == x.Key) + }).ToList() }; } public void Update(PetBindingModel model) @@ -68,5 +94,31 @@ namespace VeterinaryDatabaseImplement.Models PetBreed = PetBreed, PetGender = PetGender }; + public void UpdateVisits(VeterinaryDatabase context, PetBindingModel model) + { + var visitPets = context.VisitPets.Where(rec => rec.PetId == model.Id).ToList(); + if (visitPets != null) + { + context.VisitPets.RemoveRange(visitPets.Where(rec => !model.VisitPets.ContainsKey(rec.VisitId))); + context.SaveChanges(); + foreach (var visit in visitPets) + { + model.VisitPets.Remove(visit.VisitId); + } + context.SaveChanges(); + } + var pet = context.Pets.First(x => x.Id == Id); + foreach (var pc in model.VisitPets) + { + context.VisitPets.Add(new VisitPet + { + Pet = pet, + Visit = context.Visits.First(x => x.Id == pc.Key), + + }); + context.SaveChanges(); + } + _visitPets = null; + } } } diff --git a/VeterinaryView/VeterinaryRestApi/Controllers/PetController.cs b/VeterinaryView/VeterinaryRestApi/Controllers/PetController.cs index 2a559d4..859251b 100644 --- a/VeterinaryView/VeterinaryRestApi/Controllers/PetController.cs +++ b/VeterinaryView/VeterinaryRestApi/Controllers/PetController.cs @@ -3,6 +3,7 @@ using VeterinaryContracts.BindingModels; using VeterinaryContracts.BusinessLogicContracts; using VeterinaryContracts.SearchModels; using VeterinaryContracts.ViewModels; +using VeterinaryDatabaseImplement.Models; namespace VeterinaryRestApi.Controllers { @@ -18,22 +19,6 @@ namespace VeterinaryRestApi.Controllers _pet = pet; } [HttpGet] - public Tuple>? GetPet(int petId) - { - try - { - var elem = _pet.ReadElement(new PetSearchModel { Id = petId }); - if (elem == null) - return null; - return Tuple.Create(elem, elem.VisitPets.Select(x => x.Value.Name).ToList()); - } - catch (Exception ex) - { - _logger.LogError(ex, "Ошибка получения услуги по id={Id}", petId); - throw; - } - } - [HttpGet] public List GetPets(int ownerId) { try @@ -42,7 +27,7 @@ namespace VeterinaryRestApi.Controllers } catch (Exception ex) { - _logger.LogError(ex, "Ошибка получения списка услуг"); + _logger.LogError(ex, "Ошибка получения списка животных"); throw; } } @@ -55,7 +40,7 @@ namespace VeterinaryRestApi.Controllers } catch (Exception ex) { - _logger.LogError(ex, "Не удалось создать услугу"); + _logger.LogError(ex, "Не удалось создать животного"); throw; } } @@ -65,12 +50,11 @@ namespace VeterinaryRestApi.Controllers { try { - model.Pets = null!; return _pet.Update(model); } catch (Exception ex) { - _logger.LogError(ex, "Не удалось обновить услугу"); + _logger.LogError(ex, "Не удалось обновить животное"); throw; } } @@ -84,7 +68,7 @@ namespace VeterinaryRestApi.Controllers } catch (Exception ex) { - _logger.LogError(ex, "Ошибка удаления услуги"); + _logger.LogError(ex, "Ошибка удаления животного"); throw; } } diff --git a/VeterinaryView/VeterinaryRestApi/Controllers/PurchaseController.cs b/VeterinaryView/VeterinaryRestApi/Controllers/PurchaseController.cs new file mode 100644 index 0000000..33aafb9 --- /dev/null +++ b/VeterinaryView/VeterinaryRestApi/Controllers/PurchaseController.cs @@ -0,0 +1,63 @@ +using Microsoft.AspNetCore.Mvc; +using VeterinaryContracts.BindingModels; +using VeterinaryContracts.BusinessLogicContracts; +using VeterinaryContracts.SearchModels; +using VeterinaryContracts.ViewModels; + +namespace VeterinaryRestApi.Controllers +{ + [Route("api/[controller]/[action]")] + [ApiController] + public class PurchaseController : Controller + { + private readonly ILogger _logger; + private readonly IPurchaseLogic _purchase; + public PurchaseController(ILogger logger, IPurchaseLogic purchase) + { + _logger = logger; + _purchase = purchase; + } + [HttpGet] + public Tuple>? GetPurchase(int purchaseId) + { + try + { + var elem = _purchase.ReadElement(new PurchaseSearchModel { Id = purchaseId }); + if (elem == null) + return null; + return Tuple.Create(elem, elem.PurchasePet.Select(x => x.Value.PetName).ToList()); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения покупки по id={Id}", purchaseId); + throw; + } + } + [HttpGet] + public List GetPurchases() + { + try + { + return _purchase.ReadList(null); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения списка покупок"); + throw; + } + } + [HttpPost] + public bool CreatePurchase(PurchaseBindingModel model) + { + try + { + return _purchase.CreatePurchase(model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Не удалось создать покупку"); + throw; + } + } + } +} diff --git a/VeterinaryView/VeterinaryRestApi/Controllers/VisitController.cs b/VeterinaryView/VeterinaryRestApi/Controllers/VisitController.cs new file mode 100644 index 0000000..e171e8d --- /dev/null +++ b/VeterinaryView/VeterinaryRestApi/Controllers/VisitController.cs @@ -0,0 +1,64 @@ +using Microsoft.AspNetCore.Mvc; +using VeterinaryContracts.BindingModels; +using VeterinaryContracts.BusinessLogicContracts; +using VeterinaryContracts.SearchModels; +using VeterinaryContracts.ViewModels; +using VeterinaryDatabaseImplement.Models; + +namespace VeterinaryRestApi.Controllers +{ + [Route("api/[controller]/[action]")] + [ApiController] + public class VisitController : Controller + { + private readonly ILogger _logger; + private readonly IVisitLogic _visit; + public VisitController(ILogger logger, IVisitLogic visit) + { + _logger = logger; + _visit = visit; + } + [HttpGet] + public Tuple>? GetVisit(int visitId) + { + try + { + var elem = _visit.ReadElement(new VisitSearchModel { Id = visitId }); + if (elem == null) + return null; + return Tuple.Create(elem, elem.VisitPet.Select(x => x.Value.PetName).ToList()); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения визита по id={Id}", visitId); + throw; + } + } + [HttpGet] + public List GetVisits(int ownerId) + { + try + { + return _visit.ReadList(new VisitSearchModel { OwnerId = ownerId }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения списка визитов"); + throw; + } + } + [HttpPost] + public bool CreateVisit(VisitBindingModel model) + { + try + { + return _visit.CreateVisit(model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Не удалось создать визит"); + throw; + } + } + } +} diff --git a/VeterinaryView/VeterinaryShowOwnerApp/Controllers/HomeController.cs b/VeterinaryView/VeterinaryShowOwnerApp/Controllers/HomeController.cs index 85489f8..d0d6cb5 100644 --- a/VeterinaryView/VeterinaryShowOwnerApp/Controllers/HomeController.cs +++ b/VeterinaryView/VeterinaryShowOwnerApp/Controllers/HomeController.cs @@ -4,7 +4,10 @@ using VeterinaryContracts.BindingModels; using VeterinaryContracts.ViewModels; using VeterinaryShowOwnerApp.Models; using VeterinaryShowOwnerApp; - +using System.Reflection; +using VeterinaryDataModels.Models; +using VeterinaryContracts.SearchModels; +using Microsoft.Extensions.Hosting; namespace VeterinaryShowOwnerApp.Controllers { @@ -124,7 +127,7 @@ namespace VeterinaryShowOwnerApp.Controllers } if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(type) || string.IsNullOrEmpty(breed) || string.IsNullOrEmpty(gender)) { - throw new Exception("Ошибка введённых данных"); + throw new Exception("Ошибка в введённых данных"); } APIOwner.PostRequest("api/pet/createpet", new PetBindingModel { @@ -190,7 +193,7 @@ namespace VeterinaryShowOwnerApp.Controllers Response.Redirect("Index"); } [HttpGet] - public Tuple? GetPet(int petId) + public Tuple>? GetPet(int petId) { if (APIOwner.Owner == null) { @@ -201,16 +204,240 @@ namespace VeterinaryShowOwnerApp.Controllers { return default; } - string table = ""; - result.Item1.MedicineAnimals.Clear(); - for (int i = 0; i < result.Item2.Count; i++) + return result; + } + public IActionResult Visits() + { + if (APIOwner.Owner == null) { - var animal = result.Item2[i]; - table += ""; - table += $"{animal}"; - table += ""; + return Redirect("~/Home/Enter"); } - return Tuple.Create(result.Item1, table); + return View(APIOwner.GetRequest>($"api/visit/getvisit?ownerid={APIOwner.Owner.Id}")); + + } + public IActionResult CreateVisit() + { + if (APIOwner.Owner == null) + { + return Redirect("~/Home/Enter"); + } + ViewBag.Pets = APIOwner.GetRequest>($"api/pet/getpet"); + + return View(); + } + [HttpPost] + public void CreateVisit(string name, List pets, DateTime dateTime) + { + if (APIOwner.Owner == null) + { + throw new Exception("Вы как суда попали? Суда вход только авторизованным"); + } + if (string.IsNullOrEmpty(name)) + { + throw new Exception("Ошибка в введенных данных"); + } + Dictionary a = new Dictionary(); + foreach (int pet in pets) + { + a.Add(pet, new PetSearchModel { Id = pet } as IPetModel); + } + APIOwner.PostRequest("api/visit/createvisit", new VisitBindingModel + { + VisitName = name, + VisitPet = a, + DateVisit = dateTime, + + }); + Response.Redirect("Index"); + } + public IActionResult DeleteVisit() + { + if (APIOwner.Owner == null) + { + return Redirect("~/Home/Enter"); + } + ViewBag.Visits = APIOwner.GetRequest>($"api/visit/getvisit?ownerid={APIOwner.Owner.Id}"); + return View(); + } + + [HttpPost] + public void DeleteDrug(int visit) + { + if (APIOwner.Owner == null) + { + throw new Exception("Вы как суда попали? Суда вход только авторизованным"); + } + APIOwner.PostRequest("api/visit/deletevisit", new VisitBindingModel + { + Id = visit + }); + Response.Redirect("Index"); + } + public IActionResult UpdateVisit() + { + if (APIOwner.Owner == null) + { + return Redirect("~/Home/Enter"); + } + ViewBag.Visits = APIOwner.GetRequest>($"api/visit/getvisits?ownerid={APIOwner.Owner.Id}"); + ViewBag.Pets = APIOwner.GetRequest>($"api/pet/getpet"); + return View(); + } + + [HttpPost] + public void UpdateVisit(int visit, string name, List pets) + { + if (APIOwner.Owner == null) + { + throw new Exception("Вы как суда попали? Суда вход только авторизованным"); + } + if (string.IsNullOrEmpty(name)) + { + throw new Exception("Ошибка в введенных данных"); + } + Dictionary a = new Dictionary(); + foreach (int pet in pets) + { + a.Add(pet, new PetSearchModel { Id = pet } as IPetModel); + } + APIOwner.PostRequest("api/visit/updatevisit", new VisitBindingModel + { + Id = visit, + VisitName = name, + VisitPet = a + }); + Response.Redirect("Index"); + } + [HttpGet] + public Tuple>? GetVisit(int visitId) + { + if (APIOwner.Owner == null) + { + throw new Exception("Вы как суда попали? Суда вход только авторизованным"); + } + var result = APIOwner.GetRequest>>($"api/visit/getvisit?visitid={visitId}"); + if (result == null) + { + return default; + } + return result; + } + public IActionResult Purchase() + { + if (APIOwner.Owner == null) + { + return Redirect("~/Home/Enter"); + } + return View(APIOwner.GetRequest>($"api/purchase/getpyrchase?ownerid={APIOwner.Owner.Id}")); + + } + public IActionResult CreatePurchase() + { + if (APIOwner.Owner == null) + { + return Redirect("~/Home/Enter"); + } + ViewBag.Pets = APIOwner.GetRequest>($"api/pet/getpets"); + return View(); + } + [HttpPost] + public void CreatePurchase(int drug, List pets, int count) + { + if (APIOwner.Owner == null) + { + throw new Exception("Вы как суда попали? Суда вход только авторизованным"); + } + if (count <= 0) + { + throw new Exception("Количество должно быть выше 0"); + } + Dictionary a = new Dictionary(); + foreach (int pet in pets) + { + a.Add(pet, new PetSearchModel { Id = pet } as IPetModel); + } + APIOwner.PostRequest("api/purchase/createpurchase", new PurchaseBindingModel + { + OwnerId = APIOwner.Owner.Id, + DrugId = drug, + PurchasePet = a, + Sum = Calc(count, drug) + }); + Response.Redirect("Index"); + } + [HttpPost] + public double Calc(int count, int drug) + { + var dru = APIOwner.GetRequest($"api/main/getdrug?drugId={drug}" + ); + return count * (dru?.Price ?? 1); + } + public IActionResult DeletePurchase() + { + if (APIOwner.Owner == null) + { + return Redirect("~/Home/Enter"); + } + ViewBag.Purchases = APIOwner.GetRequest>($"api/purchase/getpurchases?ownerid={APIOwner.Owner.Id}"); + return View(); + } + [HttpPost] + public void DeletePurchase(int purchase) + { + if (APIOwner.Owner == null) + { + throw new Exception("Вы как суда попали? Суда вход только авторизованным"); + } + APIOwner.PostRequest("api/purchase/deletepurchase", new PurchaseBindingModel + { + Id = purchase + }); + Response.Redirect("Index"); + } + public IActionResult UpdateService() + { + if (APIOwner.Owner == null) + { + return Redirect("~/Home/Enter"); + } + ViewBag.Purchases = APIOwner.GetRequest>($"api/purchase/getpurchases?ownerid={APIOwner.Owner.Id}"); + ViewBag.Pets = APIOwner.GetRequest>($"api/pet/getpets"); + return View(); + } + [HttpPost] + public void UpdatePurchase(int drug, List pets, int count) + { + if (APIOwner.Owner == null) + { + throw new Exception("Вы как суда попали? Суда вход только авторизованным"); + } + Dictionary a = new Dictionary(); + foreach (int pet in pets) + { + a.Add(pet, new PetSearchModel { Id = pet } as IPetModel); + } + APIOwner.PostRequest("api/purchase/updatepurchase", new PurchaseBindingModel + { + OwnerId = APIOwner.Owner.Id, + DrugId = drug, + PurchasePet = a, + Sum = Calc(count, drug) + }); + Response.Redirect("Index"); + } + [HttpGet] + public Tuple>? GetPurchase(int purchaseId) + { + if (APIOwner.Owner == null) + { + throw new Exception("Вы как суда попали? Суда вход только авторизованным"); + } + var result = APIOwner.GetRequest>>($"api/purchase/getpurchase?purchaseid={purchaseId}"); + if (result == null) + { + return default; + } + return result; } } } \ No newline at end of file diff --git a/VeterinaryView/VeterinaryShowOwnerApp/VeterinaryShowOwnerApp.csproj b/VeterinaryView/VeterinaryShowOwnerApp/VeterinaryShowOwnerApp.csproj index 0f888e1..d5d3580 100644 --- a/VeterinaryView/VeterinaryShowOwnerApp/VeterinaryShowOwnerApp.csproj +++ b/VeterinaryView/VeterinaryShowOwnerApp/VeterinaryShowOwnerApp.csproj @@ -8,6 +8,7 @@ + @@ -26,4 +27,8 @@ + + + + diff --git a/VeterinaryView/VeterinaryShowOwnerApp/Views/Home/CreatePurchase.cshtml b/VeterinaryView/VeterinaryShowOwnerApp/Views/Home/CreatePurchase.cshtml new file mode 100644 index 0000000..b8679f8 --- /dev/null +++ b/VeterinaryView/VeterinaryShowOwnerApp/Views/Home/CreatePurchase.cshtml @@ -0,0 +1,6 @@ +namespace VeterinaryShowOwnerApp.Views.Home +{ + public class CreatePurchase + { + } +} diff --git a/VeterinaryView/VeterinaryShowOwnerApp/Views/Home/CreateVisit.cshtml b/VeterinaryView/VeterinaryShowOwnerApp/Views/Home/CreateVisit.cshtml new file mode 100644 index 0000000..75b519b --- /dev/null +++ b/VeterinaryView/VeterinaryShowOwnerApp/Views/Home/CreateVisit.cshtml @@ -0,0 +1,38 @@ +@{ + ViewData["Title"] = "CreateVisit"; +} + +
+

Создание посещения

+
+
+
+
Название:
+
+ +
+
+
+
Дата:
+
+ +
+
+
+
Животные:
+
+ +
+
+
+
+
+ +
+
+