Skip to content

Commit

Permalink
api get organization post files
Browse files Browse the repository at this point in the history
  • Loading branch information
hieuhani committed Aug 2, 2024
1 parent 7b454d7 commit 2e9bed9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
28 changes: 27 additions & 1 deletion apps/api/src/organization/my-organization-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
updatePost,
findOrganizationRoles,
findOrganizationMetaSchemas,
getPostComments,
uploadFile,
getGcsImageServingUrl,
createFile,
Expand All @@ -33,6 +32,7 @@ import {
findOrganizationAvailableTags,
createComment,
findCommentsByTargetAndTargetId,
findByModelNameAndModelId,
} from "@publiz/core";
import { useCurrentAppUser } from "../user";
import { useCheckOrganizationUser } from "./middleware";
Expand Down Expand Up @@ -657,3 +657,29 @@ myOrganizationRouter.get(
return c.json({ data: comments });
}
);

myOrganizationRouter.get(
"/:organization_id/posts/:post_id/files",
useCurrentAppUser({ required: true }),
useCheckOrganizationUser(),
async (c) => {
const container = c.get("container");
const postId = await getPostIdFromCache(container, c.req.param("post_id"));
const organizationId = await getOrganizationIdFromCache(
container,
c.req.param("organization_id")
);

const _verifyPostBelongToOrganization = await getOrganizationPostById(
container,
organizationId,
postId
);
const files = await findByModelNameAndModelId(
container,
"post",
"" + postId
);
return c.json({ data: files });
}
);
9 changes: 9 additions & 0 deletions packages/core/src/file/usecase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
InsertableFileRow,
createFileCrudRepository,
deleteByModelNameAndModelId,
findByModelNameAndModelId as findByModelNameAndModelIdRepo,
} from "@publiz/sqldb";
import { Container } from "../container";
import { FileModel } from "./model";
Expand Down Expand Up @@ -45,6 +46,14 @@ export const getFileById = async (
);
};

export const findByModelNameAndModelId = async (
container: Container,
modelName: string,
modelId: string
): Promise<FileModel[]> => {
return findByModelNameAndModelIdRepo(container.sqlDb, modelName, modelId);
};

const withFileUrl = async (container: Container, file: FileModel) => {
const fileUrl = await getFileUrl(container, file);
return { ...file, fileUrl };
Expand Down
1 change: 1 addition & 0 deletions packages/sqldb/src/post/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const getPostsByUserId = async (db: SqlDatabase, authorId: number) => {
.selectFrom("posts")
.selectAll()
.where("authorId", "=", authorId)
.orderBy("createdAt", "desc")
.execute();
};

Expand Down

0 comments on commit 2e9bed9

Please sign in to comment.