Add solution for exercise 3

This commit is contained in:
Alexander Ungar 2023-10-10 20:43:36 +02:00
parent ebddbbcb5a
commit e553b097f6
2 changed files with 10 additions and 3 deletions

View file

@ -1,7 +1,7 @@
import { type BudgetId, type BudgetSummary } from "../../domain/budget";
interface BudgetSummaryRepository {
// TODO 2.2 - add a function "insert" for inserting a new BudgetSummary
insert: (summary: BudgetSummary) => Promise<BudgetSummary>;
find: (budgetId: BudgetId) => Promise<BudgetSummary | undefined>;
}

View file

@ -3,7 +3,14 @@ import { BudgetSummaryEntityConverter } from "../entity/converters";
import { type BudgetId } from "../../../domain/budget";
import { BudgetSummaryModel } from "./models";
// TODO 2.3 - implement the new "insertBudgetSummary" function
export const insertBudgetSummary: BudgetSummaryRepository["insert"] = async (
summary,
) => {
const summaryEntity = BudgetSummaryEntityConverter.toEntity(summary);
const summaryModel = new BudgetSummaryModel(summaryEntity);
const updatedSummary = await summaryModel.save();
return BudgetSummaryEntityConverter.toDomain(updatedSummary);
};
export const findBudgetSummary: BudgetSummaryRepository["find"] = async (
budgetId: BudgetId,
@ -15,7 +22,7 @@ export const findBudgetSummary: BudgetSummaryRepository["find"] = async (
};
const BudgetSummaryMongoRepository = (): BudgetSummaryRepository => ({
// TODO 2.4. - add the "insertBudgetSummary" function to the repository
insert: insertBudgetSummary,
find: findBudgetSummary,
});