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 e0c31ab
Show file tree
Hide file tree
Showing 18 changed files with 83 additions and 23 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 Down Expand Up @@ -49,6 +51,8 @@ public Either<Failure, Expense> create(ExpenseCommand command) {

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 @@ -36,6 +38,8 @@ public Either<Failure, Void> delete(UUID expenseId) {

expenses.delete(expense.get());

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

return Either.right(null);
}
}
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 Down Expand Up @@ -60,7 +62,9 @@ public Either<Failure, Expense> update(UUID expenseId, ExpenseCommand command) {
.category(possibleCategory.get())
);

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

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

return Either.right(expense.get());
}
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 Down Expand Up @@ -51,6 +54,8 @@ public Either<Failure, Income> create(IncomeCommand command) {

incomes.save(income);

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

return Either.right(income);
}
}
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 DefaultIncomeSourceService implements IncomeSourceService {
Expand Down Expand Up @@ -50,6 +52,7 @@ public Either<Failure, List<IncomeSource>> createDefault(AccountIdentifier accou
.toList();

incomeSources.saveAll(values);
log.debug("Default Income Sources {} were created", values);

return Either.right(values);
}
Expand All @@ -69,6 +72,8 @@ public Either<Failure, IncomeSource> create(IncomeSourceCommand command) {
var incomeSource = new IncomeSource(command.name(), new AccountIdentifier(command.accountId()));
incomeSources.save(incomeSource);

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

return Either.right(incomeSource);
}

Expand Down Expand Up @@ -99,6 +104,7 @@ public Either<Failure, IncomeSource> update(UUID incomeSourceId, IncomeSourceCom

incomeSource.get().setName(command.name());
incomeSources.save(incomeSource.get()); // FIXME: fix the save anti-pattern
log.debug("Income Source {} was updated", incomeSource.get().getId());

return Either.right(incomeSource.get());
}
Expand All @@ -120,6 +126,7 @@ public Either<Failure, Void> delete(UUID incomeSourceId) {
}

incomeSources.delete(incomeSource.get());
log.debug("Income Source {} was deleted", incomeSource.get().getId());

return Either.right(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
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;

import java.util.UUID;

import static java.util.Objects.isNull;

@Slf4j
@Service
@Transactional
@RequiredArgsConstructor
Expand All @@ -35,6 +37,7 @@ public Either<Failure, Void> delete(UUID incomeId) {
income.get().delete();

incomes.delete(income.get());
log.debug("Income {} was deleted", income.get().getId());

return Either.right(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,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 io.myfinbox.income.domain.IncomeSource.IncomeSourceIdentifier;
import static java.util.Objects.isNull;

@Slf4j
@Service
@RequiredArgsConstructor
@Transactional
Expand Down Expand Up @@ -61,6 +63,7 @@ public Either<Failure, Income> update(UUID incomeId, IncomeCommand command) {
);

incomes.save(income.get());
log.debug("Income {} was updated", income.get().getId());

return Either.right(income.get());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ExpenseEventsListener {
*/
@ApplicationModuleListener
public void on(ExpenseCreated event) {
log.debug("Received ExpenseCreated event: {}", event);
log.debug("[Plan] Received ExpenseCreated event: {}", event);

// Record the created expense
var expenseRecord = expenseRecordTrackerUseCase.recordCreated(ExpenseCreatedRecord.builder()
Expand All @@ -38,7 +38,7 @@ public void on(ExpenseCreated event) {
.build());

if (expenseRecord.isEmpty()) {
log.debug("ExpenseCreated event: {} skipped", event);
log.debug("[Plan] ExpenseCreated event: {} skipped", event);
}
}
}
Loading

0 comments on commit e0c31ab

Please sign in to comment.