-
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
done #325
base: main
Are you sure you want to change the base?
done #325
Changes from all 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,47 @@ | ||
package mate.academy.springboot.datajpa.controller; | ||
|
||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import mate.academy.springboot.datajpa.dto.request.RequestCategoryDto; | ||
import mate.academy.springboot.datajpa.dto.response.ResponseCategoryDto; | ||
import mate.academy.springboot.datajpa.mapper.DtoMapper; | ||
import mate.academy.springboot.datajpa.model.Category; | ||
import mate.academy.springboot.datajpa.service.CategoryService; | ||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PatchMapping; | ||
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 org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/categories") | ||
@RequiredArgsConstructor | ||
public class CategoryController { | ||
private final DtoMapper<RequestCategoryDto, ResponseCategoryDto, Category> mapper; | ||
private final CategoryService categoryService; | ||
|
||
@PostMapping | ||
public ResponseCategoryDto create(@RequestParam RequestCategoryDto categoryDto) { | ||
Category category = categoryService.save(mapper.mapToModel(categoryDto)); | ||
return mapper.mapToDto(category); | ||
} | ||
|
||
@GetMapping | ||
public ResponseCategoryDto get(@PathVariable Long id) { | ||
return mapper.mapToDto(categoryService.getId(id)); | ||
} | ||
|
||
@DeleteMapping | ||
public void delete(@PathVariable Long id) { | ||
categoryService.delete(id); | ||
} | ||
|
||
@PatchMapping | ||
public ResponseCategoryDto update(@RequestBody @Valid RequestCategoryDto dto) { | ||
return mapper.mapToDto(categoryService.update(mapper.mapToModel(dto))); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package mate.academy.springboot.datajpa.controller; | ||
|
||
import jakarta.validation.Valid; | ||
import jakarta.validation.constraints.Positive; | ||
import java.math.BigDecimal; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import mate.academy.springboot.datajpa.dto.request.RequestProductDto; | ||
import mate.academy.springboot.datajpa.dto.response.ResponseProductDto; | ||
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.service.ProductService; | ||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PatchMapping; | ||
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 org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/products") | ||
@RequiredArgsConstructor | ||
public class ProductController { | ||
private final DtoMapper<RequestProductDto, ResponseProductDto, Product> mapper; | ||
private final ProductService productService; | ||
|
||
@PostMapping | ||
public ResponseProductDto create(@RequestParam RequestProductDto productDto) { | ||
Product product = productService.save(mapper.mapToModel(productDto)); | ||
return mapper.mapToDto(product); | ||
} | ||
|
||
@GetMapping("/{id}") | ||
public ResponseProductDto get(@PathVariable Long id) { | ||
return mapper.mapToDto(productService.getId(id)); | ||
} | ||
|
||
@DeleteMapping("/{id}") | ||
public void delete(@PathVariable Long id) { | ||
productService.delete(id); | ||
} | ||
|
||
@PatchMapping | ||
public ResponseProductDto update(@RequestBody @Valid RequestProductDto dto) { | ||
return mapper.mapToDto(productService.update(mapper.mapToModel(dto))); | ||
} | ||
|
||
@GetMapping("/price") | ||
List<ResponseProductDto> getAllByPriceBetween(@RequestParam @Positive BigDecimal from, | ||
@RequestParam @Positive BigDecimal to) { | ||
return productService.getAllByPriceBetween(from, to).stream() | ||
.map(mapper::mapToDto) | ||
.toList(); | ||
} | ||
|
||
@GetMapping("/categories") | ||
List<ResponseProductDto> getAllByCategoryIn(@RequestParam List<Category> categories) { | ||
return productService.getAllByCategoryIn(categories).stream() | ||
.map(mapper::mapToDto) | ||
.toList(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package mate.academy.springboot.datajpa.dto.request; | ||
|
||
import jakarta.validation.constraints.NotNull; | ||
import lombok.Data; | ||
|
||
@Data | ||
public class RequestCategoryDto { | ||
@NotNull | ||
private String name; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package mate.academy.springboot.datajpa.dto.request; | ||
|
||
import jakarta.validation.constraints.NotNull; | ||
import jakarta.validation.constraints.Positive; | ||
import java.math.BigDecimal; | ||
import lombok.Data; | ||
|
||
@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 not to use Data annotation for entities. It's better to use Getter Setter etc. |
||
public class RequestProductDto { | ||
@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.dto.response; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class ResponseCategoryDto { | ||
private Long id; | ||
private String name; | ||
} |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,14 @@ | ||||
package mate.academy.springboot.datajpa.dto.response; | ||||
|
||||
import java.math.BigDecimal; | ||||
import lombok.Data; | ||||
import mate.academy.springboot.datajpa.model.Category; | ||||
|
||||
@Data | ||||
public class ResponseProductDto { | ||||
private Long id; | ||||
private String title; | ||||
private BigDecimal price; | ||||
private Category category; | ||||
} | ||||
|
||||
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
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
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,27 @@ | ||||
package mate.academy.springboot.datajpa.mapper.impl; | ||||
|
||||
import mate.academy.springboot.datajpa.dto.request.RequestCategoryDto; | ||||
import mate.academy.springboot.datajpa.dto.response.ResponseCategoryDto; | ||||
import mate.academy.springboot.datajpa.mapper.DtoMapper; | ||||
import mate.academy.springboot.datajpa.model.Category; | ||||
import org.springframework.stereotype.Component; | ||||
|
||||
@Component | ||||
public class CategoryMapperImpl implements DtoMapper<RequestCategoryDto, | ||||
ResponseCategoryDto, Category> { | ||||
|
||||
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
fix the same issue in all places |
||||
@Override | ||||
public Category mapToModel(RequestCategoryDto dto) { | ||||
Category category = new Category(); | ||||
category.setName(dto.getName()); | ||||
return category; | ||||
} | ||||
|
||||
@Override | ||||
public ResponseCategoryDto mapToDto(Category category) { | ||||
ResponseCategoryDto dto = new ResponseCategoryDto(); | ||||
dto.setId(category.getId()); | ||||
dto.setName(category.getName()); | ||||
return dto; | ||||
} | ||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package mate.academy.springboot.datajpa.mapper.impl; | ||
|
||
import mate.academy.springboot.datajpa.dto.request.RequestProductDto; | ||
import mate.academy.springboot.datajpa.dto.response.ResponseProductDto; | ||
import mate.academy.springboot.datajpa.mapper.DtoMapper; | ||
import mate.academy.springboot.datajpa.model.Category; | ||
import mate.academy.springboot.datajpa.model.Product; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class ProductMapperImpl implements | ||
DtoMapper<RequestProductDto, ResponseProductDto, Product> { | ||
@Override | ||
public Product mapToModel(RequestProductDto 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 ResponseProductDto mapToDto(Product product) { | ||
ResponseProductDto dto = new ResponseProductDto(); | ||
dto.setId(product.getId()); | ||
dto.setTitle(product.getTitle()); | ||
dto.setPrice(product.getPrice()); | ||
dto.setCategory(product.getCategory()); | ||
return dto; | ||
} | ||
} |
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; | ||
|
||
@Entity | ||
@Data | ||
@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,23 @@ | ||
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 jakarta.persistence.Table; | ||
import java.math.BigDecimal; | ||
import lombok.Data; | ||
|
||
@Entity | ||
@Data | ||
@Table(name = "products") | ||
public class Product { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
private String title; | ||
private BigDecimal price; | ||
@ManyToOne | ||
private Category category; | ||
} |
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,15 @@ | ||
package mate.academy.springboot.datajpa.repository; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.List; | ||
import mate.academy.springboot.datajpa.model.Category; | ||
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> getAllByPriceBetween(BigDecimal from, BigDecimal to); | ||
|
||
List<Product> getAllByCategoryIn(List<Category> categories); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package mate.academy.springboot.datajpa.service; | ||
|
||
import mate.academy.springboot.datajpa.model.Category; | ||
|
||
public interface CategoryService { | ||
Category save(Category category); | ||
|
||
Category getId(Long id); | ||
|
||
void delete(Long id); | ||
|
||
Category update(Category model); | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package mate.academy.springboot.datajpa.service; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.List; | ||
import mate.academy.springboot.datajpa.model.Category; | ||
import mate.academy.springboot.datajpa.model.Product; | ||
|
||
public interface ProductService { | ||
Product save(Product product); | ||
|
||
Product getId(Long id); | ||
|
||
void delete(Long id); | ||
|
||
Product update(Product model); | ||
|
||
List<Product> findAll(); | ||
|
||
List<Product> getAllByPriceBetween(BigDecimal from, BigDecimal to); | ||
|
||
List<Product> getAllByCategoryIn(List<Category> categories); | ||
} |
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.
don't you forget to specify path variable within URL here? Fix the same issue in all places