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

done #308

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

done #308

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
23 changes: 23 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,29 @@
<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>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package mate.academy.springboot.datajpa.controller;

import mate.academy.springboot.datajpa.model.Category;
import mate.academy.springboot.datajpa.model.dto.CategoryRequestDto;
import mate.academy.springboot.datajpa.model.dto.CategoryResponseDto;
import mate.academy.springboot.datajpa.service.CategoryService;
import mate.academy.springboot.datajpa.service.mapper.DtoMapper;
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")
public class CategoryController {
private final CategoryService categoryService;
private final DtoMapper<Category, CategoryResponseDto, CategoryRequestDto> categoryMapper;

public CategoryController(CategoryService categoryService, DtoMapper<Category,
CategoryResponseDto, CategoryRequestDto> categoryMapper) {
this.categoryService = categoryService;
this.categoryMapper = categoryMapper;
}

@PostMapping
public CategoryResponseDto create(@RequestBody CategoryRequestDto requestDto) {
return categoryMapper.toDto(categoryService.create(categoryMapper.toEntity(requestDto)));
}

@GetMapping("/{id}")
public CategoryResponseDto get(@PathVariable Long id) {
return categoryMapper.toDto(categoryService.get(id));
}

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

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

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;
import mate.academy.springboot.datajpa.model.Product;
import mate.academy.springboot.datajpa.model.dto.ProductRequestDto;
import mate.academy.springboot.datajpa.model.dto.ProductResponseDto;
import mate.academy.springboot.datajpa.service.CategoryService;
import mate.academy.springboot.datajpa.service.ProductService;
import mate.academy.springboot.datajpa.service.mapper.DtoMapper;
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;

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

public ProductController(CategoryService categoryService,
ProductService productService, DtoMapper<Product, ProductResponseDto,
ProductRequestDto> productMapper) {
this.productService = productService;
this.productMapper = productMapper;
}

@PostMapping
public ProductResponseDto create(@RequestBody ProductRequestDto requestDto) {
return productMapper.toDto(productService.create(productMapper.toEntity(requestDto)));
}

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

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

@PutMapping("/{id}")
public ProductResponseDto update(@PathVariable Long id,
@RequestBody ProductRequestDto requestDto) {
Product updatedProduct = productMapper.toEntity(requestDto);
updatedProduct.setId(id);
return productMapper.toDto(productService.update(updatedProduct));
}

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

@GetMapping("/get-by-categories")
public List<ProductResponseDto> getAllByCategories(@RequestParam List<String> categories) {
return productService.findAllByCategoryIn(categories)
.stream()
.map(productMapper::toDto)
.collect(Collectors.toList());
}
}
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;
}
21 changes: 21 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,21 @@
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 java.math.BigDecimal;
import lombok.Data;

@Data
@Entity
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,8 @@
package mate.academy.springboot.datajpa.model.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.model.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,12 @@
package mate.academy.springboot.datajpa.model.dto;

import java.math.BigDecimal;
import lombok.Data;
import mate.academy.springboot.datajpa.model.Category;

@Data
public class ProductRequestDto {
private String title;
private BigDecimal price;
private Category category;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package mate.academy.springboot.datajpa.model.dto;

import java.math.BigDecimal;
import lombok.Data;
import mate.academy.springboot.datajpa.model.Category;

@Data
public class ProductResponseDto {
private Long id;
private String title;
private BigDecimal price;
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> getProductsByPriceBetween(BigDecimal from, BigDecimal to);

List<Product> findProductByCategoryIn(List<String> categories);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package mate.academy.springboot.datajpa.service;

import mate.academy.springboot.datajpa.model.Category;

public interface CategoryService {
Category create(Category category);

Category get(Long id);

void delete(Long id);

Category update(Category category);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package mate.academy.springboot.datajpa.service;

import java.util.NoSuchElementException;
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 create(Category category) {
return categoryRepository.save(category);
}

@Override
public Category get(Long id) {
return categoryRepository.findById(id).orElseThrow(() ->
new NoSuchElementException("couldn't get category by id: " + id));
}

@Override
public void delete(Long id) {
if (!categoryRepository.existsById(id)) {
throw new NoSuchElementException("couldn't delete category by id: " + id);
}
categoryRepository.deleteById(id);
}

@Override
public Category update(Category category) {
if (!categoryRepository.existsById(category.getId())) {
throw new NoSuchElementException("couldn't update category: " + category);
}
return categoryRepository.save(category);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package mate.academy.springboot.datajpa.service;

import java.math.BigDecimal;
import java.util.List;
import mate.academy.springboot.datajpa.model.Product;

public interface ProductService {
Product create(Product product);

Product get(Long id);

void delete(Long id);

Product update(Product product);

List<Product> getAllBetweenPrice(BigDecimal from, BigDecimal to);

List<Product> findAllByCategoryIn(List<String> categories);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package mate.academy.springboot.datajpa.service;

import java.math.BigDecimal;
import java.util.List;
import java.util.NoSuchElementException;
import mate.academy.springboot.datajpa.model.Product;
import mate.academy.springboot.datajpa.repository.ProductRepository;
import org.springframework.stereotype.Service;

@Service
public class ProductServiceImpl implements ProductService {
private final ProductRepository productRepository;

public ProductServiceImpl(ProductRepository productRepository) {
this.productRepository = productRepository;
}

@Override
public Product create(Product product) {
return productRepository.save(product);
}

@Override
public Product get(Long id) {
return productRepository.findById(id).orElseThrow(() ->
new NoSuchElementException("couldn't get product by id: " + id));
}

@Override
public void delete(Long id) {
if (!productRepository.existsById(id)) {
throw new NoSuchElementException("couldn't delete product by id: " + id);
}
productRepository.deleteById(id);
}

@Override
public Product update(Product product) {
if (!productRepository.existsById(product.getId())) {
throw new NoSuchElementException("couldn't update product: " + product);
}
return productRepository.save(product);

}

@Override
public List<Product> getAllBetweenPrice(BigDecimal from, BigDecimal to) {
return productRepository.getProductsByPriceBetween(from, to);
}

@Override
public List<Product> findAllByCategoryIn(List<String> categories) {
return productRepository.findProductByCategoryIn(categories);
}
}
Loading
Loading