-
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
My branch #322
Open
V-Solianyk
wants to merge
4
commits into
mate-academy:main
Choose a base branch
from
V-Solianyk:my_branch
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
My branch #322
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/main/java/mate/academy/springboot/datajpa/controller/CategoryController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package mate.academy.springboot.datajpa.controller; | ||
|
||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import mate.academy.springboot.datajpa.dto.request.CategoryRequestDto; | ||
import mate.academy.springboot.datajpa.dto.response.CategoryResponseDto; | ||
import mate.academy.springboot.datajpa.mapper.RequestDtoMapper; | ||
import mate.academy.springboot.datajpa.mapper.ResponseDtoMapper; | ||
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.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") | ||
@RequiredArgsConstructor | ||
public class CategoryController { | ||
private final CategoryService categoryService; | ||
private final ResponseDtoMapper<CategoryResponseDto, Category> responseDtoMapper; | ||
private final RequestDtoMapper<CategoryRequestDto, Category> requestDtoMapper; | ||
|
||
@PostMapping | ||
public CategoryResponseDto create(@RequestBody @Valid CategoryRequestDto requestDto) { | ||
Category category = categoryService.create(requestDtoMapper | ||
.mapToModel(requestDto)); | ||
return responseDtoMapper.mapToDto(category); | ||
} | ||
|
||
@GetMapping("/{id}") | ||
public CategoryResponseDto get(@PathVariable Long id) { | ||
Category category = categoryService.getById(id); | ||
return responseDtoMapper.mapToDto(category); | ||
} | ||
|
||
@DeleteMapping("/{id}") | ||
public void delete(@PathVariable Long id) { | ||
categoryService.delete(id); | ||
} | ||
|
||
@PutMapping("/{id}") | ||
public CategoryResponseDto update(@RequestBody @Valid CategoryRequestDto requestDto, | ||
@PathVariable Long id) { | ||
Category category = categoryService.update(requestDtoMapper.mapToModel(requestDto), id); | ||
return responseDtoMapper.mapToDto(category); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
src/main/java/mate/academy/springboot/datajpa/controller/ProductController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package mate.academy.springboot.datajpa.controller; | ||
|
||
import jakarta.validation.Valid; | ||
import java.math.BigDecimal; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import lombok.RequiredArgsConstructor; | ||
import mate.academy.springboot.datajpa.dto.request.ProductRequestDto; | ||
import mate.academy.springboot.datajpa.dto.response.ProductResponseDto; | ||
import mate.academy.springboot.datajpa.mapper.RequestDtoMapper; | ||
import mate.academy.springboot.datajpa.mapper.ResponseDtoMapper; | ||
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.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") | ||
@RequiredArgsConstructor | ||
public class ProductController { | ||
private final ProductService productService; | ||
private final ResponseDtoMapper<ProductResponseDto, Product> responseDtoMapper; | ||
private final RequestDtoMapper<ProductRequestDto, Product> requestDtoMapper; | ||
|
||
@PostMapping | ||
public ProductResponseDto create(@RequestBody @Valid ProductRequestDto productRequestDto) { | ||
Product product = productService.create(requestDtoMapper.mapToModel(productRequestDto)); | ||
return responseDtoMapper.mapToDto(product); | ||
} | ||
|
||
@GetMapping("/{id}") | ||
public ProductResponseDto getById(@PathVariable Long id) { | ||
Product product = productService.getById(id); | ||
return responseDtoMapper.mapToDto(product); | ||
} | ||
|
||
@DeleteMapping("/{id}") | ||
public void delete(@PathVariable Long id) { | ||
productService.delete(id); | ||
} | ||
|
||
@PutMapping("/{id}") | ||
public ProductResponseDto update(@PathVariable Long id, | ||
@RequestBody @Valid ProductRequestDto productRequestDto) { | ||
Product product = productService.update(requestDtoMapper | ||
.mapToModel(productRequestDto), id); | ||
return responseDtoMapper.mapToDto(product); | ||
} | ||
|
||
@GetMapping("/price") | ||
public List<ProductResponseDto> getAllByPriceBetween(@RequestParam BigDecimal from, | ||
@RequestParam BigDecimal to) { | ||
List<Product> products = productService.getAllByPriceBetween(from, to); | ||
return products.stream() | ||
.map(responseDtoMapper::mapToDto) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
@GetMapping("/category") | ||
public List<ProductResponseDto> getAllByCategories(@RequestParam List<String> categoryNames) { | ||
List<Product> products = productService.getAllByCategoryNames(categoryNames); | ||
return products.stream() | ||
.map(responseDtoMapper::mapToDto) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/mate/academy/springboot/datajpa/dao/CategoryRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package mate.academy.springboot.datajpa.dao; | ||
|
||
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> { | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/mate/academy/springboot/datajpa/dao/ProductRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package mate.academy.springboot.datajpa.dao; | ||
|
||
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> findByCategoryNameIn(List<String> categoryNames); | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/mate/academy/springboot/datajpa/dto/request/CategoryRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package mate.academy.springboot.datajpa.dto.request; | ||
|
||
import jakarta.validation.constraints.NotNull; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
public class CategoryRequestDto { | ||
@NotNull | ||
private String name; | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/mate/academy/springboot/datajpa/dto/request/ProductRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package mate.academy.springboot.datajpa.dto.request; | ||
|
||
import jakarta.validation.constraints.NotNull; | ||
import jakarta.validation.constraints.Positive; | ||
import java.math.BigDecimal; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Setter | ||
@Getter | ||
public class ProductRequestDto { | ||
@NotNull | ||
private String title; | ||
@Positive | ||
@NotNull | ||
private BigDecimal price; | ||
@NotNull | ||
private Long categoryId; | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/mate/academy/springboot/datajpa/dto/response/CategoryResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package mate.academy.springboot.datajpa.dto.response; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Setter | ||
@Getter | ||
public class CategoryResponseDto { | ||
private Long id; | ||
private String name; | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/mate/academy/springboot/datajpa/dto/response/ProductResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
public class ProductResponseDto { | ||
private Long id; | ||
private String title; | ||
private BigDecimal price; | ||
private Long categoryId; | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/mate/academy/springboot/datajpa/mapper/RequestDtoMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/mate/academy/springboot/datajpa/mapper/ResponseDtoMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package mate.academy.springboot.datajpa.mapper; | ||
|
||
public interface ResponseDtoMapper<D, T> { | ||
D mapToDto(T model); | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/mate/academy/springboot/datajpa/mapper/impl/CategoryRequestDtoMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package mate.academy.springboot.datajpa.mapper.impl; | ||
|
||
import mate.academy.springboot.datajpa.dto.request.CategoryRequestDto; | ||
import mate.academy.springboot.datajpa.mapper.RequestDtoMapper; | ||
import mate.academy.springboot.datajpa.model.Category; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class CategoryRequestDtoMapper implements RequestDtoMapper<CategoryRequestDto, Category> { | ||
@Override | ||
public Category mapToModel(CategoryRequestDto dto) { | ||
Category category = new Category(); | ||
category.setName(dto.getName()); | ||
return category; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/mate/academy/springboot/datajpa/mapper/impl/CategoryResponseDtoMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package mate.academy.springboot.datajpa.mapper.impl; | ||
|
||
import mate.academy.springboot.datajpa.dto.response.CategoryResponseDto; | ||
import mate.academy.springboot.datajpa.mapper.ResponseDtoMapper; | ||
import mate.academy.springboot.datajpa.model.Category; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class CategoryResponseDtoMapper implements ResponseDtoMapper<CategoryResponseDto, Category> { | ||
@Override | ||
public CategoryResponseDto mapToDto(Category category) { | ||
CategoryResponseDto categoryResponseDto = new CategoryResponseDto(); | ||
categoryResponseDto.setId(category.getId()); | ||
categoryResponseDto.setName(category.getName()); | ||
return categoryResponseDto; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/mate/academy/springboot/datajpa/mapper/impl/ProductRequestDtoMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package mate.academy.springboot.datajpa.mapper.impl; | ||
|
||
import mate.academy.springboot.datajpa.dto.request.ProductRequestDto; | ||
import mate.academy.springboot.datajpa.mapper.RequestDtoMapper; | ||
import mate.academy.springboot.datajpa.model.Category; | ||
import mate.academy.springboot.datajpa.model.Product; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class ProductRequestDtoMapper implements RequestDtoMapper<ProductRequestDto, Product> { | ||
@Override | ||
public Product mapToModel(ProductRequestDto dto) { | ||
Product product = new Product(); | ||
product.setTitle(dto.getTitle()); | ||
product.setPrice(dto.getPrice()); | ||
product.setCategory(new Category(dto.getCategoryId())); | ||
return product; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/mate/academy/springboot/datajpa/mapper/impl/ProductResponseDtoMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package mate.academy.springboot.datajpa.mapper.impl; | ||
|
||
import mate.academy.springboot.datajpa.dto.response.ProductResponseDto; | ||
import mate.academy.springboot.datajpa.mapper.ResponseDtoMapper; | ||
import mate.academy.springboot.datajpa.model.Product; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class ProductResponseDtoMapper implements ResponseDtoMapper<ProductResponseDto, 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; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/mate/academy/springboot/datajpa/model/Category.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
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.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
import lombok.ToString; | ||
|
||
@Entity | ||
@Table(name = "categories") | ||
@Setter | ||
@Getter | ||
@NoArgsConstructor | ||
@ToString | ||
public class Category { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
private String name; | ||
|
||
public Category(Long id) { | ||
this.id = id; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/mate/academy/springboot/datajpa/model/Product.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package mate.academy.springboot.datajpa.model; | ||
|
||
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.Getter; | ||
import lombok.Setter; | ||
|
||
@Entity | ||
@Table(name = "products") | ||
@Setter | ||
@Getter | ||
public class Product { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
private String title; | ||
private BigDecimal price; | ||
@ManyToOne(fetch = FetchType.LAZY) | ||
private Category category; | ||
|
||
@Override | ||
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. you can use @tostring 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. |
||
public String toString() { | ||
return "Product{" | ||
+ "id=" + id | ||
+ ", title='" + title + '\'' | ||
+ ", price=" + price | ||
+ '}'; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
it's not a mistake, but you can use @requiredargsconstructor instead of creating a constructor