По сути готовая 5 лаба

This commit is contained in:
DyCTaTOR 2024-06-07 00:07:21 +04:00
parent f11901be1f
commit ab784c2d69
8 changed files with 2772 additions and 127 deletions

Binary file not shown.

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,127 @@
package com.example.demo.directions.api;
import java.util.List;
import org.modelmapper.ModelMapper;
import org.modelmapper.TypeToken;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.example.demo.core.configuration.Constants;
import com.example.demo.department.api.DepartmentDto;
import com.example.demo.department.service.DepartmentService;
import com.example.demo.directions.model.DirectionsEntity;
import com.example.demo.directions.service.DirectionsService;
import jakarta.validation.Valid;
@Controller
@RequestMapping(DirectionsAdminController.URL)
public class DirectionsAdminController {
public static final String URL = Constants.ADMIN_PREFIX + "/directions";
private static final String DIRECTIONS_URL = DirectionsController.URL;
private static final String DIRECTIONS_EDIT_VIEW = "direction-edit";
private static final String DIRECTIONS_ATTRIBUTE = "directions";
private static final String DEPARTMENT_ATTRIBUTE = "departments";
private static final String PAGE_ATTRIBUTE = "page";
private final DirectionsService directionsService;
private final ModelMapper modelMapper;
private final DepartmentService departmentService;
public DirectionsAdminController(DirectionsService directionsService, ModelMapper modelMapper,
DepartmentService departmentService) {
this.directionsService = directionsService;
this.modelMapper = modelMapper;
this.departmentService = departmentService;
}
private DirectionsDto toDto(DirectionsEntity entity) {
return modelMapper.map(entity, DirectionsDto.class);
}
private DirectionsEntity toEntity(DirectionsDto dto) {
final DirectionsEntity entity = modelMapper.map(dto, DirectionsEntity.class);
entity.setDepartment(departmentService.get(dto.getDepartmentId()));
return entity;
}
private List<DepartmentDto> getDepartments(){
return modelMapper.map(departmentService.getAll(), new TypeToken<List<DepartmentDto>>(){}.getType());
}
@GetMapping("/edit/")
public String create(Model model,
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page) {
model.addAttribute(DIRECTIONS_ATTRIBUTE, new DirectionsDto());
model.addAttribute(DEPARTMENT_ATTRIBUTE, getDepartments());
model.addAttribute(PAGE_ATTRIBUTE, page);
return DIRECTIONS_EDIT_VIEW;
}
@PostMapping("/edit/")
public String create(
@ModelAttribute(name = DIRECTIONS_ATTRIBUTE) @Valid DirectionsDto direction,
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
BindingResult bindingResult,
Model model,
RedirectAttributes redirectAttributes) {
if (bindingResult.hasErrors()) {
model.addAttribute(PAGE_ATTRIBUTE, page);
return DIRECTIONS_EDIT_VIEW;
}
redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page);
directionsService.create(toEntity(direction));
return Constants.REDIRECT_VIEW + DIRECTIONS_URL;
}
@GetMapping("/edit/{id}")
public String update(
@PathVariable(name = "id") Long id,
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
Model model) {
if (id <= 0) {
throw new IllegalArgumentException();
}
model.addAttribute(DIRECTIONS_ATTRIBUTE, toDto(directionsService.get(id)));
model.addAttribute(DEPARTMENT_ATTRIBUTE, getDepartments());
model.addAttribute(PAGE_ATTRIBUTE, page);
return DIRECTIONS_EDIT_VIEW;
}
@PostMapping("/edit/{id}")
public String update(
@PathVariable(name = "id") Long id,
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
@ModelAttribute(name = DIRECTIONS_ATTRIBUTE) @Valid DirectionsDto direction,
BindingResult bindingResult,
Model model,
RedirectAttributes redirectAttributes) {
if (bindingResult.hasErrors()) {
return DIRECTIONS_EDIT_VIEW;
}
if (id <= 0) {
throw new IllegalArgumentException();
}
redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page);
directionsService.update(id, toEntity(direction));
return Constants.REDIRECT_VIEW + DIRECTIONS_URL;
}
@PostMapping("/delete/{id}")
public String delete(
@PathVariable(name = "id") Long id,
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
RedirectAttributes redirectAttributes) {
redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page);
directionsService.delete(id);
return Constants.REDIRECT_VIEW + DIRECTIONS_URL;
}
}

View File

@ -1,69 +1,37 @@
package com.example.demo.directions.api; package com.example.demo.directions.api;
import java.util.List;
import java.util.Map; import java.util.Map;
import org.modelmapper.ModelMapper; import org.modelmapper.ModelMapper;
import org.modelmapper.TypeToken;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.example.demo.core.api.PageAttributesMapper; import com.example.demo.core.api.PageAttributesMapper;
import com.example.demo.core.configuration.Constants; import com.example.demo.core.configuration.Constants;
import com.example.demo.department.api.DepartmentDto;
import com.example.demo.department.service.DepartmentService;
import com.example.demo.directions.model.DirectionsEntity;
import com.example.demo.directions.model.DirectionsGrouped; import com.example.demo.directions.model.DirectionsGrouped;
import com.example.demo.directions.service.DirectionsService; import com.example.demo.directions.service.DirectionsService;
import jakarta.validation.Valid;
@Controller @Controller
@RequestMapping(DirectionsController.URL) @RequestMapping(DirectionsController.URL)
public class DirectionsController { public class DirectionsController {
public static final String URL = "/directions"; public static final String URL = "/directions";
private static final String DIRECTIONS_VIEW = "directions"; private static final String DIRECTIONS_VIEW = "directions";
private static final String DIRECTIONS_EDIT_VIEW = "direction-edit";
private static final String DIRECTIONS_ATTRIBUTE = "directions";
private static final String DEPARTMENT_ATTRIBUTE = "departments";
private static final String PAGE_ATTRIBUTE = "page"; private static final String PAGE_ATTRIBUTE = "page";
private final DirectionsService directionsService; private final DirectionsService directionsService;
private final ModelMapper modelMapper; private final ModelMapper modelMapper;
private final DepartmentService departmentService;
public DirectionsController(DirectionsService directionsService, ModelMapper modelMapper, public DirectionsController(DirectionsService directionsService, ModelMapper modelMapper) {
DepartmentService departmentService) {
this.directionsService = directionsService; this.directionsService = directionsService;
this.modelMapper = modelMapper; this.modelMapper = modelMapper;
this.departmentService = departmentService;
}
private DirectionsDto toDto(DirectionsEntity entity) {
return modelMapper.map(entity, DirectionsDto.class);
}
private DirectionsEntity toEntity(DirectionsDto dto) {
final DirectionsEntity entity = modelMapper.map(dto, DirectionsEntity.class);
entity.setDepartment(departmentService.get(dto.getDepartmentId()));
return entity;
} }
private DirectionsGroupedDto toGroupedDto(DirectionsGrouped entity){ private DirectionsGroupedDto toGroupedDto(DirectionsGrouped entity){
return modelMapper.map(entity, DirectionsGroupedDto.class); return modelMapper.map(entity, DirectionsGroupedDto.class);
} }
private List<DepartmentDto> getDepartments(){
return modelMapper.map(departmentService.getAll(), new TypeToken<List<DepartmentDto>>(){}.getType());
}
@GetMapping @GetMapping
public String getAllWithDepartment( public String getAllWithDepartment(
@RequestParam(name=PAGE_ATTRIBUTE, defaultValue = "0") int page, @RequestParam(name=PAGE_ATTRIBUTE, defaultValue = "0") int page,
@ -76,72 +44,4 @@ public class DirectionsController {
model.addAttribute("name", name); model.addAttribute("name", name);
return DIRECTIONS_VIEW; return DIRECTIONS_VIEW;
} }
@GetMapping("/edit/")
public String create(Model model,
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page) {
model.addAttribute(DIRECTIONS_ATTRIBUTE, new DirectionsDto());
model.addAttribute(DEPARTMENT_ATTRIBUTE, getDepartments());
model.addAttribute(PAGE_ATTRIBUTE, page);
return DIRECTIONS_EDIT_VIEW;
}
@PostMapping("/edit/")
public String create(
@ModelAttribute(name = DIRECTIONS_ATTRIBUTE) @Valid DirectionsDto direction,
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
BindingResult bindingResult,
Model model,
RedirectAttributes redirectAttributes) {
if (bindingResult.hasErrors()) {
model.addAttribute(PAGE_ATTRIBUTE, page);
return DIRECTIONS_EDIT_VIEW;
}
redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page);
directionsService.create(toEntity(direction));
return Constants.REDIRECT_VIEW + URL;
}
@GetMapping("/edit/{id}")
public String update(
@PathVariable(name = "id") Long id,
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
Model model) {
if (id <= 0) {
throw new IllegalArgumentException();
}
model.addAttribute(DIRECTIONS_ATTRIBUTE, toDto(directionsService.get(id)));
model.addAttribute(DEPARTMENT_ATTRIBUTE, getDepartments());
model.addAttribute(PAGE_ATTRIBUTE, page);
return DIRECTIONS_EDIT_VIEW;
}
@PostMapping("/edit/{id}")
public String update(
@PathVariable(name = "id") Long id,
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
@ModelAttribute(name = DIRECTIONS_ATTRIBUTE) @Valid DirectionsDto direction,
BindingResult bindingResult,
Model model,
RedirectAttributes redirectAttributes) {
if (bindingResult.hasErrors()) {
return DIRECTIONS_EDIT_VIEW;
}
if (id <= 0) {
throw new IllegalArgumentException();
}
redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page);
directionsService.update(id, toEntity(direction));
return Constants.REDIRECT_VIEW + URL;
}
@PostMapping("/delete/{id}")
public String delete(
@PathVariable(name = "id") Long id,
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
RedirectAttributes redirectAttributes) {
redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page);
directionsService.delete(id);
return Constants.REDIRECT_VIEW + URL;
}
} }

View File

@ -1,6 +1,6 @@
package com.example.demo.entrysData.api; package com.example.demo.entrysData.api;
import groovyjarjarantlr4.v4.runtime.misc.NotNull; import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size; import jakarta.validation.constraints.Size;
@ -8,7 +8,8 @@ public class EntrysDataSignupDto {
@NotBlank @NotBlank
@Size(min = 3, max = 20) @Size(min = 3, max = 20)
private String login; private String login;
@NotNull private Long Id;
@Min(1)
private Long departmentId; private Long departmentId;
@NotBlank @NotBlank
@Size(min = 3, max = 20) @Size(min = 3, max = 20)
@ -29,6 +30,14 @@ public class EntrysDataSignupDto {
return password; return password;
} }
public Long getId(){
return Id;
}
public void setId(Long Id){
this.Id = Id;
}
public Long getDepartmentId(){ public Long getDepartmentId(){
return departmentId; return departmentId;
} }

View File

@ -46,7 +46,12 @@
</a> </a>
</th:block> </th:block>
</ul> </ul>
<ul class="navbar-nav"> <ul class="navbar-nav" th:if="${not #strings.isEmpty(userName)}">
<form th:action="@{/logout}" method="post">
<button type="submit" class="navbar-brand nav-link" onclick="return confirm('Вы уверены?')">
Выход ([[${userName}]])
</button>
</form>
</ul> </ul>
</div> </div>
</th:block> </th:block>

View File

@ -7,7 +7,7 @@
<body> <body>
<main layout:fragment="content"> <main layout:fragment="content">
<form action="#" th:action="@{/directions/edit/{id}(id=${directions.id},(page=${page}))}" <form action="#" th:action="@{/admin/directions/edit/{id}(id=${directions.id},(page=${page}))}"
th:object="${directions}" method="post"> th:object="${directions}" method="post">
<div class="mb-3"> <div class="mb-3">
<label for="id" class="form-label">ID</label> <label for="id" class="form-label">ID</label>

View File

@ -23,9 +23,12 @@
</div> </div>
</form> </form>
</div> </div>
<div> <th:block sec:authorize="hasRole('ADMIN')">
<a th:href="@{/admin/directions/edit/(page=${page})}" class="btn btn-danger mt-4">Добавить направление</a> <div>
</div> <a th:href="@{/admin/directions/edit/(page=${page})}" class="btn btn-danger mt-4">Добавить
направление</a>
</div>
</th:block>
<th:block th:switch="${items.size()}"> <th:block th:switch="${items.size()}">
<h2 th:case="0">Данные отсутствуют</h2> <h2 th:case="0">Данные отсутствуют</h2>
<th:block th:case="*"> <th:block th:case="*">
@ -38,8 +41,10 @@
<th scope="col" class="w-25">Направление</th> <th scope="col" class="w-25">Направление</th>
<th scope="col" class="w-25">Кафедра</th> <th scope="col" class="w-25">Кафедра</th>
<th scope="col" class="w-25">Предметы ЕГЭ</th> <th scope="col" class="w-25">Предметы ЕГЭ</th>
<th scope="col" class="w-5"></th> <th:block sec:authorize="hasRole('ADMIN')">
<th scope="col" class="w-5"></th> <th scope="col" class="w-5"></th>
<th scope="col" class="w-5"></th>
</th:block>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@ -49,23 +54,26 @@
<td th:text="${direction.name}"></td> <td th:text="${direction.name}"></td>
<td th:text="${direction.departmentName}"></td> <td th:text="${direction.departmentName}"></td>
<td th:text="${direction.things}"></td> <td th:text="${direction.things}"></td>
<td> <th:block sec:authorize="hasRole('ADMIN')">
<form th:action="@{/admin/directions/edit/{id}(id=${direction.id})}" method="get"> <td>
<input type="hidden" th:name="page" th:value="${page}"> <form th:action="@{/admin/directions/edit/{id}(id=${direction.id})}" method="get">
<button type="submit" class="btn btn-link button-link"> <input type="hidden" th:name="page" th:value="${page}">
<i class="fa fa-edit">Изменить</i> <button type="submit" class="btn btn-link button-link">
</button> <i class="fa fa-edit">Изменить</i>
</form> </button>
</td> </form>
<td> </td>
<form th:action="@{/admin/directions/delete/{id}(id=${direction.id})}" method="post"> <td>
<input type="hidden" th:name="page" th:value="${page}"> <form th:action="@{/admin/directions/delete/{id}(id=${direction.id})}"
<button type="submit" class="btn btn-link button-link" method="post">
onclick="return confirm('Вы уверены?')"> <input type="hidden" th:name="page" th:value="${page}">
<i class="fa fa-trash">Удалить</i> <button type="submit" class="btn btn-link button-link"
</button> onclick="return confirm('Вы уверены?')">
</form> <i class="fa fa-trash">Удалить</i>
</td> </button>
</form>
</td>
</th:block>
</tr> </tr>
</tbody> </tbody>
</table> </table>