diff --git a/data.mv.db b/data.mv.db index 3a72557..6302d81 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 19f2106..c8ee00b 100644 --- a/src/main/java/com/example/demo/directions/api/DirectionsController.java +++ b/src/main/java/com/example/demo/directions/api/DirectionsController.java @@ -1,21 +1,31 @@ package com.example.demo.directions.api; +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.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.directions.model.DirectionsEntity; import com.example.demo.directions.model.DirectionsGrouped; import com.example.demo.directions.service.DirectionsService; +import jakarta.validation.Valid; + @Controller @RequestMapping(DirectionsController.URL) public class DirectionsController { @@ -23,6 +33,7 @@ public class DirectionsController { 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 final DirectionsService directionsService; private final ModelMapper modelMapper; @@ -49,6 +60,10 @@ public class DirectionsController { return modelMapper.map(entity, DirectionsGroupedDto.class); } + private List getDepartments(){ + return modelMapper.map(departmentService.getAll(), new TypeToken>(){}.getType()); + } + @GetMapping public String getAllWithDepartment( @RequestParam(name=PAGE_ATTRIBUTE, defaultValue = "0") int page, @@ -59,32 +74,72 @@ public class DirectionsController { model.addAttribute(PAGE_ATTRIBUTE, page); return DIRECTIONS_VIEW; } + + @GetMapping("/edit/") + public String create(Model model, + @RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page) { + model.addAttribute(DIRECTIONS_ATTRIBUTE, new DirectionsGroupedDto()); + model.addAttribute(DEPARTMENT_ATTRIBUTE, getDepartments()); + model.addAttribute(PAGE_ATTRIBUTE, page); + return DIRECTIONS_EDIT_VIEW; + } - // @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(directionsService.getAll(departmentId, page, size), this::toDto); - // } + @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("/{id}") - // public DirectionsDto get(@PathVariable(name = "id") Long id) { - // return toDto(directionsService.get(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 - // public DirectionsDto create(@RequestBody @Valid DirectionsDto dto) { - // return toDto(directionsService.create(toEntity(dto))); - // } + @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; + } - // @PutMapping("/{id}") - // public DirectionsDto update(@PathVariable(name = "id") Long id, @RequestBody DirectionsDto dto) { - // return toDto(directionsService.update(id, toEntity(dto))); - // } - - // @DeleteMapping("/{id}") - // public DirectionsDto delete(@PathVariable(name = "id") Long id) { - // return toDto(directionsService.delete(id)); - // } + @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; + } } 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 2b08ead..cb70726 100644 --- a/src/main/java/com/example/demo/news/api/NewsController.java +++ b/src/main/java/com/example/demo/news/api/NewsController.java @@ -63,6 +63,7 @@ public class NewsController { @GetMapping("/edit/") public String create(Model model) { model.addAttribute(NEWS_ATTRIBUTE, new NewsDto()); + model.addAttribute(DEPARTMENT_ATTRIBUTE, getDepartments()); return NEWS_EDIT_VIEW; } diff --git a/src/main/resources/templates/direction-edit.html b/src/main/resources/templates/direction-edit.html new file mode 100644 index 0000000..067dc70 --- /dev/null +++ b/src/main/resources/templates/direction-edit.html @@ -0,0 +1,53 @@ + + + + + Редактировать новость + + + +
+
+
+ + +
+
+ + +
+
+
+ + +
+
+
+
+ + +
+
+
+
+ + +
+
+
+
+ + Отмена +
+
+
+ + + \ No newline at end of file diff --git a/src/main/resources/templates/directions.html b/src/main/resources/templates/directions.html index 547276b..899a486 100644 --- a/src/main/resources/templates/directions.html +++ b/src/main/resources/templates/directions.html @@ -18,13 +18,13 @@ - ID - Код - Направление - Кафедра - Предметы ЕГЭ - - + ID + Код + Направление + Кафедра + Предметы ЕГЭ + + @@ -37,17 +37,18 @@
- - - +
- - - +
diff --git a/src/main/resources/templates/new-edit.html b/src/main/resources/templates/new-edit.html index 362f8e6..75900f6 100644 --- a/src/main/resources/templates/new-edit.html +++ b/src/main/resources/templates/new-edit.html @@ -23,6 +23,12 @@
+
+ + +
+
+
-
+
+
diff --git a/src/main/resources/templates/news.html b/src/main/resources/templates/news.html index 838eeff..af3c2fa 100644 --- a/src/main/resources/templates/news.html +++ b/src/main/resources/templates/news.html @@ -20,6 +20,7 @@ ID + Дата публикации Заголовок Описание @@ -29,6 +30,7 @@ +