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

Implemented all needed repositories/services/controllers #323

Open
wants to merge 6 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-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.28</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</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 jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import mate.academy.springboot.datajpa.dto.request.CategoryRequestDto;
import mate.academy.springboot.datajpa.dto.response.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.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 service;
private final DtoMapper<CategoryRequestDto, CategoryResponseDto, Category> mapper;

@PostMapping
public CategoryResponseDto create(@RequestBody @Valid CategoryRequestDto dto) {
Category category = service.add(mapper.mapToModel(dto));
return mapper.mapToDto(category);
Comment on lines +28 to +29

Choose a reason for hiding this comment

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

let's be consistent in your code style. Either use additional variables everywhere or avoid creating it

}

@GetMapping("/{id}")
public CategoryResponseDto get(@PathVariable Long id) {
Category category = service.getById(id);
return mapper.mapToDto(category);
}

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

@PutMapping("/{id}")
public CategoryResponseDto update(@PathVariable Long id,
@RequestBody @Valid CategoryRequestDto dto) {
Category category = service.update(mapper.mapToModel(dto), id);
return mapper.mapToDto(category);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package mate.academy.springboot.datajpa.controller;

import jakarta.validation.Valid;
import java.math.BigDecimal;
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.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;

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

@PostMapping
public ProductResponseDto create(@RequestBody @Valid ProductRequestDto dto) {
Product mappedProduct = mapper.mapToModel(dto);
Product product = service.add(mappedProduct);
return mapper.mapToDto(product);
}

@GetMapping("/{id}")
public ProductResponseDto get(@PathVariable Long id) {
Product product = service.getById(id);
return mapper.mapToDto(product);
}

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

@PutMapping("/{id}")
public ProductResponseDto update(@PathVariable Long id,
@RequestBody @Valid ProductRequestDto dto) {
Product product = service.update(mapper.mapToModel(dto), id);
return mapper.mapToDto(product);
}

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

@GetMapping("/categories")
public List<ProductResponseDto> getByCategory(@RequestParam List<String> categoryNames) {
return service.findAllByCategoryNameIn(categoryNames).stream()
.map(mapper::mapToDto)
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package mate.academy.springboot.datajpa.dto.request;

import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;

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

import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
import java.math.BigDecimal;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class ProductRequestDto {
@NotNull
private String title;
@NotNull
@Positive
private BigDecimal price;
@NotNull
private Long categoryId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package mate.academy.springboot.datajpa.dto.response;

import lombok.Getter;
import lombok.Setter;

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

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

@Getter
@Setter
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<Q, S, M> extends RequestDtoMapper<Q, M>, ResponseDtoMapper<S, M> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package mate.academy.springboot.datajpa.mapper;

public interface RequestDtoMapper<Q, M> {
M mapToModel(Q dto);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package mate.academy.springboot.datajpa.mapper;

public interface ResponseDtoMapper<S, M> {
S mapToDto(M t);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package mate.academy.springboot.datajpa.mapper.impl;

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

@Component
public class CategoryMapper
implements DtoMapper<CategoryRequestDto, CategoryResponseDto, Category> {
@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,30 @@
package mate.academy.springboot.datajpa.mapper.impl;

import mate.academy.springboot.datajpa.dto.request.ProductRequestDto;
import mate.academy.springboot.datajpa.dto.response.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 ProductMapper 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 mapToModel(ProductRequestDto dto) {
Product product = new Product();
product.setTitle(dto.getTitle());
product.setPrice(dto.getPrice());
product.setCategory(new Category(dto.getCategoryId()));
return product;
}
}
28 changes: 28 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,28 @@
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.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString
@NoArgsConstructor
@Entity
@Table(name = "categories")
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;

public Category(Long id) {
this.id = id;
}
}
35 changes: 35 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,35 @@
package mate.academy.springboot.datajpa.model;

import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
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)

Choose a reason for hiding this comment

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

image
I would like an additional explanation from mentors in this situation)

Choose a reason for hiding this comment

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

I selected the wrong area for the comment, I am interested in this class of entity with the Lazy fetch type
image
image

private Long id;
private String title;
private BigDecimal price;
@ManyToOne(fetch = FetchType.LAZY)
private Category category;

@Override
public String toString() {
return "Product{"
+ "id=" + id
+ ", title='" + title + '\''
+ ", price=" + price
+ '}';
}
}
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> categoryNames);
}
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 add(Category category);

Category getById(Long id);

void delete(Long id);

Category update(Category category, Long id);
}
Loading
Loading