Skip to content

Commit

Permalink
add createdBy updatedBy tags to searchresults
Browse files Browse the repository at this point in the history
reset searchinput on navigation
  • Loading branch information
thooyork committed May 16, 2023
1 parent ca21f17 commit 314d6c5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
11 changes: 10 additions & 1 deletion client/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
}
});
Expand All @@ -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 = () => {
Expand Down
22 changes: 9 additions & 13 deletions server/src/routes/posts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand All @@ -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));
Expand Down Expand Up @@ -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<TagEntity[]> {
if (!bodyJson.stringTags || bodyJson.stringTags.length <= 0) {
return Promise.resolve([]);
Expand All @@ -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);

Expand Down

0 comments on commit 314d6c5

Please sign in to comment.