From 8f8d048296351bd05b679be8e9f4c30aefe60b21 Mon Sep 17 00:00:00 2001
From: DyCTaTOR <125912249+DyCTaTOR@users.noreply.github.com>
Date: Fri, 7 Jun 2024 16:51:34 +0400
Subject: [PATCH] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?=
=?UTF-8?q?=D0=B5=D0=BD=D0=BD=D0=B0=D1=8F=204?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.hintrc | 5 +
.../directions/api/DirectionsController.java | 4 +
.../entrysData/api/EntrysDataController.java | 121 +++++++++++++-----
.../example/demo/news/api/NewsController.java | 5 +
src/main/resources/templates/default.html | 2 +-
.../resources/templates/direction-edit.html | 2 +-
src/main/resources/templates/directions.html | 5 +-
src/main/resources/templates/news.html | 6 +-
src/main/resources/templates/user-edit.html | 50 ++++++++
9 files changed, 163 insertions(+), 37 deletions(-)
create mode 100644 .hintrc
diff --git a/.hintrc b/.hintrc
new file mode 100644
index 0000000..aa8de6b
--- /dev/null
+++ b/.hintrc
@@ -0,0 +1,5 @@
+{
+ "extends": [
+ "development"
+ ]
+}
\ No newline at end of file
diff --git a/src/main/java/com/example/demo/directions/api/DirectionsController.java b/src/main/java/com/example/demo/directions/api/DirectionsController.java
index 9da03c3..95fbdfb 100644
--- a/src/main/java/com/example/demo/directions/api/DirectionsController.java
+++ b/src/main/java/com/example/demo/directions/api/DirectionsController.java
@@ -35,8 +35,12 @@ public class DirectionsController {
@GetMapping
public String getAllWithDepartment(
@RequestParam(name=PAGE_ATTRIBUTE, defaultValue = "0") int page,
+ @RequestParam(name="reset", required = false, defaultValue = "false") String reset,
Model model,
String name) {
+ if (reset.contains("true")){
+ name = "";
+ }
final Map attributes = PageAttributesMapper.
toAttributes(directionsService.getAllWithDepartment(page, Constants.DEFAULT_PAGE_SIZE, name), this::toGroupedDto);
model.addAllAttributes(attributes);
diff --git a/src/main/java/com/example/demo/entrysData/api/EntrysDataController.java b/src/main/java/com/example/demo/entrysData/api/EntrysDataController.java
index a1436cf..d24ff48 100644
--- a/src/main/java/com/example/demo/entrysData/api/EntrysDataController.java
+++ b/src/main/java/com/example/demo/entrysData/api/EntrysDataController.java
@@ -4,28 +4,36 @@ import java.util.List;
import java.util.Map;
import org.modelmapper.ModelMapper;
+import org.modelmapper.TypeToken;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
-import org.springframework.web.bind.annotation.DeleteMapping;
+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.api.PageAttributesMapper;
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.entrysData.model.EntrysDataEntity;
-import com.example.demo.entrysData.model.EntrysDataGrouped;
import com.example.demo.entrysData.model.EntrysDataGroupedDepartment;
import com.example.demo.entrysData.service.EntrysDataService;
+import jakarta.validation.Valid;
+
@Controller
@RequestMapping(EntrysDataController.URL)
public class EntrysDataController {
public static final String URL = Constants.ADMIN_PREFIX + "/users";
private static final String USERS_VIEW = "users";
private static final String USERS_EDIT_VIEW = "user-edit";
+ private static final String DEPARTMENTS_ATTRIBUTE = "departments";
+ private static final String DEPARTMENT_ITEM_ATTRIBUTE = "departmentItem";
private static final String USERS_ATTRIBUTE = "users";
private static final String PAGE_ATTRIBUTE = "page";
private final EntrysDataService entrysDataService;
@@ -39,27 +47,30 @@ public class EntrysDataController {
this.departmentService = departmentService;
}
- private EntrysDataDto toDto(EntrysDataEntity entity) {
+ private EntrysDataDto toDto(EntrysDataEntity entity){
return modelMapper.map(entity, EntrysDataDto.class);
}
- private EntrysDataGroupedDto toDto(EntrysDataGrouped entity) {
- return modelMapper.map(entity, EntrysDataGroupedDto.class);
- }
-
private EntrysDataGroupedDepartmentDto toDto(EntrysDataGroupedDepartment entity){
return modelMapper.map(entity, EntrysDataGroupedDepartmentDto.class);
}
- // private EntrysDataEntity toEntity(EntrysDataDto dto) {
- // final EntrysDataEntity entity = modelMapper.map(dto, EntrysDataEntity.class);
- // entity.setDepartment(departmentService.get(dto.getDepartmentId()));
- // return entity;
- // }
+ private EntrysDataEntity toEntity(EntrysDataDto dto) {
+ final EntrysDataEntity entity = modelMapper.map(dto, EntrysDataEntity.class);
+ entity.setDepartment(departmentService.get(dto.getDepartmentId()));
+ return entity;
+ }
+
+ private List getDepartments(){
+ return modelMapper.map(departmentService.getAll(), new TypeToken>(){}.getType());
+ }
+
+ private DepartmentDto getDepartment(Long id){
+ return modelMapper.map(departmentService.get(id), new TypeToken(){}.getType());
+ }
@GetMapping
public String getAll(
- @RequestParam(name = "departmentId", defaultValue = "0") Long departmentId,
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
Model model){
final Map attributes = PageAttributesMapper.
@@ -70,28 +81,72 @@ public class EntrysDataController {
return USERS_VIEW;
}
- @GetMapping("/{id}")
- public EntrysDataDto get(@PathVariable(name = "id") Long id) {
- return toDto(entrysDataService.get(id));
+ @GetMapping("/edit/")
+ public String create(Model model,
+ @RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page) {
+ model.addAttribute(USERS_ATTRIBUTE, new EntrysDataDto());
+ model.addAttribute(DEPARTMENTS_ATTRIBUTE, getDepartments());
+ model.addAttribute(PAGE_ATTRIBUTE, page);
+ return USERS_EDIT_VIEW;
}
- // @PostMapping
- // public EntrysDataDto create(@RequestBody @Valid EntrysDataDto dto) {
- // return toDto(entrysDataService.create(toEntity(dto)));
- // }
-
- // @PutMapping("/{id}")
- // public EntrysDataDto update(@PathVariable(name = "id") Long id, @RequestBody EntrysDataDto dto) {
- // return toDto(entrysDataService.update(id, toEntity(dto)));
- // }
-
- @DeleteMapping("/{id}")
- public EntrysDataDto delete(@PathVariable(name = "id") Long id) {
- return toDto(entrysDataService.delete(id));
+ @PostMapping("/edit/")
+ public String create(
+ @ModelAttribute(name = USERS_ATTRIBUTE) @Valid EntrysDataDto dto,
+ @RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
+ BindingResult bindingResult,
+ Model model,
+ RedirectAttributes redirectAttributes){
+ if (bindingResult.hasErrors()) {
+ model.addAttribute(PAGE_ATTRIBUTE, page);
+ return USERS_EDIT_VIEW;
+ }
+ redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page);
+ entrysDataService.create(toEntity(dto));
+ 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();
+ }
+ EntrysDataEntity entity = entrysDataService.get(id);
+ model.addAttribute(USERS_ATTRIBUTE, toDto(entity));
+ model.addAttribute(DEPARTMENTS_ATTRIBUTE, getDepartments());
+ model.addAttribute(PAGE_ATTRIBUTE, page);
+ return USERS_EDIT_VIEW;
+ }
+
+ @PostMapping("/edit/{id}")
+ public String update(
+ @PathVariable(name = "id") Long id,
+ @RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
+ @ModelAttribute(name = USERS_ATTRIBUTE) @Valid EntrysDataDto dto,
+ BindingResult bindingResult,
+ Model model,
+ RedirectAttributes redirectAttributes) {
+ if (bindingResult.hasErrors()) {
+ return USERS_EDIT_VIEW;
+ }
+ if (id <= 0) {
+ throw new IllegalArgumentException();
+ }
+ redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page);
+ entrysDataService.update(id, toEntity(dto));
+ return Constants.REDIRECT_VIEW + URL;
}
- @GetMapping("/count")
- public List getCount() {
- return entrysDataService.getCount().stream().map(this::toDto).toList();
- }
+ @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);
+ entrysDataService.delete(id);
+ return Constants.REDIRECT_VIEW + URL;
+}
}
diff --git a/src/main/java/com/example/demo/news/api/NewsController.java b/src/main/java/com/example/demo/news/api/NewsController.java
index 353e309..4b92064 100644
--- a/src/main/java/com/example/demo/news/api/NewsController.java
+++ b/src/main/java/com/example/demo/news/api/NewsController.java
@@ -50,7 +50,12 @@ public class NewsController {
@RequestParam(name=PAGE_ATTRIBUTE, defaultValue = "0") int page,
@RequestParam(name="departmentId", required = false) Long departmentId,
@RequestParam(name=DESCRIPTION_ATTRIBUTE, required = false) String description,
+ @RequestParam(name="reset", required = false, defaultValue = "false") String reset,
Model model) {
+ if (reset.contains("true")){
+ description = "";
+ departmentId = null;
+ }
final Map attributes = PageAttributesMapper.
toAttributes(newsService.getAll(page, Constants.DEFAULT_PAGE_SIZE_FOR_NEWS, departmentId, description), this::toDto);
model.addAllAttributes(attributes);
diff --git a/src/main/resources/templates/default.html b/src/main/resources/templates/default.html
index 7efb51c..cdf6aa3 100644
--- a/src/main/resources/templates/default.html
+++ b/src/main/resources/templates/default.html
@@ -60,7 +60,7 @@
diff --git a/src/main/resources/templates/directions.html b/src/main/resources/templates/directions.html
index e8d0eb0..9787110 100644
--- a/src/main/resources/templates/directions.html
+++ b/src/main/resources/templates/directions.html
@@ -14,7 +14,10 @@
diff --git a/src/main/resources/templates/direction-edit.html b/src/main/resources/templates/direction-edit.html
index 6f1fb23..2b74fc0 100644
--- a/src/main/resources/templates/direction-edit.html
+++ b/src/main/resources/templates/direction-edit.html
@@ -2,7 +2,7 @@