feature: add bank account domain improvements

This commit is contained in:
Alexander
2022-04-18 10:27:30 +03:00
parent 39430d8426
commit e7f1a901d9
6 changed files with 38 additions and 17 deletions

View File

@@ -0,0 +1,10 @@
package com.eventsourcing.bankAccount.exceptions;
public class BankAccountDocumentNotFoundException extends RuntimeException {
public BankAccountDocumentNotFoundException() {
}
public BankAccountDocumentNotFoundException(String id) {
super("bank account document not found id:" + id);
}
}

View File

@@ -7,6 +7,7 @@ import com.eventsourcing.bankAccount.events.AddressUpdatedEvent;
import com.eventsourcing.bankAccount.events.BalanceDepositedEvent; import com.eventsourcing.bankAccount.events.BalanceDepositedEvent;
import com.eventsourcing.bankAccount.events.BankAccountCreatedEvent; import com.eventsourcing.bankAccount.events.BankAccountCreatedEvent;
import com.eventsourcing.bankAccount.events.EmailChangedEvent; import com.eventsourcing.bankAccount.events.EmailChangedEvent;
import com.eventsourcing.bankAccount.exceptions.BankAccountDocumentNotFoundException;
import com.eventsourcing.bankAccount.repository.BankAccountMongoRepository; import com.eventsourcing.bankAccount.repository.BankAccountMongoRepository;
import com.eventsourcing.es.Event; import com.eventsourcing.es.Event;
import com.eventsourcing.es.EventStoreDB; import com.eventsourcing.es.EventStoreDB;
@@ -114,7 +115,7 @@ public class BankAccountMongoProjection implements Projection {
log.info("(when) EmailChangedEvent: {}, aggregateID: {}", event, event.getAggregateId()); log.info("(when) EmailChangedEvent: {}, aggregateID: {}", event, event.getAggregateId());
final var documentOptional = mongoRepository.findByAggregateId(event.getAggregateId()); final var documentOptional = mongoRepository.findByAggregateId(event.getAggregateId());
if (documentOptional.isEmpty()) if (documentOptional.isEmpty())
throw new RuntimeException("Bank Account Document not found id: {}" + event.getAggregateId()); throw new BankAccountDocumentNotFoundException(event.getAggregateId());
final var document = documentOptional.get(); final var document = documentOptional.get();
document.setEmail(event.getNewEmail()); document.setEmail(event.getNewEmail());
@@ -126,7 +127,7 @@ public class BankAccountMongoProjection implements Projection {
log.info("(when) AddressUpdatedEvent: {}, aggregateID: {}", event, event.getAggregateId()); log.info("(when) AddressUpdatedEvent: {}, aggregateID: {}", event, event.getAggregateId());
final var documentOptional = mongoRepository.findByAggregateId(event.getAggregateId()); final var documentOptional = mongoRepository.findByAggregateId(event.getAggregateId());
if (documentOptional.isEmpty()) if (documentOptional.isEmpty())
throw new RuntimeException("Bank Account Document not found id: {}" + event.getAggregateId()); throw new BankAccountDocumentNotFoundException(event.getAggregateId());
final var document = documentOptional.get(); final var document = documentOptional.get();
document.setAddress(event.getNewAddress()); document.setAddress(event.getNewAddress());
@@ -138,11 +139,10 @@ public class BankAccountMongoProjection implements Projection {
log.info("(when) BalanceDepositedEvent: {}, aggregateID: {}", event, event.getAggregateId()); log.info("(when) BalanceDepositedEvent: {}, aggregateID: {}", event, event.getAggregateId());
final var documentOptional = mongoRepository.findByAggregateId(event.getAggregateId()); final var documentOptional = mongoRepository.findByAggregateId(event.getAggregateId());
if (documentOptional.isEmpty()) if (documentOptional.isEmpty())
throw new RuntimeException("Bank Account Document not found id: {}" + event.getAggregateId()); throw new BankAccountDocumentNotFoundException(event.getAggregateId());
final var document = documentOptional.get(); final var document = documentOptional.get();
final var balance = document.getBalance(); final var newBalance = document.getBalance().add(event.getAmount());
final var newBalance = balance.add(event.getAmount());
document.setBalance(newBalance); document.setBalance(newBalance);
mongoRepository.save(document); mongoRepository.save(document);
} }

View File

@@ -35,7 +35,7 @@ public class KafkaEventBus implements EventBus {
log.info("publishing kafka record value >>>>> {}", new String(record.value())); log.info("publishing kafka record value >>>>> {}", new String(record.value()));
} catch (Exception ex) { } catch (Exception ex) {
log.error("(KafkaEventBus) publish get", ex); log.error("(KafkaEventBus) publish get timeout", ex);
throw new RuntimeException(ex); throw new RuntimeException(ex);
} }
} }

View File

@@ -0,0 +1,6 @@
package com.eventsourcing.exceptions;
import java.time.LocalDateTime;
public record ExceptionResponseDTO(int Status, String message, LocalDateTime timestamp) {
}

View File

@@ -1,6 +0,0 @@
package com.eventsourcing.exceptions;
import java.time.LocalDateTime;
public record NotFoundResponseDTO(int Status, String message, LocalDateTime timestamp) {
}

View File

@@ -1,8 +1,11 @@
package com.eventsourcing.filters; package com.eventsourcing.filters;
import com.eventsourcing.bankAccount.exceptions.BankAccountDocumentNotFoundException;
import com.eventsourcing.bankAccount.exceptions.InvalidAddressException;
import com.eventsourcing.bankAccount.exceptions.InvalidEmailException;
import com.eventsourcing.es.exceptions.AggregateNotFoundException; import com.eventsourcing.es.exceptions.AggregateNotFoundException;
import com.eventsourcing.exceptions.InternalServerErrorResponse; import com.eventsourcing.exceptions.InternalServerErrorResponse;
import com.eventsourcing.exceptions.NotFoundResponseDTO; import com.eventsourcing.exceptions.ExceptionResponseDTO;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
@@ -43,10 +46,18 @@ public class GlobalControllerAdvice {
} }
@ResponseStatus(HttpStatus.NOT_FOUND) @ResponseStatus(HttpStatus.NOT_FOUND)
@ExceptionHandler(AggregateNotFoundException.class) @ExceptionHandler({AggregateNotFoundException.class, BankAccountDocumentNotFoundException.class})
public ResponseEntity<NotFoundResponseDTO> handleAggregateNotFoundException(AggregateNotFoundException ex) { public ResponseEntity<ExceptionResponseDTO> handleAggregateNotFoundExceptions(AggregateNotFoundException ex) {
final var notFoundResponseDTO = new NotFoundResponseDTO(HttpStatus.NOT_FOUND.value(), ex.getMessage(), LocalDateTime.now()); final var notFoundResponseDTO = new ExceptionResponseDTO(HttpStatus.NOT_FOUND.value(), ex.getMessage(), LocalDateTime.now());
log.error("AggregateNotFoundException response ex:", ex); log.error("handleAggregateNotFoundExceptions response ex:", ex);
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(notFoundResponseDTO); return ResponseEntity.status(HttpStatus.NOT_FOUND).body(notFoundResponseDTO);
} }
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler({InvalidAddressException.class, InvalidEmailException.class})
public ResponseEntity<ExceptionResponseDTO> handleInvalidAggregateExceptions(AggregateNotFoundException ex) {
final var notFoundResponseDTO = new ExceptionResponseDTO(HttpStatus.BAD_REQUEST.value(), ex.getMessage(), LocalDateTime.now());
log.error("handleInvalidAggregateExceptions response ex:", ex);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(notFoundResponseDTO);
}
} }