-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose REST API for creating a classic plan
- Loading branch information
Showing
13 changed files
with
424 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
server/src/main/java/io/myfinbox/spendingplan/application/ClassicPlanBuilderService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package io.myfinbox.spendingplan.application; | ||
|
||
import io.myfinbox.shared.Failure; | ||
import io.myfinbox.spendingplan.domain.ClassicJarDistribution; | ||
import io.myfinbox.spendingplan.domain.Plan; | ||
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.Arrays; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional | ||
class ClassicPlanBuilderService implements ClassicPlanBuilderUseCase { | ||
|
||
static final String CLASSIC_SPENDING_PLAN = "My classic spending plan"; | ||
static final String CLASSIC_PLAN_DESCRIPTION = "My classic plan distribution: Necessities(55%), Long Term Savings(10%), " + | ||
"Education(10%), Play(10%), Financial(10%), Give(5%)."; | ||
|
||
private final CreatePlanUseCase createPlanUseCase; | ||
private final CreateJarUseCase createJarUseCase; | ||
|
||
@Override | ||
public Either<Failure, Plan> create(CreateClassicPlanCommand command) { | ||
log.debug("Classic spending plan creation ..."); | ||
|
||
var planCommand = PlanCommand.builder() | ||
.accountId(command.accountId()) | ||
.amount(command.amount()) | ||
.currencyCode(command.currencyCode()) | ||
.name(CLASSIC_SPENDING_PLAN) | ||
.description(CLASSIC_PLAN_DESCRIPTION) | ||
.build(); | ||
|
||
var planEither = createPlanUseCase.create(planCommand); | ||
if (planEither.isLeft()) { | ||
return planEither; | ||
} | ||
|
||
var jarCommands = Arrays.stream(ClassicJarDistribution.values()) | ||
.map(jar -> JarCommand.builder() | ||
.name(jar.jarName()) | ||
.percentage(jar.percentage()) | ||
.description(jar.description()) | ||
.build()) | ||
.toList(); | ||
|
||
var planIdentifier = planEither.get().getId(); | ||
for (var jarCommand : jarCommands) { | ||
var jarEither = createJarUseCase.create(planIdentifier.id(), jarCommand); | ||
if (jarEither.isLeft()) { | ||
return Either.left(jarEither.getLeft()); | ||
} | ||
} | ||
|
||
log.debug("Classic spending plan was created '{}' , with 6 jars.", CLASSIC_SPENDING_PLAN); | ||
return Either.right(planEither.get()); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
server/src/main/java/io/myfinbox/spendingplan/application/ClassicPlanBuilderUseCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package io.myfinbox.spendingplan.application; | ||
|
||
import io.myfinbox.shared.Failure; | ||
import io.myfinbox.spendingplan.domain.Plan; | ||
import io.vavr.control.Either; | ||
import lombok.Builder; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.UUID; | ||
|
||
/** | ||
* Use case interface for creating classic plans. | ||
*/ | ||
public interface ClassicPlanBuilderUseCase { | ||
|
||
/** | ||
* Creates a classic plan based on the provided command. | ||
* | ||
* @param command The command containing information for creating the classic plan. | ||
* @return Either a failure or the created plan. | ||
*/ | ||
Either<Failure, Plan> create(CreateClassicPlanCommand command); | ||
|
||
@Builder | ||
record CreateClassicPlanCommand(UUID accountId, | ||
BigDecimal amount, | ||
String currencyCode) { | ||
public static final String FIELD_ACCOUNT_ID = "accountId"; | ||
public static final String FIELD_AMOUNT = "amount"; | ||
public static final String FIELD_CURRENCY_CODE = "currencyCode"; | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
server/src/main/java/io/myfinbox/spendingplan/domain/ClassicJarDistribution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package io.myfinbox.spendingplan.domain; | ||
|
||
/** | ||
* Enumeration representing the distribution of funds into classic jars. | ||
* <a href="https://www.harveker.com/blog/6-step-money-managing-system/">source 1</a> and | ||
* <a href="https://note.moneylover.me/get-a-millionaire-mind-set-with-6-jars-of-money-management-system/">source 2</a> | ||
*/ | ||
public enum ClassicJarDistribution { | ||
|
||
/** | ||
* Represents necessities spending: rent, food, bills, etc. | ||
*/ | ||
NECESSITIES("Necessities", 55, | ||
"Necessities spending: Rent, Food, Bills etc."), | ||
|
||
/** | ||
* Represents long-term savings spending: big purchases, vacations, rainy day fund, unexpected medical expenses. | ||
*/ | ||
LONG_TERM_SAVING("Long Term Savings", 10, | ||
"Long Term Savings spending: Big Purchases, Vacations, Rainy Day Fund, Unexpected Medical Expenses."), | ||
|
||
/** | ||
* Represents education spending: coaching, mentoring, books, courses, etc. | ||
*/ | ||
EDUCATION("Education", 10, | ||
"Education spending: Coaching, Mentoring, Books, Courses, etc."), | ||
|
||
/** | ||
* Represents play spending: spoiling yourself & your family, leisure expenses, fun, etc. | ||
*/ | ||
PLAY("Play", 10, | ||
"Play spending: Spoiling yourself & your family, Leisure expenses, Fun, etc."), | ||
|
||
/** | ||
* Represents financial spending: stocks, mutual funds, passive income vehicles, real estate investing, any other investments. | ||
*/ | ||
FINANCIAL("Financial", 10, | ||
"Financial spending: Stocks, Mutual Funds, Passive income Vehicles, Real Estate investing, Any other investments."), | ||
|
||
/** | ||
* Represents give spending: charitable, donations. | ||
*/ | ||
GIVE("Give", 5, "Give spending: Charitable, Donations."); | ||
|
||
private final String jarName; | ||
private final int percentage; | ||
private final String description; | ||
|
||
ClassicJarDistribution(String jarName, int percentage, String description) { | ||
this.jarName = jarName; | ||
this.percentage = percentage; | ||
this.description = description; | ||
} | ||
|
||
public String jarName() { | ||
return jarName; | ||
} | ||
|
||
public int percentage() { | ||
return percentage; | ||
} | ||
|
||
public String description() { | ||
return description; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.