Ого, вау еще и 5 сделал

This commit is contained in:
ArtemEmelyanov 2023-05-15 21:57:44 +04:00
parent f2a3f2164c
commit b489ac9547
10 changed files with 107 additions and 25 deletions

Binary file not shown.

View File

@ -1,28 +1,29 @@
package ru.IP_LabWorks.IP.University.Contoller.MVC;
import jakarta.validation.Valid;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import ru.IP_LabWorks.IP.University.Contoller.DTO.GroupDTO;
import ru.IP_LabWorks.IP.University.Contoller.DTO.StudentDTO;
import ru.IP_LabWorks.IP.University.Model.Student;
import ru.IP_LabWorks.IP.University.Model.Subject;
import ru.IP_LabWorks.IP.University.Service.GroupService;
import ru.IP_LabWorks.IP.University.Service.StudentService;
import ru.IP_LabWorks.IP.University.Service.SubjectService;
import java.util.List;
import java.util.stream.Collectors;
@Controller
@RequestMapping("/group")
public class GroupMvcController {
private final GroupService groupService;
private final SubjectService subjectService;
public GroupMvcController(GroupService groupService)
public GroupMvcController(GroupService groupService, SubjectService subjectService)
{
this.groupService = groupService;
this.subjectService = subjectService;
}
@GetMapping
@ -69,6 +70,7 @@ public class GroupMvcController {
return "redirect:/group";
}
@GetMapping("/students/{id}")
public String getStudentsFromGroup(@PathVariable Long id, Model model) {
List<Student> students = groupService.getStudentFromGroup(id);
@ -82,4 +84,45 @@ public class GroupMvcController {
model.addAttribute("subjects", subjects);
return "view-subjects";
}
@GetMapping("/getStudentsUndefined/{id}")
public String getStudentsFromUndefinedGroup(@PathVariable Long id, Model model){
List<Student> students = groupService.getStudentsUndefined();
model.addAttribute("undefinedStudents", students);
model.addAttribute("groupId", id);
model.addAttribute("groupDTO", new GroupDTO(groupService.findGroup(id)));
return "add-students-to-group";
}
@PostMapping("/addStudents/{id}")
public String addStudentToGroup(@PathVariable Long id,
@RequestParam("studentId") List<String> studentsIds){
List<Long> studentIdsAsLong = studentsIds.stream()
.map(Long::parseLong)
.collect(Collectors.toList());
groupService.addStudentToGroup(id, studentIdsAsLong);
return "redirect:/group";
}
@PostMapping("/deleteStudentFromGroup/{id}")
public String deleteStudentFromGroup(@PathVariable Long id) {
groupService.deleteStudentFromGroup(id);
return "redirect:/group";
}
@GetMapping("/addSubjectToGroup/{id}")
public String addSubjectToGroupForm(@PathVariable Long id, Model model) {
model.addAttribute("groupDTO", new GroupDTO(groupService.findGroup(id)));
model.addAttribute("groupId", id);
model.addAttribute("subjects", subjectService.findAllSubjects());
return "add-subject-to-group";
}
@PostMapping("/addSubjectToGroup/{id}")
public String addSubjectToGroup(@PathVariable Long id,
@RequestParam("subjectId") List<Long> subjectIds) {
groupService.addSubjectToGroup(id, subjectIds);
return "redirect:/group";
}
}

View File

@ -57,9 +57,9 @@ public class StudentMvcController {
return "student-edit";
}
if (id == null || id <= 0) {
studentService.addStudent(studentDTO.getName(), studentDTO.getBirthDate(), studentDTO.getGroupId());
studentService.addStudent(studentDTO.getName(), studentDTO.getBirthDate());
} else {
studentService.updateStudent(id, studentDTO.getName(), studentDTO.getBirthDate(), studentDTO.getGroupId());
studentService.updateStudent(id, studentDTO.getName(), studentDTO.getBirthDate());
}
return "redirect:/student";
}

View File

@ -7,11 +7,13 @@ import ru.IP_LabWorks.IP.University.Service.GroupService;
import ru.IP_LabWorks.IP.University.Service.StudentService;
import jakarta.validation.Valid;
import ru.IP_LabWorks.IP.WebConfiguration;
import java.time.LocalDate;
import java.util.List;
@RestController
@RequestMapping("/student")
@RequestMapping(WebConfiguration.REST_API + "/student")
public class StudentController {
private final StudentService studentService;
private final GroupService groupService;

View File

@ -0,0 +1,37 @@
<!DOCTYPE html>
<html lang="en"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{default}">
<head>
</head>
<body>
<div layout:fragment="content">
<div th:text="${errors}" class="margin-bottom alert-danger"></div>
<form action="#" th:action="@{/group/addStudents/{id}(id=${groupId})}" method="post">
<div class="mb-3">
<label for="name" class="form-label">Название</label>
<input type="text" class="form-control" id="name" th:field="${groupDTO.name}" readonly>
</div>
<div class="form-group">
<label for="name">Выберите студентов:</label>
<ul class="list-group">
<li class="list-group-item" th:each="student, iterator: ${undefinedStudents}">
<div class="form-check">
<input class="form-check-input" type="checkbox" name="studentId" th:value="${student.id}">
<label class="form-check-label" th:text="${student.name}"></label>
</div>
</li>
</ul>
</div>
<div class="mb-3">
<button type="submit" class="btn btn-primary button-fixed">
<span>Добавить</span>
</button>
<a class="btn btn-secondary button-fixed" th:href="@{/group}">
Назад
</a>
</div>
</form>
</div>
</body>
</html>

View File

@ -7,19 +7,19 @@
<body>
<div layout:fragment="content">
<div th:text="${errors}" class="margin-bottom alert-danger"></div>
<form action="#" th:action="@{/subject/addSubjectToGroup/{id}(id=${subjectId})}" method="post">
<form action="#" th:action="@{/group/addSubjectToGroup/{id}(id=${groupId})}" method="post">
<div class="mb-3">
<label for="name" class="form-label">Название</label>
<input type="text" class="form-control" id="name" th:field="${subjectDTO.name}" readonly>
<input type="text" class="form-control" id="name" th:field="${groupDTO.name}" readonly>
</div>
<div class="form-group">
<label for="name">Выберите группы для добавления:</label>
<label for="name">Выберите предметы для добавления:</label>
<ul class="list-group">
<li class="list-group-item" th:each="group, iterator: ${groups}">
<li class="list-group-item" th:each="subject, iterator: ${subjects}">
<div class="form-check">
<input class="form-check-input" type="checkbox" th:id="'groupId-' + ${group.id}"
th:value="${group.id}" th:checked="${subjectDTO.groupIds.contains(group.id)}" name="groupId">
<label class="form-check-label" th:for="'groupId-' + ${group.id}" th:text="${group.name}"></label>
<input class="form-check-input" type="checkbox" th:id="'subjectId-' + ${subject.id}"
th:value="${subject.id}" th:checked="${groupDTO.subjectIds.contains(subject.id)}" name="subjectId">
<label class="form-check-label" th:for="'subjectId-' + ${subject.id}" th:text="${subject.name}"></label>
</div>
</li>
</ul>
@ -28,7 +28,7 @@
<button type="submit" class="btn btn-primary button-fixed">
<span>Добавить</span>
</button>
<a class="btn btn-secondary button-fixed" th:href="@{/subject}">
<a class="btn btn-secondary button-fixed" th:href="@{/group}">
Назад
</a>
</div>

View File

@ -34,6 +34,10 @@
</button>
<a class="btn btn-primary button-fixed button-sm"
th:href="@{/group/students/{id}(id=${group.id})}">Посмотреть студентов</a>
<a class="btn btn-primary button-fixed button-sm"
th:href="@{/group/getStudentsUndefined/{id}(id=${group.id})}">Добавить студентов</a>
<a class="btn btn-primary button-fixed button-sm"
th:href="@{/group/addSubjectToGroup/{id}(id=${group.id})}"> Добавить предметы</a>
<a class="btn btn-primary button-fixed button-sm"
th:href="@{/group/subjects/{id}(id=${group.id})}">Посмотреть предметы</a>
</div>

View File

@ -16,12 +16,6 @@
<label for="birthDate" class="form-label">Дата рождения</label>
<input type="date" class="form-control" id="birthDate" th:field="${studentDTO.birthDate}" required="true">
</div>
<div class="mb-3">
<label for="group" class="form-label">Группа</label>
<select id="group" class="form-control" th:field="${studentDTO.groupId}">
<option th:each="group : ${Groups}" th:value="${group.id}" th:text="${group.name}"></option>
</select>
</div>
<div class="mb-3">
<button type="submit" class="btn btn-primary button-fixed">
<span th:if="${studentId == null}">Добавить</span>

View File

@ -32,10 +32,6 @@
th:attr="onclick=|confirm('Удалить запись?') && document.getElementById('remove-${subject.id}').click()|">
<i class="fa fa-trash" aria-hidden="true"></i> Удалить
</button>
<a class="btn btn-primary button-fixed button-sm"
th:href="@{/subject/addSubjectToGroup/{id}(id=${subject.id})}">
<i class="fa fa-pencil" aria-hidden="true"></i> Добавить предметы в группы
</a>
</div>
<form th:action="@{/subject/delete/{id}(id=${subject.id})}" method="post">
<button th:id="'remove-' + ${subject.id}" type="submit" style="display: none">

View File

@ -12,12 +12,18 @@
<tr>
<th scope="col">ФИО</th>
<th scope="col">Дата рождения</th>
<th></th>
</tr>
</thead>
<tbody>
<tr th:each="student, iterator: ${students}">
<td th:text="${student.name}"></td>
<td th:text="${student.birthDate}"></td>
<td>
<form method="post" th:action="@{/group/deleteStudentFromGroup/{id}(id=${student.id})}">
<button type="submit" class="btn btn-danger button-fixed button-sm">Удалить</button>
</form>
</td>
</tr>
</tbody>
<a class="btn btn-secondary button-fixed" th:href="@{/group}">