diff --git a/backend/build.gradle b/backend/build.gradle index 3918860..0b03705 100644 --- a/backend/build.gradle +++ b/backend/build.gradle @@ -33,6 +33,7 @@ dependencies { implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity6' testImplementation 'org.springframework.boot:spring-boot-starter-test' + testImplementation 'org.junit.jupiter:junit-jupiter:5.7.0' } tasks.named('test') { diff --git a/backend/src/main/java/com/example/backend/BackendApplication.java b/backend/src/main/java/com/example/backend/BackendApplication.java index 7200524..2f85ea0 100644 --- a/backend/src/main/java/com/example/backend/BackendApplication.java +++ b/backend/src/main/java/com/example/backend/BackendApplication.java @@ -6,6 +6,8 @@ import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import com.example.backend.categories.model.CategorieEntity; +import com.example.backend.categories.service.CategorieService; import com.example.backend.users.model.UserEntity; import com.example.backend.users.model.UserRole; import com.example.backend.users.service.UserService; @@ -17,9 +19,11 @@ public class BackendApplication implements CommandLineRunner { private final Logger _logger = LoggerFactory.getLogger(BackendApplication.class); private final UserService userService; + private final CategorieService categorieService; - public BackendApplication(UserService userService) { + public BackendApplication(UserService userService, CategorieService categorieService) { this.userService = userService; + this.categorieService = categorieService; } public static void main(String[] args) { @@ -39,6 +43,8 @@ public class BackendApplication implements CommandLineRunner { final var u6 = new UserEntity(null, "6", "1234"); final var u7 = new UserEntity(null, "7", "1234"); + final var cat1 = new CategorieEntity(null, "Драма", null); + admin.setRole(UserRole.ADMIN); vasya.setRole(UserRole.USER); @@ -60,6 +66,8 @@ public class BackendApplication implements CommandLineRunner { userService.create(u6); userService.create(u7); + categorieService.create(cat1); + _logger.info("Admin user added"); } diff --git a/backend/src/main/java/com/example/backend/categories/api/CategorieController.java b/backend/src/main/java/com/example/backend/categories/api/CategorieController.java index 909c201..b4c4c88 100644 --- a/backend/src/main/java/com/example/backend/categories/api/CategorieController.java +++ b/backend/src/main/java/com/example/backend/categories/api/CategorieController.java @@ -3,8 +3,12 @@ package com.example.backend.categories.api; import org.modelmapper.ModelMapper; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.multipart.MultipartFile; +import org.springframework.web.servlet.mvc.support.RedirectAttributes; import com.example.backend.categories.model.CategorieEntity; +import com.example.backend.categories.repository.CategorieRepository; import com.example.backend.categories.service.CategorieService; import com.example.backend.core.configurations.Constants; @@ -14,6 +18,12 @@ 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.PostMapping; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; @Controller @RequestMapping(CategorieController.URL) @@ -26,11 +36,16 @@ public class CategorieController { private static final String CATEGORIE_EDIT_VIEW = "categorie-edit"; private final CategorieService categorieService; + public final CategorieRepository categorieRepository; private final ModelMapper modelMapper; - public CategorieController(CategorieService categorieService, ModelMapper modelMapper) { + private static String UPLOAD_DIR = System.getProperty("user.dir") + "/uploads"; + + public CategorieController(CategorieService categorieService, ModelMapper modelMapper, + CategorieRepository categorieRepository) { this.categorieService = categorieService; this.modelMapper = modelMapper; + this.categorieRepository = categorieRepository; } private CategorieDTO toDto(CategorieEntity entity) { @@ -44,7 +59,7 @@ public class CategorieController { @GetMapping() public String getAll(Model model) { model.addAttribute( - "items", + "categories", categorieService.getAll().stream() .map(this::toDto) .toList()); @@ -52,13 +67,29 @@ public class CategorieController { } @GetMapping("/edit/") - public String create(@ModelAttribute(name = CATEGORIE_ATTRIBUTE) @Valid CategorieDTO categorieDTO, - BindingResult bindingResult, - Model model) { - if (bindingResult.hasErrors()) - return CATEGORIE_EDIT_VIEW; - categorieService.create(toEntity(categorieDTO)); - return Constants.REDIRECT_VIEW + URL; + public String create(Model model) { + model.addAttribute(CATEGORIE_ATTRIBUTE, new CategorieDTO()); + return CATEGORIE_EDIT_VIEW; } + @PostMapping("/edit/") + public String create(@ModelAttribute(name = CATEGORIE_ATTRIBUTE) @Valid CategorieDTO categorieDTO, + BindingResult bindingResult, RedirectAttributes redirectAttributes, + @RequestParam("image") MultipartFile imageFile) { + try { + if (bindingResult.hasErrors()) { + return CATEGORIE_EDIT_VIEW; + } + CategorieEntity category = toEntity(categorieDTO); + if (!imageFile.isEmpty()) { + Path fileNameAndPath = Paths.get(UPLOAD_DIR, imageFile.getOriginalFilename()); + Files.write(fileNameAndPath, imageFile.getBytes()); + category.setImage("/uploads/" + imageFile.getOriginalFilename()); + } + categorieService.create(category); + } catch (IOException e) { + e.printStackTrace(); + } + return Constants.REDIRECT_VIEW + URL; + } } diff --git a/backend/src/main/resources/templates/categorie-edit.html b/backend/src/main/resources/templates/categorie-edit.html new file mode 100644 index 0000000..0f981be --- /dev/null +++ b/backend/src/main/resources/templates/categorie-edit.html @@ -0,0 +1,98 @@ + + + + + Создать категорию + + + +
+
+ + + + +
+ + +
+
+ + +
+ +
+
+ + + + +
+ Вернуться на категории +
+ + + + + + + + + + + + + + --> \ No newline at end of file diff --git a/backend/src/main/resources/templates/categories.html b/backend/src/main/resources/templates/categories.html index c01d0ca..1c7168f 100644 --- a/backend/src/main/resources/templates/categories.html +++ b/backend/src/main/resources/templates/categories.html @@ -3,16 +3,46 @@ Категории +
-
-
- - {categorie.name} -
-
+ +

Данные отсутствуют

+ +
+
+
+ +
+
+
+
+
+
+
+ Добавить категорию +
+
+
diff --git a/data.mv.db b/data.mv.db index dda8478..c56ab13 100644 Binary files a/data.mv.db and b/data.mv.db differ