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

complete #324

Open
wants to merge 1 commit 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
24 changes: 24 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
https://raw.githubusercontent.com/mate-academy/style-guides/master/java/checkstyle.xml
</maven.checkstyle.plugin.configLocation>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand All @@ -30,7 +31,30 @@
<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.projectlombok</groupId>
<artifactId>lombok</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>
</dependencies>

<build>
<plugins>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package mate.academy.springboot.datajpa.controller;

import mate.academy.springboot.datajpa.dto.CategoryRequestDto;
import mate.academy.springboot.datajpa.dto.CategoryResponseDto;
import mate.academy.springboot.datajpa.dto.mapper.CategoryMapper;
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.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;

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

public CategoryController(CategoryService categoryService, CategoryMapper categoryMapper) {
this.categoryService = categoryService;
this.categoryMapper = categoryMapper;
}

@PostMapping
public CategoryResponseDto save(@RequestBody CategoryRequestDto categoryRequestDto) {
return categoryMapper
.toDto(categoryService.save(categoryMapper.toModel(categoryRequestDto)));
}

@PostMapping("/update")
public CategoryResponseDto update(@RequestParam Long id,
@RequestBody CategoryRequestDto categoryRequestDto) {
Category category = categoryMapper.toModel(categoryRequestDto);
category.setId(id);
return categoryMapper
.toDto(categoryService.save(category));
}

@GetMapping
public CategoryResponseDto getById(@RequestParam Long id) {
return categoryMapper.toDto(categoryService.getById(id));
}

@DeleteMapping
public void deleteById(@RequestParam Long id) {
categoryService.deleteCategoryById(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package mate.academy.springboot.datajpa.controller;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;
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.dto.mapper.ProductMapper;
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.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;

@RestController
@RequestMapping("/products")
public class ProductController {
private final ProductService productService;
private final DtoMapper<Product, ProductRequestDto, ProductResponseDto> productMapper;

public ProductController(ProductService productService, ProductMapper productMapper) {
this.productService = productService;
this.productMapper = productMapper;
}

@PostMapping
public ProductResponseDto save(@RequestBody ProductRequestDto productRequestDto) {
return productMapper.toDto(
productService.save(productMapper.toModel(productRequestDto)));
}

@PostMapping("/update")
public ProductResponseDto update(@RequestParam Long id,
@RequestBody ProductRequestDto productRequestDto) {
Product product = productMapper.toModel(productRequestDto);
product.setId(id);
return productMapper.toDto(
productService.save(product));
}

@GetMapping
public ProductResponseDto getById(@RequestParam Long id) {
return productMapper.toDto(productService.getByID(id));
}

@DeleteMapping
public void deleteById(@RequestParam Long id) {
productService.deleteById(id);
}

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

@GetMapping("/in-categories")
public List<ProductResponseDto> findAllByCategoryIn(@RequestParam List<Long> ids) {
return productService.findAllByCategoryIn(ids)
.stream()
.map(productMapper::toDto)
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package mate.academy.springboot.datajpa.dto;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class CategoryRequestDto {
private String name;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package mate.academy.springboot.datajpa.dto;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class CategoryResponseDto {
private String name;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package mate.academy.springboot.datajpa.dto;

import java.math.BigDecimal;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
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,13 @@
package mate.academy.springboot.datajpa.dto;

import java.math.BigDecimal;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class ProductResponseDto {
private String title;
private BigDecimal price;
private String categoryName;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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 extends DtoMapper<Category, CategoryRequestDto, CategoryResponseDto> {
public Category toModel(CategoryRequestDto categoryRequestDto) {
Category category = new Category();
category.setName(categoryRequestDto.getName());
return category;
}

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

public abstract class DtoMapper<M, R, A> {
public abstract A toDto(M model);

public abstract M toModel(R requestDto);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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 extends DtoMapper<Product, ProductRequestDto, ProductResponseDto> {
private final CategoryService categoryService;

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

public Product toModel(ProductRequestDto productRequestDto) {
Product product = new Product();
product.setTitle(productRequestDto.getTitle());
product.setPrice(productRequestDto.getPrice());
product.setCategory(categoryService.getById(productRequestDto.getCategoryId()));
return product;
}

public ProductResponseDto toDto(Product product) {
ProductResponseDto responseDto = new ProductResponseDto();
responseDto.setTitle(product.getTitle());
responseDto.setPrice(product.getPrice());
responseDto.setCategoryName(product.getCategory().getName());
return responseDto;
}
}
20 changes: 20 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,20 @@
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.Getter;
import lombok.Setter;

@Getter
@Setter
@Entity
@Table(name = "categories")
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,15 @@
package mate.academy.springboot.datajpa.repository;

import java.math.BigDecimal;
import java.util.List;
import mate.academy.springboot.datajpa.model.Category;
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> findAllByCategoryIn(List<Category> categories);
}
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 category);

Category getById(Long id);

void deleteCategoryById(Long id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package mate.academy.springboot.datajpa.service;

import mate.academy.springboot.datajpa.model.Category;
import mate.academy.springboot.datajpa.repository.CategoryRepository;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl implements CategoryService {
private final CategoryRepository categoryRepository;

public CategoryServiceImpl(CategoryRepository categoryRepository) {
this.categoryRepository = categoryRepository;
}

@Override
public Category save(Category category) {
return categoryRepository.save(category);
}

@Override
public Category getById(Long id) {
return categoryRepository.getReferenceById(id);
}

@Override
public void deleteCategoryById(Long id) {
categoryRepository.deleteById(id);
}
}
Loading
Loading