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

solution jpa #316

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
27 changes: 27 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,33 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
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.dto.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;

@RestController
@RequestMapping("/categories")
@RequiredArgsConstructor
public class CategoryController {
private final CategoryService categoryService;
private final DtoMapper<Category, CategoryResponseDto, CategoryRequestDto> categoryMapper;

@PostMapping
public CategoryResponseDto create(@RequestBody CategoryRequestDto requestDto) {
Category category = categoryService.create(categoryMapper.mapToModel(requestDto));
return categoryMapper.mapToDto(category);
}

@GetMapping("{id}")
public CategoryResponseDto getById(@PathVariable Long id) {
Category category = categoryService.getById(id);
return categoryMapper.mapToDto(category);
}

@DeleteMapping("{id}")
public void delete(@PathVariable Long id) {
categoryService.delete(id);
}

@PutMapping("/{id}")
public CategoryResponseDto update(@PathVariable Long id,
@RequestBody CategoryRequestDto requestDto) {
Category category = categoryMapper.mapToModel(requestDto);
category.setId(id);
return categoryMapper.mapToDto(categoryService.create(category));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package mate.academy.springboot.datajpa.controller;

import java.math.BigDecimal;
import lombok.AllArgsConstructor;
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;

@AllArgsConstructor
@RestController
@RequestMapping("/inject")
public class InjectController {
private final ProductService productService;
private final CategoryService categoryService;

@GetMapping
public String injectData() {
Category phones = new Category();
phones.setName("Phones");
categoryService.create(phones);
Category macBook = new Category();
macBook.setName("Laptop");
categoryService.create(macBook);

Product iphoneX = new Product();
iphoneX.setTitle("Iphone X");
iphoneX.setPrice(BigDecimal.valueOf(999));
iphoneX.setCategory(phones);
productService.create(iphoneX);

Product iphone12 = new Product();
iphone12.setTitle("Iphone 12");
iphone12.setPrice(BigDecimal.valueOf(1299));
iphone12.setCategory(phones);
productService.create(iphone12);

Product airMax = new Product();
airMax.setTitle("MacBook Air");
airMax.setPrice(BigDecimal.valueOf(2100));
airMax.setCategory(macBook);
productService.create(airMax);
return "Done";
}
}
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.stream.Collectors;
import lombok.AllArgsConstructor;
import mate.academy.springboot.datajpa.dto.ProductRequestDto;
import mate.academy.springboot.datajpa.dto.ProductResponseDto;
import mate.academy.springboot.datajpa.dto.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<Product, ProductResponseDto, ProductRequestDto> productMapper;

@PostMapping
public ProductResponseDto create(@RequestBody ProductRequestDto productRequestDto) {
Product product = productService.create(productMapper.mapToModel(productRequestDto));
return productMapper.mapToDto(product);
}

@GetMapping("/{id}")
public ProductResponseDto getById(@PathVariable Long id) {
return productMapper.mapToDto(productService.getById(id));
}

@DeleteMapping("/{id}")
public void delete(@PathVariable Long id) {
productService.delete(id);
}

@PutMapping("/{id}")
public ProductResponseDto update(@PathVariable Long id,
@RequestBody ProductRequestDto productRequestDto) {
Product product = productMapper.mapToModel(productRequestDto);
product.setId(id);
productService.update(product);
return productMapper.mapToDto(product);
}

@GetMapping("/price-between")
public List<ProductResponseDto> findAllByPriceBetween(@RequestParam BigDecimal from,
@RequestParam BigDecimal to) {
return productService.findAllByPriceBetween(from, to)
.stream()
.map(productMapper::mapToDto)
.collect(Collectors.toList());
}

@GetMapping("/by-category")
public List<ProductResponseDto> findAllByCategoryIn(@RequestParam List<String> categories) {
return productService.findAllByCategoryNameIn(categories)
.stream()
.map(productMapper::mapToDto)
.collect(Collectors.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
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,26 @@
package mate.academy.springboot.datajpa.dto.mapper;

import mate.academy.springboot.datajpa.dto.CategoryRequestDto;
import mate.academy.springboot.datajpa.dto.CategoryResponseDto;
import mate.academy.springboot.datajpa.model.Category;
import org.springframework.stereotype.Component;

@Component
public class CategoryMapper implements
DtoMapper<Category, CategoryResponseDto, CategoryRequestDto> {

@Override
public Category mapToModel(CategoryRequestDto dto) {
Category category = new Category();
category.setName(dto.getName());
return category;
}

@Override
public CategoryResponseDto mapToDto(Category category) {
CategoryResponseDto categoryResponseDto = new CategoryResponseDto();
categoryResponseDto.setId(category.getId());
categoryResponseDto.setName(category.getName());
return categoryResponseDto;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package mate.academy.springboot.datajpa.dto.mapper;

public interface DtoMapper<M, D, R> {
D mapToDto(M model);

M mapToModel(R dto);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package mate.academy.springboot.datajpa.dto.mapper;

import mate.academy.springboot.datajpa.dto.ProductRequestDto;
import mate.academy.springboot.datajpa.dto.ProductResponseDto;
import mate.academy.springboot.datajpa.model.Product;
import mate.academy.springboot.datajpa.service.CategoryService;
import org.springframework.stereotype.Component;

@Component
public class ProductMapper implements DtoMapper<Product, ProductResponseDto, ProductRequestDto> {
private final CategoryService categoryService;

public ProductMapper(CategoryService categoryService) {
this.categoryService = categoryService;
}

@Override
public Product mapToModel(ProductRequestDto dto) {
Product product = new Product();
product.setTitle(dto.getTitle());
product.setPrice(dto.getPrice());
product.setCategory(categoryService.getById(dto.getCategoryId()));
return 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;
}
}
16 changes: 16 additions & 0 deletions src/main/java/mate/academy/springboot/datajpa/model/Category.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package mate.academy.springboot.datajpa.model;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.Data;

@Data
@Entity
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
}
25 changes: 25 additions & 0 deletions src/main/java/mate/academy/springboot/datajpa/model/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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.ManyToOne;
import jakarta.persistence.Table;
import java.math.BigDecimal;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Entity
@Table(name = "products")
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
private BigDecimal price;
@ManyToOne
private Category category;
}
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> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package mate.academy.springboot.datajpa.repository;

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> findAllByCategoryNameIn(List<String> categories);
}
Loading
Loading