@@ -13,6 +13,7 @@ import { UnauthorizedError } from "../errors/UnauthorizedError.js";
13
13
import { MarkdownConverterServer } from "../markdown-converter-server.js" ;
14
14
import { authMiddleware } from "../service/middleware/auth.js" ;
15
15
import { extractJsonBody , extractUploadFiles , multipleFilesUpload } from "../service/middleware/files-upload.js" ;
16
+ import { In } from "typeorm" ;
16
17
17
18
const router : Router = express . Router ( ) ;
18
19
@@ -35,17 +36,22 @@ router.get("/page/:page([0-9]+)/count/:count([0-9]+)/search/:search/operator/:op
35
36
// TODO : add createdBy and tags to search results
36
37
await AppDataSource . manager
37
38
. createQueryBuilder ( PostEntity , "post" )
38
- . select ( [ "post.* " , "ts_rank_cd(sp.post_tsv, to_tsquery(:searchTerm)) as rank" , "count(*) over() as count" ] )
39
+ . select ( [ "post.id as post_id " , "ts_rank_cd(sp.post_tsv, to_tsquery(:searchTerm)) as rank" , "count(*) over() as count" ] )
39
40
. setParameter ( "searchTerm" , searchTerm )
40
41
. leftJoin ( "search_posts" , "sp" , "sp.post_id = id" )
41
42
. where ( "sp.post_tsv @@ to_tsquery(:searchTerm)" , { searchTerm : searchTerm } )
42
43
. orderBy ( "rank" , "DESC" )
43
44
. offset ( skipEntries )
44
45
. limit ( itemsPerPage )
45
46
. getRawMany ( )
46
- . then ( ( result ) => {
47
- const count = result ?. map ( ( r ) => r . count ) as number [ ] ;
48
- return res . status ( 200 ) . json ( { data : [ result , count [ 0 ] ] } ) ;
47
+ . then ( async ( result ) => {
48
+ const idArray = result ?. map ( ( r ) => parseInt ( r . post_id ) ) as number [ ] ;
49
+ const count = result ?. map ( ( r ) => parseInt ( r . count ) ) as number [ ] ;
50
+
51
+ await AppDataSource . manager
52
+ . getRepository ( PostEntity )
53
+ . findBy ( { id : In ( idArray ) } )
54
+ . then ( ( result ) => res . status ( 200 ) . json ( { data : [ result , count [ 0 ] ] } ) ) ;
49
55
} )
50
56
. catch ( ( err ) => next ( err ) ) ;
51
57
} ) ;
@@ -249,10 +255,10 @@ router.post("/:id(\\d+$)", authMiddleware, multipleFilesUpload, async (req: Requ
249
255
} else if ( ! permissionsForUser ( account . user ) . canEditPost && ( account . user . id === null || account . user . id !== post . createdBy ?. id ) ) {
250
256
return next ( new ForbiddenError ( ) ) ;
251
257
}
252
- /*
253
- const tagsToUseInPost: TagEntity[] = await getPersistedTagsForPost(body).catch((err) => {
258
+
259
+ const tagsToUseInPost : TagEntity [ ] = await getPersistedTagsForPost ( post , body ) . catch ( ( err ) => {
254
260
throw new InternalServerError ( true , "Error getting tags" + err ) ;
255
- });*/
261
+ } ) ;
256
262
257
263
AppDataSource . manager
258
264
. transaction ( async ( manager ) => {
@@ -282,7 +288,7 @@ router.post("/:id(\\d+$)", authMiddleware, multipleFilesUpload, async (req: Requ
282
288
. getRepository ( PostEntity )
283
289
. findOneByOrFail ( { id : postId } )
284
290
. then ( ( post ) => {
285
- post . tags = [ ] ;
291
+ post . tags = tagsToUseInPost ;
286
292
return post ;
287
293
} )
288
294
. then ( ( updatedPost ) => manager . getRepository ( PostEntity ) . save ( updatedPost ) )
0 commit comments