Commit 057c3619 authored by Khai Yuan ​Liew's avatar Khai Yuan ​Liew

[AFP-60] Fixed merge conflict

parents 5d69c51e c2cc691d
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;
......@@ -34,10 +35,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 org.springframework.web.reactive.function.BodyInserters;
import reactor.core.publisher.Flux;
import reactor.core.scheduler.Schedulers;
import java.util.List;
import static org.springframework.http.ResponseEntity.notFound;
import static org.springframework.http.ResponseEntity.ok;
import reactor.core.publisher.Mono;
@Service
public class PromotionService {
......@@ -28,9 +24,10 @@ public class PromotionService {
}
public Flux<PromotionDto> bulkSearchSku(List<String> skus){
return Flux.fromIterable(skus)
.flatMap(sku -> findPromotionsByProductSku(sku));
return Flux.fromIterable(skus).flatMap(sku -> findPromotionsByProductSku(sku));
}
public Mono<PromotionDto> createPromotion(Promotion promotion){
return promotionRepository.save(promotion).map(PromotionDto::generateDtoFromPromotion);
}
}
......@@ -14,7 +14,6 @@ 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.Flux;
import reactor.core.publisher.Mono;
import java.util.Arrays;
......@@ -52,6 +51,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