diff --git a/data.mv.db b/data.mv.db index f725e33..3a72557 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 4803438..19f2106 100644 --- a/src/main/java/com/example/demo/directions/api/DirectionsController.java +++ b/src/main/java/com/example/demo/directions/api/DirectionsController.java @@ -51,7 +51,7 @@ public class DirectionsController { @GetMapping public String getAllWithDepartment( - @RequestParam(name="PAGE_ATTRIBUTE", defaultValue = "0") int page, + @RequestParam(name=PAGE_ATTRIBUTE, defaultValue = "0") int page, Model model) { final Map attributes = PageAttributesMapper. toAttributes(directionsService.getAllWithDepartment(page, Constants.DEFAULT_PAGE_SIZE), this::toGroupedDto); diff --git a/src/main/java/com/example/demo/directions/service/DirectionsService.java b/src/main/java/com/example/demo/directions/service/DirectionsService.java index f45ffa4..eecafc3 100644 --- a/src/main/java/com/example/demo/directions/service/DirectionsService.java +++ b/src/main/java/com/example/demo/directions/service/DirectionsService.java @@ -6,6 +6,7 @@ import java.util.stream.StreamSupport; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -81,9 +82,8 @@ public class DirectionsService { return existsEntity; } - @Transactional + @Transactional(readOnly = true) public Page getAllWithDepartment(int page, int size) { - final Pageable pageRequest = PageRequest.of(page, size); - return repository.findAllWithDepartment(pageRequest); + return repository.findAllWithDepartment(PageRequest.of(page, size, Sort.by("id"))); } } 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 e6bb797..2b08ead 100644 --- a/src/main/java/com/example/demo/news/api/NewsController.java +++ b/src/main/java/com/example/demo/news/api/NewsController.java @@ -1,15 +1,20 @@ package com.example.demo.news.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.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; 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.news.model.NewsEntity; import com.example.demo.news.service.NewsService; @@ -23,6 +28,7 @@ public class NewsController { private static final String NEWS_VIEW = "news"; private static final String NEWS_EDIT_VIEW = "new-edit"; private static final String NEWS_ATTRIBUTE = "news"; + private static final String DEPARTMENT_ATTRIBUTE = "departments"; private final NewsService newsService; private final ModelMapper modelMapper; private final DepartmentService departmentService; @@ -43,11 +49,9 @@ public class NewsController { return entity; } - // @GetMapping - // public List getAll(@RequestParam(name = "departmentId", defaultValue - // = "0") Long departmentId) { - // return newsService.getAll(departmentId).stream().map(this::toDto).toList(); - // } + private List getDepartments(){ + return modelMapper.map(departmentService.getAll(), new TypeToken>(){}.getType()); + } @GetMapping public String getAll(Model model) { @@ -56,14 +60,22 @@ public class NewsController { return NEWS_VIEW; } - @GetMapping("/{id}") - public NewsDto get(@PathVariable(name = "id") Long id) { - return toDto(newsService.get(id)); + @GetMapping("/edit/") + public String create(Model model) { + model.addAttribute(NEWS_ATTRIBUTE, new NewsDto()); + return NEWS_EDIT_VIEW; } - @PostMapping - public NewsDto create(@RequestBody @Valid NewsDto dto) { - return toDto(newsService.create(toEntity(dto))); + @PostMapping("/edit/") + public String create( + @ModelAttribute(name = NEWS_ATTRIBUTE) @Valid NewsDto newItem, + BindingResult bindingResult, + Model model) { + if (bindingResult.hasErrors()) { + return NEWS_EDIT_VIEW; + } + newsService.create(toEntity(newItem)); + return Constants.REDIRECT_VIEW + URL; } @PostMapping("/delete/{id}") @@ -81,17 +93,23 @@ public class NewsController { throw new IllegalArgumentException(); } model.addAttribute(NEWS_ATTRIBUTE, toDto(newsService.get(id))); + model.addAttribute(DEPARTMENT_ATTRIBUTE, getDepartments()); return NEWS_EDIT_VIEW; } - // @PutMapping("/{id}") - // public NewsDto update(@PathVariable(name = "id") Long id, @RequestBody - // NewsDto dto) { - // return toDto(newsService.update(id, toEntity(dto))); - // } - - // @DeleteMapping("/{id}") - // public NewsDto delete(@PathVariable(name = "id") Long id) { - // return toDto(newsService.delete(id)); - // } + @PostMapping("/edit/{id}") + public String update( + @PathVariable(name = "id") Long id, + @ModelAttribute(name = NEWS_ATTRIBUTE) @Valid NewsDto newItem, + BindingResult bindingResult, + Model model) { + if (bindingResult.hasErrors()) { + return NEWS_EDIT_VIEW; + } + if (id <= 0) { + throw new IllegalArgumentException(); + } + newsService.update(id, toEntity(newItem)); + return Constants.REDIRECT_VIEW + URL; + } } diff --git a/src/main/resources/public/css/style.css b/src/main/resources/public/css/style.css index ba7619b..bd6781f 100644 --- a/src/main/resources/public/css/style.css +++ b/src/main/resources/public/css/style.css @@ -86,7 +86,7 @@ td form { } .mainSt { - color: #060647; + color: #008B8B; font-size: 50px; } diff --git a/src/main/resources/templates/new-edit.html b/src/main/resources/templates/new-edit.html index e69de29..362f8e6 100644 --- a/src/main/resources/templates/new-edit.html +++ b/src/main/resources/templates/new-edit.html @@ -0,0 +1,45 @@ + + + + + Редактировать новость + + + +
+
+
+ + +
+
+ + +
+
+
+ + +
+
+
+
+ + +
+
+
+ + Отмена +
+
+
+ + + \ No newline at end of file diff --git a/src/main/resources/templates/news.html b/src/main/resources/templates/news.html index 54852d0..838eeff 100644 --- a/src/main/resources/templates/news.html +++ b/src/main/resources/templates/news.html @@ -13,31 +13,42 @@ Новости -
-
-
- -
- - -
- - - -
-
- - - -
-
-
-
-
+ Добавить новость
+ + + + + + + + + + + + + + + + + + + + +
IDЗаголовокОписание
+
+ +
+
+
+ +
+