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

[product-4] Implement Product API for front-end: Get the list of all products #58

Closed
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
aab5673
product-1: added a mapper for ProductInfoRequestResponseDto
UladzislauM Jul 24, 2023
49a2915
product-1: changed the type of id to UUID
UladzislauM Jul 24, 2023
d208e2d
product-1: added ProductInfoRequestResponseDto
UladzislauM Jul 24, 2023
a2b997a
product-1: added ProductInfoService
UladzislauM Jul 24, 2023
83b9ebb
product-1: changed the controller according to business requirements
UladzislauM Jul 24, 2023
782a219
product-1: corrected for correct paginated display of the display
UladzislauM Jul 24, 2023
f6f2dc8
product-4: renamed the DTO for the response
UladzislauM Jul 27, 2023
73f5bc3
product-4: renamed the mapper DTO for the response
UladzislauM Jul 27, 2023
142594f
product-4: renamed getAllProducts to getProducts
UladzislauM Jul 27, 2023
c1a3f2b
product-4: renamed ProductInfoServiceImpl to ProductService
UladzislauM Jul 28, 2023
a112531
product-4: removed the extra imports
UladzislauM Jul 28, 2023
7519bf3
Merge branch 'Sunagatov:development' into feature/product-4
UladzislauM Jul 29, 2023
207edc1
product-4: deleted pageable and add several parameters
UladzislauM Jul 30, 2023
c20c908
product-4: deleted pageable and add several parameters
UladzislauM Jul 30, 2023
c58382c
product-4: add dto for correct response
UladzislauM Jul 30, 2023
98bd7e2
product-4: changed endpoint for new request pattern
UladzislauM Jul 30, 2023
668a8e0
product-4: changed service for new request pattern
UladzislauM Jul 30, 2023
cdbfe52
product-4: corrected in compliance with the documentation
UladzislauM Jul 31, 2023
27c1ec2
product-4: fixed the design
UladzislauM Jul 31, 2023
2e93711
product-4: changed the service layer following the example of the ato…
UladzislauM Aug 1, 2023
34a7368
Merge branch 'Sunagatov:development' into feature/product-4
UladzislauM Aug 1, 2023
c6cdf5d
product-4: renamed the method for issuing all products
UladzislauM Aug 1, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.zufar.onlinestore.product.dto.PriceDetailsDto;
import com.zufar.onlinestore.product.dto.ProductInfoDto;
import com.zufar.onlinestore.product.dto.ProductInfoFullDto;
import com.zufar.onlinestore.product.dto.ProductInfoRequestResponseDto;
import com.zufar.onlinestore.product.entity.ProductInfo;
import org.springframework.stereotype.Component;

Expand Down Expand Up @@ -33,6 +34,18 @@ public ProductInfoFullDto convertToFullDto(final ProductInfo entity) {
);
}

public ProductInfoRequestResponseDto convertToRequestResponseDto(final ProductInfo entity) {
Sunagatov marked this conversation as resolved.
Show resolved Hide resolved
PriceDetailsDto priceDetails = new PriceDetailsDto(entity.getPrice(), entity.getCurrency());

return new ProductInfoRequestResponseDto(
entity.getId(),
entity.getDescription(),
entity.getName(),
priceDetails,
entity.getQuantity()
);
}

public ProductInfo convertToEntity(final ProductInfoFullDto dto) {
Sunagatov marked this conversation as resolved.
Show resolved Hide resolved
return new ProductInfo(
dto.id(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.zufar.onlinestore.product.dto;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;

import java.util.UUID;

public record ProductInfoRequestResponseDto(
Sunagatov marked this conversation as resolved.
Show resolved Hide resolved

@NotNull(message = "Id is the mandatory attribute")
Sunagatov marked this conversation as resolved.
Show resolved Hide resolved
UUID id,

@NotBlank(message = "Name is the mandatory attribute")
@Size(max = 100, message = "Name length must be less than 100 characters")
String name,

@NotBlank(message = "Description is the mandatory attribute")
@Size(max = 1000, message = "Description length must be less than 1000 characters")
String description,

@NotNull(message = "PriceDetails is the mandatory attribute")
PriceDetailsDto priceDetails,

@NotNull(message = "Quantity is the mandatory attribute")
Integer quantity
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@

import com.zufar.onlinestore.product.converter.ProductInfoDtoConverter;
import com.zufar.onlinestore.product.dto.ProductInfoDto;
import com.zufar.onlinestore.product.dto.ProductInfoRequestResponseDto;
import com.zufar.onlinestore.product.entity.ProductInfo;
import com.zufar.onlinestore.product.repository.ProductInfoRepository;

import com.zufar.onlinestore.product.service.ProductInfoService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.Optional;
import java.util.UUID;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -25,13 +28,15 @@
@Validated
@RequestMapping(value = "/api/products")
public class ProductsEndpoint {

private final ProductInfoRepository productInfoRepository;
Copy link
Owner

Choose a reason for hiding this comment

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

delete productInfoRepository and productInfoDtoConverter from ProductsEndpoint class, repository and converter classes should be injected to service class like ProductInfoService

private final ProductInfoDtoConverter productInfoDtoConverter;
private final ProductInfoService productInfoService;

@GetMapping("/{id}")
Copy link
Owner

Choose a reason for hiding this comment

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

rename id like productId please

public ResponseEntity<ProductInfoDto> getProductInfoById(@PathVariable("id") final String id) {
Copy link
Owner

Choose a reason for hiding this comment

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

rename id like productId please

log.info("Received request to get the ProductInfo with id - {}.", id);
Optional<ProductInfo> ProductInfo = productInfoRepository.findById(Integer.parseInt(id));
Optional<ProductInfo> ProductInfo = productInfoRepository.findById(UUID.fromString(id));
Copy link
Owner

Choose a reason for hiding this comment

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

create getProductById method in ProductInfoService and ProductInfoServiceImpl and use this class here like productInfoService.getProductById(productId);

if (ProductInfo.isEmpty()) {
log.info("the ProductInfo with id - {} is absent.", id);
return ResponseEntity.notFound()
Expand All @@ -44,21 +49,17 @@ public ResponseEntity<ProductInfoDto> getProductInfoById(@PathVariable("id") fin
}

@GetMapping
public ResponseEntity<Collection<ProductInfoDto>> getAllProducts() {
log.info("Received request to get all ProductInfos");
Collection<ProductInfo> productInfoCollection = productInfoRepository.findAll();
public ResponseEntity<Page<ProductInfoRequestResponseDto>> getAllProducts(Pageable pageable) {
log.info("Received request to get all ProductInfos (controller): {}", pageable);
Page<ProductInfoRequestResponseDto> productInfoCollection = productInfoService.getAllProducts(pageable);
if (productInfoCollection.isEmpty()) {
log.info("All ProductInfos are absent.");
return ResponseEntity.notFound()
.build();
}
Collection<ProductInfoDto> ProductInfos = productInfoCollection.stream()
.map(productInfoDtoConverter::convertToDto)
.toList();

log.info("All ProductInfos were retrieved - {}.", ProductInfos);
log.info("All ProductInfos were retrieved - {}.", productInfoCollection);
return ResponseEntity.ok()
.body(ProductInfos);
.body(productInfoCollection);
}

}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.zufar.onlinestore.product.repository;

import com.zufar.onlinestore.product.entity.ProductInfo;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.UUID;

@Repository
public interface ProductInfoRepository extends JpaRepository<ProductInfo, Integer> {
public interface ProductInfoRepository extends JpaRepository<ProductInfo, UUID> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.zufar.onlinestore.product.service;

import com.zufar.onlinestore.product.dto.ProductInfoRequestResponseDto;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

public interface ProductInfoService {
Sunagatov marked this conversation as resolved.
Show resolved Hide resolved

Page<ProductInfoRequestResponseDto> getAllProducts(Pageable pageable);
Copy link
Owner

Choose a reason for hiding this comment

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

did you test this?
does it work?

Please test

Sunagatov marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.zufar.onlinestore.product.service.impl;

import com.zufar.onlinestore.product.converter.ProductInfoDtoConverter;
import com.zufar.onlinestore.product.dto.ProductInfoRequestResponseDto;
import com.zufar.onlinestore.product.repository.ProductInfoRepository;
import com.zufar.onlinestore.product.service.ProductInfoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Slf4j
@RequiredArgsConstructor
@Service
public class ProductInfoServiceImpl implements ProductInfoService {

private final ProductInfoRepository productInfoRepository;
private final ProductInfoDtoConverter productInfoDtoConverter;

@Override
public Page<ProductInfoRequestResponseDto> getAllProducts(Pageable pageable) {
Copy link
Owner

Choose a reason for hiding this comment

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

This method returns not all products, it returns some limited products
rename method name like getProducts please

log.info("Received request to get all ProductInfos (service)");
return productInfoRepository.findAll(pageable)
.map(productInfoDtoConverter::convertToRequestResponseDto);
}
}
Loading