forked from mate-academy/jv-springboot-data-jpa
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8de1ae2
commit 05e8a2e
Showing
22 changed files
with
562 additions
and
8 deletions.
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
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
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
25 changes: 25 additions & 0 deletions
25
src/main/java/mate/academy/springboot/datajpa/config/SpringFoxConfig.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,25 @@ | ||
package mate.academy.springboot.datajpa.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Import; | ||
import springfox.documentation.builders.PathSelectors; | ||
import springfox.documentation.builders.RequestHandlerSelectors; | ||
import springfox.documentation.spi.DocumentationType; | ||
import springfox.documentation.spring.data.rest.configuration.SpringDataRestConfiguration; | ||
import springfox.documentation.spring.web.plugins.Docket; | ||
import springfox.documentation.swagger2.annotations.EnableSwagger2; | ||
|
||
@EnableSwagger2 | ||
@Configuration | ||
@Import(SpringDataRestConfiguration.class) | ||
public class SpringFoxConfig { | ||
@Bean | ||
public Docket api() { | ||
return new Docket(DocumentationType.SWAGGER_2) | ||
.select() | ||
.apis(RequestHandlerSelectors.basePackage("mate.academy.springboot.datajpa")) | ||
.paths(PathSelectors.any()) | ||
.build(); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
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,50 @@ | ||
package mate.academy.springboot.datajpa.controller; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import mate.academy.springboot.datajpa.dto.request.CategoryRequestDto; | ||
import mate.academy.springboot.datajpa.dto.response.CategoryResponseDto; | ||
import mate.academy.springboot.datajpa.model.Category; | ||
import mate.academy.springboot.datajpa.services.CategoryService; | ||
import mate.academy.springboot.datajpa.services.mapper.CategoryMapper; | ||
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 CategoryMapper categoryMapper; | ||
|
||
@PostMapping | ||
public CategoryResponseDto create(@RequestBody | ||
CategoryRequestDto categoryRequestDto) { | ||
return categoryMapper.toDto( | ||
categoryService.create( | ||
categoryMapper.toModel(categoryRequestDto))); | ||
} | ||
|
||
@GetMapping("/{id}") | ||
public CategoryResponseDto getById(@PathVariable Long id) { | ||
return categoryMapper.toDto(categoryService.getById(id)); | ||
} | ||
|
||
@DeleteMapping("/{id}") | ||
public void deleteById(@PathVariable Long id) { | ||
categoryService.deleteById(id); | ||
} | ||
|
||
@PutMapping("/{id}") | ||
public CategoryResponseDto update(@PathVariable Long id, | ||
@RequestBody CategoryRequestDto categoryRequestDto) { | ||
Category category = categoryService.getById(id); | ||
category.setName(categoryRequestDto.getName()); | ||
return categoryMapper.toDto(categoryService.create(category)); | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
src/main/java/mate/academy/springboot/datajpa/controller/InjectController.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,68 @@ | ||
package mate.academy.springboot.datajpa.controller; | ||
|
||
import java.math.BigDecimal; | ||
import lombok.RequiredArgsConstructor; | ||
import mate.academy.springboot.datajpa.model.Category; | ||
import mate.academy.springboot.datajpa.model.Product; | ||
import mate.academy.springboot.datajpa.services.CategoryService; | ||
import mate.academy.springboot.datajpa.services.ProductService; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
public class InjectController { | ||
private final ProductService productService; | ||
private final CategoryService categoryService; | ||
|
||
@GetMapping("/inject") | ||
public String inject() { | ||
Category product = new Category(); | ||
product.setName("products"); | ||
product = categoryService.create(product); | ||
|
||
Category toys = new Category(); | ||
toys.setName("toys"); | ||
toys = categoryService.create(toys); | ||
|
||
Category drinks = new Category(); | ||
drinks.setName("drinks"); | ||
drinks = categoryService.create(drinks); | ||
|
||
Category smartPhone = new Category(); | ||
smartPhone.setName("smartPhone"); | ||
smartPhone = categoryService.create(smartPhone); | ||
|
||
Product pizza = new Product(); | ||
pizza.setCategory(product); | ||
pizza.setPrice(BigDecimal.valueOf(1.4)); | ||
pizza.setTitle("pizza"); | ||
pizza = productService.create(pizza); | ||
|
||
Product bear = new Product(); | ||
bear.setCategory(toys); | ||
bear.setPrice(BigDecimal.valueOf(2.1)); | ||
bear.setTitle("bear"); | ||
bear = productService.create(bear); | ||
|
||
Product coffee = new Product(); | ||
coffee.setCategory(drinks); | ||
coffee.setPrice(BigDecimal.valueOf(1.13)); | ||
coffee.setTitle("coffee"); | ||
coffee = productService.create(coffee); | ||
|
||
Product phone = new Product(); | ||
phone.setCategory(smartPhone); | ||
phone.setPrice(BigDecimal.valueOf(124.5)); | ||
phone.setTitle("phone"); | ||
phone = productService.create(phone); | ||
|
||
Product beer = new Product(); | ||
beer.setCategory(drinks); | ||
beer.setPrice(BigDecimal.valueOf(2.1)); | ||
beer.setTitle("beer"); | ||
beer = productService.create(beer); | ||
|
||
return "categories and products was created"; | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
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,80 @@ | ||
package mate.academy.springboot.datajpa.controller; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.Arrays; | ||
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.model.Category; | ||
import mate.academy.springboot.datajpa.model.Product; | ||
import mate.academy.springboot.datajpa.services.CategoryService; | ||
import mate.academy.springboot.datajpa.services.ProductService; | ||
import mate.academy.springboot.datajpa.services.mapper.ProductMapper; | ||
import org.springframework.http.HttpStatus; | ||
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.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/products") | ||
@RequiredArgsConstructor | ||
public class ProductController { | ||
private final ProductService productService; | ||
private final ProductMapper productMapper; | ||
private final CategoryService categoryService; | ||
|
||
@PostMapping | ||
public ProductResponseDto create(@RequestBody ProductRequestDto productRequestDto) { | ||
return productMapper.toDto( | ||
productService.create( | ||
productMapper.toModel(productRequestDto))); | ||
} | ||
|
||
@GetMapping("/{id}") | ||
public ProductResponseDto getById(@PathVariable Long id) { | ||
return productMapper.toDto(productService.getById(id)); | ||
} | ||
|
||
@DeleteMapping("/{id}") | ||
@ResponseStatus(HttpStatus.OK) | ||
public void delete(@PathVariable Long id) { | ||
productService.deleteById(id); | ||
} | ||
|
||
@PutMapping("/{id}") | ||
public ProductResponseDto update(@PathVariable Long id, | ||
@RequestBody ProductRequestDto productRequestDto) { | ||
Product product = productMapper.toModel(productRequestDto); | ||
product.setId(id); | ||
return productMapper.toDto(productService.create(product)); | ||
} | ||
|
||
@GetMapping("/by-price") | ||
public List<ProductResponseDto> getAllProductsWithPriceBetween(@RequestParam BigDecimal from, | ||
@RequestParam BigDecimal to) { | ||
return productService.findAllByPriceBetween(from, to).stream() | ||
.map(p -> productMapper.toDto(p)) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
@GetMapping("/by-categories") | ||
public List<ProductResponseDto> getAllWithCategory(@RequestParam String category) { | ||
List<Long> categoriesId = categoryService.getCategoriesByNameIn( | ||
Arrays.asList(category.split(","))) | ||
.stream() | ||
.map(Category::getId) | ||
.collect(Collectors.toList()); | ||
return productService.getAllWithCategories(categoriesId).stream() | ||
.map(productMapper::toDto) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
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,8 @@ | ||
package mate.academy.springboot.datajpa.dto.request; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class CategoryRequestDto { | ||
private String name; | ||
} |
11 changes: 11 additions & 0 deletions
11
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,11 @@ | ||
package mate.academy.springboot.datajpa.dto.request; | ||
|
||
import java.math.BigDecimal; | ||
import lombok.Data; | ||
|
||
@Data | ||
public class ProductRequestDto { | ||
private String title; | ||
private BigDecimal price; | ||
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 String title; | ||
private BigDecimal price; | ||
private Long categoryId; | ||
} |
18 changes: 18 additions & 0 deletions
18
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,18 @@ | ||
package mate.academy.springboot.datajpa.model; | ||
|
||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.GenerationType; | ||
import javax.persistence.Id; | ||
import javax.persistence.Table; | ||
import lombok.Data; | ||
|
||
@Entity | ||
@Data | ||
@Table(name = "categories") | ||
public class Category { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
private String name; | ||
} |
Oops, something went wrong.