-
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
Completed all points in Readme #330
Open
VRuban373
wants to merge
1
commit into
mate-academy:main
Choose a base branch
from
VRuban373:hwjpa
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
Changes from all commits
Commits
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
49 changes: 49 additions & 0 deletions
49
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,49 @@ | ||
package mate.academy.springboot.datajpa.controller; | ||
|
||
import javax.validation.Valid; | ||
import lombok.AllArgsConstructor; | ||
import mate.academy.springboot.datajpa.dto.request.CategoryRequestDto; | ||
import mate.academy.springboot.datajpa.dto.response.CategoryResponseDto; | ||
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.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; | ||
|
||
@AllArgsConstructor | ||
@RestController | ||
@RequestMapping("/categories") | ||
public class CategoryController { | ||
private final CategoryService categoryService; | ||
private final DtoMapper<CategoryRequestDto, CategoryResponseDto, Category> categoryMapper; | ||
|
||
@PostMapping | ||
public CategoryResponseDto create(@RequestBody @Valid CategoryRequestDto categoryRequestDto) { | ||
return categoryMapper.mapToDto(categoryService.save( | ||
categoryMapper.mapToEntity(categoryRequestDto))); | ||
} | ||
|
||
@GetMapping("/{id}") | ||
public CategoryResponseDto get(@PathVariable Long id) { | ||
return categoryMapper.mapToDto(categoryService.getById(id)); | ||
} | ||
|
||
@PutMapping("/{id}") | ||
public CategoryResponseDto update(@PathVariable Long id, | ||
@RequestBody @Valid CategoryRequestDto categoryRequestDto) { | ||
Category category = categoryMapper.mapToEntity(categoryRequestDto); | ||
category.setId(id); | ||
return categoryMapper.mapToDto(categoryService.save(category)); | ||
} | ||
|
||
@DeleteMapping("/{id}") | ||
public void delete(@PathVariable Long id) { | ||
categoryService.deleteById(id); | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
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,70 @@ | ||
package mate.academy.springboot.datajpa.controller; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import javax.validation.Valid; | ||
import javax.validation.constraints.Positive; | ||
import lombok.AllArgsConstructor; | ||
import mate.academy.springboot.datajpa.dto.request.ProductRequestDto; | ||
import mate.academy.springboot.datajpa.dto.response.ProductResponseDto; | ||
import mate.academy.springboot.datajpa.mapper.DtoMapper; | ||
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; | ||
|
||
@AllArgsConstructor | ||
@RestController | ||
@RequestMapping("/products") | ||
public class ProductController { | ||
private final ProductService productService; | ||
private final DtoMapper<ProductRequestDto, ProductResponseDto, Product> productMapper; | ||
|
||
@PostMapping | ||
public ProductResponseDto create(@RequestBody @Valid ProductRequestDto productRequestDto) { | ||
Product product = productMapper.mapToEntity(productRequestDto); | ||
return productMapper.mapToDto(productService.save(product)); | ||
} | ||
|
||
@GetMapping("/{id}") | ||
public ProductResponseDto get(@PathVariable Long id) { | ||
return productMapper.mapToDto(productService.getById(id)); | ||
} | ||
|
||
@GetMapping("/by-price") | ||
public List<ProductResponseDto> getAllByPrice(@RequestParam @Positive BigDecimal from, | ||
@RequestParam @Positive BigDecimal to) { | ||
return productService.getAllByPricePriceBetween(from, to).stream() | ||
.map(productMapper::mapToDto) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
@GetMapping("/by-categories") | ||
public List<ProductResponseDto> getAllByCategories(@RequestParam Set<String> categoryIds) { | ||
return productService.getAllByCategoryNameIn(categoryIds).stream() | ||
.map(productMapper::mapToDto) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
@PutMapping("/{id}") | ||
public ProductResponseDto update(@PathVariable Long id, | ||
@RequestBody @Valid ProductRequestDto productRequestDto) { | ||
Product product = productMapper.mapToEntity(productRequestDto); | ||
product.setId(id); | ||
return productMapper.mapToDto(productService.save(product)); | ||
} | ||
|
||
@DeleteMapping("/{id}") | ||
public void delete(@PathVariable Long id) { | ||
productService.deleteById(id); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
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,10 @@ | ||
package mate.academy.springboot.datajpa.dto.request; | ||
|
||
import javax.validation.constraints.NotNull; | ||
import lombok.Data; | ||
|
||
@Data | ||
public class CategoryRequestDto { | ||
@NotNull | ||
private String name; | ||
} |
16 changes: 16 additions & 0 deletions
16
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,16 @@ | ||
package mate.academy.springboot.datajpa.dto.request; | ||
|
||
import java.math.BigDecimal; | ||
import javax.validation.constraints.NotNull; | ||
import javax.validation.constraints.Positive; | ||
import lombok.Data; | ||
|
||
@Data | ||
public class ProductRequestDto { | ||
@Positive | ||
private BigDecimal price; | ||
@NotNull | ||
private String title; | ||
@Positive | ||
private Long categoryId; | ||
} |
9 changes: 9 additions & 0 deletions
9
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,9 @@ | ||
package mate.academy.springboot.datajpa.dto.response; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class CategoryResponseDto { | ||
private Long id; | ||
private String name; | ||
} |
12 changes: 12 additions & 0 deletions
12
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,12 @@ | ||
package mate.academy.springboot.datajpa.dto.response; | ||
|
||
import java.math.BigDecimal; | ||
import lombok.Data; | ||
|
||
@Data | ||
public class ProductResponseDto { | ||
private Long id; | ||
private BigDecimal price; | ||
private String title; | ||
private Long categoriesId; | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/mate/academy/springboot/datajpa/mapper/DtoMapper.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,4 @@ | ||
package mate.academy.springboot.datajpa.mapper; | ||
|
||
public interface DtoMapper<D, R, T> extends RequestDtoMapper<D, T>, ResponseDtoMapper<T, R> { | ||
} |
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 mapToEntity(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<T, D> { | ||
D mapToDto(T entity); | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/mate/academy/springboot/datajpa/mapper/impl/CategoryMapper.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,26 @@ | ||
package mate.academy.springboot.datajpa.mapper.impl; | ||
|
||
import mate.academy.springboot.datajpa.dto.request.CategoryRequestDto; | ||
import mate.academy.springboot.datajpa.dto.response.CategoryResponseDto; | ||
import mate.academy.springboot.datajpa.mapper.DtoMapper; | ||
import mate.academy.springboot.datajpa.model.Category; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class CategoryMapper implements DtoMapper<CategoryRequestDto, | ||
CategoryResponseDto, Category> { | ||
@Override | ||
public Category mapToEntity(CategoryRequestDto dto) { | ||
Category category = new Category(); | ||
category.setName(dto.getName()); | ||
return category; | ||
} | ||
|
||
@Override | ||
public CategoryResponseDto mapToDto(Category entity) { | ||
CategoryResponseDto categoryResponseDto = new CategoryResponseDto(); | ||
categoryResponseDto.setId(entity.getId()); | ||
categoryResponseDto.setName(entity.getName()); | ||
return categoryResponseDto; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/mate/academy/springboot/datajpa/mapper/impl/ProductMapper.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,30 @@ | ||
package mate.academy.springboot.datajpa.mapper.impl; | ||
|
||
import mate.academy.springboot.datajpa.dto.request.ProductRequestDto; | ||
import mate.academy.springboot.datajpa.dto.response.ProductResponseDto; | ||
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 ProductMapper implements DtoMapper<ProductRequestDto, ProductResponseDto, Product> { | ||
@Override | ||
public Product mapToEntity(ProductRequestDto dto) { | ||
Product product = new Product(); | ||
product.setPrice(dto.getPrice()); | ||
product.setTitle(dto.getTitle()); | ||
product.setCategory(new Category(dto.getCategoryId())); | ||
return product; | ||
} | ||
|
||
@Override | ||
public ProductResponseDto mapToDto(Product entity) { | ||
ProductResponseDto productResponseDto = new ProductResponseDto(); | ||
productResponseDto.setId(entity.getId()); | ||
productResponseDto.setTitle(entity.getTitle()); | ||
productResponseDto.setPrice(entity.getPrice()); | ||
productResponseDto.setCategoriesId(entity.getCategory().getId()); | ||
return productResponseDto; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
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,27 @@ | ||
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.EqualsAndHashCode; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@EqualsAndHashCode | ||
@Entity | ||
@Table(name = "categories") | ||
public class Category { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
private String name; | ||
|
||
public Category(Long categoryId) { | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
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,28 @@ | ||
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.EqualsAndHashCode; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
@EqualsAndHashCode | ||
@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) | ||
private Category category; | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/mate/academy/springboot/datajpa/repository/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.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> { | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/mate/academy/springboot/datajpa/repository/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,15 @@ | ||
package mate.academy.springboot.datajpa.repository; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.Collection; | ||
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> getAllProductsByPriceBetween(BigDecimal from, BigDecimal to); | ||
|
||
List<Product> getAllProductsByCategoryNameIn(Collection<String> categoriesName); | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/mate/academy/springboot/datajpa/service/CategoryService.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.service; | ||
|
||
import mate.academy.springboot.datajpa.model.Category; | ||
|
||
public interface CategoryService { | ||
Category save(Category categoryMapper); | ||
|
||
Category getById(Long id); | ||
|
||
void deleteById(Long id); | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/mate/academy/springboot/datajpa/service/ProductService.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,20 @@ | ||
package mate.academy.springboot.datajpa.service; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.List; | ||
import java.util.Set; | ||
import mate.academy.springboot.datajpa.model.Product; | ||
|
||
public interface ProductService { | ||
Product save(Product product); | ||
|
||
Product getById(Long id); | ||
|
||
void deleteById(Long id); | ||
|
||
List<Product> getAll(); | ||
|
||
List<Product> getAllByPricePriceBetween(BigDecimal from, BigDecimal to); | ||
|
||
List<Product> getAllByCategoryNameIn(Set<String> categories); | ||
} |
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.
I'd recommend using specific Lombok annotations here instead of general Data. You can fix the same issue in all places