Commit d6474c5a authored by Khai Yuan ​Liew's avatar Khai Yuan ​Liew

[AFP-58] Work on DELETE product controller

parent 391f5a10
package com.nisum.ascend.inventory.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(value = HttpStatus.NOT_FOUND)
public class ResourceNotFoundException extends Exception{
HttpStatus status;
public ResourceNotFoundException(HttpStatus status, String message) {
super(message);
this.status = status;
}
}
package com.nisum.ascend.inventory.service; package com.nisum.ascend.inventory.service;
import com.nisum.ascend.inventory.dto.ProductDto; import com.nisum.ascend.inventory.dto.ProductDto;
import com.nisum.ascend.inventory.exception.ResourceNotFoundException;
import com.nisum.ascend.inventory.model.Product; import com.nisum.ascend.inventory.model.Product;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
import com.nisum.ascend.inventory.repository.ProductRepository; import com.nisum.ascend.inventory.repository.ProductRepository;
...@@ -20,7 +22,8 @@ public class ProductService { ...@@ -20,7 +22,8 @@ public class ProductService {
.findBySku(sku) .findBySku(sku)
.map(ProductDto::generateDtoFromProduct) .map(ProductDto::generateDtoFromProduct)
.flatMap(existingProduct -> productRepository.deleteProduct(existingProduct) .flatMap(existingProduct -> productRepository.deleteProduct(existingProduct)
.then(Mono.just(existingProduct))); //Insert Exception Handling after AFP-56 merge .then(Mono.just(existingProduct)))
.switchIfEmpty(Mono.error(new ResourceNotFoundException(HttpStatus.NOT_FOUND, "product not found")));
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment