Commit 3132ce08 authored by Julius Wu's avatar Julius Wu

edit promo is completed

parent c2cc691d
......@@ -46,11 +46,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
......
......@@ -5,8 +5,10 @@ import com.nisum.ascend.promotions.model.Promotion;
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
import org.springframework.stereotype.Repository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@Repository
public interface PromotionRepository extends ReactiveMongoRepository<Promotion, String> {
Flux<Promotion> findByProductSku(String sku);
Mono<Promotion> findByPromotionId(String promoId);
}
......@@ -25,4 +25,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);
});
}
}
......@@ -99,4 +99,18 @@ 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);
}
}
\ No newline at end of file
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