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/data.mv.db b/data.mv.db index 0ad2082..5f6b523 100644 Binary files a/data.mv.db and b/data.mv.db differ 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 3bcc38b..390f0ac 100644 --- a/src/main/java/com/example/demo/directions/api/DirectionsController.java +++ b/src/main/java/com/example/demo/directions/api/DirectionsController.java @@ -67,8 +67,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 b43ef63..c6dffb6 100644 --- a/src/main/java/com/example/demo/entrysData/api/EntrysDataController.java +++ b/src/main/java/com/example/demo/entrysData/api/EntrysDataController.java @@ -4,22 +4,23 @@ 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.PutMapping; -import org.springframework.web.bind.annotation.RequestBody; 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; @@ -31,6 +32,8 @@ public class EntrysDataController { public static final String URL = "/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; @@ -44,14 +47,10 @@ 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); } @@ -62,17 +61,16 @@ public class EntrysDataController { return entity; } - // @GetMapping - // public PageDto getAll( - // @RequestParam(name = "departmentId", defaultValue = "0") Long departmentId, - // @RequestParam(name = "page", defaultValue = "0") int page, - // @RequestParam(name = "size", defaultValue = Constants.DEFAULT_PAGE_SIZE) int size) { - // return PageDtoMapper.toDto(entrysDataService.getAll(departmentId, page, size), this::toDto); - // } + 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. @@ -83,35 +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))); + @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; } - @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)); - } - - @PutMapping("/{id}/password") - public EntrysDataDto updatePassword(@PathVariable(name = "id") Long id, - @RequestParam(name = "newPassword") String newPas, - @RequestParam(name = "oldPassword") String oldPas) { - return toDto(entrysDataService.updatePassword(id, newPas, oldPas)); - } - - @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 c1aa97d..00c7d7c 100644 --- a/src/main/java/com/example/demo/news/api/NewsController.java +++ b/src/main/java/com/example/demo/news/api/NewsController.java @@ -68,7 +68,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 b5e472c..e811db7 100644 --- a/src/main/resources/templates/default.html +++ b/src/main/resources/templates/default.html @@ -16,7 +16,7 @@