Skip to content

Commit

Permalink
Product update code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
kaizerpwn committed May 12, 2024
1 parent 5842eec commit 2485c9c
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.ibrahimokic.ordermanagement.domain.entity.Address;
import com.ibrahimokic.ordermanagement.domain.dto.AddressDto;
import com.ibrahimokic.ordermanagement.mapper.impl.AddressMapperImpl;
import com.ibrahimokic.ordermanagement.mapper.impl.AddressMapperImpl;
import com.ibrahimokic.ordermanagement.service.AddressService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.ArraySchema;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public ResponseEntity<?> updateProduct(@PathVariable Long productId, @Valid @Req
if (updatedProduct.isPresent()) {
return ResponseEntity.ok().body(productMapper.mapTo(updatedProduct.get()));
} else {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("An error occurred while deleting a product.");
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("An error occurred while updating an product.");
}
} catch (Exception e) {
return ResponseEntity
Expand All @@ -125,7 +125,7 @@ public ResponseEntity<String> deleteProduct(@PathVariable Long productId) {
if (deletionResult) {
return ResponseEntity.ok().body("Product deleted successfully.");
} else {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("An error occurred while deleting product " + productId + ".");
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("An error occurred while deleting an product " + productId + ".");
}
} catch (Exception e) {
return ResponseEntity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ public class UserDto {
@JsonProperty(value = "password", access = JsonProperty.Access.WRITE_ONLY)
@NotNull
@NotBlank(message = "Password is required")
@Size(
min = 5,
message = "The password '${validatedValue}' must be greater then {min}"
)
@Size(min = 5, message = "The password '${validatedValue}' must be greater then {min}")
private String password;

@JsonProperty("email")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.ibrahimokic.ordermanagement.domain.dto.OrderItemDto;
import com.ibrahimokic.ordermanagement.domain.entity.OrderItem;
import com.ibrahimokic.ordermanagement.mapper.Mapper;
import jakarta.validation.Valid;
import org.modelmapper.ModelMapper;
import org.springframework.stereotype.Component;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public OrderMapperImpl(ModelMapper modelMapper) {
@Override
public OrderDto mapTo(Order order) {
OrderDto orderDto = modelMapper.map(order, OrderDto.class);
System.out.println(orderDto);
orderDto.setUserId(order.getUser().getUserId());
return orderDto;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

import com.ibrahimokic.ordermanagement.domain.dto.ProductDto;
import com.ibrahimokic.ordermanagement.domain.entity.Product;
import com.ibrahimokic.ordermanagement.mapper.impl.ProductMapperImpl;
import com.ibrahimokic.ordermanagement.repository.OrderItemRepository;
import com.ibrahimokic.ordermanagement.repository.ProductRepository;
import com.ibrahimokic.ordermanagement.service.ProductService;

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
Expand All @@ -20,6 +21,7 @@
public class ProductServiceImpl implements ProductService {
private final ProductRepository productRepository;
private final OrderItemRepository orderItemRepository;
private final ProductMapperImpl productMapper;

@Override
public List<Product> getAllProducts() {
Expand Down Expand Up @@ -58,18 +60,18 @@ public Product createProduct(Product product) {
}

@Override
public Optional<Product> updateProduct(Long productId, ProductDto updatedProductDto) {
public Optional<Product> updateProduct(Long productId, @Valid ProductDto updatedProductDto) {
try {
Optional<Product> optionalExistingProduct = productRepository.findById(productId);

if (optionalExistingProduct.isPresent()) {
Product existingProduct = optionalExistingProduct.get();
BeanUtils.copyProperties(updatedProductDto, existingProduct);
Product existingProduct = productMapper.mapFrom(updatedProductDto);
existingProduct.setProductId(productId);
try {
productRepository.save(existingProduct);
return Optional.of(existingProduct);
} catch (Exception e) {
return Optional.empty();
throw new RuntimeException("Product not found");
}
} else {
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ public static boolean checkIfAddressIsDifferent(Address address, AddressDto addr
!Objects.equals(address.getCity(), addressDto.getCity()) ||
!Objects.equals(address.getZip(), addressDto.getZip()) ||
!Objects.equals(address.getCountry(), addressDto.getCountry())) {

System.out.println(address.getStreet() + " "+ addressDto.getStreet());
System.out.println(address.getCity() + " "+ addressDto.getCity());
System.out.println(address.getZip() + " "+ addressDto.getZip());
System.out.println(address.getCountry() + " "+ addressDto.getCountry());
return true;
}
return false;
Expand Down

0 comments on commit 2485c9c

Please sign in to comment.