From c87f2caaacfdadbcd904507b90502e49b63a5f99 Mon Sep 17 00:00:00 2001 From: Danil Markov Date: Wed, 17 May 2023 22:54:39 +0400 Subject: [PATCH] =?UTF-8?q?EducationStatus=20crud=20=D1=81=20=D0=B2=D1=8B?= =?UTF-8?q?=D0=B1=D0=BE=D1=80=D0=BE=D0=BC=20=D0=B8=D0=B7=20=D1=81=D0=BF?= =?UTF-8?q?=D0=B8=D1=81=D0=BA=D0=B0=20=C2=AB=D0=A1=D1=82=D1=83=D0=B4=D0=B5?= =?UTF-8?q?=D0=BD=D1=82=D1=8B=C2=BB=20=D0=B7=D0=B0=D0=BF=D0=B8=D1=81=D0=B8?= =?UTF-8?q?=20=D0=BA=20=D0=BD=D0=B5=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/EducationStatusLogic.cs | 5 + .../IEducationStatusLogic.cs | 1 + .../EducationStatusSearchModel.cs | 2 + .../SearchModels/StudentSearchModel.cs | 2 +- .../IEducationStatusStorage.cs | 1 + .../Implements/EducationStatusStorage.cs | 13 ++ .../Implements/StudentStorage.cs | 13 +- .../Controllers/EducationStatusController.cs | 103 ++++++++++++++++ .../Controllers/HomeController.cs | 20 ++- .../Controllers/StudentController.cs | 13 +- .../Views/EducationStatus/AddStudent.cshtml | 58 +++++++++ .../Views/EducationStatus/Create.cshtml | 20 +++ .../Views/EducationStatus/Update.cshtml | 30 +++++ .../Views/Home/EducationStatuses.cshtml | 72 ++++++++++- .../educationstatus-add-student.js | 115 ++++++++++++++++++ .../educationstatus/educationstatus-create.js | 36 ++++++ .../educationstatus/educationstatus-update.js | 38 ++++++ .../js/educationstatus/educationstatuses.js | 25 ++++ .../wwwroot/js/student/student-create.js | 6 +- .../wwwroot/js/student/student-update.js | 7 +- .../wwwroot/js/student/students.js | 8 +- .../Controllers/EducationStatusController.cs | 113 +++++++++++++++-- .../Controllers/StudentController.cs | 21 +++- 23 files changed, 692 insertions(+), 30 deletions(-) create mode 100644 UniversityProvider/Controllers/EducationStatusController.cs create mode 100644 UniversityProvider/Views/EducationStatus/AddStudent.cshtml create mode 100644 UniversityProvider/Views/EducationStatus/Create.cshtml create mode 100644 UniversityProvider/Views/EducationStatus/Update.cshtml create mode 100644 UniversityProvider/wwwroot/js/educationstatus/educationstatus-add-student.js create mode 100644 UniversityProvider/wwwroot/js/educationstatus/educationstatus-create.js create mode 100644 UniversityProvider/wwwroot/js/educationstatus/educationstatus-update.js create mode 100644 UniversityProvider/wwwroot/js/educationstatus/educationstatuses.js diff --git a/UniversityBusinessLogic/BusinessLogics/EducationStatusLogic.cs b/UniversityBusinessLogic/BusinessLogics/EducationStatusLogic.cs index 21a46b2..844df6e 100644 --- a/UniversityBusinessLogic/BusinessLogics/EducationStatusLogic.cs +++ b/UniversityBusinessLogic/BusinessLogics/EducationStatusLogic.cs @@ -71,6 +71,11 @@ namespace UniversityBusinessLogic.BusinessLogics return list; } + public int GetNumberOfPages(int userId, int pageSize = 10) + { + return _esStorage.GetNumberOfPages(userId, pageSize); + } + private void CheckModel(EducationStatusBindingModel model, bool withParams = true) { if (model == null) diff --git a/UniversityContracts/BusinessLogicContracts/IEducationStatusLogic.cs b/UniversityContracts/BusinessLogicContracts/IEducationStatusLogic.cs index 5016c60..32078d2 100644 --- a/UniversityContracts/BusinessLogicContracts/IEducationStatusLogic.cs +++ b/UniversityContracts/BusinessLogicContracts/IEducationStatusLogic.cs @@ -16,5 +16,6 @@ namespace UniversityContracts.BusinessLogicContracts bool Delete(EducationStatusBindingModel model); List? ReadList(EducationStatusSearchModel? model); EducationStatusViewModel? ReadElement(EducationStatusSearchModel model); + public int GetNumberOfPages(int userId, int pageSize = 10); } } diff --git a/UniversityContracts/SearchModels/EducationStatusSearchModel.cs b/UniversityContracts/SearchModels/EducationStatusSearchModel.cs index 2c27b20..7b41030 100644 --- a/UniversityContracts/SearchModels/EducationStatusSearchModel.cs +++ b/UniversityContracts/SearchModels/EducationStatusSearchModel.cs @@ -11,5 +11,7 @@ namespace UniversityContracts.SearchModels public int? Id { get; set; } public string? Name { get; set; } public int? UserId { get; set; } + public int? PageNumber { get; set; } + public int? PageSize { get; set; } } } diff --git a/UniversityContracts/SearchModels/StudentSearchModel.cs b/UniversityContracts/SearchModels/StudentSearchModel.cs index 19649b1..3b4e24a 100644 --- a/UniversityContracts/SearchModels/StudentSearchModel.cs +++ b/UniversityContracts/SearchModels/StudentSearchModel.cs @@ -11,10 +11,10 @@ namespace UniversityContracts.SearchModels public int? Id { get; set; } public int? StudentCard { get; set; } public int? UserId { get; set; } + public int? EducationStatusId { get; set; } public DateTime? DateFrom { get; set; } public DateTime? DateTo { get; set; } public bool? Disciplines { get; set; } - public int? PageNumber { get; set; } public int? PageSize { get; set; } } diff --git a/UniversityContracts/StoragesContracts/IEducationStatusStorage.cs b/UniversityContracts/StoragesContracts/IEducationStatusStorage.cs index 9d69479..14c90f6 100644 --- a/UniversityContracts/StoragesContracts/IEducationStatusStorage.cs +++ b/UniversityContracts/StoragesContracts/IEducationStatusStorage.cs @@ -17,5 +17,6 @@ namespace UniversityContracts.StoragesContracts EducationStatusViewModel? Insert(EducationStatusBindingModel model); EducationStatusViewModel? Update(EducationStatusBindingModel model); EducationStatusViewModel? Delete(EducationStatusBindingModel model); + public int GetNumberOfPages(int userId, int pageSize = 10); } } diff --git a/UniversityDataBaseImplemet/Implements/EducationStatusStorage.cs b/UniversityDataBaseImplemet/Implements/EducationStatusStorage.cs index e38110e..1652349 100644 --- a/UniversityDataBaseImplemet/Implements/EducationStatusStorage.cs +++ b/UniversityDataBaseImplemet/Implements/EducationStatusStorage.cs @@ -27,6 +27,7 @@ namespace UniversityDataBaseImplemet.Implements || record.Name.Equals(model.Name)) ?.GetViewModel; } + public List GetFilteredList(EducationStatusSearchModel model) { using var context = new Database(); @@ -51,6 +52,7 @@ namespace UniversityDataBaseImplemet.Implements return new(); } } + public List GetFullList() { using var context = new Database(); @@ -59,6 +61,7 @@ namespace UniversityDataBaseImplemet.Implements .Select(record => record.GetViewModel) .ToList(); } + public EducationStatusViewModel? Insert(EducationStatusBindingModel model) { var newEducationStatus = EducationStatus.Create(model); @@ -74,6 +77,7 @@ namespace UniversityDataBaseImplemet.Implements .FirstOrDefault(record => record.Id.Equals(newEducationStatus.Id)) ?.GetViewModel; } + public EducationStatusViewModel? Update(EducationStatusBindingModel model) { using var context = new Database(); @@ -98,6 +102,7 @@ namespace UniversityDataBaseImplemet.Implements throw; } } + public EducationStatusViewModel? Delete(EducationStatusBindingModel model) { using var context = new Database(); @@ -112,5 +117,13 @@ namespace UniversityDataBaseImplemet.Implements context.SaveChanges(); return educationStatus.GetViewModel; } + + public int GetNumberOfPages(int userId, int pageSize) + { + using var context = new Database(); + int carsCount = context.Students.Where(c => c.UserId == userId).Count(); + int numberOfpages = (int)Math.Ceiling((double)carsCount / pageSize); + return numberOfpages != 0 ? numberOfpages : 1; + } } } diff --git a/UniversityDataBaseImplemet/Implements/StudentStorage.cs b/UniversityDataBaseImplemet/Implements/StudentStorage.cs index d515b19..6bddfd3 100644 --- a/UniversityDataBaseImplemet/Implements/StudentStorage.cs +++ b/UniversityDataBaseImplemet/Implements/StudentStorage.cs @@ -43,7 +43,9 @@ namespace UniversityDataBaseImplemet.Implements if (model.UserId.HasValue && model.PageNumber.HasValue && model.PageSize.HasValue) { return context.Students - .Where(x => x.UserId == model.UserId) + .Include(record => record.User) + .Include(record => record.EducationStatus) + .Where(x => x.UserId == model.UserId) .Skip(model.PageSize.Value * (model.PageNumber.Value - 1)) .Take(model.PageSize.Value) .Select(x => x.GetViewModel) @@ -58,6 +60,15 @@ namespace UniversityDataBaseImplemet.Implements .Select(record => record.GetViewModel) .ToList(); } + else if (model.UserId.HasValue && model.EducationStatusId.HasValue) + { + return context.Students + .Include(record => record.User) + .Include(record => record.EducationStatus) + .Where(record => record.UserId == model.UserId && record.EducationStatusId != model.EducationStatusId) + .Select(record => record.GetViewModel) + .ToList(); + } else if (model.UserId.HasValue) { return context.Students diff --git a/UniversityProvider/Controllers/EducationStatusController.cs b/UniversityProvider/Controllers/EducationStatusController.cs new file mode 100644 index 0000000..b6f77fa --- /dev/null +++ b/UniversityProvider/Controllers/EducationStatusController.cs @@ -0,0 +1,103 @@ +using Microsoft.AspNetCore.Mvc; +using UniversityContracts.BindingModels; +using UniversityContracts.ViewModels; + +namespace UniversityProvider.Controllers +{ + public class EducationStatusController : Controller + { + public IActionResult Create() + { + if (APIClient.User == null) + { + return Redirect("~/Home/Enter"); + } + return View(); + } + + [HttpPost] + public void Create([FromBody] EducationStatusBindingModel educationstatusModel) + { + if (APIClient.User == null) + { + throw new Exception("403"); + } + educationstatusModel.UserId = APIClient.User.Id; + APIClient.PostRequest("api/educationstatus/create", educationstatusModel); + Response.Redirect("/Home/EducationStatuses"); + } + + public IActionResult Update(int id) + { + if (APIClient.User == null) + { + return Redirect("~/Home/Enter"); + } + ViewBag.EducationStatus = APIClient.GetRequest($"api/educationstatus/get?id={id}"); + return View(); + } + + [HttpPost] + public void Update([FromBody] EducationStatusBindingModel educationstatusModel) + { + if (APIClient.User == null) + { + throw new Exception("403"); + } + educationstatusModel.UserId = APIClient.User.Id; + APIClient.PostRequest("api/educationstatus/update", educationstatusModel); + Response.Redirect("/Home/EducationStatuses"); + } + + public IActionResult AddStudent(int id) + { + if (APIClient.User == null) + { + return Redirect("~/Home/Enter"); + } + ViewBag.EducationStatusId = id; + return View(); + } + + [HttpPost] + public void AddStudent([FromBody] StudentBindingModel studentModel) + { + if (APIClient.User == null) + { + throw new Exception("403"); + } + APIClient.PostRequest("api/student/update", studentModel); + } + + [HttpPost] + public void Delete(int id) + { + if (APIClient.User == null) + { + throw new Exception("403"); + } + APIClient.PostRequest($"api/educationstatus/delete", new EducationStatusBindingModel() { Id = id }); + Response.Redirect("/Home/EducationStatuses"); + } + + public List GetAllByUser() + { + if (APIClient.User == null) + { + return new(); + } + List? educationstatus = APIClient.GetRequest>($"api/educationstatus/getallbyuser?userId={APIClient.User.Id}"); + return educationstatus ?? new(); + } + + public EducationStatusViewModel? Get(int id) + { + if (APIClient.User == null) + { + return new(); + } + EducationStatusViewModel? educationstatus = APIClient.GetRequest($"api/educationstatus/get?id={id}"); + return educationstatus; + } + } +} diff --git a/UniversityProvider/Controllers/HomeController.cs b/UniversityProvider/Controllers/HomeController.cs index 9407dfd..b0973b3 100644 --- a/UniversityProvider/Controllers/HomeController.cs +++ b/UniversityProvider/Controllers/HomeController.cs @@ -80,7 +80,25 @@ namespace UniversityProvider.Controllers return View(); } - [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] + public IActionResult EducationStatuses(int page) + { + if (APIClient.User == null) + { + return Redirect("~/Home/Enter"); + } + if (page == 0) + { + page = 1; + } + ViewBag.EducationStatuses = APIClient.GetRequest> + ($"api/educationstatus/getmany?userId={APIClient.User.Id}&page={page}"); + ViewBag.Page = page; + ViewBag.NumberOfPages = APIClient.GetRequest + ($"api/student/getnumberofpages?userId={APIClient.User.Id}"); + return View(); + } + + [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() { return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); diff --git a/UniversityProvider/Controllers/StudentController.cs b/UniversityProvider/Controllers/StudentController.cs index d939b9b..c736b47 100644 --- a/UniversityProvider/Controllers/StudentController.cs +++ b/UniversityProvider/Controllers/StudentController.cs @@ -50,7 +50,7 @@ namespace UniversityProvider.Controllers Response.Redirect("/Home/Students"); } - [HttpPost] + [HttpPost] public void Delete(int id) { if (APIClient.User == null) @@ -71,8 +71,17 @@ namespace UniversityProvider.Controllers return students ?? new(); } + public List GetAllByUserAndEducationStatus(int educationstatus) + { + if (APIClient.User == null) + { + return new(); + } + List? students = APIClient.GetRequest>($"api/student/getallbyuserandeducationstatus?userId={APIClient.User.Id}&educationstatus={educationstatus}"); + return students ?? new(); + } - public StudentViewModel? Get(int id) + public StudentViewModel? Get(int id) { if (APIClient.User == null) { diff --git a/UniversityProvider/Views/EducationStatus/AddStudent.cshtml b/UniversityProvider/Views/EducationStatus/AddStudent.cshtml new file mode 100644 index 0000000..50dd34f --- /dev/null +++ b/UniversityProvider/Views/EducationStatus/AddStudent.cshtml @@ -0,0 +1,58 @@ +@using UniversityContracts.ViewModels + +@{ + ViewData["Title"] = "Статус обучения"; +} + +@{ +

Назначение статуса обучения студенту

+ + if (ViewBag.EducationStatusId == null) + { +

Войдите в аккаунт

+ return; + } + +
+
+ + +
+
+ +
+
+ + +
+
+ + + +
+
+

Итог:

+ + + + + + + + + + + + + + +
Выберите студента!
+
+
+} + + \ No newline at end of file diff --git a/UniversityProvider/Views/EducationStatus/Create.cshtml b/UniversityProvider/Views/EducationStatus/Create.cshtml new file mode 100644 index 0000000..73aa0b6 --- /dev/null +++ b/UniversityProvider/Views/EducationStatus/Create.cshtml @@ -0,0 +1,20 @@ +@{ + ViewData["Title"] = "Статус обучения"; +} + +

Добавление статуса обучения

+ +
+
+

+
+
+ +

Название:

+ + + + + \ No newline at end of file diff --git a/UniversityProvider/Views/EducationStatus/Update.cshtml b/UniversityProvider/Views/EducationStatus/Update.cshtml new file mode 100644 index 0000000..fde41a3 --- /dev/null +++ b/UniversityProvider/Views/EducationStatus/Update.cshtml @@ -0,0 +1,30 @@ +@using UniversityContracts.ViewModels + +@{ + ViewData["Title"] = "Статус обучения"; +} + +@{ + +

Изменение данных статуса обучения

+ + if (ViewBag.EducationStatus == null) + { +

Войдите в аккаунт

+ return; + } +
+
+

+
+
+ +

Название:

+ + + +} + + \ No newline at end of file diff --git a/UniversityProvider/Views/Home/EducationStatuses.cshtml b/UniversityProvider/Views/Home/EducationStatuses.cshtml index e1dd794..0ac08ea 100644 --- a/UniversityProvider/Views/Home/EducationStatuses.cshtml +++ b/UniversityProvider/Views/Home/EducationStatuses.cshtml @@ -1,5 +1,71 @@ -@* - For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 -*@ +@using UniversityContracts.ViewModels + @{ + ViewData["Title"] = "Статусы обучения"; } + +
+

Статусы обучения

+
+ + +
+ @{ + if (ViewBag.EducationStatuses == null) + { +

Войдите в аккаунт

+ return; + } + +
+
+ + / @ViewBag.NumberOfPages +
+ + Перейти + +
+ + + + + + + + + + + @foreach (var item in ViewBag.EducationStatuses) + { + + + + + + + } + +
+ Название + + Выбор студента + + Изменить запись + + Удалить запись +
+ @item.Name + + Назначить статус для студента + + Изменить + + Удалить +
+ } +
+ + diff --git a/UniversityProvider/wwwroot/js/educationstatus/educationstatus-add-student.js b/UniversityProvider/wwwroot/js/educationstatus/educationstatus-add-student.js new file mode 100644 index 0000000..c620601 --- /dev/null +++ b/UniversityProvider/wwwroot/js/educationstatus/educationstatus-add-student.js @@ -0,0 +1,115 @@ +const select = document.getElementById("studentsSelect"); +const educationStatusId = document.getElementById("educationStatusLabel").dataset.id; +const educationStatusInput = document.getElementById("educationStatusInput"); +const studentTable = document.getElementById("studentTable"); +const updateBtn = document.getElementById("update-button"); + +var students = []; +var educationStatus = null; + +window.addEventListener("load", () => { + $.ajax({ + url: `/student/getallbyuserandeducationstatus?educationstatus=${educationStatusId}`, + type: "GET", + contentType: "json" + }).done((result) => { + students = result; + students.forEach((student) => { + createStudentOption(student); + }); + }); + + $.ajax({ + url: `/educationstatus/get?id=${educationStatusId}`, + type: 'GET', + contentType: 'json' + }).done((result) => { + console.log(result); + educationStatus = result; + fillEducationStatusInput(educationStatus); + }); +}); + +updateBtn.addEventListener("click", () => { + if (select.selectedIndex === -1) { + return; + } + + var student = students.find(x => x.id === parseInt(select.value)); + student.educationStatusId = educationStatus.id; + + $.ajax({ + url: "/student/update", + type: "POST", + contentType: "application/json", + data: JSON.stringify(student) + }).done(() => { + window.location.href = "/Home/EducationStatuses"; + }); +}) + +const createStudentOption = (student) => { + const option = document.createElement("option"); + option.value = student.id; + option.innerHTML = student.name + " " + student.surname + ", " + student.studentCard; + select.appendChild(option); + select.selectedIndex = -1; +} + +const fillEducationStatusInput = (educationStatus) => { + educationStatusInput.value = educationStatus.name +} + +select.addEventListener("change", () => { + studentTable.innerHTML = ""; + const trName = document.createElement('tr'); + const trSurname = document.createElement('tr'); + const trStudentCard = document.createElement('tr'); + const trEducationStatusName = document.createElement('tr'); + + var student = students.find(x => x.id === parseInt(select.value)); + + const tdNameField = document.createElement('td'); + tdNameField.innerHTML = "Имя:"; + const tdSurnameField = document.createElement('td'); + tdSurnameField.innerHTML = "Фамилия:"; + const tdStudentCardField = document.createElement('td'); + tdStudentCardField.innerHTML = "Номер студ. билета:"; + const tdEducationStatusNameField = document.createElement('td'); + tdEducationStatusNameField.innerHTML = "Статус обучения:"; + + const tdNewEducationStatusName = document.createElement('td'); + tdNewEducationStatusName.innerHTML = educationStatus.name; + const tdArrowEducationStatusName = document.createElement('td'); + tdArrowEducationStatusName.innerHTML = "--->" + const tdName = document.createElement('td'); + tdName.innerHTML = student.name; + const tdSurname = document.createElement('td'); + tdSurname.innerHTML = student.surname; + const tdStudentCard = document.createElement('td'); + tdStudentCard.innerHTML = student.studentCard; + const tdEducationStatusName = document.createElement('td'); + tdEducationStatusName.innerHTML = student.educationStatusName; + + trName.appendChild(tdNameField); + trName.appendChild(document.createElement('td')); + trName.appendChild(document.createElement('td')); + trName.appendChild(tdName); + trSurname.appendChild(tdSurnameField); + trSurname.appendChild(document.createElement('td')); + trSurname.appendChild(document.createElement('td')); + trSurname.appendChild(tdSurname); + trStudentCard.appendChild(tdStudentCardField); + trStudentCard.appendChild(document.createElement('td')); + trStudentCard.appendChild(document.createElement('td')); + trStudentCard.appendChild(tdStudentCard); + trEducationStatusName.appendChild(tdEducationStatusNameField); + trEducationStatusName.appendChild(tdNewEducationStatusName); + trEducationStatusName.appendChild(tdArrowEducationStatusName); + trEducationStatusName.appendChild(tdEducationStatusName); + + studentTable.appendChild(trName); + studentTable.appendChild(trSurname); + studentTable.appendChild(trStudentCard); + studentTable.appendChild(trEducationStatusName); +}) \ No newline at end of file diff --git a/UniversityProvider/wwwroot/js/educationstatus/educationstatus-create.js b/UniversityProvider/wwwroot/js/educationstatus/educationstatus-create.js new file mode 100644 index 0000000..f82b746 --- /dev/null +++ b/UniversityProvider/wwwroot/js/educationstatus/educationstatus-create.js @@ -0,0 +1,36 @@ +const createBtn = document.getElementById("create-button"); +const nameInput = document.getElementById("name-input"); + +createBtn.addEventListener("click", () => { + if (!correctData()) { + return; + } + if (!validate()) { + return; + } +}); + +const correctData = function () { + + return true; +}; + +const validate = function () { + + return true; +}; + +createBtn.addEventListener("click", () => { + let educationstatus = { + "Name": nameInput.value, + }; + console.log(educationstatus) + $.ajax({ + url: "/educationstatus/create", + type: "POST", + contentType: "application/json", + data: JSON.stringify(educationstatus) + }).done(() => { + window.location.href = "/Home/EducationStatuses"; + }); +}); \ No newline at end of file diff --git a/UniversityProvider/wwwroot/js/educationstatus/educationstatus-update.js b/UniversityProvider/wwwroot/js/educationstatus/educationstatus-update.js new file mode 100644 index 0000000..64cc0ec --- /dev/null +++ b/UniversityProvider/wwwroot/js/educationstatus/educationstatus-update.js @@ -0,0 +1,38 @@ +const updateBtn = document.getElementById("update-button"); +const nameInput = document.getElementById("name-input") +const studId = document.getElementById("vb-id").dataset.id + +updateBtn.addEventListener("click", () => { + if (!correctData()) { + return; + } + if (!validate()) { + return; + } +}); + +const correctData = function () { + + return true; +}; + +const validate = function () { + + return true; +}; + +updateBtn.addEventListener("click", () => { + let educationstatus = { + "Id": parseInt(studId), + "Name": nameInput.value, + }; + console.log(educationstatus) + $.ajax({ + url: "/educationstatus/update", + type: "POST", + contentType: "application/json", + data: JSON.stringify(educationstatus) + }).done(() => { + window.location.href = "/Home/EducationStatuses"; + }); +}); \ No newline at end of file diff --git a/UniversityProvider/wwwroot/js/educationstatus/educationstatuses.js b/UniversityProvider/wwwroot/js/educationstatus/educationstatuses.js new file mode 100644 index 0000000..4736c7c --- /dev/null +++ b/UniversityProvider/wwwroot/js/educationstatus/educationstatuses.js @@ -0,0 +1,25 @@ +const goToPageBtn = document.getElementById("go-button"); +const pageInput = document.getElementById("page-input"); +const removeButtons = document.querySelectorAll(".remove-btn"); + +removeButtons.forEach(function (button) { + button.addEventListener("click", function (event) { + var id = this.dataset.id; + + var result = confirm("Вы уверены, что хотите удалить эту запись?"); + if (result) { + $.ajax({ + url: "/educationstatus/delete", + type: "POST", + data: { Id: id } + }).done(() => { + window.location.reload(); + }); + } + }); +}); + +pageInput.addEventListener("input", () => { + const pageNumber = parseInt(pageInput.value); + goToPageBtn.href = `/Home/Students?page=${pageNumber}`; +}); diff --git a/UniversityProvider/wwwroot/js/student/student-create.js b/UniversityProvider/wwwroot/js/student/student-create.js index e394f13..468d26b 100644 --- a/UniversityProvider/wwwroot/js/student/student-create.js +++ b/UniversityProvider/wwwroot/js/student/student-create.js @@ -32,9 +32,9 @@ createBtn.addEventListener("click", () => { }; console.log(student) $.ajax({ - url: `/student/create`, - type: 'POST', - contentType: 'application/json', + url: "/student/create", + type: "POST", + contentType: "application/json", data: JSON.stringify(student) }).done(() => { window.location.href = "/Home/Students"; diff --git a/UniversityProvider/wwwroot/js/student/student-update.js b/UniversityProvider/wwwroot/js/student/student-update.js index b586bcc..10f83f4 100644 --- a/UniversityProvider/wwwroot/js/student/student-update.js +++ b/UniversityProvider/wwwroot/js/student/student-update.js @@ -12,7 +12,6 @@ updateBtn.addEventListener("click", () => { if (!validate()) { return; } - form.submit(); }); const correctData = function () { @@ -35,9 +34,9 @@ updateBtn.addEventListener("click", () => { }; console.log(student) $.ajax({ - url: `/student/update`, - type: 'POST', - contentType: 'application/json', + url: "/student/update", + type: "POST", + contentType: "application/json", data: JSON.stringify(student) }).done(() => { window.location.href = "/Home/Students"; diff --git a/UniversityProvider/wwwroot/js/student/students.js b/UniversityProvider/wwwroot/js/student/students.js index b7bbb64..052c4dc 100644 --- a/UniversityProvider/wwwroot/js/student/students.js +++ b/UniversityProvider/wwwroot/js/student/students.js @@ -1,16 +1,16 @@ const goToPageBtn = document.getElementById("go-button"); const pageInput = document.getElementById("page-input"); -const removeButtons = document.querySelectorAll('.remove-btn'); +const removeButtons = document.querySelectorAll(".remove-btn"); removeButtons.forEach(function (button) { - button.addEventListener('click', function (event) { + button.addEventListener("click", function (event) { var id = this.dataset.id; var result = confirm("Вы уверены, что хотите удалить эту запись?"); if (result) { $.ajax({ - url: `/student/delete`, - type: 'POST', + url: "/student/delete", + type: "POST", data: { Id: id } }).done(() => { window.location.reload(); diff --git a/UniversityRestAPI/Controllers/EducationStatusController.cs b/UniversityRestAPI/Controllers/EducationStatusController.cs index 1bfbcf2..18714a7 100644 --- a/UniversityRestAPI/Controllers/EducationStatusController.cs +++ b/UniversityRestAPI/Controllers/EducationStatusController.cs @@ -1,12 +1,111 @@ using Microsoft.AspNetCore.Mvc; +using UniversityContracts.BindingModels; +using UniversityContracts.BusinessLogicContracts; +using UniversityContracts.SearchModels; +using UniversityContracts.ViewModels; namespace UniversityRestAPI.Controllers { - public class EducationStatusController : Controller - { - public IActionResult Index() - { - return View(); - } - } + [Route("api/[controller]/[action]")] + [ApiController] + public class EducationStatusController : Controller + { + private readonly IEducationStatusLogic _educationStatusLogic; + + public EducationStatusController(IEducationStatusLogic educationStatusLogic) + { + _educationStatusLogic = educationStatusLogic; + } + + [HttpGet] + public EducationStatusViewModel? Get(int id) + { + try + { + return _educationStatusLogic.ReadElement(new EducationStatusSearchModel { Id = id }); + } + catch (Exception ex) + { + throw; + } + } + + [HttpGet] + public List? GetAllByUser(int userId) + { + try + { + return _educationStatusLogic.ReadList(null); + } + catch (Exception ex) + { + throw; + } + } + + [HttpGet] + public List? GetMany(int userId, int page) + { + try + { + return _educationStatusLogic.ReadList(new EducationStatusSearchModel { UserId = userId, PageNumber = page, PageSize = 10 }); + } + catch (Exception ex) + { + throw; + } + } + + [HttpGet] + public int GetNumberOfPages(int userId) + { + try + { + return _educationStatusLogic.GetNumberOfPages(userId); + } + catch (Exception ex) + { + throw; + } + } + + [HttpPost] + public void Create(EducationStatusBindingModel model) + { + try + { + _educationStatusLogic.Create(model); + } + catch (Exception ex) + { + throw; + } + } + + [HttpPost] + public void Update(EducationStatusBindingModel model) + { + try + { + _educationStatusLogic.Update(model); + } + catch (Exception ex) + { + throw; + } + } + + [HttpPost] + public void Delete(EducationStatusBindingModel model) + { + try + { + _educationStatusLogic.Delete(new() { Id = model.Id }); + } + catch (Exception ex) + { + throw; + } + } + } } diff --git a/UniversityRestAPI/Controllers/StudentController.cs b/UniversityRestAPI/Controllers/StudentController.cs index b8a6ac0..eb2f173 100644 --- a/UniversityRestAPI/Controllers/StudentController.cs +++ b/UniversityRestAPI/Controllers/StudentController.cs @@ -29,13 +29,13 @@ namespace UniversityRestAPI.Controllers throw; } } - - [HttpGet] + + [HttpGet] public List? GetAllByUser(int userId) { try { - return _studentLogic.ReadList(null); + return _studentLogic.ReadList(new StudentSearchModel { UserId = userId }); } catch (Exception ex) { @@ -43,7 +43,20 @@ namespace UniversityRestAPI.Controllers } } - [HttpGet] + [HttpGet] + public List? GetAllByUserAndEducationStatus(int userId, int educationstatus) + { + try + { + return _studentLogic.ReadList(new StudentSearchModel { UserId = userId, EducationStatusId = educationstatus }); + } + catch (Exception ex) + { + throw; + } + } + + [HttpGet] public List? GetMany(int userId, int page) { try