From b65cb9169d8b5cee2ee2bd0f250b08a0f9241962 Mon Sep 17 00:00:00 2001 From: JackKaif Date: Mon, 19 Feb 2024 17:27:20 +0300 Subject: [PATCH] Complete crud task --- java-spring-ru/crud/settings.gradle.kts | 2 +- .../controller/ProductsController.java | 58 ++++++++++++++++++- .../java/exercise/mapper/CategoryMapper.java | 10 ++++ .../java/exercise/mapper/ProductMapper.java | 16 +++++ .../java/exercise/mapper/ReferenceMapper.java | 10 ++++ 5 files changed, 94 insertions(+), 2 deletions(-) diff --git a/java-spring-ru/crud/settings.gradle.kts b/java-spring-ru/crud/settings.gradle.kts index 3196f69..eef7316 100644 --- a/java-spring-ru/crud/settings.gradle.kts +++ b/java-spring-ru/crud/settings.gradle.kts @@ -1,3 +1,3 @@ -rootProject.name = "one-to-many" +rootProject.name = "crud" // spring.jpa.generate-ddl = true // spring.jpa.hibernate.ddl-auto diff --git a/java-spring-ru/crud/src/main/java/exercise/controller/ProductsController.java b/java-spring-ru/crud/src/main/java/exercise/controller/ProductsController.java index 4feb9c2..b6d1224 100644 --- a/java-spring-ru/crud/src/main/java/exercise/controller/ProductsController.java +++ b/java-spring-ru/crud/src/main/java/exercise/controller/ProductsController.java @@ -1,11 +1,14 @@ package exercise.controller; import java.util.List; +import java.util.Set; import exercise.dto.ProductCreateDTO; import exercise.dto.ProductDTO; import exercise.dto.ProductUpdateDTO; import exercise.mapper.ProductMapper; +import exercise.repository.CategoryRepository; +import jakarta.validation.ConstraintViolationException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.DeleteMapping; @@ -28,10 +31,63 @@ public class ProductsController { @Autowired private ProductRepository productRepository; + @Autowired + private CategoryRepository categoryRepository; + @Autowired private ProductMapper productMapper; // BEGIN - + @GetMapping("") + public List index() { + return productRepository.findAll().stream() + .map(productMapper::map) + .toList(); + } + + @GetMapping("/{id}") + public ProductDTO show(@PathVariable Long id) { + var product = productRepository.findById(id) + .orElseThrow(() -> new ResourceNotFoundException("Product with id " + id + " not found")); + return productMapper.map(product); + } + + @PostMapping("") + @ResponseStatus(HttpStatus.CREATED) + public ProductDTO create(@RequestBody ProductCreateDTO newProduct) { + var product = productMapper.map(newProduct); + if (product.getCategory() == null) { + throw new ConstraintViolationException(Set.of()); + } + product.getCategory().getProducts().add(product); + productRepository.save(product); + return productMapper.map(product); + } + + @PutMapping("/{id}") + public ProductDTO update(@PathVariable Long id, + @RequestBody ProductUpdateDTO editedProduct) { + var product = productRepository.findById(id) + .orElseThrow(() -> new ResourceNotFoundException("Product with id " + id + " not found")); + product.getCategory().getProducts().remove(product); + productMapper.update(editedProduct, product); + var category = categoryRepository.findById(editedProduct.getCategoryId().get()) + .orElseThrow(() -> { + throw new ConstraintViolationException(Set.of()); + }); + product.setCategory(category); + category.getProducts().add(product); + productRepository.save(product); + return productMapper.map(product); + } + + @DeleteMapping("/{id}") + @ResponseStatus(HttpStatus.NO_CONTENT) + public void delete(@PathVariable Long id) { + var product = productRepository.findById(id) + .orElseThrow(() -> new ResourceNotFoundException("Product with id " + id + " not found")); + product.getCategory().getProducts().remove(product); + productRepository.delete(product); + } // END } diff --git a/java-spring-ru/crud/src/main/java/exercise/mapper/CategoryMapper.java b/java-spring-ru/crud/src/main/java/exercise/mapper/CategoryMapper.java index 256a89c..3109e68 100644 --- a/java-spring-ru/crud/src/main/java/exercise/mapper/CategoryMapper.java +++ b/java-spring-ru/crud/src/main/java/exercise/mapper/CategoryMapper.java @@ -9,5 +9,15 @@ import org.mapstruct.ReportingPolicy; // BEGIN +@Mapper( + uses = { JsonNullableMapper.class, ReferenceMapper.class}, + nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE, + componentModel = MappingConstants.ComponentModel.SPRING, + unmappedTargetPolicy = ReportingPolicy.IGNORE +) +public abstract class CategoryMapper { + public abstract Category map(CategoryCreateDTO dto); + public abstract CategoryDTO map (Category model); +} // END diff --git a/java-spring-ru/crud/src/main/java/exercise/mapper/ProductMapper.java b/java-spring-ru/crud/src/main/java/exercise/mapper/ProductMapper.java index 45a736b..49df4a9 100644 --- a/java-spring-ru/crud/src/main/java/exercise/mapper/ProductMapper.java +++ b/java-spring-ru/crud/src/main/java/exercise/mapper/ProductMapper.java @@ -12,5 +12,21 @@ import org.mapstruct.ReportingPolicy; // BEGIN +@Mapper( + uses = { JsonNullableMapper.class, ReferenceMapper.class}, + nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE, + componentModel = MappingConstants.ComponentModel.SPRING, + unmappedTargetPolicy = ReportingPolicy.IGNORE +) +public abstract class ProductMapper { + @Mapping(target = "category", source = "categoryId") + public abstract Product map(ProductCreateDTO dto); + @Mapping(target = "categoryName", source = "category.name") + @Mapping(target = "categoryId", source = "category.id") + public abstract ProductDTO map(Product model); + + public abstract void update(ProductUpdateDTO dto, + @MappingTarget Product model); +} // END diff --git a/java-spring-ru/crud/src/main/java/exercise/mapper/ReferenceMapper.java b/java-spring-ru/crud/src/main/java/exercise/mapper/ReferenceMapper.java index bb0ff35..2523a85 100644 --- a/java-spring-ru/crud/src/main/java/exercise/mapper/ReferenceMapper.java +++ b/java-spring-ru/crud/src/main/java/exercise/mapper/ReferenceMapper.java @@ -9,5 +9,15 @@ import jakarta.persistence.EntityManager; // BEGIN +@Mapper( + componentModel = MappingConstants.ComponentModel.SPRING +) +public abstract class ReferenceMapper { + @Autowired + private EntityManager entityManager; + public T toEntity(Long id, @TargetType Class entityClass) { + return id != null ? entityManager.find(entityClass, id) : null; + } +} // END