Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,48 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>

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?

Copy link
Author

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

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.0.2</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not use @Data annotation. It's better to use Getter Setter etc. Please fix the same issue in all places

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> {

Choose a reason for hiding this comment

The 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;
}
}
Loading
Loading