Commit c2cc691d authored by John Lam's avatar John Lam

Merge branch 'AFP-61' into dev

parents d7e70e39 f9d0c96d
package com.nisum.ascend.promotions.controller;
import com.nisum.ascend.promotions.dto.PromotionDto;
import com.nisum.ascend.promotions.exception.PromotionAlreadyExistsException;
import com.nisum.ascend.promotions.model.Promotion;
import com.nisum.ascend.promotions.repository.PromotionRepository;
import com.nisum.ascend.promotions.service.PromotionService;
......@@ -33,10 +34,9 @@ public class PromotionsController {
return null;
}
@PostMapping()
public Mono<ResponseEntity<PromotionDto>> createPromotion(@RequestBody Promotion newPromotion){
//TODO: create promotion
return null;
@PostMapping("")
public Mono<PromotionDto> createPromotion(@RequestBody Promotion newPromotion){
return promotionService.createPromotion(newPromotion).onErrorMap(throwable -> new PromotionAlreadyExistsException());
}
@PostMapping("/bulkSearch")
......
package com.nisum.ascend.promotions.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(value = HttpStatus.NOT_ACCEPTABLE)
public class PromotionAlreadyExistsException extends RuntimeException{
public PromotionAlreadyExistsException(){
super("promotion already existed");
}
}
package com.nisum.ascend.promotions.exception;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebExceptionHandler;
import reactor.core.publisher.Mono;
@Component
@Order(-2)
class RestWebExceptionHandler implements WebExceptionHandler {
@Override
public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
if (ex instanceof PromotionAlreadyExistsException) {
exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
return exchange.getResponse().setComplete();
}
return Mono.error(ex);
}
}
package com.nisum.ascend.promotions.service;
import com.nisum.ascend.promotions.dto.PromotionDto;
import com.nisum.ascend.promotions.model.Promotion;
import com.nisum.ascend.promotions.repository.PromotionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@Service
public class PromotionService {
......@@ -19,4 +21,8 @@ public class PromotionService {
public Flux<PromotionDto> findPromotionsByProductSku(String sku){
return promotionRepository.findByProductSku(sku).map(PromotionDto::generateDtoFromPromotion);
}
public Mono<PromotionDto> createPromotion(Promotion promotion){
return promotionRepository.save(promotion).map(PromotionDto::generateDtoFromPromotion);
}
}
......@@ -14,6 +14,7 @@ import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.reactive.server.WebTestClient;
import reactor.core.publisher.Mono;
import java.util.List;
......@@ -49,6 +50,18 @@ class PromotionsControllerTest {
});
}
@Test
void createPromotion(){
Promotion promotion = new Promotion("50OFF","SH1234", (float) 0.5,5);
webTestClient.post().uri("/api/promos")
.contentType(MediaType.valueOf(MediaType.APPLICATION_JSON_VALUE))
.body(Mono.just(promotion),Promotion.class)
.exchange()
.expectStatus().isOk()
.expectBody()
.jsonPath("$.promotionId","50OFF");
}
@Test
void getPromotionsByProductSku(){
String skuWithOnePromo = "AFP-1";
......
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