Skip to content

Commit

Permalink
get comments paginated controller
Browse files Browse the repository at this point in the history
  • Loading branch information
huytran17 committed Aug 29, 2024
1 parent 257835e commit d67f8d0
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Request } from "express";
import { get } from "lodash";
import { HttpStatusCode } from "../../../../constants/http-status-code";
import {
GetCommentsPaginated,
IGetCommentsPaginated,
} from "../../../../use-cases/comment/get-comments-paginated";

export default function makeGetCommentsPaginatedController({
getCommentsPaginated,
}: {
getCommentsPaginated: GetCommentsPaginated;
}) {
return async function getCommentsPaginatedController(
httpRequest: Request & { context: { validated: {} } }
) {
const headers = {
"Content-Type": "application/json",
};

try {
const { query, page, entries_per_page } = <IGetCommentsPaginated>(
get(httpRequest, "context.validated", {})
);

const comments = await getCommentsPaginated({
query,
page: Number(page),
entries_per_page: Number(entries_per_page),
});

return {
headers,
statusCode: HttpStatusCode.OK,
body: {
data: comments,
},
};
} catch (error) {
throw {
headers,
statusCode: HttpStatusCode.INTERNAL_SERVER_ERROR,
body: {
data: error.message,
},
};
}
};
}
13 changes: 12 additions & 1 deletion core/server/src/data-access/controllers/admin/comment/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import {
getComment,
getComments,
getCommentsPaginated,
hardDeleteComment,
} from "../../../../use-cases/comment";
import makeGetCommentsController from "./get-comments";
import makeGetCommentsPaginatedController from "./get-comments-paginated";
import makeHardDeleteCommentController from "./hard-delete-comment";

const getCommentsPaginatedController = makeGetCommentsPaginatedController({
getCommentsPaginated,
});

const getCommentsController = makeGetCommentsController({
getComments,
});
Expand All @@ -18,6 +24,11 @@ const hardDeleteCommentController = makeHardDeleteCommentController({
export default Object.freeze({
hardDeleteCommentController,
getCommentsController,
getCommentsPaginatedController,
});

export { getCommentsController, hardDeleteCommentController };
export {
getCommentsController,
getCommentsPaginatedController,
hardDeleteCommentController,
};

0 comments on commit d67f8d0

Please sign in to comment.