Skip to content

Commit

Permalink
read post from public id
Browse files Browse the repository at this point in the history
  • Loading branch information
hieuhani committed Jul 29, 2024
1 parent eefbe43 commit 9f72eb7
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 32 deletions.
39 changes: 22 additions & 17 deletions apps/api/src/organization/my-organization-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import { z } from "zod";
import { slugify } from "../lib/slugify";
import { normalizeMetadata } from "../lib/object";
import { getOrganizationIdFromCache } from "./lib";
import { getPostIdFromCache } from "../post/lib";

export const myOrganizationRouter = new Hono<AppEnv>();

Expand Down Expand Up @@ -174,72 +175,76 @@ myOrganizationRouter.post(
);

myOrganizationRouter.put(
"/:organization_id/posts/:id",
"/:organization_id/posts/:post_id",
zValidator("json", updatePostSchema),
useCurrentAppUser({ required: true }),
useCheckOrganizationUser(),
async (c) => {
const payload = c.req.valid("json");
const id = c.req.param("id");
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 myPost = await getOrganizationPostById(
container,
organizationId,
+id
postId
);
const updatedPost = await updatePost(container, +myPost.id, payload);
return c.json({ data: updatedPost });
}
);

myOrganizationRouter.delete(
"/:organization_id/posts/:id",
"/:organization_id/posts/:post_id",
zValidator("json", createPostSchema),
useCurrentAppUser({ required: true }),
useCheckOrganizationUser("Administrator"),
async (c) => {
const id = c.req.param("id");
const container = c.get("container");
await deletePost(container, +id);
const postId = await getPostIdFromCache(container, c.req.param("post_id"));
await deletePost(container, postId);
return c.body(null, 204);
}
);

myOrganizationRouter.get(
"/:organization_id/posts/:id",
"/:organization_id/posts/:post_id",
useCurrentAppUser({ required: true }),
useCheckOrganizationUser(),
async (c) => {
const id = c.req.param("id");
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 post = await getOrganizationPostById(container, organizationId, +id);
const post = await getOrganizationPostById(
container,
organizationId,
postId
);
return c.json({ data: post });
}
);

myOrganizationRouter.get(
"/:organization_id/posts/:id/comments",
"/:organization_id/posts/:post_id/comments",
useCurrentAppUser({ required: true }),
useCheckOrganizationUser(),
async (c) => {
const currentUser = c.get("currentAppUser");
const id = c.req.param("id");
const container = c.get("container");
// this function guarantees that this post is from the organization
const organizationId = await getOrganizationIdFromCache(
container,
c.req.param("organization_id")
);
const postId = await getPostIdFromCache(container, c.req.param("post_id"));
const myPost = await getOrganizationPostById(
container,
currentUser.id,
+id
organizationId,
postId
);

const comments = await getPostComments(container, +myPost.id);
Expand Down
22 changes: 22 additions & 0 deletions apps/api/src/post/lib.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { LRUCache } from "lru-cache";
import { getPostId, Container } from "@publiz/core";

const cache = new LRUCache<string, number>({
max: 1000,
});

export const getPostIdFromCache = async (
container: Container,
id: string
): Promise<number> => {
const cacheKey = `postId:${id}`;
if (cache.has(cacheKey)) {
const value = cache.get(cacheKey);
if (value) {
return value;
}
}
const postId = await getPostId(container, id);
cache.set(cacheKey, postId);
return postId;
};
7 changes: 4 additions & 3 deletions apps/api/src/post/moderating-post-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import { useCurrentAppUser } from "../user";
import { z } from "zod";
import { zValidator } from "@hono/zod-validator";
import { getPostIdFromCache } from "./lib";

export const moderatingPostRouter = new Hono<AppEnv>();

Expand Down Expand Up @@ -77,17 +78,17 @@ export const createPostReaction = z.object({
});

moderatingPostRouter.post(
"/:id/reactions",
"/:post_id/reactions",
zValidator("json", createPostReaction),
useCurrentAppUser({ required: true }),
async (c) => {
const container = c.get("container");
const currentAppUser = c.get("currentAppUser");
const payload = c.req.valid("json");
await checkUserHasContentModerationPack(container, currentAppUser.id);
const id = c.req.param("id");
const postId = await getPostIdFromCache(container, c.req.param("post_id"));
const reactionPost = await createReactionPostCrudUseCase(container).create({
postId: +id,
postId,
userId: currentAppUser.id,
reactionId: payload.reactionId,
});
Expand Down
28 changes: 17 additions & 11 deletions apps/api/src/post/my-post-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
findPostsByUserId,
} from "@publiz/core";
import { createPostSchema, updatePostSchema } from "./schema";
import { getPostIdFromCache } from "./lib";

export const myPostRouter = new Hono<AppEnv>();

Expand All @@ -30,27 +31,32 @@ myPostRouter.post(
);

myPostRouter.put(
"/:id",
"/:post_id",
useCurrentAppUser({ required: true }),
zValidator("json", updatePostSchema),
async (c) => {
const currentUser = c.get("currentAppUser");
const container = c.get("container");
const payload = c.req.valid("json");
const id = c.req.param("id");
const myPost = await getMyPostById(container, currentUser.id, +id);
const updatedPost = await updatePost(container, myPost.id, payload);
const postId = await getPostIdFromCache(container, c.req.param("post_id"));
// verify that the post belongs to the current user
await getMyPostById(container, currentUser.id, postId);
const updatedPost = await updatePost(container, postId, payload);
return c.json({ data: updatedPost });
}
);

myPostRouter.get("/:id", useCurrentAppUser({ required: true }), async (c) => {
const container = c.get("container");
const id = c.req.param("id");
const currentUser = c.get("currentAppUser");
const post = await getMyPostById(container, currentUser.id, +id);
return c.json({ data: post });
});
myPostRouter.get(
"/:post_id",
useCurrentAppUser({ required: true }),
async (c) => {
const container = c.get("container");
const postId = await getPostIdFromCache(container, c.req.param("post_id"));
const currentUser = c.get("currentAppUser");
const post = await getMyPostById(container, currentUser.id, postId);
return c.json({ data: post });
}
);

myPostRouter.get("/", useCurrentAppUser({ required: true }), async (c) => {
const container = c.get("container");
Expand Down
13 changes: 12 additions & 1 deletion packages/core/src/post/usecase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
findPostTagsByPostId,
getPostsByUserId as getPostsByUserIdRepo,
getContentModerationApproveReaction,
getPostByPublicId,
} from "@publiz/sqldb";
import { customAlphabet } from "nanoid";
import { Validator } from "@cfworker/json-schema";
Expand Down Expand Up @@ -181,7 +182,6 @@ export const getPostById = async (
withOrganization?: boolean;
}
) => {
console.log(id);
const post = await getPostByIdRepo(container.sqlDb, id, context);
return makePublicEntity(post);
};
Expand Down Expand Up @@ -269,3 +269,14 @@ export const findPosts = async (
rows: paginatedPosts.rows.map(makePublicEntity),
};
};

export const getPostId = async (
container: Container,
idOrSlug: string | number
) => {
if (Number.isInteger(Number(idOrSlug))) {
return +idOrSlug;
}
const post = await getPostByPublicId(container.sqlDb, "" + idOrSlug);
return post.id;
};
12 changes: 12 additions & 0 deletions packages/sqldb/src/post/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,22 @@ import { Database, SqlDatabase } from "../database";
import { JsonValue } from "../kysely";
import { ExpressionBuilder } from "kysely";
import { executeWithCursorPagination } from "../pagination/cursor";
import { PostRow } from "./model";

export const createPostCrudRepository = (db: SqlDatabase) =>
createCrudRepository(db, "posts");

export const getPostByPublicId = (
db: SqlDatabase,
publicId: string
): Promise<PostRow> => {
return db
.selectFrom("posts")
.selectAll()
.where("publicId", "=", publicId)
.executeTakeFirstOrThrow();
};

export const getPostByIdAndUserId = async (
db: SqlDatabase,
postId: number,
Expand Down

0 comments on commit 9f72eb7

Please sign in to comment.