Commit 1e87cd4f authored by Ben Anderson's avatar Ben Anderson

Added the GET /api/promos/{id} route with validations

parent b02db479
package com.nisum.ascend.promotions.controller;
import com.nisum.ascend.promotions.dto.PromotionDto;
import com.nisum.ascend.promotions.exception.PromotionNotFoundException;
import com.nisum.ascend.promotions.model.Promotion;
import com.nisum.ascend.promotions.repository.PromotionRepository;
import com.nisum.ascend.promotions.service.PromotionService;
......@@ -28,9 +29,10 @@ public class PromotionsController {
}
@GetMapping("/{id}")
public Mono<ResponseEntity<PromotionDto>> getPromotionById(@PathVariable String id){
//TODO: get promo by ID
return null;
public Mono<PromotionDto> getPromotionById(@PathVariable String id){
return promotionService
.findPromoById(id)
.onErrorMap(throwable -> new PromotionNotFoundException(id));
}
@PostMapping()
......
package com.nisum.ascend.promotions.exception;
public class PromotionNotFoundException extends RuntimeException {
public PromotionNotFoundException(String id) {
super("The promotion with the ID of " + id + " was not found.");
}
}
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 PromotionNotFoundException) {
exchange.getResponse().setStatusCode(HttpStatus.NOT_FOUND);
// marks the response as complete and forbids writing to it
return exchange.getResponse().setComplete();
}
return Mono.error(ex);
}
}
\ No newline at end of file
......@@ -5,8 +5,11 @@ 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);
}
......@@ -5,6 +5,7 @@ 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 {
......@@ -16,6 +17,11 @@ public class PromotionService {
return promotionRepository.findAll().map(PromotionDto::generateDtoFromPromotion);
}
public Mono<PromotionDto> findPromoById(String promoId) {
return promotionRepository.findByPromotionId(promoId)
.map(PromotionDto::generateDtoFromPromotion);
}
public Flux<PromotionDto> findPromotionsByProductSku(String sku){
return promotionRepository.findByProductSku(sku).map(PromotionDto::generateDtoFromPromotion);
}
......
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