Skip to content

Commit

Permalink
Merge remote-tracking branch 'github/PR/64' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
floscher committed May 9, 2023
2 parents 3047454 + 5a3b618 commit a47d21a
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions server/src/routes/posts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -35,17 +36,22 @@ 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 })
.orderBy("rank", "DESC")
.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));
});
Expand Down Expand Up @@ -249,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) => {
Expand Down Expand Up @@ -282,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))
Expand Down

0 comments on commit a47d21a

Please sign in to comment.