Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Volodymyr-Mykychak committed Sep 23, 2023
1 parent 8de1ae2 commit 05e8a2e
Show file tree
Hide file tree
Showing 22 changed files with 562 additions and 8 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ jobs:

steps:
- uses: actions/checkout@v2
- name: Set up JDK 17
- name: Set up JDK 11
uses: actions/setup-java@v2
with:
java-version: '17'
java-version: '11'
distribution: 'adopt'
cache: maven
- name: Build with Maven
Expand Down
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Your task is:
- id
- title
- price
- category (one product can have one category but the category can have multiple products)
- category (one product can have one category)
- create model `Category` with fields
- id
- name
Expand All @@ -29,5 +29,3 @@ Your task is:
- delete Category by ID
- update Category
- create required DTOs and mappers

__Before submitting solution make sure you checked it first with__ [checklist](https://mate-academy.github.io/jv-program-common-mistakes/java-spring-boot/spring-data-jpa/jv-springboot-data-jpa.html)
59 changes: 57 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.0</version>
<version>2.6.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>mate.academy.springboot</groupId>
Expand All @@ -14,7 +14,7 @@
<name>data-jpa</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
<java.version>11</java.version>
<maven.checkstyle.plugin.version>3.1.1</maven.checkstyle.plugin.version>
<maven.checkstyle.plugin.configLocation>
https://raw.githubusercontent.com/mate-academy/style-guides/master/java/checkstyle.xml
Expand All @@ -29,8 +29,63 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.6.5</version>
</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>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.5</version>
<type>pom</type>
</dependency>


<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.7.5</version>
</dependency>
</dependencies>

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

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.data.rest.configuration.SpringDataRestConfiguration;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@EnableSwagger2
@Configuration
@Import(SpringDataRestConfiguration.class)
public class SpringFoxConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("mate.academy.springboot.datajpa"))
.paths(PathSelectors.any())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package mate.academy.springboot.datajpa.controller;

import lombok.RequiredArgsConstructor;
import mate.academy.springboot.datajpa.dto.request.CategoryRequestDto;
import mate.academy.springboot.datajpa.dto.response.CategoryResponseDto;
import mate.academy.springboot.datajpa.model.Category;
import mate.academy.springboot.datajpa.services.CategoryService;
import mate.academy.springboot.datajpa.services.mapper.CategoryMapper;
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 CategoryMapper categoryMapper;

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

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

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

@PutMapping("/{id}")
public CategoryResponseDto update(@PathVariable Long id,
@RequestBody CategoryRequestDto categoryRequestDto) {
Category category = categoryService.getById(id);
category.setName(categoryRequestDto.getName());
return categoryMapper.toDto(categoryService.create(category));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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.services.CategoryService;
import mate.academy.springboot.datajpa.services.ProductService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
public class InjectController {
private final ProductService productService;
private final CategoryService categoryService;

@GetMapping("/inject")
public String inject() {
Category product = new Category();
product.setName("products");
product = categoryService.create(product);

Category toys = new Category();
toys.setName("toys");
toys = categoryService.create(toys);

Category drinks = new Category();
drinks.setName("drinks");
drinks = categoryService.create(drinks);

Category smartPhone = new Category();
smartPhone.setName("smartPhone");
smartPhone = categoryService.create(smartPhone);

Product pizza = new Product();
pizza.setCategory(product);
pizza.setPrice(BigDecimal.valueOf(1.4));
pizza.setTitle("pizza");
pizza = productService.create(pizza);

Product bear = new Product();
bear.setCategory(toys);
bear.setPrice(BigDecimal.valueOf(2.1));
bear.setTitle("bear");
bear = productService.create(bear);

Product coffee = new Product();
coffee.setCategory(drinks);
coffee.setPrice(BigDecimal.valueOf(1.13));
coffee.setTitle("coffee");
coffee = productService.create(coffee);

Product phone = new Product();
phone.setCategory(smartPhone);
phone.setPrice(BigDecimal.valueOf(124.5));
phone.setTitle("phone");
phone = productService.create(phone);

Product beer = new Product();
beer.setCategory(drinks);
beer.setPrice(BigDecimal.valueOf(2.1));
beer.setTitle("beer");
beer = productService.create(beer);

return "categories and products was created";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package mate.academy.springboot.datajpa.controller;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import mate.academy.springboot.datajpa.dto.request.ProductRequestDto;
import mate.academy.springboot.datajpa.dto.response.ProductResponseDto;
import mate.academy.springboot.datajpa.model.Category;
import mate.academy.springboot.datajpa.model.Product;
import mate.academy.springboot.datajpa.services.CategoryService;
import mate.academy.springboot.datajpa.services.ProductService;
import mate.academy.springboot.datajpa.services.mapper.ProductMapper;
import org.springframework.http.HttpStatus;
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.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/products")
@RequiredArgsConstructor
public class ProductController {
private final ProductService productService;
private final ProductMapper productMapper;
private final CategoryService categoryService;

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

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

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

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

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

@GetMapping("/by-categories")
public List<ProductResponseDto> getAllWithCategory(@RequestParam String category) {
List<Long> categoriesId = categoryService.getCategoriesByNameIn(
Arrays.asList(category.split(",")))
.stream()
.map(Category::getId)
.collect(Collectors.toList());
return productService.getAllWithCategories(categoriesId).stream()
.map(productMapper::toDto)
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package mate.academy.springboot.datajpa.dto.request;

import lombok.Data;

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

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,9 @@
package mate.academy.springboot.datajpa.dto.response;

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.dto.response;

import java.math.BigDecimal;
import lombok.Data;

@Data
public class ProductResponseDto {
private Long id;
private String title;
private BigDecimal price;
private Long categoryId;
}
18 changes: 18 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,18 @@
package mate.academy.springboot.datajpa.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.Data;

@Entity
@Data
@Table(name = "categories")
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
}
Loading

0 comments on commit 05e8a2e

Please sign in to comment.