From 919682b2e6b545b42eefac993c8b742092006ad6 Mon Sep 17 00:00:00 2001 From: Maryna Hryshchenko Date: Wed, 16 Aug 2023 15:48:28 +0300 Subject: [PATCH 1/5] task done --- pom.xml | 31 ++++++++ .../controller/CategoryController.java | 51 ++++++++++++++ .../datajpa/controller/ProductController.java | 70 +++++++++++++++++++ .../springboot/datajpa/mapper/DtoMapper.java | 4 ++ .../datajpa/mapper/RequestDtoMapper.java | 5 ++ .../datajpa/mapper/ResponseDtoMapper.java | 5 ++ .../datajpa/mapper/impl/CategoryMapper.java | 26 +++++++ .../datajpa/mapper/impl/ProductMapper.java | 32 +++++++++ .../springboot/datajpa/model/Category.java | 16 +++++ .../springboot/datajpa/model/Product.java | 21 ++++++ .../model/dto/request/CategoryRequestDto.java | 10 +++ .../model/dto/request/ProductRequestDto.java | 16 +++++ .../dto/response/CategoryResponseDto.java | 9 +++ .../dto/response/ProductResponseDto.java | 12 ++++ .../repository/CategoryRepository.java | 9 +++ .../datajpa/repository/ProductRepository.java | 14 ++++ .../datajpa/service/CategoryService.java | 6 ++ .../datajpa/service/GenersalService.java | 9 +++ .../datajpa/service/ProductService.java | 12 ++++ .../service/impl/CategoryServiceImpl.java | 32 +++++++++ .../service/impl/ProductServiceImpl.java | 44 ++++++++++++ src/main/resources/application.properties | 8 +++ 22 files changed, 442 insertions(+) create mode 100644 src/main/java/mate/academy/springboot/datajpa/controller/CategoryController.java create mode 100644 src/main/java/mate/academy/springboot/datajpa/controller/ProductController.java create mode 100644 src/main/java/mate/academy/springboot/datajpa/mapper/DtoMapper.java create mode 100644 src/main/java/mate/academy/springboot/datajpa/mapper/RequestDtoMapper.java create mode 100644 src/main/java/mate/academy/springboot/datajpa/mapper/ResponseDtoMapper.java create mode 100644 src/main/java/mate/academy/springboot/datajpa/mapper/impl/CategoryMapper.java create mode 100644 src/main/java/mate/academy/springboot/datajpa/mapper/impl/ProductMapper.java create mode 100644 src/main/java/mate/academy/springboot/datajpa/model/Category.java create mode 100644 src/main/java/mate/academy/springboot/datajpa/model/Product.java create mode 100644 src/main/java/mate/academy/springboot/datajpa/model/dto/request/CategoryRequestDto.java create mode 100644 src/main/java/mate/academy/springboot/datajpa/model/dto/request/ProductRequestDto.java create mode 100644 src/main/java/mate/academy/springboot/datajpa/model/dto/response/CategoryResponseDto.java create mode 100644 src/main/java/mate/academy/springboot/datajpa/model/dto/response/ProductResponseDto.java create mode 100644 src/main/java/mate/academy/springboot/datajpa/repository/CategoryRepository.java create mode 100644 src/main/java/mate/academy/springboot/datajpa/repository/ProductRepository.java create mode 100644 src/main/java/mate/academy/springboot/datajpa/service/CategoryService.java create mode 100644 src/main/java/mate/academy/springboot/datajpa/service/GenersalService.java create mode 100644 src/main/java/mate/academy/springboot/datajpa/service/ProductService.java create mode 100644 src/main/java/mate/academy/springboot/datajpa/service/impl/CategoryServiceImpl.java create mode 100644 src/main/java/mate/academy/springboot/datajpa/service/impl/ProductServiceImpl.java diff --git a/pom.xml b/pom.xml index cf704585..e248d7ac 100644 --- a/pom.xml +++ b/pom.xml @@ -30,6 +30,37 @@ org.springframework.boot spring-boot-autoconfigure + + com.h2database + h2 + runtime + + + org.projectlombok + lombok + + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springframework.boot + spring-boot-starter-web + + + org.jetbrains + annotations + 24.0.1 + compile + + + org.springframework.boot + spring-boot-starter-validation + diff --git a/src/main/java/mate/academy/springboot/datajpa/controller/CategoryController.java b/src/main/java/mate/academy/springboot/datajpa/controller/CategoryController.java new file mode 100644 index 00000000..34203152 --- /dev/null +++ b/src/main/java/mate/academy/springboot/datajpa/controller/CategoryController.java @@ -0,0 +1,51 @@ +package mate.academy.springboot.datajpa.controller; + +import jakarta.validation.Valid; +import lombok.AllArgsConstructor; +import mate.academy.springboot.datajpa.mapper.DtoMapper; +import mate.academy.springboot.datajpa.model.Category; +import mate.academy.springboot.datajpa.model.dto.request.CategoryRequestDto; +import mate.academy.springboot.datajpa.model.dto.response.CategoryResponseDto; +import mate.academy.springboot.datajpa.service.CategoryService; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +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.RestController; + +@RestController +@RequestMapping("/categories") +@AllArgsConstructor +public class CategoryController { + private final CategoryService categoryService; + private final DtoMapper mapper; + + @PostMapping + public CategoryResponseDto create(@RequestBody + @Validated CategoryRequestDto categoryRequestDto) { + Category category = categoryService.create(mapper.mapToModel(categoryRequestDto)); + return mapper.mapToDto(category); + } + + @GetMapping("/{id}") + public CategoryResponseDto get(@PathVariable Long id) { + return mapper.mapToDto(categoryService.get(id)); + } + + @PutMapping("/{id}") + public CategoryResponseDto update(@PathVariable Long id, + @RequestBody @Valid CategoryRequestDto categoryRequestDto) { + Category category = mapper.mapToModel(categoryRequestDto); + category.setId(id); + return mapper.mapToDto(categoryService.create(category)); + } + + @DeleteMapping("/{id}") + public void delete(@PathVariable Long id) { + categoryService.remove(id); + } +} diff --git a/src/main/java/mate/academy/springboot/datajpa/controller/ProductController.java b/src/main/java/mate/academy/springboot/datajpa/controller/ProductController.java new file mode 100644 index 00000000..fccc710a --- /dev/null +++ b/src/main/java/mate/academy/springboot/datajpa/controller/ProductController.java @@ -0,0 +1,70 @@ +package mate.academy.springboot.datajpa.controller; + +import jakarta.validation.Valid; +import jakarta.validation.constraints.Positive; +import java.math.BigDecimal; +import java.util.List; +import java.util.stream.Collectors; +import lombok.AllArgsConstructor; +import mate.academy.springboot.datajpa.mapper.DtoMapper; +import mate.academy.springboot.datajpa.model.Product; +import mate.academy.springboot.datajpa.model.dto.request.ProductRequestDto; +import mate.academy.springboot.datajpa.model.dto.response.ProductResponseDto; +import mate.academy.springboot.datajpa.service.ProductService; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +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.bind.annotation.RestController; + +@RestController +@RequestMapping("/products") +@AllArgsConstructor +public class ProductController { + private final ProductService productService; + private final DtoMapper mapper; + + @PostMapping + public ProductResponseDto create(@RequestBody @Validated ProductRequestDto categoryRequestDto) { + Product product = productService.create(mapper.mapToModel(categoryRequestDto)); + return mapper.mapToDto(product); + } + + @GetMapping("/{id}") + public ProductResponseDto get(@PathVariable Long id) { + return mapper.mapToDto(productService.get(id)); + } + + @GetMapping("/by-price") + public List getAllByPrice(@RequestParam @Positive BigDecimal from, + @RequestParam @Positive BigDecimal to) { + return productService.findAllByPriceBetween(from, to).stream() + .map(mapper::mapToDto) + .collect(Collectors.toList()); + } + + @GetMapping("/by-categories") + public List getAllByCategories(@RequestParam String[] categoriesId) { + return productService.findAllByCategoryNameIn(List.of(categoriesId)).stream() + .map(mapper::mapToDto) + .collect(Collectors.toList()); + } + + @PutMapping("/{id}") + public ProductResponseDto update(@PathVariable Long id, + @RequestBody @Valid ProductRequestDto productRequestDto) { + Product product = mapper.mapToModel(productRequestDto); + product.setId(id); + return mapper.mapToDto(productService.create(product)); + } + + @DeleteMapping("/{id}") + public void delete(@PathVariable Long id) { + productService.remove(id); + } +} diff --git a/src/main/java/mate/academy/springboot/datajpa/mapper/DtoMapper.java b/src/main/java/mate/academy/springboot/datajpa/mapper/DtoMapper.java new file mode 100644 index 00000000..0ebb93bd --- /dev/null +++ b/src/main/java/mate/academy/springboot/datajpa/mapper/DtoMapper.java @@ -0,0 +1,4 @@ +package mate.academy.springboot.datajpa.mapper; + +public interface DtoMapper extends RequestDtoMapper, ResponseDtoMapper { +} diff --git a/src/main/java/mate/academy/springboot/datajpa/mapper/RequestDtoMapper.java b/src/main/java/mate/academy/springboot/datajpa/mapper/RequestDtoMapper.java new file mode 100644 index 00000000..92248eea --- /dev/null +++ b/src/main/java/mate/academy/springboot/datajpa/mapper/RequestDtoMapper.java @@ -0,0 +1,5 @@ +package mate.academy.springboot.datajpa.mapper; + +public interface RequestDtoMapper { + T mapToModel(D dto); +} diff --git a/src/main/java/mate/academy/springboot/datajpa/mapper/ResponseDtoMapper.java b/src/main/java/mate/academy/springboot/datajpa/mapper/ResponseDtoMapper.java new file mode 100644 index 00000000..6b602d44 --- /dev/null +++ b/src/main/java/mate/academy/springboot/datajpa/mapper/ResponseDtoMapper.java @@ -0,0 +1,5 @@ +package mate.academy.springboot.datajpa.mapper; + +public interface ResponseDtoMapper { + D mapToDto(T t); +} diff --git a/src/main/java/mate/academy/springboot/datajpa/mapper/impl/CategoryMapper.java b/src/main/java/mate/academy/springboot/datajpa/mapper/impl/CategoryMapper.java new file mode 100644 index 00000000..c5a2e845 --- /dev/null +++ b/src/main/java/mate/academy/springboot/datajpa/mapper/impl/CategoryMapper.java @@ -0,0 +1,26 @@ +package mate.academy.springboot.datajpa.mapper.impl; + +import mate.academy.springboot.datajpa.mapper.DtoMapper; +import mate.academy.springboot.datajpa.model.Category; +import mate.academy.springboot.datajpa.model.dto.request.CategoryRequestDto; +import mate.academy.springboot.datajpa.model.dto.response.CategoryResponseDto; +import org.springframework.stereotype.Component; + +@Component +public class CategoryMapper implements DtoMapper { + @Override + public Category mapToModel(CategoryRequestDto dto) { + Category category = new Category(); + category.setName(dto.getName()); + return category; + } + + @Override + public CategoryResponseDto mapToDto(Category category) { + CategoryResponseDto categoryResponseDto = new CategoryResponseDto(); + categoryResponseDto.setId(category.getId()); + categoryResponseDto.setName(category.getName()); + return categoryResponseDto; + } +} diff --git a/src/main/java/mate/academy/springboot/datajpa/mapper/impl/ProductMapper.java b/src/main/java/mate/academy/springboot/datajpa/mapper/impl/ProductMapper.java new file mode 100644 index 00000000..ba1e6438 --- /dev/null +++ b/src/main/java/mate/academy/springboot/datajpa/mapper/impl/ProductMapper.java @@ -0,0 +1,32 @@ +package mate.academy.springboot.datajpa.mapper.impl; + +import mate.academy.springboot.datajpa.mapper.DtoMapper; +import mate.academy.springboot.datajpa.model.Category; +import mate.academy.springboot.datajpa.model.Product; +import mate.academy.springboot.datajpa.model.dto.request.ProductRequestDto; +import mate.academy.springboot.datajpa.model.dto.response.ProductResponseDto; +import org.springframework.stereotype.Component; + +@Component +public class ProductMapper implements DtoMapper { + @Override + public Product mapToModel(ProductRequestDto dto) { + Product product = new Product(); + product.setTitle(dto.getTitle()); + product.setPrice(dto.getPrice()); + Category category = new Category(); + category.setId(dto.getCategoryId()); + product.setCategory(category); + return product; + } + + @Override + public ProductResponseDto mapToDto(Product product) { + ProductResponseDto productResponseDto = new ProductResponseDto(); + productResponseDto.setId(product.getId()); + productResponseDto.setTitle(product.getTitle()); + productResponseDto.setPrice(product.getPrice()); + productResponseDto.setCategoryId(product.getCategory().getId()); + return productResponseDto; + } +} diff --git a/src/main/java/mate/academy/springboot/datajpa/model/Category.java b/src/main/java/mate/academy/springboot/datajpa/model/Category.java new file mode 100644 index 00000000..02b66ad4 --- /dev/null +++ b/src/main/java/mate/academy/springboot/datajpa/model/Category.java @@ -0,0 +1,16 @@ +package mate.academy.springboot.datajpa.model; + +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import lombok.Data; + +@Data +@Entity +public class Category { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + private Long name; +} diff --git a/src/main/java/mate/academy/springboot/datajpa/model/Product.java b/src/main/java/mate/academy/springboot/datajpa/model/Product.java new file mode 100644 index 00000000..098e6825 --- /dev/null +++ b/src/main/java/mate/academy/springboot/datajpa/model/Product.java @@ -0,0 +1,21 @@ +package mate.academy.springboot.datajpa.model; + +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.ManyToOne; +import java.math.BigDecimal; +import lombok.Data; + +@Data +@Entity +public class Product { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + private String title; + private BigDecimal price; + @ManyToOne + private Category category; +} diff --git a/src/main/java/mate/academy/springboot/datajpa/model/dto/request/CategoryRequestDto.java b/src/main/java/mate/academy/springboot/datajpa/model/dto/request/CategoryRequestDto.java new file mode 100644 index 00000000..edf0085e --- /dev/null +++ b/src/main/java/mate/academy/springboot/datajpa/model/dto/request/CategoryRequestDto.java @@ -0,0 +1,10 @@ +package mate.academy.springboot.datajpa.model.dto.request; + +import jakarta.validation.constraints.NotNull; +import lombok.Data; + +@Data +public class CategoryRequestDto { + @NotNull + private Long name; +} diff --git a/src/main/java/mate/academy/springboot/datajpa/model/dto/request/ProductRequestDto.java b/src/main/java/mate/academy/springboot/datajpa/model/dto/request/ProductRequestDto.java new file mode 100644 index 00000000..404e6bb2 --- /dev/null +++ b/src/main/java/mate/academy/springboot/datajpa/model/dto/request/ProductRequestDto.java @@ -0,0 +1,16 @@ +package mate.academy.springboot.datajpa.model.dto.request; + +import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Positive; +import java.math.BigDecimal; +import lombok.Data; + +@Data +public class ProductRequestDto { + @NotNull + private String title; + @Positive + private BigDecimal price; + @Positive + private Long categoryId; +} diff --git a/src/main/java/mate/academy/springboot/datajpa/model/dto/response/CategoryResponseDto.java b/src/main/java/mate/academy/springboot/datajpa/model/dto/response/CategoryResponseDto.java new file mode 100644 index 00000000..a1d78228 --- /dev/null +++ b/src/main/java/mate/academy/springboot/datajpa/model/dto/response/CategoryResponseDto.java @@ -0,0 +1,9 @@ +package mate.academy.springboot.datajpa.model.dto.response; + +import lombok.Data; + +@Data +public class CategoryResponseDto { + private Long id; + private Long name; +} diff --git a/src/main/java/mate/academy/springboot/datajpa/model/dto/response/ProductResponseDto.java b/src/main/java/mate/academy/springboot/datajpa/model/dto/response/ProductResponseDto.java new file mode 100644 index 00000000..2e5a8238 --- /dev/null +++ b/src/main/java/mate/academy/springboot/datajpa/model/dto/response/ProductResponseDto.java @@ -0,0 +1,12 @@ +package mate.academy.springboot.datajpa.model.dto.response; + +import java.math.BigDecimal; +import lombok.Data; + +@Data +public class ProductResponseDto { + private Long id; + private String title; + private BigDecimal price; + private Long categoryId; +} diff --git a/src/main/java/mate/academy/springboot/datajpa/repository/CategoryRepository.java b/src/main/java/mate/academy/springboot/datajpa/repository/CategoryRepository.java new file mode 100644 index 00000000..ed06bf2b --- /dev/null +++ b/src/main/java/mate/academy/springboot/datajpa/repository/CategoryRepository.java @@ -0,0 +1,9 @@ +package mate.academy.springboot.datajpa.repository; + +import mate.academy.springboot.datajpa.model.Category; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface CategoryRepository extends JpaRepository { +} diff --git a/src/main/java/mate/academy/springboot/datajpa/repository/ProductRepository.java b/src/main/java/mate/academy/springboot/datajpa/repository/ProductRepository.java new file mode 100644 index 00000000..95d33af1 --- /dev/null +++ b/src/main/java/mate/academy/springboot/datajpa/repository/ProductRepository.java @@ -0,0 +1,14 @@ +package mate.academy.springboot.datajpa.repository; + +import java.math.BigDecimal; +import java.util.List; +import mate.academy.springboot.datajpa.model.Product; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface ProductRepository extends JpaRepository { + List findAllByPriceBetween(BigDecimal from, BigDecimal to); + + List findAllByCategoryNameIn(List categoryName); +} diff --git a/src/main/java/mate/academy/springboot/datajpa/service/CategoryService.java b/src/main/java/mate/academy/springboot/datajpa/service/CategoryService.java new file mode 100644 index 00000000..2454f626 --- /dev/null +++ b/src/main/java/mate/academy/springboot/datajpa/service/CategoryService.java @@ -0,0 +1,6 @@ +package mate.academy.springboot.datajpa.service; + +import mate.academy.springboot.datajpa.model.Category; + +public interface CategoryService extends GenersalService { +} diff --git a/src/main/java/mate/academy/springboot/datajpa/service/GenersalService.java b/src/main/java/mate/academy/springboot/datajpa/service/GenersalService.java new file mode 100644 index 00000000..13b67909 --- /dev/null +++ b/src/main/java/mate/academy/springboot/datajpa/service/GenersalService.java @@ -0,0 +1,9 @@ +package mate.academy.springboot.datajpa.service; + +public interface GenersalService { + T create(T model); + + T get(Long id); + + void remove(Long id); +} diff --git a/src/main/java/mate/academy/springboot/datajpa/service/ProductService.java b/src/main/java/mate/academy/springboot/datajpa/service/ProductService.java new file mode 100644 index 00000000..4c08bf13 --- /dev/null +++ b/src/main/java/mate/academy/springboot/datajpa/service/ProductService.java @@ -0,0 +1,12 @@ +package mate.academy.springboot.datajpa.service; + +import java.math.BigDecimal; +import java.util.List; +import mate.academy.springboot.datajpa.model.Product; + +public interface ProductService extends GenersalService { + List findAllByPriceBetween(BigDecimal from, BigDecimal to); + + List findAllByCategoryNameIn(List categoryName); + +} diff --git a/src/main/java/mate/academy/springboot/datajpa/service/impl/CategoryServiceImpl.java b/src/main/java/mate/academy/springboot/datajpa/service/impl/CategoryServiceImpl.java new file mode 100644 index 00000000..632aa9c6 --- /dev/null +++ b/src/main/java/mate/academy/springboot/datajpa/service/impl/CategoryServiceImpl.java @@ -0,0 +1,32 @@ +package mate.academy.springboot.datajpa.service.impl; + +import java.util.NoSuchElementException; +import mate.academy.springboot.datajpa.model.Category; +import mate.academy.springboot.datajpa.repository.CategoryRepository; +import mate.academy.springboot.datajpa.service.CategoryService; +import org.springframework.stereotype.Service; + +@Service +public class CategoryServiceImpl implements CategoryService { + private final CategoryRepository categoryRepository; + + public CategoryServiceImpl(CategoryRepository categoryRepository) { + this.categoryRepository = categoryRepository; + } + + @Override + public Category create(Category model) { + return categoryRepository.save(model); + } + + @Override + public Category get(Long id) { + return categoryRepository.findById(id).orElseThrow(() + -> new NoSuchElementException("Can`t find ")); + } + + @Override + public void remove(Long id) { + categoryRepository.deleteById(id); + } +} diff --git a/src/main/java/mate/academy/springboot/datajpa/service/impl/ProductServiceImpl.java b/src/main/java/mate/academy/springboot/datajpa/service/impl/ProductServiceImpl.java new file mode 100644 index 00000000..d4f6c52f --- /dev/null +++ b/src/main/java/mate/academy/springboot/datajpa/service/impl/ProductServiceImpl.java @@ -0,0 +1,44 @@ +package mate.academy.springboot.datajpa.service.impl; + +import java.math.BigDecimal; +import java.util.List; +import java.util.NoSuchElementException; +import mate.academy.springboot.datajpa.model.Product; +import mate.academy.springboot.datajpa.repository.ProductRepository; +import mate.academy.springboot.datajpa.service.ProductService; +import org.springframework.stereotype.Service; + +@Service +public class ProductServiceImpl implements ProductService { + private final ProductRepository productRepository; + + public ProductServiceImpl(ProductRepository productRepository) { + this.productRepository = productRepository; + } + + @Override + public Product create(Product model) { + return productRepository.save(model); + } + + @Override + public Product get(Long id) { + return productRepository.findById(id).orElseThrow(() + -> new NoSuchElementException("Can`t get product by id: " + id)); + } + + @Override + public void remove(Long id) { + productRepository.deleteById(id); + } + + @Override + public List findAllByPriceBetween(BigDecimal from, BigDecimal to) { + return productRepository.findAllByPriceBetween(from, to); + } + + @Override + public List findAllByCategoryNameIn(List categoryName) { + return productRepository.findAllByCategoryNameIn(categoryName); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 8b137891..aeb6a523 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1,9 @@ +spring.datasource.url=jdbc:h2:mem:testdb +spring.datasource.driverClassName=org.h2.Driver +spring.datasource.username=sa +spring.datasource.password=password +spring.jpa.database-platform=org.hibernate.dialect.H2Dialect +spring.h2.console.enabled=true +spring.jpa.show-sql=true +spring.jpa.properties.hibernate.format_sql=true From 8e60d10e9b2e82e234afd714a98aab36dc620906 Mon Sep 17 00:00:00 2001 From: Maryna Hryshchenko Date: Wed, 16 Aug 2023 19:35:20 +0300 Subject: [PATCH 2/5] changed parametr in Category --- .../java/mate/academy/springboot/datajpa/model/Category.java | 2 +- .../datajpa/model/dto/request/CategoryRequestDto.java | 2 +- .../datajpa/model/dto/response/CategoryResponseDto.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/mate/academy/springboot/datajpa/model/Category.java b/src/main/java/mate/academy/springboot/datajpa/model/Category.java index 02b66ad4..98f76c97 100644 --- a/src/main/java/mate/academy/springboot/datajpa/model/Category.java +++ b/src/main/java/mate/academy/springboot/datajpa/model/Category.java @@ -12,5 +12,5 @@ public class Category { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; - private Long name; + private String name; } diff --git a/src/main/java/mate/academy/springboot/datajpa/model/dto/request/CategoryRequestDto.java b/src/main/java/mate/academy/springboot/datajpa/model/dto/request/CategoryRequestDto.java index edf0085e..30c2fcf6 100644 --- a/src/main/java/mate/academy/springboot/datajpa/model/dto/request/CategoryRequestDto.java +++ b/src/main/java/mate/academy/springboot/datajpa/model/dto/request/CategoryRequestDto.java @@ -6,5 +6,5 @@ @Data public class CategoryRequestDto { @NotNull - private Long name; + private String name; } diff --git a/src/main/java/mate/academy/springboot/datajpa/model/dto/response/CategoryResponseDto.java b/src/main/java/mate/academy/springboot/datajpa/model/dto/response/CategoryResponseDto.java index a1d78228..1ea5a7d5 100644 --- a/src/main/java/mate/academy/springboot/datajpa/model/dto/response/CategoryResponseDto.java +++ b/src/main/java/mate/academy/springboot/datajpa/model/dto/response/CategoryResponseDto.java @@ -5,5 +5,5 @@ @Data public class CategoryResponseDto { private Long id; - private Long name; + private String name; } From f7b918b2661299732d1b35049f92222ffd355a4a Mon Sep 17 00:00:00 2001 From: Maryna Hryshchenko Date: Thu, 17 Aug 2023 09:04:25 +0300 Subject: [PATCH 3/5] add some annotations --- .../mate/academy/springboot/datajpa/model/Category.java | 2 ++ .../mate/academy/springboot/datajpa/model/Product.java | 8 +++++++- .../springboot/datajpa/service/CategoryService.java | 2 +- .../service/{GenersalService.java => GeneralService.java} | 2 +- .../springboot/datajpa/service/ProductService.java | 2 +- 5 files changed, 12 insertions(+), 4 deletions(-) rename src/main/java/mate/academy/springboot/datajpa/service/{GenersalService.java => GeneralService.java} (76%) diff --git a/src/main/java/mate/academy/springboot/datajpa/model/Category.java b/src/main/java/mate/academy/springboot/datajpa/model/Category.java index 98f76c97..a8a4b3b4 100644 --- a/src/main/java/mate/academy/springboot/datajpa/model/Category.java +++ b/src/main/java/mate/academy/springboot/datajpa/model/Category.java @@ -4,10 +4,12 @@ import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; +import jakarta.persistence.Table; import lombok.Data; @Data @Entity +@Table(name = "categories") public class Category { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) diff --git a/src/main/java/mate/academy/springboot/datajpa/model/Product.java b/src/main/java/mate/academy/springboot/datajpa/model/Product.java index 098e6825..cc6d9338 100644 --- a/src/main/java/mate/academy/springboot/datajpa/model/Product.java +++ b/src/main/java/mate/academy/springboot/datajpa/model/Product.java @@ -1,21 +1,27 @@ package mate.academy.springboot.datajpa.model; +import jakarta.persistence.CascadeType; import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import jakarta.persistence.ManyToOne; +import jakarta.persistence.Table; import java.math.BigDecimal; import lombok.Data; +import lombok.EqualsAndHashCode; @Data @Entity +@Table(name = "products") public class Product { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String title; private BigDecimal price; - @ManyToOne + @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.MERGE) + @EqualsAndHashCode.Exclude private Category category; } diff --git a/src/main/java/mate/academy/springboot/datajpa/service/CategoryService.java b/src/main/java/mate/academy/springboot/datajpa/service/CategoryService.java index 2454f626..8e011560 100644 --- a/src/main/java/mate/academy/springboot/datajpa/service/CategoryService.java +++ b/src/main/java/mate/academy/springboot/datajpa/service/CategoryService.java @@ -2,5 +2,5 @@ import mate.academy.springboot.datajpa.model.Category; -public interface CategoryService extends GenersalService { +public interface CategoryService extends GeneralService { } diff --git a/src/main/java/mate/academy/springboot/datajpa/service/GenersalService.java b/src/main/java/mate/academy/springboot/datajpa/service/GeneralService.java similarity index 76% rename from src/main/java/mate/academy/springboot/datajpa/service/GenersalService.java rename to src/main/java/mate/academy/springboot/datajpa/service/GeneralService.java index 13b67909..6cfbdca4 100644 --- a/src/main/java/mate/academy/springboot/datajpa/service/GenersalService.java +++ b/src/main/java/mate/academy/springboot/datajpa/service/GeneralService.java @@ -1,6 +1,6 @@ package mate.academy.springboot.datajpa.service; -public interface GenersalService { +public interface GeneralService { T create(T model); T get(Long id); diff --git a/src/main/java/mate/academy/springboot/datajpa/service/ProductService.java b/src/main/java/mate/academy/springboot/datajpa/service/ProductService.java index 4c08bf13..ddfebc3b 100644 --- a/src/main/java/mate/academy/springboot/datajpa/service/ProductService.java +++ b/src/main/java/mate/academy/springboot/datajpa/service/ProductService.java @@ -4,7 +4,7 @@ import java.util.List; import mate.academy.springboot.datajpa.model.Product; -public interface ProductService extends GenersalService { +public interface ProductService extends GeneralService { List findAllByPriceBetween(BigDecimal from, BigDecimal to); List findAllByCategoryNameIn(List categoryName); From fb93613c6c2138cdacad11bae3dcf2138451493a Mon Sep 17 00:00:00 2001 From: Maryna Hryshchenko Date: Fri, 18 Aug 2023 13:59:51 +0300 Subject: [PATCH 4/5] after review --- .../datajpa/controller/CategoryController.java | 6 +++--- .../datajpa/controller/ProductController.java | 6 +++--- .../academy/springboot/datajpa/model/Category.java | 10 ++++++++-- .../mate/academy/springboot/datajpa/model/Product.java | 9 +++++++-- .../springboot/datajpa/service/GeneralService.java | 2 +- .../springboot/datajpa/service/ProductService.java | 1 - .../datajpa/service/impl/CategoryServiceImpl.java | 4 ++-- .../datajpa/service/impl/ProductServiceImpl.java | 2 +- 8 files changed, 25 insertions(+), 15 deletions(-) diff --git a/src/main/java/mate/academy/springboot/datajpa/controller/CategoryController.java b/src/main/java/mate/academy/springboot/datajpa/controller/CategoryController.java index 34203152..1cdddcaf 100644 --- a/src/main/java/mate/academy/springboot/datajpa/controller/CategoryController.java +++ b/src/main/java/mate/academy/springboot/datajpa/controller/CategoryController.java @@ -25,9 +25,9 @@ public class CategoryController { private final DtoMapper mapper; @PostMapping - public CategoryResponseDto create(@RequestBody + public CategoryResponseDto save(@RequestBody @Validated CategoryRequestDto categoryRequestDto) { - Category category = categoryService.create(mapper.mapToModel(categoryRequestDto)); + Category category = categoryService.save(mapper.mapToModel(categoryRequestDto)); return mapper.mapToDto(category); } @@ -41,7 +41,7 @@ public CategoryResponseDto update(@PathVariable Long id, @RequestBody @Valid CategoryRequestDto categoryRequestDto) { Category category = mapper.mapToModel(categoryRequestDto); category.setId(id); - return mapper.mapToDto(categoryService.create(category)); + return mapper.mapToDto(categoryService.save(category)); } @DeleteMapping("/{id}") diff --git a/src/main/java/mate/academy/springboot/datajpa/controller/ProductController.java b/src/main/java/mate/academy/springboot/datajpa/controller/ProductController.java index fccc710a..3b880bbb 100644 --- a/src/main/java/mate/academy/springboot/datajpa/controller/ProductController.java +++ b/src/main/java/mate/academy/springboot/datajpa/controller/ProductController.java @@ -30,8 +30,8 @@ public class ProductController { private final DtoMapper mapper; @PostMapping - public ProductResponseDto create(@RequestBody @Validated ProductRequestDto categoryRequestDto) { - Product product = productService.create(mapper.mapToModel(categoryRequestDto)); + public ProductResponseDto save(@RequestBody @Validated ProductRequestDto categoryRequestDto) { + Product product = productService.save(mapper.mapToModel(categoryRequestDto)); return mapper.mapToDto(product); } @@ -60,7 +60,7 @@ public ProductResponseDto update(@PathVariable Long id, @RequestBody @Valid ProductRequestDto productRequestDto) { Product product = mapper.mapToModel(productRequestDto); product.setId(id); - return mapper.mapToDto(productService.create(product)); + return mapper.mapToDto(productService.save(product)); } @DeleteMapping("/{id}") diff --git a/src/main/java/mate/academy/springboot/datajpa/model/Category.java b/src/main/java/mate/academy/springboot/datajpa/model/Category.java index a8a4b3b4..932e5a3e 100644 --- a/src/main/java/mate/academy/springboot/datajpa/model/Category.java +++ b/src/main/java/mate/academy/springboot/datajpa/model/Category.java @@ -5,9 +5,15 @@ import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import jakarta.persistence.Table; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; -@Data +@Getter +@Setter +@NoArgsConstructor +@EqualsAndHashCode @Entity @Table(name = "categories") public class Category { diff --git a/src/main/java/mate/academy/springboot/datajpa/model/Product.java b/src/main/java/mate/academy/springboot/datajpa/model/Product.java index cc6d9338..8bd8223a 100644 --- a/src/main/java/mate/academy/springboot/datajpa/model/Product.java +++ b/src/main/java/mate/academy/springboot/datajpa/model/Product.java @@ -9,10 +9,15 @@ import jakarta.persistence.ManyToOne; import jakarta.persistence.Table; import java.math.BigDecimal; -import lombok.Data; import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; -@Data +@Getter +@Setter +@NoArgsConstructor +@EqualsAndHashCode @Entity @Table(name = "products") public class Product { diff --git a/src/main/java/mate/academy/springboot/datajpa/service/GeneralService.java b/src/main/java/mate/academy/springboot/datajpa/service/GeneralService.java index 6cfbdca4..007889ba 100644 --- a/src/main/java/mate/academy/springboot/datajpa/service/GeneralService.java +++ b/src/main/java/mate/academy/springboot/datajpa/service/GeneralService.java @@ -1,7 +1,7 @@ package mate.academy.springboot.datajpa.service; public interface GeneralService { - T create(T model); + T save(T model); T get(Long id); diff --git a/src/main/java/mate/academy/springboot/datajpa/service/ProductService.java b/src/main/java/mate/academy/springboot/datajpa/service/ProductService.java index ddfebc3b..9ec64823 100644 --- a/src/main/java/mate/academy/springboot/datajpa/service/ProductService.java +++ b/src/main/java/mate/academy/springboot/datajpa/service/ProductService.java @@ -8,5 +8,4 @@ public interface ProductService extends GeneralService { List findAllByPriceBetween(BigDecimal from, BigDecimal to); List findAllByCategoryNameIn(List categoryName); - } diff --git a/src/main/java/mate/academy/springboot/datajpa/service/impl/CategoryServiceImpl.java b/src/main/java/mate/academy/springboot/datajpa/service/impl/CategoryServiceImpl.java index 632aa9c6..28e89e38 100644 --- a/src/main/java/mate/academy/springboot/datajpa/service/impl/CategoryServiceImpl.java +++ b/src/main/java/mate/academy/springboot/datajpa/service/impl/CategoryServiceImpl.java @@ -15,14 +15,14 @@ public CategoryServiceImpl(CategoryRepository categoryRepository) { } @Override - public Category create(Category model) { + public Category save(Category model) { return categoryRepository.save(model); } @Override public Category get(Long id) { return categoryRepository.findById(id).orElseThrow(() - -> new NoSuchElementException("Can`t find ")); + -> new NoSuchElementException("Can`t find category by id: " + id)); } @Override diff --git a/src/main/java/mate/academy/springboot/datajpa/service/impl/ProductServiceImpl.java b/src/main/java/mate/academy/springboot/datajpa/service/impl/ProductServiceImpl.java index d4f6c52f..26479fc3 100644 --- a/src/main/java/mate/academy/springboot/datajpa/service/impl/ProductServiceImpl.java +++ b/src/main/java/mate/academy/springboot/datajpa/service/impl/ProductServiceImpl.java @@ -17,7 +17,7 @@ public ProductServiceImpl(ProductRepository productRepository) { } @Override - public Product create(Product model) { + public Product save(Product model) { return productRepository.save(model); } From 602be8996552d4b54360ad2020fd60cea5915fe1 Mon Sep 17 00:00:00 2001 From: Maryna Hryshchenko Date: Tue, 29 Aug 2023 10:42:37 +0300 Subject: [PATCH 5/5] after mentors review --- .../datajpa/controller/ProductController.java | 4 ++-- .../springboot/datajpa/service/ProductService.java | 2 +- .../datajpa/service/impl/CategoryServiceImpl.java | 6 ++---- .../datajpa/service/impl/ProductServiceImpl.java | 10 ++++------ 4 files changed, 9 insertions(+), 13 deletions(-) diff --git a/src/main/java/mate/academy/springboot/datajpa/controller/ProductController.java b/src/main/java/mate/academy/springboot/datajpa/controller/ProductController.java index 3b880bbb..09a18214 100644 --- a/src/main/java/mate/academy/springboot/datajpa/controller/ProductController.java +++ b/src/main/java/mate/academy/springboot/datajpa/controller/ProductController.java @@ -49,8 +49,8 @@ public List getAllByPrice(@RequestParam @Positive BigDecimal } @GetMapping("/by-categories") - public List getAllByCategories(@RequestParam String[] categoriesId) { - return productService.findAllByCategoryNameIn(List.of(categoriesId)).stream() + public List getAllByCategoryIds(@RequestParam String[] categoryIds) { + return productService.findByCategoryIds(List.of(categoryIds)).stream() .map(mapper::mapToDto) .collect(Collectors.toList()); } diff --git a/src/main/java/mate/academy/springboot/datajpa/service/ProductService.java b/src/main/java/mate/academy/springboot/datajpa/service/ProductService.java index 9ec64823..09bc25e8 100644 --- a/src/main/java/mate/academy/springboot/datajpa/service/ProductService.java +++ b/src/main/java/mate/academy/springboot/datajpa/service/ProductService.java @@ -7,5 +7,5 @@ public interface ProductService extends GeneralService { List findAllByPriceBetween(BigDecimal from, BigDecimal to); - List findAllByCategoryNameIn(List categoryName); + List findByCategoryIds(List categoryName); } diff --git a/src/main/java/mate/academy/springboot/datajpa/service/impl/CategoryServiceImpl.java b/src/main/java/mate/academy/springboot/datajpa/service/impl/CategoryServiceImpl.java index 28e89e38..2fbdf931 100644 --- a/src/main/java/mate/academy/springboot/datajpa/service/impl/CategoryServiceImpl.java +++ b/src/main/java/mate/academy/springboot/datajpa/service/impl/CategoryServiceImpl.java @@ -1,19 +1,17 @@ package mate.academy.springboot.datajpa.service.impl; import java.util.NoSuchElementException; +import lombok.RequiredArgsConstructor; import mate.academy.springboot.datajpa.model.Category; import mate.academy.springboot.datajpa.repository.CategoryRepository; import mate.academy.springboot.datajpa.service.CategoryService; import org.springframework.stereotype.Service; @Service +@RequiredArgsConstructor public class CategoryServiceImpl implements CategoryService { private final CategoryRepository categoryRepository; - public CategoryServiceImpl(CategoryRepository categoryRepository) { - this.categoryRepository = categoryRepository; - } - @Override public Category save(Category model) { return categoryRepository.save(model); diff --git a/src/main/java/mate/academy/springboot/datajpa/service/impl/ProductServiceImpl.java b/src/main/java/mate/academy/springboot/datajpa/service/impl/ProductServiceImpl.java index 26479fc3..41c2073c 100644 --- a/src/main/java/mate/academy/springboot/datajpa/service/impl/ProductServiceImpl.java +++ b/src/main/java/mate/academy/springboot/datajpa/service/impl/ProductServiceImpl.java @@ -3,19 +3,17 @@ import java.math.BigDecimal; import java.util.List; import java.util.NoSuchElementException; +import lombok.RequiredArgsConstructor; import mate.academy.springboot.datajpa.model.Product; import mate.academy.springboot.datajpa.repository.ProductRepository; import mate.academy.springboot.datajpa.service.ProductService; import org.springframework.stereotype.Service; @Service +@RequiredArgsConstructor public class ProductServiceImpl implements ProductService { private final ProductRepository productRepository; - public ProductServiceImpl(ProductRepository productRepository) { - this.productRepository = productRepository; - } - @Override public Product save(Product model) { return productRepository.save(model); @@ -38,7 +36,7 @@ public List findAllByPriceBetween(BigDecimal from, BigDecimal to) { } @Override - public List findAllByCategoryNameIn(List categoryName) { - return productRepository.findAllByCategoryNameIn(categoryName); + public List findByCategoryIds(List categoryNames) { + return productRepository.findAllByCategoryNameIn(categoryNames); } }