mirror of
https://github.com/netlight/my-finance-pal-backend.git
synced 2024-11-10 08:57:45 +01:00
Add missing DELETE endpoint for Budgets
This commit is contained in:
parent
496104c9dc
commit
2b04906dd7
6 changed files with 37 additions and 1 deletions
|
@ -1,7 +1,8 @@
|
||||||
import { type Budget } from "../../domain/budget";
|
import { type Budget, type BudgetId } from "../../domain/budget";
|
||||||
|
|
||||||
interface BudgetRepository {
|
interface BudgetRepository {
|
||||||
findAll: () => Promise<Budget[]>;
|
findAll: () => Promise<Budget[]>;
|
||||||
|
delete: (id: BudgetId) => Promise<{ deleted: boolean }>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default BudgetRepository;
|
export default BudgetRepository;
|
||||||
|
|
|
@ -7,8 +7,14 @@ export const findBudgets: BudgetRepository["findAll"] = async () => {
|
||||||
return found.map(BudgetEntityConverter.toDomain);
|
return found.map(BudgetEntityConverter.toDomain);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const deleteBudget: BudgetRepository["delete"] = async (budgetId) => {
|
||||||
|
const deleted = await BudgetSummaryModel.deleteOne({ id: budgetId.value });
|
||||||
|
return { deleted: deleted.deletedCount > 0 };
|
||||||
|
};
|
||||||
|
|
||||||
const BudgetMongoRepository = (): BudgetRepository => ({
|
const BudgetMongoRepository = (): BudgetRepository => ({
|
||||||
findAll: findBudgets,
|
findAll: findBudgets,
|
||||||
|
delete: deleteBudget,
|
||||||
});
|
});
|
||||||
|
|
||||||
export default BudgetMongoRepository;
|
export default BudgetMongoRepository;
|
||||||
|
|
|
@ -45,6 +45,16 @@ export const getBudgetSummary =
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const deleteBudget =
|
||||||
|
(deleteBudget: BudgetUseCases["deleteBudget"]) =>
|
||||||
|
async (req: Request, res: Response): Promise<void> => {
|
||||||
|
const budgetId = new UUID(req.params.budgetId);
|
||||||
|
const deleteResult = await deleteBudget(budgetId);
|
||||||
|
res.status(
|
||||||
|
deleteResult.deleted ? StatusCodes.NO_CONTENT : StatusCodes.NOT_FOUND
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
const BudgetRouter = (budgetUseCases: BudgetUseCases): Router => {
|
const BudgetRouter = (budgetUseCases: BudgetUseCases): Router => {
|
||||||
const router = Router();
|
const router = Router();
|
||||||
router.post(
|
router.post(
|
||||||
|
@ -59,6 +69,10 @@ const BudgetRouter = (budgetUseCases: BudgetUseCases): Router => {
|
||||||
toExpressPath(apiPaths.getBudgetSummary),
|
toExpressPath(apiPaths.getBudgetSummary),
|
||||||
asyncHandler(getBudgetSummary(budgetUseCases.getBudgetSummary))
|
asyncHandler(getBudgetSummary(budgetUseCases.getBudgetSummary))
|
||||||
);
|
);
|
||||||
|
router.delete(
|
||||||
|
toExpressPath(apiPaths.deleteBudget),
|
||||||
|
asyncHandler(deleteBudget(budgetUseCases.deleteBudget))
|
||||||
|
);
|
||||||
|
|
||||||
return router;
|
return router;
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,3 +1,8 @@
|
||||||
|
/**
|
||||||
|
* Reformats an OpenAPI defined path to a by Express readable path
|
||||||
|
* I.e. rewrites path param notation from OpenAPI format to Express format
|
||||||
|
* @param openApiPath The in the OpenAPI spec defined path
|
||||||
|
*/
|
||||||
const toExpressPath = (openApiPath: string): string =>
|
const toExpressPath = (openApiPath: string): string =>
|
||||||
openApiPath.replaceAll(/({)(.*)(})/g, ":$2");
|
openApiPath.replaceAll(/({)(.*)(})/g, ":$2");
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@ import type BudgetSummaryRepository from "../../repository/budget/budgetSummaryR
|
||||||
import type BudgetUseCases from "./budgetUseCases";
|
import type BudgetUseCases from "./budgetUseCases";
|
||||||
import UUID from "../../domain/uuid";
|
import UUID from "../../domain/uuid";
|
||||||
import type BudgetRepository from "../../repository/budget/budgetRepository";
|
import type BudgetRepository from "../../repository/budget/budgetRepository";
|
||||||
|
import budgetRepository from "../../repository/budget/budgetRepository";
|
||||||
|
|
||||||
export const createBudget: (
|
export const createBudget: (
|
||||||
insertBudget: BudgetSummaryRepository["insert"]
|
insertBudget: BudgetSummaryRepository["insert"]
|
||||||
|
@ -28,6 +29,13 @@ export const getBudgets: (
|
||||||
return await findAll();
|
return await findAll();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const deleteBudget: (
|
||||||
|
deleteFromPersistence: BudgetRepository["delete"]
|
||||||
|
) => BudgetUseCases["deleteBudget"] =
|
||||||
|
(deleteFromPersistence) => async (budgetId) => {
|
||||||
|
return await deleteFromPersistence(budgetId);
|
||||||
|
};
|
||||||
|
|
||||||
const BudgetService: (
|
const BudgetService: (
|
||||||
budgetSummaryRepo: BudgetSummaryRepository,
|
budgetSummaryRepo: BudgetSummaryRepository,
|
||||||
budgetRepo: BudgetRepository
|
budgetRepo: BudgetRepository
|
||||||
|
@ -35,6 +43,7 @@ const BudgetService: (
|
||||||
createBudget: createBudget(budgetSummaryRepo.insert),
|
createBudget: createBudget(budgetSummaryRepo.insert),
|
||||||
getBudgetSummary: getBudgetSummary(budgetSummaryRepo.find),
|
getBudgetSummary: getBudgetSummary(budgetSummaryRepo.find),
|
||||||
getBudgets: getBudgets(budgetRepo.findAll),
|
getBudgets: getBudgets(budgetRepo.findAll),
|
||||||
|
deleteBudget: deleteBudget(budgetRepo.delete),
|
||||||
});
|
});
|
||||||
|
|
||||||
export default BudgetService;
|
export default BudgetService;
|
||||||
|
|
|
@ -9,6 +9,7 @@ interface BudgetUseCases {
|
||||||
createBudget: (newBudget: NewBudget) => Promise<Budget>;
|
createBudget: (newBudget: NewBudget) => Promise<Budget>;
|
||||||
getBudgetSummary: (budgetId: BudgetId) => Promise<BudgetSummary | undefined>;
|
getBudgetSummary: (budgetId: BudgetId) => Promise<BudgetSummary | undefined>;
|
||||||
getBudgets: () => Promise<Budget[]>;
|
getBudgets: () => Promise<Budget[]>;
|
||||||
|
deleteBudget: (budgetId: BudgetId) => Promise<{ deleted: boolean }>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default BudgetUseCases;
|
export default BudgetUseCases;
|
||||||
|
|
Loading…
Reference in a new issue