Search in sources :

Example 6 with DomainException

use of com.actionworks.flashsale.domain.exception.DomainException in project flash-sale by ThoughtsBeta.

the class FlashItemDomainServiceImpl method onlineFlashItem.

@Override
public void onlineFlashItem(Long itemId) {
    logger.info("itemOnline|上线秒杀品|{}", itemId);
    if (itemId == null) {
        throw new DomainException(PARAMS_INVALID);
    }
    Optional<FlashItem> flashItemOptional = flashItemRepository.findById(itemId);
    if (!flashItemOptional.isPresent()) {
        throw new DomainException(FLASH_ITEM_DOES_NOT_EXIST);
    }
    FlashItem flashItem = flashItemOptional.get();
    if (FlashItemStatus.isOnline(flashItem.getStatus())) {
        return;
    }
    flashItem.setStatus(FlashItemStatus.ONLINE.getCode());
    flashItemRepository.save(flashItem);
    logger.info("itemOnline|秒杀品已上线|{}", itemId);
    FlashItemEvent flashItemPublishEvent = new FlashItemEvent();
    flashItemPublishEvent.setEventType(FlashItemEventType.ONLINE);
    flashItemPublishEvent.setFlashItem(flashItem);
    domainEventPublisher.publish(flashItemPublishEvent);
    logger.info("itemOnline|秒杀品上线事件已发布|{}", itemId);
}
Also used : DomainException(com.actionworks.flashsale.domain.exception.DomainException) FlashItemEvent(com.actionworks.flashsale.domain.event.FlashItemEvent) FlashItem(com.actionworks.flashsale.domain.model.entity.FlashItem)

Example 7 with DomainException

use of com.actionworks.flashsale.domain.exception.DomainException in project flash-sale by ThoughtsBeta.

the class FlashItemDomainServiceImpl method offlineFlashItem.

@Override
public void offlineFlashItem(Long itemId) {
    logger.info("itemOffline|下线秒杀品|{}", itemId);
    if (itemId == null) {
        throw new DomainException(PARAMS_INVALID);
    }
    Optional<FlashItem> flashItemOptional = flashItemRepository.findById(itemId);
    if (!flashItemOptional.isPresent()) {
        throw new DomainException(FLASH_ITEM_DOES_NOT_EXIST);
    }
    FlashItem flashItem = flashItemOptional.get();
    if (FlashItemStatus.isOffline(flashItem.getStatus())) {
        return;
    }
    flashItem.setStatus(FlashItemStatus.OFFLINE.getCode());
    flashItemRepository.save(flashItem);
    logger.info("itemOffline|秒杀品已下线|{}", itemId);
    FlashItemEvent flashItemEvent = new FlashItemEvent();
    flashItemEvent.setEventType(FlashItemEventType.OFFLINE);
    flashItemEvent.setFlashItem(flashItem);
    domainEventPublisher.publish(flashItemEvent);
    logger.info("itemOffline|秒杀品下线事件已发布|{}", itemId);
}
Also used : DomainException(com.actionworks.flashsale.domain.exception.DomainException) FlashItemEvent(com.actionworks.flashsale.domain.event.FlashItemEvent) FlashItem(com.actionworks.flashsale.domain.model.entity.FlashItem)

Example 8 with DomainException

use of com.actionworks.flashsale.domain.exception.DomainException in project flash-sale by ThoughtsBeta.

the class FlashOrderDomainServiceImpl method placeOrder.

@Override
public boolean placeOrder(Long userId, FlashOrder flashOrder) {
    logger.info("placeOrder|下单|{},{}", userId, JSON.toJSONString(flashOrder));
    if (flashOrder == null || !flashOrder.validateParamsForCreate()) {
        throw new DomainException(PARAMS_INVALID);
    }
    flashOrder.setStatus(FlashOrderStatus.CREATED.getCode());
    boolean saveSuccess = flashOrderRepository.save(flashOrder);
    if (saveSuccess) {
        FlashOrderEvent flashOrderEvent = new FlashOrderEvent();
        flashOrderEvent.setEventType(FlashOrderEventType.CREATED);
        domainEventPublisher.publish(flashOrderEvent);
    }
    logger.info("placeOrder|订单已创建成功|{},{}", userId, JSON.toJSONString(flashOrder));
    return saveSuccess;
}
Also used : DomainException(com.actionworks.flashsale.domain.exception.DomainException) FlashOrderEvent(com.actionworks.flashsale.domain.event.FlashOrderEvent)

Example 9 with DomainException

use of com.actionworks.flashsale.domain.exception.DomainException in project flash-sale by ThoughtsBeta.

the class BadRequestExceptionHandler method handleConflict.

@ExceptionHandler(value = { BizException.class, FlowException.class, AuthException.class, DomainException.class })
protected ResponseEntity<Object> handleConflict(RuntimeException ex, WebRequest request) {
    ExceptionResponse exceptionResponse = new ExceptionResponse();
    if (ex instanceof UndeclaredThrowableException) {
        if (((UndeclaredThrowableException) ex).getUndeclaredThrowable() instanceof FlowException) {
            exceptionResponse.setErrorCode(LIMIT_BLOCK.getCode());
            exceptionResponse.setErrorMessage(LIMIT_BLOCK.getDesc());
        }
    } else if (ex instanceof BizException || ex instanceof DomainException) {
        exceptionResponse.setErrorCode(BIZ_ERROR.getCode());
        exceptionResponse.setErrorMessage(ex.getMessage());
    } else if (ex instanceof AuthException) {
        exceptionResponse.setErrorCode(AUTH_ERROR.getCode());
        exceptionResponse.setErrorMessage(AUTH_ERROR.getDesc());
    }
    logger.error("expectedException|预期错误|{},{}", ex.getMessage(), ex);
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setContentType(MediaType.APPLICATION_JSON);
    return handleExceptionInternal(ex, JSON.toJSONString(exceptionResponse), httpHeaders, HttpStatus.BAD_REQUEST, request);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) DomainException(com.actionworks.flashsale.domain.exception.DomainException) FlowException(com.alibaba.csp.sentinel.slots.block.flow.FlowException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) BizException(com.actionworks.flashsale.app.exception.BizException) ResponseEntityExceptionHandler(org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 10 with DomainException

use of com.actionworks.flashsale.domain.exception.DomainException in project flash-sale by ThoughtsBeta.

the class FlashOrderDomainServiceImpl method cancelOrder.

@Override
public boolean cancelOrder(Long userId, Long orderId) {
    logger.info("placeOrder|取消订单|{},{}", userId, orderId);
    if (StringUtils.isEmpty(userId) || orderId == null) {
        throw new DomainException(PARAMS_INVALID);
    }
    Optional<FlashOrder> flashOrderOptional = flashOrderRepository.findById(orderId);
    if (!flashOrderOptional.isPresent()) {
        throw new DomainException(FLASH_ITEM_DOES_NOT_EXIST);
    }
    FlashOrder flashOrder = flashOrderOptional.get();
    if (!flashOrder.getUserId().equals(userId)) {
        throw new DomainException(FLASH_ITEM_DOES_NOT_EXIST);
    }
    if (FlashOrderStatus.isCancled(flashOrder.getStatus())) {
        return false;
    }
    flashOrder.setStatus(FlashOrderStatus.CANCELED.getCode());
    boolean saveSuccess = flashOrderRepository.updateStatus(flashOrder);
    if (saveSuccess) {
        FlashOrderEvent flashOrderEvent = new FlashOrderEvent();
        flashOrderEvent.setEventType(FlashOrderEventType.CANCEL);
        domainEventPublisher.publish(flashOrderEvent);
    }
    logger.info("placeOrder|订单已取消|{},{}", userId, orderId);
    return saveSuccess;
}
Also used : DomainException(com.actionworks.flashsale.domain.exception.DomainException) FlashOrder(com.actionworks.flashsale.domain.model.entity.FlashOrder) FlashOrderEvent(com.actionworks.flashsale.domain.event.FlashOrderEvent)

Aggregations

DomainException (com.actionworks.flashsale.domain.exception.DomainException)13 FlashActivityEvent (com.actionworks.flashsale.domain.event.FlashActivityEvent)4 FlashItemEvent (com.actionworks.flashsale.domain.event.FlashItemEvent)3 StockBucketEvent (com.actionworks.flashsale.domain.event.StockBucketEvent)3 FlashOrderEvent (com.actionworks.flashsale.domain.event.FlashOrderEvent)2 FlashActivity (com.actionworks.flashsale.domain.model.entity.FlashActivity)2 FlashItem (com.actionworks.flashsale.domain.model.entity.FlashItem)2 BizException (com.actionworks.flashsale.app.exception.BizException)1 Bucket (com.actionworks.flashsale.domain.model.Bucket)1 FlashOrder (com.actionworks.flashsale.domain.model.entity.FlashOrder)1 FlowException (com.alibaba.csp.sentinel.slots.block.flow.FlowException)1 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)1 HttpHeaders (org.springframework.http.HttpHeaders)1 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)1 ResponseEntityExceptionHandler (org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler)1