From 4ce8f7ebff967265ac88098c7d48218771af40ad Mon Sep 17 00:00:00 2001 From: Christophe Bach Date: Mon, 8 May 2023 11:36:44 +0200 Subject: [PATCH 1/2] fix search results --- server/src/routes/posts.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/server/src/routes/posts.ts b/server/src/routes/posts.ts index 95546a2..c0b6025 100644 --- a/server/src/routes/posts.ts +++ b/server/src/routes/posts.ts @@ -13,6 +13,7 @@ import { UnauthorizedError } from "../errors/UnauthorizedError.js"; import { MarkdownConverterServer } from "../markdown-converter-server.js"; import { authMiddleware } from "../service/middleware/auth.js"; import { extractJsonBody, extractUploadFiles, multipleFilesUpload } from "../service/middleware/files-upload.js"; +import { In } from "typeorm"; const router: Router = express.Router(); @@ -35,7 +36,7 @@ router.get("/page/:page([0-9]+)/count/:count([0-9]+)/search/:search/operator/:op // TODO : add createdBy and tags to search results await AppDataSource.manager .createQueryBuilder(PostEntity, "post") - .select(["post.*", "ts_rank_cd(sp.post_tsv, to_tsquery(:searchTerm)) as rank", "count(*) over() as count"]) + .select(["post.id as post_id", "ts_rank_cd(sp.post_tsv, to_tsquery(:searchTerm)) as rank", "count(*) over() as count"]) .setParameter("searchTerm", searchTerm) .leftJoin("search_posts", "sp", "sp.post_id = id") .where("sp.post_tsv @@ to_tsquery(:searchTerm)", { searchTerm: searchTerm }) @@ -43,9 +44,14 @@ router.get("/page/:page([0-9]+)/count/:count([0-9]+)/search/:search/operator/:op .offset(skipEntries) .limit(itemsPerPage) .getRawMany() - .then((result) => { - const count = result?.map((r) => r.count) as number[]; - return res.status(200).json({ data: [result, count[0]] }); + .then(async (result) => { + const idArray = result?.map((r) => parseInt(r.post_id)) as number[]; + const count = result?.map((r) => parseInt(r.count)) as number[]; + + await AppDataSource.manager + .getRepository(PostEntity) + .findBy({ id: In(idArray) }) + .then((result) => res.status(200).json({ data: [result, count[0]] })); }) .catch((err) => next(err)); }); From 5a3b6183be4754353059d61c402fb93093c824b5 Mon Sep 17 00:00:00 2001 From: Christophe Bach Date: Mon, 8 May 2023 16:30:23 +0200 Subject: [PATCH 2/2] fix tags in edit mode --- server/src/routes/posts.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/server/src/routes/posts.ts b/server/src/routes/posts.ts index c0b6025..e603b52 100644 --- a/server/src/routes/posts.ts +++ b/server/src/routes/posts.ts @@ -255,10 +255,10 @@ router.post("/:id(\\d+$)", authMiddleware, multipleFilesUpload, async (req: Requ } else if (!permissionsForUser(account.user).canEditPost && (account.user.id === null || account.user.id !== post.createdBy?.id)) { return next(new ForbiddenError()); } - /* - const tagsToUseInPost: TagEntity[] = await getPersistedTagsForPost(body).catch((err) => { + + const tagsToUseInPost: TagEntity[] = await getPersistedTagsForPost(post, body).catch((err) => { throw new InternalServerError(true, "Error getting tags" + err); - });*/ + }); AppDataSource.manager .transaction(async (manager) => { @@ -288,7 +288,7 @@ router.post("/:id(\\d+$)", authMiddleware, multipleFilesUpload, async (req: Requ .getRepository(PostEntity) .findOneByOrFail({ id: postId }) .then((post) => { - post.tags = []; + post.tags = tagsToUseInPost; return post; }) .then((updatedPost) => manager.getRepository(PostEntity).save(updatedPost))