From a3bd2346ca407a85d078aaea37464cece3beaa06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=BE=D0=BB=D0=B0=D0=B9?= Date: Tue, 16 May 2023 14:38:49 +0400 Subject: [PATCH] =?UTF-8?q?CRUD=20=D0=B4=D0=BB=D1=8F=20=D0=BA=D0=BE=D0=BC?= =?UTF-8?q?=D0=BC=D0=B5=D0=BD=D1=82=D0=B0=D1=80=D0=B8=D0=B5=D0=B2=20=D0=B3?= =?UTF-8?q?=D0=BE=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/Worker/BuildStorage.cs | 4 - .../Implements/Worker/CommentStorage.cs | 5 +- .../Controllers/BuildController.cs | 5 +- .../Controllers/CommentController.cs | 75 +++++++++- .../Controllers/HomeController.cs | 107 +++++++++++++-- .../Views/Home/Builds.cshtml | 13 +- .../Views/Home/Comments.cshtml | 128 ++++++++++++++++-- 7 files changed, 302 insertions(+), 35 deletions(-) diff --git a/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/BuildStorage.cs b/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/BuildStorage.cs index 2d4f1a8..522a159 100644 --- a/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/BuildStorage.cs +++ b/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/BuildStorage.cs @@ -56,7 +56,6 @@ namespace HardwareShopDatabaseImplement.Implements.Worker return context.Builds .Include(x => x.Purchases) .ThenInclude(x => x.Purchase) - .Where(x => x.Id == model.Id) .FirstOrDefault(x => (!string.IsNullOrEmpty(model.BuildName) && x.BuildName == model.BuildName) || (model.Id.HasValue && x.Id == model.Id)) ?.GetViewModel; @@ -75,7 +74,6 @@ namespace HardwareShopDatabaseImplement.Implements.Worker return context.Builds .Include(x => x.Purchases) .ThenInclude(x => x.Purchase) - .Where(x => x.UserId == model.Id) .FirstOrDefault(x => x.Id == newBuild.Id) ?.GetViewModel; } @@ -89,7 +87,6 @@ namespace HardwareShopDatabaseImplement.Implements.Worker var build = context.Builds .Include(x => x.Purchases) .ThenInclude(x => x.Purchase) - .Where(x => x.UserId == model.UserId) .FirstOrDefault(x => x.Id == model.Id); if (build == null) { @@ -114,7 +111,6 @@ namespace HardwareShopDatabaseImplement.Implements.Worker var element = context.Builds .Include(x => x.Purchases) .ThenInclude(x => x.Purchase) - .Where(x => x.UserId == model.Id) .FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { diff --git a/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/CommentStorage.cs b/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/CommentStorage.cs index cf3df50..81c91ba 100644 --- a/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/CommentStorage.cs +++ b/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/CommentStorage.cs @@ -63,7 +63,10 @@ namespace HardwareShopDatabaseImplement.Implements.Worker } context.Comments.Add(newComment); context.SaveChanges(); - return newComment.GetViewModel; + return context.Comments + .Include(x => x.Build) + .FirstOrDefault(x => x.Id == newComment.Id) + ?.GetViewModel; } public CommentViewModel? Update(CommentBindingModel model) diff --git a/HardwareShop/HardwareShopRestApi/Controllers/BuildController.cs b/HardwareShop/HardwareShopRestApi/Controllers/BuildController.cs index abcbde9..e2dd255 100644 --- a/HardwareShop/HardwareShopRestApi/Controllers/BuildController.cs +++ b/HardwareShop/HardwareShopRestApi/Controllers/BuildController.cs @@ -23,7 +23,7 @@ namespace HardwareShopRestApi.Controllers _buildLogic = buildLogic; } - [HttpGet] + [HttpGet] public List? GetBuilds(int userId) { try @@ -35,11 +35,12 @@ namespace HardwareShopRestApi.Controllers } catch (Exception ex) { - _logger.LogError(ex, "Ошибка получения списка сборок"); + _logger.LogError(ex, "Ошибка получения списка сборок пользоватля"); throw; } } + [HttpGet] public BuildViewModel? GetBuild(int buildId) { diff --git a/HardwareShop/HardwareShopRestApi/Controllers/CommentController.cs b/HardwareShop/HardwareShopRestApi/Controllers/CommentController.cs index 9b1361f..64aa4ca 100644 --- a/HardwareShop/HardwareShopRestApi/Controllers/CommentController.cs +++ b/HardwareShop/HardwareShopRestApi/Controllers/CommentController.cs @@ -19,5 +19,78 @@ namespace HardwareShopRestApi.Controllers _logger = logger; _commentLogic = commentLogic; } - } + + [HttpGet] + public List? GetComments(int userId) + { + try + { + return _commentLogic.ReadList(new CommentSearchModel + { + UserId = userId + }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения списка комментариев пользоватля"); + throw; + } + } + + [HttpGet] + public CommentViewModel? GetComment(int commentId) + { + try + { + return _commentLogic.ReadElement(new() { Id = commentId }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка чтения комментария"); + throw; + } + } + + [HttpPost] + public void Create(CommentBindingModel model) + { + try + { + _commentLogic.Create(model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка создания комментария"); + throw; + } + } + + [HttpPost] + public void Update(CommentBindingModel model) + { + try + { + _commentLogic.Update(model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка обновления комментария"); + throw; + } + } + + [HttpPost] + public void Delete(CommentBindingModel model) + { + try + { + _commentLogic.Delete(model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка удаления комментария"); + throw; + } + } + } } diff --git a/HardwareShop/HardwareShopWorkerApp/Controllers/HomeController.cs b/HardwareShop/HardwareShopWorkerApp/Controllers/HomeController.cs index 5a0fc62..e33e5dc 100644 --- a/HardwareShop/HardwareShopWorkerApp/Controllers/HomeController.cs +++ b/HardwareShop/HardwareShopWorkerApp/Controllers/HomeController.cs @@ -6,6 +6,7 @@ using HardwareShopDataModels.Enums; using HardwareShopWorkerApp.Models; using Microsoft.AspNetCore.Mvc; using System.Diagnostics; +using static System.Net.Mime.MediaTypeNames; namespace HardwareShopWorkerApp.Controllers { @@ -137,8 +138,7 @@ namespace HardwareShopWorkerApp.Controllers APIClient.PostRequest("api/build/update", new BuildBindingModel { Id = buildId, - BuildName = name, - UserId = APIClient.User.Id + BuildName = name }); Response.Redirect("Builds"); } @@ -157,22 +157,112 @@ namespace HardwareShopWorkerApp.Controllers } APIClient.PostRequest("api/build/DeleteBuild", new BuildBindingModel { - Id = deleteBuildId, - UserId = APIClient.User.Id + Id = deleteBuildId }); Response.Redirect("Builds"); } + public IActionResult MainWorker() { return View(); } public IActionResult Comments() - { - return View(); + { + if (APIClient.User == null) + { + return Redirect("~/Home/Enter"); + } + ViewBag.Builds = APIClient.GetRequest>($"api/build/getbuilds?userId={APIClient.User.Id}"); + return View(APIClient.GetRequest>($"api/comment/getcomments?userId={APIClient.User.Id}")); } - public IActionResult listComponents() + [HttpPost] + public void CreateComment(int buildId, string text) + { + if (APIClient.User == null) + { + throw new Exception("Вы как суда попали? Суда вход только авторизованным"); + } + if (string.IsNullOrEmpty(text)) + { + throw new Exception($"Текст не должен быть пустым"); + } + if (buildId <= 0) + { + throw new Exception($"Идентификатор сборки должен быть больше 0"); + } + APIClient.PostRequest("api/comment/create", new CommentBindingModel + { + BuildId = buildId, + UserId = APIClient.User.Id, + Text = text + }); + Response.Redirect("Comments"); + } + + [HttpGet] + public CommentViewModel GetComment(int commentId) + { + if (APIClient.User == null) + { + throw new Exception("Вы как суда попали? Суда вход только авторизованным"); + } + if (commentId <= 0) + { + throw new Exception($"Идентификтаор комментария не может быть ниже или равен 0"); + } + var result = APIClient.GetRequest($"api/comment/getcomment?commentId={commentId}"); + if (result == null) + { + return null; + } + return result; + } + + [HttpPost] + public void UpdateComment(string text, int commentId) + { + if (APIClient.User == null) + { + throw new Exception("Вы как суда попали? Суда вход только авторизованным"); + } + if (string.IsNullOrEmpty(text)) + { + throw new Exception($"Текст комментария не должно быть пустым"); + } + if (commentId <= 0) + { + throw new Exception($"Идентификтаор комментария не может быть ниже или равен 0"); + } + APIClient.PostRequest("api/comment/update", new CommentBindingModel + { + Id = commentId, + Text = text + }); + Response.Redirect("Comments"); + } + + + [HttpPost] + public void DeleteComment(int deleteCommentId) + { + if (APIClient.User == null) + { + throw new Exception("Вы как суда попали? Суда вход только авторизованным"); + } + if (deleteCommentId <= 0) + { + throw new Exception($"Идентификтаор комментария не может быть ниже или равен 0"); + } + APIClient.PostRequest("api/comment/delete", new CommentBindingModel + { + Id = deleteCommentId, + }); + Response.Redirect("Comments"); + } + + public IActionResult listComponents() { return View(); } @@ -208,8 +298,7 @@ namespace HardwareShopWorkerApp.Controllers { return Redirect("~/Home/Enter"); } - var list = APIClient.GetRequest>($"api/build/getbuilds?userId={APIClient.User.Id}"); - return View(list); + return View(APIClient.GetRequest>($"api/build/getbuilds?userId={APIClient.User.Id}")); } [HttpPost] diff --git a/HardwareShop/HardwareShopWorkerApp/Views/Home/Builds.cshtml b/HardwareShop/HardwareShopWorkerApp/Views/Home/Builds.cshtml index d1ad45e..2fd872c 100644 --- a/HardwareShop/HardwareShopWorkerApp/Views/Home/Builds.cshtml +++ b/HardwareShop/HardwareShopWorkerApp/Views/Home/Builds.cshtml @@ -29,6 +29,9 @@ Название + + Действия + @@ -45,10 +48,10 @@ @Html.DisplayFor(modelItem => item.BuildName) - - - - +
+ + +
} @@ -112,7 +115,7 @@