Доделанные новости + доделка направлений

This commit is contained in:
DyCTaTOR 2024-05-21 00:46:22 +04:00
parent ca5b3d06f1
commit e8a77a66d1
7 changed files with 157 additions and 38 deletions

Binary file not shown.

View File

@ -1,21 +1,31 @@
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.department.service.DepartmentService;
import com.example.demo.directions.model.DirectionsEntity; 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 {
@ -23,6 +33,7 @@ public class DirectionsController {
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_EDIT_VIEW = "direction-edit";
private static final String DIRECTIONS_ATTRIBUTE = "directions"; 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;
@ -49,6 +60,10 @@ public class DirectionsController {
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,
@ -60,31 +75,71 @@ public class DirectionsController {
return DIRECTIONS_VIEW; return DIRECTIONS_VIEW;
} }
// @GetMapping @GetMapping("/edit/")
// public PageDto<DirectionsDto> getAll( public String create(Model model,
// @RequestParam(name = "departmentId", defaultValue = "0") Long departmentId, @RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page) {
// @RequestParam(name = "page", defaultValue = "0") int page, model.addAttribute(DIRECTIONS_ATTRIBUTE, new DirectionsGroupedDto());
// @RequestParam(name = "size", defaultValue = Constants.DEFAULT_PAGE_SIZE) int size) { model.addAttribute(DEPARTMENT_ATTRIBUTE, getDepartments());
// return PageDtoMapper.toDto(directionsService.getAll(departmentId, page, size), this::toDto); model.addAttribute(PAGE_ATTRIBUTE, page);
// } return DIRECTIONS_EDIT_VIEW;
}
// @GetMapping("/{id}")
// public DirectionsDto get(@PathVariable(name = "id") Long id) { @PostMapping("/edit/")
// return toDto(directionsService.get(id)); public String create(
// } @ModelAttribute(name = DIRECTIONS_ATTRIBUTE) @Valid DirectionsDto direction,
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
// @PostMapping BindingResult bindingResult,
// public DirectionsDto create(@RequestBody @Valid DirectionsDto dto) { Model model,
// return toDto(directionsService.create(toEntity(dto))); RedirectAttributes redirectAttributes) {
// } if (bindingResult.hasErrors()) {
model.addAttribute(PAGE_ATTRIBUTE, page);
// @PutMapping("/{id}") return DIRECTIONS_EDIT_VIEW;
// public DirectionsDto update(@PathVariable(name = "id") Long id, @RequestBody DirectionsDto dto) { }
// return toDto(directionsService.update(id, toEntity(dto))); redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page);
// } directionsService.create(toEntity(direction));
return Constants.REDIRECT_VIEW + URL;
// @DeleteMapping("/{id}") }
// public DirectionsDto delete(@PathVariable(name = "id") Long id) {
// return toDto(directionsService.delete(id)); @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

@ -63,6 +63,7 @@ public class NewsController {
@GetMapping("/edit/") @GetMapping("/edit/")
public String create(Model model) { public String create(Model model) {
model.addAttribute(NEWS_ATTRIBUTE, new NewsDto()); model.addAttribute(NEWS_ATTRIBUTE, new NewsDto());
model.addAttribute(DEPARTMENT_ATTRIBUTE, getDepartments());
return NEWS_EDIT_VIEW; return NEWS_EDIT_VIEW;
} }

View File

@ -0,0 +1,53 @@
<!DOCTYPE html>
<html lang="ru" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
<head>
<title>Редактировать новость</title>
</head>
<body>
<main layout:fragment="content">
<form action="#" th:action="@{/directions/edit/{id}(id=${directions.id})}" th:object="${directions}"
method="post">
<div class="mb-3">
<label for="id" class="form-label">ID</label>
<input type="text" th:value="*{id}" id="id" class="form-control" readonly disabled>
</div>
<div class="mb-3">
<label for="name" class="form-label">Направление</label>
<input type="text" th:field="*{name}" id="name" class="form-control">
<div th:if="${#fields.hasErrors('name')}" th:errors="*{name}" class="invalid-feedback"></div>
</div>
<div class="mb-3">
<label for="code" class="form-label">Код</label>
<input type="text" th:field="*{code}" id="code" class="form-control">
<div th:if="${#fields.hasErrors('code')}" th:errors="*{code}" class="invalid-feedback">
</div>
</div>
<div class="mb-3">
<label for="things" class="form-label">Предметы ЕГЭ</label>
<input type="text" th:field="*{things}" id="things" class="form-control">
<div th:if="${#fields.hasErrors('things')}" th:errors="*{things}" class="invalid-feedback">
</div>
</div>
<div class="mb-3">
<label for="departmentId" class="form-label">Кафедра</label>
<select th:field="*{departmentId}" id="departmentId" class="form-control">
<option th:if="*{departmentId} == null" value="">Выберите кафедру</option>
<option th:unless="*{departmentId} == null" th:value="*{departmentId}" th:text="*{name}"></option>
<option th:each=" department : ${departments}" th:value="${department.id}"
th:text="${department.name}" th:selected="*{departmentId} == ${department.id}">
</option>
</select>
<div th:if="${#fields.hasErrors('departmentId')}" th:errors="*{departmentId}" class="invalid-feedback">
</div>
</div>
<div class="mb-3 d-flex flex-row">
<button class="btn btn-primary me-2 button-fixed-width" type="submit">Сохранить</button>
<a class="btn btn-secondary button-fixed-width" href="/directions">Отмена</a>
</div>
</form>
</main>
</body>
</html>

View File

@ -18,13 +18,13 @@
<caption></caption> <caption></caption>
<thead> <thead>
<tr> <tr>
<th scope="col" class="w-10">ID</th> <th scope="col" class="w-5">ID</th>
<th scope="col" class="w-auto">Код</th> <th scope="col" class="w-10">Код</th>
<th scope="col" class="w-10">Направление</th> <th scope="col" class="w-25">Направление</th>
<th scope="col" class="w-10">Кафедра</th> <th scope="col" class="w-25">Кафедра</th>
<th scope="col" class="w-10">Предметы ЕГЭ</th> <th scope="col" class="w-25">Предметы ЕГЭ</th>
<th scope="col" class="w-10"></th> <th scope="col" class="w-5"></th>
<th scope="col" class="w-10"></th> <th scope="col" class="w-5"></th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@ -37,17 +37,18 @@
<td> <td>
<form th:action="@{/directions/edit/{id}(id=${direction.id})}" method="get"> <form th:action="@{/directions/edit/{id}(id=${direction.id})}" method="get">
<input type="hidden" th:name="page" th:value="${page}"> <input type="hidden" th:name="page" th:value="${page}">
<a type="submit" class="btn btn-link button-link"> <button type="submit" class="btn btn-link button-link">
<i class="fa fa-edit"></i> <i class="fa fa-edit">Изменить</i>
</a> </button>
</form> </form>
</td> </td>
<td> <td>
<form th:action="@{/directions/delete/{id}(id=${direction.id})}" method="post"> <form th:action="@{/directions/delete/{id}(id=${direction.id})}" method="post">
<input type="hidden" th:name="page" th:value="${page}"> <input type="hidden" th:name="page" th:value="${page}">
<a type="submit" class="btn btn-link button-link" onclick="return confirm('Вы уверены?')"> <button type="submit" class="btn btn-link button-link"
<i class="fa fa-trash"></i> onclick="return confirm('Вы уверены?')">
</a> <i class="fa fa-trash">Удалить</i>
</button>
</form> </form>
</td> </td>
</tr> </tr>

View File

@ -23,6 +23,12 @@
<div th:if="${#fields.hasErrors('description')}" th:errors="*{description}" class="invalid-feedback"> <div th:if="${#fields.hasErrors('description')}" th:errors="*{description}" class="invalid-feedback">
</div> </div>
</div> </div>
<div class="mb-3">
<label for="date" class="form-label">Дата</label>
<input type="date" th:field="*{date}" id="date" class="form-control">
<div th:if="${#fields.hasErrors('date')}" th:errors="*{date}" class="invalid-feedback">
</div>
</div>
<div class="mb-3"> <div class="mb-3">
<label for="departmentId" class="form-label">Кафедра</label> <label for="departmentId" class="form-label">Кафедра</label>
<select th:field="*{departmentId}" id="departmentId" class="form-control"> <select th:field="*{departmentId}" id="departmentId" class="form-control">
@ -32,7 +38,8 @@
th:text="${department.name}" th:selected="*{departmentId} == ${department.id}"> th:text="${department.name}" th:selected="*{departmentId} == ${department.id}">
</option> </option>
</select> </select>
<div th:if="${#fields.hasErrors('clientId')}" th:errors="*{clientId}" class="invalid-feedback"></div> <div th:if="${#fields.hasErrors('departmentId')}" th:errors="*{departmentId}" class="invalid-feedback">
</div>
</div> </div>
<div class="mb-3 d-flex flex-row"> <div class="mb-3 d-flex flex-row">
<button class="btn btn-primary me-2 button-fixed-width" type="submit">Сохранить</button> <button class="btn btn-primary me-2 button-fixed-width" type="submit">Сохранить</button>

View File

@ -20,6 +20,7 @@
<thead> <thead>
<tr> <tr>
<th scope="col" class="w-5">ID</th> <th scope="col" class="w-5">ID</th>
<th scope="col" class="w-10">Дата публикации</th>
<th scope="col" class="w-25">Заголовок</th> <th scope="col" class="w-25">Заголовок</th>
<th scope="col" class="w-50">Описание</th> <th scope="col" class="w-50">Описание</th>
<th scope="col" class="w-10"></th> <th scope="col" class="w-10"></th>
@ -29,6 +30,7 @@
<tbody> <tbody>
<tr th:each="newItem : ${news}"> <tr th:each="newItem : ${news}">
<th scope="row" th:text="${newItem.id}"></th> <th scope="row" th:text="${newItem.id}"></th>
<td th:text="${newItem.date}"></td>
<td th:text="${newItem.name}"></td> <td th:text="${newItem.name}"></td>
<td th:text="${newItem.description}"></td> <td th:text="${newItem.description}"></td>
<td> <td>