Commit 4b7f1cf5 authored by Sumaiyya Burney's avatar Sumaiyya Burney

Merge branch 'AFP-62' into 'dev'

edit promo is completed

See merge request !9
parents f80b22ac 297582f7
......@@ -49,11 +49,13 @@ public class PromotionsController {
}
@PutMapping("/{promoId}")
public Mono<ResponseEntity<PromotionDto>> updatePromotionById(@PathVariable String promoId, @RequestBody Promotion newPromotion){
//TODO: update promotion
return null;
public Mono<ResponseEntity<PromotionDto>> updatePromotionById(@PathVariable("promoId") String promoId, @RequestBody Promotion newPromotion){
return promotionService.updatePromotion(promoId,newPromotion)
.map(updatedProduct -> ResponseEntity.ok(updatedProduct))
.defaultIfEmpty(ResponseEntity.badRequest().build());
}
@DeleteMapping("/{promoId}")
public Mono<ResponseEntity<PromotionDto>> deletePromotionById(@PathVariable String promoId){
//TODO: delete promotion
......
......@@ -25,16 +25,6 @@ class RestWebExceptionHandler implements WebExceptionHandler {
// marks the response as complete and forbids writing to it
return exchange.getResponse().writeWith(Flux.just(buffer));
}
return Mono.error(ex);
}
}
@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);
......@@ -42,5 +32,4 @@ class RestWebExceptionHandler implements WebExceptionHandler {
}
return Mono.error(ex);
}
}
......@@ -10,6 +10,5 @@ import reactor.core.publisher.Mono;
@Repository
public interface PromotionRepository extends ReactiveMongoRepository<Promotion, String> {
Flux<Promotion> findByProductSku(String sku);
Mono<Promotion> findByPromotionId(String promoId);
}
......@@ -35,4 +35,14 @@ public class PromotionService {
public Mono<PromotionDto> createPromotion(Promotion promotion){
return promotionRepository.save(promotion).map(PromotionDto::generateDtoFromPromotion);
}
public Mono<PromotionDto> updatePromotion(String promoId, Promotion promotion){
return promotionRepository.findByPromotionId(promoId)
.flatMap(promo ->{
promo.setDiscountPercentage(promotion.getDiscountPercentage());
promo.setProductSku(promotion.getProductSku());
promo.setMinimumQuantity(promotion.getMinimumQuantity());
return promotionRepository.save(promo).map(PromotionDto::generateDtoFromPromotion);
});
}
}
package com.nisum.ascend.promotions.controller;
import com.nisum.ascend.promotions.dto.PromotionDto;
import com.nisum.ascend.promotions.model.Promotion;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
......@@ -62,7 +63,9 @@ class PromotionsControllerTest {
.expectHeader().contentType(MediaType.APPLICATION_JSON_VALUE)
.expectBody()
.jsonPath("promotionId", "0003");
}
@Test
void createPromotion(){
Promotion promotion = new Promotion("50OFF","SH1234", (float) 0.5,5);
webTestClient.post().uri("/api/promos")
......@@ -111,6 +114,20 @@ class PromotionsControllerTest {
promos.forEach(p -> assertEquals(skuWithMultiplePromos, p.getProductSku()));
});
}
@Test
void editPromotion(){
Promotion promotion = new Promotion("0003","1000",(float) .5,1);
webTestClient.put()
.uri("/api/promos/0003")
.contentType(MediaType.valueOf(MediaType.APPLICATION_JSON_VALUE))
.accept(MediaType.valueOf(MediaType.APPLICATION_JSON_VALUE))
.body(Mono.just(promotion),Promotion.class)
.exchange()
.expectStatus().isOk()
.expectBody()
.jsonPath("$.minimumQuantity").isEqualTo(1);
}
@Test
public void bulkSearchTest(){
......
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