Skip to content

Commit

Permalink
Add application debug logging
Browse files Browse the repository at this point in the history
  • Loading branch information
semotpan committed Apr 25, 2024
1 parent 2073876 commit ac011c3
Show file tree
Hide file tree
Showing 18 changed files with 128 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.vavr.control.Either;
import io.vavr.control.Validation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -18,6 +19,7 @@
import static io.vavr.API.Invalid;
import static io.vavr.API.Valid;

@Slf4j
@Service
@Transactional
@RequiredArgsConstructor
Expand Down Expand Up @@ -46,6 +48,7 @@ public Either<Failure, Account> create(CreateAccountCommand cmd) {
.build();

accounts.save(account);
log.debug("Account {} was created", account.getId());

return Either.right(account);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ class AccountEventsListener {
*/
@ApplicationModuleListener
public void on(AccountCreated event) {
log.debug("Handle account create event {}", event);
log.debug("[Expense] Handle account create event {}", event);
var either = categoryService.createDefault(new AccountIdentifier(event.accountId()));

if (either.isLeft()) {
log.error("Failed to create default categories for account: {}, failure: {}", event, either.getLeft());
log.error("[Expense] Failed to create default categories for account: {}, failure: {}", event, either.getLeft());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
import io.myfinbox.shared.PaymentType;
import io.vavr.control.Either;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.javamoney.moneta.Money;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
@RequiredArgsConstructor
@Transactional
Expand All @@ -33,8 +35,8 @@ public Either<Failure, Expense> create(ExpenseCommand command) {
return Either.left(Failure.ofValidation(VALIDATION_FAILURE_MESSAGE, validation.getError().toJavaList()));
}

var category = categories.findByIdAndAccount(new CategoryIdentifier(command.categoryId()), new AccountIdentifier(command.accountId()));
if (category.isEmpty()) {
var possibleCategory = categories.findByIdAndAccount(new CategoryIdentifier(command.categoryId()), new AccountIdentifier(command.accountId()));
if (possibleCategory.isEmpty()) {
return Either.left(Failure.ofNotFound(CATEGORY_NOT_FOUND_MESSAGE));
}

Expand All @@ -44,11 +46,13 @@ public Either<Failure, Expense> create(ExpenseCommand command) {
.expenseDate(command.expenseDate())
.paymentType(PaymentType.fromValue(command.paymentType()))
.description(command.description())
.category(category.get())
.category(possibleCategory.get())
.build();

expenses.save(expense);

log.debug("Expense {} was created", expense.getId());

return Either.right(expense);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.vavr.control.Either;
import io.vavr.control.Validation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -23,6 +24,7 @@
import static java.util.Objects.nonNull;
import static org.apache.commons.lang3.StringUtils.isBlank;

@Slf4j
@Service
@RequiredArgsConstructor
class DefaultCategoryService implements CategoryService {
Expand Down Expand Up @@ -51,6 +53,8 @@ public Either<Failure, List<Category>> createDefault(AccountIdentifier account)

categories.saveAll(values);

log.debug("Default expense categories {} were created", values);

return Either.right(values);
}

Expand All @@ -69,6 +73,8 @@ public Either<Failure, Category> create(CategoryCommand command) {
var category = new Category(command.name(), new AccountIdentifier(command.accountId()));
categories.save(category);

log.debug("Expense category {} was created", category.getId());

return Either.right(category);
}

Expand All @@ -84,23 +90,25 @@ public Either<Failure, Category> update(UUID categoryId, CategoryCommand command
return Either.left(Failure.ofNotFound(CATEGORY_NOT_FOUND_MESSAGE));
}

var category = categories.findByIdAndAccount(new CategoryIdentifier(categoryId), new AccountIdentifier(command.accountId()));
if (category.isEmpty()) {
var possibleCategory = categories.findByIdAndAccount(new CategoryIdentifier(categoryId), new AccountIdentifier(command.accountId()));
if (possibleCategory.isEmpty()) {
return Either.left(Failure.ofNotFound(CATEGORY_NOT_FOUND_MESSAGE));
}

if (category.get().sameName(command.name())) {
return Either.right(category.get());
if (possibleCategory.get().sameName(command.name())) {
return Either.right(possibleCategory.get());
}

if (categories.existsByNameAndAccount(command.name(), new AccountIdentifier(command.accountId()))) {
return Either.left(Failure.ofConflict(CATEGORY_NAME_DUPLICATE_MESSAGE));
}

category.get().setName(command.name());
categories.save(category.get()); // FIXME: fix the save anti-pattern
possibleCategory.get().setName(command.name());
categories.save(possibleCategory.get()); // FIXME: fix the save anti-pattern

return Either.right(category.get());
log.debug("Expense category {} wes updated", possibleCategory.get().getId());

return Either.right(possibleCategory.get());
}

@Override
Expand All @@ -110,16 +118,18 @@ public Either<Failure, Void> delete(UUID categoryId) {
return Either.left(Failure.ofNotFound(CATEGORY_NOT_FOUND_MESSAGE));
}

var category = categories.findById(new CategoryIdentifier(categoryId));
if (category.isEmpty()) {
var possibleCategory = categories.findById(new CategoryIdentifier(categoryId));
if (possibleCategory.isEmpty()) {
return Either.left(Failure.ofNotFound(CATEGORY_NOT_FOUND_MESSAGE));
}

if (expenses.existsByCategory(category.get())) {
if (expenses.existsByCategory(possibleCategory.get())) {
return Either.left(Failure.ofConflict(CATEGORY_IN_USE_FAILURE_MESSAGE));
}

categories.delete(category.get());
categories.delete(possibleCategory.get());

log.debug("Expense category {} wes deleted", possibleCategory.get().getId());

return Either.right(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.myfinbox.shared.Failure;
import io.vavr.control.Either;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -12,6 +13,7 @@
import static io.myfinbox.expense.domain.Expense.ExpenseIdentifier;
import static java.util.Objects.isNull;

@Slf4j
@Service
@Transactional
@RequiredArgsConstructor
Expand All @@ -27,14 +29,16 @@ public Either<Failure, Void> delete(UUID expenseId) {
return Either.left(Failure.ofNotFound(EXPENSE_NOT_FOUND_MESSAGE));
}

var expense = expenses.findById(new ExpenseIdentifier(expenseId));
if (expense.isEmpty()) {
var possibleExpense = expenses.findById(new ExpenseIdentifier(expenseId));
if (possibleExpense.isEmpty()) {
return Either.left(Failure.ofNotFound(EXPENSE_NOT_FOUND_MESSAGE));
}

expense.get().delete();
possibleExpense.get().delete();

expenses.delete(expense.get());
expenses.delete(possibleExpense.get());

log.debug("Expense {} was deleted", possibleExpense.get().getId());

return Either.right(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.myfinbox.shared.PaymentType;
import io.vavr.control.Either;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.javamoney.moneta.Money;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -15,6 +16,7 @@

import static java.util.Objects.isNull;

@Slf4j
@Service
@RequiredArgsConstructor
@Transactional
Expand All @@ -40,17 +42,17 @@ public Either<Failure, Expense> update(UUID expenseId, ExpenseCommand command) {
return Either.left(Failure.ofNotFound(EXPENSE_NOT_FOUND_MESSAGE));
}

var expense = expenses.findByIdAndAccount(new ExpenseIdentifier(expenseId), new AccountIdentifier(command.accountId()));
if (expense.isEmpty()) {
var possibleExpense = expenses.findByIdAndAccount(new ExpenseIdentifier(expenseId), new AccountIdentifier(command.accountId()));
if (possibleExpense.isEmpty()) {
return Either.left(Failure.ofNotFound(EXPENSE_NOT_FOUND_MESSAGE));
}

var possibleCategory = fetchCategoryOrFailure(expense.get().getCategory(), command.categoryId(), command.accountId());
var possibleCategory = fetchCategoryOrFailure(possibleExpense.get().getCategory(), command.categoryId(), command.accountId());
if (possibleCategory.isLeft()) {
return Either.left(possibleCategory.getLeft());
}

expense.get().update(
possibleExpense.get().update(
Expense.builder()
.account(new AccountIdentifier(command.accountId()))
.amount(Money.of(command.amount(), command.currencyCode()))
Expand All @@ -60,9 +62,11 @@ public Either<Failure, Expense> update(UUID expenseId, ExpenseCommand command) {
.category(possibleCategory.get())
);

expenses.save(expense.get());
expenses.save(possibleExpense.get()); //FIXME: fix save anti-pattern

return Either.right(expense.get());
log.debug("Expense {} was updated", possibleExpense.get().getId());

return Either.right(possibleExpense.get());
}

private Either<Failure, Category> fetchCategoryOrFailure(Category category, UUID categoryId, UUID accountId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ class AccountEventsListener {
@ApplicationModuleListener
@Transactional(propagation = REQUIRES_NEW)
public void on(AccountCreated event) {
log.debug("Handle account create event {}", event);
log.debug("[Income] Handle account create event {}", event);
var either = incomeSourceService.createDefault(new AccountIdentifier(event.accountId()));

if (either.isLeft()) {
log.error("Failed to create default income sources for account: {}, failure: {}", event, either.getLeft());
log.error("[Income] Failed to create default income sources for account: {}, failure: {}", event, either.getLeft());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.myfinbox.income.application;

import io.myfinbox.expense.domain.Expense;
import io.myfinbox.income.domain.AccountIdentifier;
import io.myfinbox.income.domain.Income;
import io.myfinbox.income.domain.IncomeSource.IncomeSourceIdentifier;
Expand All @@ -9,10 +10,12 @@
import io.myfinbox.shared.PaymentType;
import io.vavr.control.Either;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.javamoney.moneta.Money;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
@Transactional
@RequiredArgsConstructor
Expand All @@ -34,9 +37,8 @@ public Either<Failure, Income> create(IncomeCommand command) {
return Either.left(Failure.ofValidation(VALIDATION_FAILURE_MESSAGE, validation.getError().toJavaList()));
}

var incomeSource = incomeSources.findByIdAndAccount(new IncomeSourceIdentifier(command.incomeSourceId()), new AccountIdentifier(command.accountId()));

if (incomeSource.isEmpty()) {
var possibleIncomeSource = incomeSources.findByIdAndAccount(new IncomeSourceIdentifier(command.incomeSourceId()), new AccountIdentifier(command.accountId()));
if (possibleIncomeSource.isEmpty()) {
return Either.left(Failure.ofNotFound(INCOME_SOURCE_NOT_FOUND_MESSAGE));
}

Expand All @@ -46,11 +48,13 @@ public Either<Failure, Income> create(IncomeCommand command) {
.paymentType(PaymentType.fromValue(command.paymentType()))
.incomeDate(command.incomeDate())
.description(command.description())
.incomeSource(incomeSource.get())
.incomeSource(possibleIncomeSource.get())
.build();

incomes.save(income);

log.debug("Income {} was created", income.getId());

return Either.right(income);
}
}
Loading

0 comments on commit ac011c3

Please sign in to comment.