-
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
Homework: JV Spring Boot Data JPA #326
base: main
Are you sure you want to change the base?
Changes from 1 commit
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,18 @@ | ||
package mate.academy.springboot.datajpa.config; | ||
|
||
import io.swagger.v3.oas.models.OpenAPI; | ||
import io.swagger.v3.oas.models.info.Info; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class SwaggerConfig { | ||
@Bean | ||
public OpenAPI apiInfo() { | ||
return new OpenAPI() | ||
.info(new Info() | ||
.title("My API Title") | ||
.description("Documentation for REST API endpoints") | ||
.version("1.0")); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package mate.academy.springboot.datajpa.controller; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import mate.academy.springboot.datajpa.dto.CategoryRequestDto; | ||
import mate.academy.springboot.datajpa.dto.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.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RequiredArgsConstructor | ||
@RestController | ||
@RequestMapping("/categories") | ||
public class CategoryController { | ||
private final CategoryService categoryService; | ||
private final DtoMapper<CategoryRequestDto, CategoryResponseDto, Category> categoryMapper; | ||
|
||
@PostMapping | ||
public CategoryResponseDto create(@RequestBody CategoryRequestDto categoryRequestDto) { | ||
Category category = categoryMapper.mapToEntity(categoryRequestDto); | ||
return categoryMapper.mapToDto(categoryService.save(category)); | ||
} | ||
|
||
@GetMapping("{id}") | ||
public CategoryResponseDto getById(@PathVariable Long id) { | ||
return categoryMapper.mapToDto(categoryService.getById(id)); | ||
} | ||
|
||
@DeleteMapping("{id}") | ||
public void deleteById(@PathVariable Long id) { | ||
categoryService.deleteById(id); | ||
} | ||
|
||
@PostMapping("{id}") | ||
public CategoryResponseDto updateById(@PathVariable Long id, | ||
@RequestBody CategoryRequestDto categoryRequestDto) { | ||
Category category = categoryMapper.mapToEntity(categoryRequestDto); | ||
category.setId(id); | ||
return categoryMapper.mapToDto(categoryService.save(category)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
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.service.CategoryService; | ||
import mate.academy.springboot.datajpa.service.ProductService; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RequiredArgsConstructor | ||
@RestController | ||
@RequestMapping("/inject") | ||
public class InjectController { | ||
private final ProductService productService; | ||
private final CategoryService categoryService; | ||
|
||
@GetMapping | ||
public String injectData() { | ||
// save categories | ||
Category phone = new Category(); | ||
phone.setName("Phone"); | ||
Category laptop = new Category(); | ||
laptop.setName("Laptop"); | ||
Category tv = new Category(); | ||
tv.setName("TV"); | ||
|
||
categoryService.save(phone); | ||
categoryService.save(laptop); | ||
categoryService.save(tv); | ||
|
||
// save products | ||
Product iphone = new Product(); | ||
iphone.setTitle("iPhone 7"); | ||
iphone.setCategory(phone); | ||
iphone.setPrice(BigDecimal.valueOf(499)); | ||
|
||
Product samsungLaptop = new Product(); | ||
samsungLaptop.setTitle("Samsung Galaxy Book3"); | ||
samsungLaptop.setCategory(laptop); | ||
samsungLaptop.setPrice(BigDecimal.valueOf(500)); | ||
|
||
Product samsungTV = new Product(); | ||
samsungTV.setTitle("Samsung Crystal UHD"); | ||
samsungTV.setCategory(tv); | ||
samsungTV.setPrice(BigDecimal.valueOf(695)); | ||
|
||
productService.save(iphone); | ||
productService.save(samsungLaptop); | ||
productService.save(samsungTV); | ||
return "Done!"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package mate.academy.springboot.datajpa.controller; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.List; | ||
import java.util.Set; | ||
import lombok.RequiredArgsConstructor; | ||
import mate.academy.springboot.datajpa.dto.ProductRequestDto; | ||
import mate.academy.springboot.datajpa.dto.ProductResponseDto; | ||
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.CategoryService; | ||
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.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RequiredArgsConstructor | ||
@RestController | ||
@RequestMapping("/products") | ||
public class ProductController { | ||
private final ProductService productService; | ||
private final DtoMapper<ProductRequestDto, ProductResponseDto, Product> productMapper; | ||
private final CategoryService categoryService; | ||
|
||
@PostMapping | ||
public ProductResponseDto create(@RequestBody ProductRequestDto productRequestDto) { | ||
Category categoryById = categoryService.getById(productRequestDto.getCategoryId()); | ||
Product product = productMapper.mapToEntity(productRequestDto); | ||
product.setCategory(categoryById); | ||
return productMapper.mapToDto(productService.save(product)); | ||
} | ||
|
||
@GetMapping("{id}") | ||
public ProductResponseDto getById(@PathVariable Long id) { | ||
return productMapper.mapToDto(productService.getById(id)); | ||
} | ||
|
||
@DeleteMapping("{id}") | ||
public void deleteById(@PathVariable Long id) { | ||
productService.deleteById(id); | ||
} | ||
|
||
@GetMapping | ||
public List<ProductResponseDto> getAll() { | ||
return productService.getAll() | ||
.stream() | ||
.map(productMapper::mapToDto) | ||
.toList(); | ||
} | ||
|
||
@GetMapping("/between") | ||
public List<ProductResponseDto> getAllByPriceBetween(@RequestParam BigDecimal from, | ||
@RequestParam BigDecimal to) { | ||
return productService.getAllByPricePriceBetween(from, to) | ||
.stream() | ||
.map(productMapper::mapToDto) | ||
.toList(); | ||
} | ||
|
||
@PostMapping("{id}") | ||
public ProductResponseDto updateById(@PathVariable Long id, | ||
@RequestBody ProductRequestDto productRequestDto) { | ||
Product product = productMapper.mapToEntity(productRequestDto); | ||
product.setId(id); | ||
Category categoryById = categoryService.getById(productRequestDto.getCategoryId()); | ||
product.setCategory(categoryById); | ||
return productMapper.mapToDto(productService.save(product)); | ||
} | ||
|
||
@GetMapping("/categories") | ||
public List<ProductResponseDto> getAllByCategoryIn( | ||
@RequestParam Set<String> categories) { | ||
return productService.getAllByCategoryNameIn(categories) | ||
.stream() | ||
.map(productMapper::mapToDto) | ||
.toList(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package mate.academy.springboot.datajpa.dto; | ||
|
||
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 not use |
||
public class CategoryRequestDto { | ||
private String name; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package mate.academy.springboot.datajpa.dto; | ||
|
||
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,11 @@ | ||
package mate.academy.springboot.datajpa.dto; | ||
|
||
import java.math.BigDecimal; | ||
import lombok.Data; | ||
|
||
@Data | ||
public class ProductRequestDto { | ||
private String title; | ||
private BigDecimal price; | ||
private Long categoryId; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package mate.academy.springboot.datajpa.dto; | ||
|
||
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,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 mapToEntity(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 entity); | ||
} |
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.CategoryRequestDto; | ||||
import mate.academy.springboot.datajpa.dto.CategoryResponseDto; | ||||
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<CategoryRequestDto, | ||||
CategoryResponseDto, 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
Please remove redundant empty lines in all places |
||||
@Override | ||||
public CategoryResponseDto mapToDto(Category category) { | ||||
CategoryResponseDto categoryResponseDto = new CategoryResponseDto(); | ||||
categoryResponseDto.setId(category.getId()); | ||||
categoryResponseDto.setName(category.getName()); | ||||
return categoryResponseDto; | ||||
} | ||||
|
||||
@Override | ||||
public Category mapToEntity(CategoryRequestDto categoryRequestDto) { | ||||
Category category = new Category(); | ||||
category.setName(categoryRequestDto.getName()); | ||||
return category; | ||||
} | ||||
} |
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.dto.ProductRequestDto; | ||
import mate.academy.springboot.datajpa.dto.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 ProductMapperImpl implements DtoMapper<ProductRequestDto, | ||
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; | ||
} | ||
|
||
@Override | ||
public Product mapToEntity(ProductRequestDto productRequestDto) { | ||
Product product = new Product(); | ||
product.setPrice(productRequestDto.getPrice()); | ||
product.setTitle(productRequestDto.getTitle()); | ||
product.setCategory(new Category(productRequestDto.getCategoryId())); | ||
return product; | ||
} | ||
} |
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.
do you really this dependency here?
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.
Not really)
I tested the code on MySQL and forgot about this dependency