-
Notifications
You must be signed in to change notification settings - Fork 343
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hw jv2 #320
base: main
Are you sure you want to change the base?
Hw jv2 #320
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<CategoryRequestDto, CategoryResponseDto, Category> 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); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<ProductRequestDto, ProductResponseDto, Product> 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<ProductResponseDto> 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<ProductResponseDto> getAllByCategories(@RequestParam String[] categoriesId) { | ||
return productService.findAllByCategoryNameIn(List.of(categoriesId)).stream() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Naming inconsistency. It is not clear from the naming of variables the method should receive category names or category ids? |
||
.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); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package mate.academy.springboot.datajpa.mapper; | ||
|
||
public interface DtoMapper<D, R, T> extends RequestDtoMapper<D, T>, ResponseDtoMapper<T, R> { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package mate.academy.springboot.datajpa.mapper; | ||
|
||
public interface RequestDtoMapper<D, T> { | ||
T mapToModel(D dto); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package mate.academy.springboot.datajpa.mapper; | ||
|
||
public interface ResponseDtoMapper<T, D> { | ||
D mapToDto(T t); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<CategoryRequestDto, | ||
CategoryResponseDto, Category> { | ||
@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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<ProductRequestDto, ProductResponseDto, Product> { | ||
@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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
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.Table; | ||
import lombok.Data; | ||
|
||
@Data | ||
@Entity | ||
@Table(name = "categories") | ||
public class Category { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
private String name; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's try to not use Data annotaion for entities. Better to use Getter Setter EqualsAndHashCode etc. |
||
@Entity | ||
@Table(name = "products") | ||
public class Product { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
private String title; | ||
private BigDecimal price; | ||
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.MERGE) | ||
@EqualsAndHashCode.Exclude | ||
private Category category; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 String name; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package mate.academy.springboot.datajpa.model.dto.response; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class CategoryResponseDto { | ||
private Long id; | ||
private String name; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Category, Long> { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Product, Long> { | ||
List<Product> findAllByPriceBetween(BigDecimal from, BigDecimal to); | ||
|
||
List<Product> findAllByCategoryNameIn(List<String> categoryName); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package mate.academy.springboot.datajpa.service; | ||
|
||
import mate.academy.springboot.datajpa.model.Category; | ||
|
||
public interface CategoryService extends GeneralService<Category> { | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,9 @@ | ||||||
package mate.academy.springboot.datajpa.service; | ||||||
|
||||||
public interface GeneralService<T> { | ||||||
T create(T model); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's rename this method for better matching what this method do and better code readability
Suggested change
|
||||||
|
||||||
T get(Long id); | ||||||
|
||||||
void remove(Long id); | ||||||
} |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -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 GeneralService<Product> { | ||||
List<Product> findAllByPriceBetween(BigDecimal from, BigDecimal to); | ||||
|
||||
List<Product> findAllByCategoryNameIn(List<String> categoryName); | ||||
|
||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use plural in the end of the variable names instead of in the middle, e.g.
categoryIds
orcategoryNames
.Please fix the similar cases in the code.