From 314d6c5f5e2c3129931e47e46225de8fe41e7c13 Mon Sep 17 00:00:00 2001 From: Thomas Haaf Date: Tue, 16 May 2023 16:05:38 +0200 Subject: [PATCH] add createdBy updatedBy tags to searchresults reset searchinput on navigation --- client/src/App.vue | 11 ++++++++++- server/src/routes/posts.ts | 22 +++++++++------------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/client/src/App.vue b/client/src/App.vue index 453efac..22ec721 100644 --- a/client/src/App.vue +++ b/client/src/App.vue @@ -144,6 +144,8 @@ watch(route, async (value) => { // prefill search input from queryParam if (value.query.search) { searchQuery.value = value.query.search as string; + } else { + searchQuery.value = ""; } }); @@ -158,7 +160,14 @@ onMounted(async () => { }); const startSearch = (search: string, operator: string = "and") => { - router.push(`/posts/?search=${search}&operator=${operator}`); + if (search) { + router.push(`/posts/?search=${search}&operator=${operator}`); + } else { + if (route.path === "/posts/") { + // rest btn (x) on search input + router.push(`/posts`); + } + } }; const isAdmin = () => { diff --git a/server/src/routes/posts.ts b/server/src/routes/posts.ts index 21c6707..b9c68e9 100644 --- a/server/src/routes/posts.ts +++ b/server/src/routes/posts.ts @@ -33,7 +33,6 @@ router.get("/page/:page([0-9]+)/count/:count([0-9]+)/search/:search/operator/:op .join(operator); } - // TODO : add createdBy and tags to search results await AppDataSource.manager .createQueryBuilder(PostEntity, "post") .select(["post.id as post_id", "ts_rank_cd(sp.post_tsv, to_tsquery(:searchTerm)) as rank", "count(*) over() as count"]) @@ -50,7 +49,10 @@ router.get("/page/:page([0-9]+)/count/:count([0-9]+)/search/:search/operator/:op await AppDataSource.manager .getRepository(PostEntity) - .findBy({ id: In(idArray) }) + .find({ + where: { id: In(idArray) }, + relations: ["createdBy", "updatedBy", "tags"], + }) .then((result) => res.status(200).json({ data: [result, count[0]] })); }) .catch((err) => next(err)); @@ -179,12 +181,6 @@ router.post( }, ); -function saveTags(savedPost: PostEntity, tags: string[]) { - const tagsToRemove = savedPost.tags.filter((tag) => !tags.includes(tag.name)); - const tagsToAdd = tags.filter((tag) => !savedPost.tags.some((savedTag) => savedTag.name === tag)); - return AppDataSource.createQueryBuilder().relation(PostEntity, "tags").addAndRemove(tagsToAdd, tagsToRemove); -} - async function getPersistedTagsForPost(post: PostEntity, bodyJson: PostRequestDto): Promise { if (!bodyJson.stringTags || bodyJson.stringTags.length <= 0) { return Promise.resolve([]); @@ -194,11 +190,11 @@ async function getPersistedTagsForPost(post: PostEntity, bodyJson: PostRequestDt const alreadySavedTags = bodyJson.stringTags?.length > 0 ? await AppDataSource.manager - .getRepository(TagEntity) - .createQueryBuilder("tagEntity") - .select() - .where("tagEntity.name IN(:...names)", { names: bodyJson.stringTags }) - .getMany() + .getRepository(TagEntity) + .createQueryBuilder("tagEntity") + .select() + .where("tagEntity.name IN(:...names)", { names: bodyJson.stringTags }) + .getMany() : await Promise.all([]); tagsToUseInPost.push(...alreadySavedTags);