@@ -29,6 +29,7 @@ import {
2929 NidType ,
3030 NotePasswordQueryDto ,
3131 NoteQueryDto ,
32+ SetNotePublishStatusDto ,
3233} from './note.dto'
3334import { NoteModel , PartialNoteModel } from './note.model'
3435import { NoteService } from './note.service'
@@ -68,8 +69,10 @@ export class NoteController {
6869 }
6970
7071 @Get ( ':id' )
71- @Auth ( )
72- async getOneNote ( @Param ( ) params : MongoIdDto ) {
72+ async getOneNote (
73+ @Param ( ) params : MongoIdDto ,
74+ @IsAuthenticated ( ) isAuthenticated : boolean ,
75+ ) {
7376 const { id } = params
7477
7578 const current = await this . noteService . model
@@ -82,6 +85,11 @@ export class NoteController {
8285 throw new CannotFindException ( )
8386 }
8487
88+ // 非认证用户只能查看已发布的手记
89+ if ( ! isAuthenticated && ! current . isPublished ) {
90+ throw new CannotFindException ( )
91+ }
92+
8593 return current
8694 }
8795
@@ -95,9 +103,9 @@ export class NoteController {
95103 const half = size >> 1
96104 const { id } = params
97105 const select = isAuthenticated
98- ? 'nid _id title created hide '
106+ ? 'nid _id title created isPublished '
99107 : 'nid _id title created'
100- const condition = isAuthenticated ? { } : { hide : false }
108+ const condition = isAuthenticated ? { } : { isPublished : true }
101109
102110 // 当前文档直接找,不用加条件,反正里面的东西是看不到的
103111 const currentDocument = await this . noteService . model
@@ -197,7 +205,7 @@ export class NoteController {
197205 ) {
198206 const { nid } = params
199207 const { password, single : isSingle } = query
200- const condition = isAuthenticated ? { } : { hide : false }
208+ const condition = isAuthenticated ? { } : { isPublished : true }
201209 const current : NoteModel | null = await this . noteService . model
202210 . findOne ( {
203211 nid,
@@ -278,8 +286,8 @@ export class NoteController {
278286 sortOrder,
279287 } = query
280288 const condition : FilterQuery < NoteModel > = isAuthenticated
281- ? { $or : [ { hide : false } , { hide : true } ] }
282- : { hide : false }
289+ ? { $or : [ { isPublished : false } , { isPublished : true } ] }
290+ : { isPublished : true }
283291
284292 return await this . noteService . getNotePaginationByTopicId (
285293 id ,
@@ -292,4 +300,16 @@ export class NoteController {
292300 { ...condition } ,
293301 )
294302 }
303+
304+ @Patch ( '/:id/publish' )
305+ @Auth ( )
306+ async setPublishStatus (
307+ @Param ( ) params : MongoIdDto ,
308+ @Body ( ) body : SetNotePublishStatusDto ,
309+ ) {
310+ await this . noteService . updateById ( params . id , {
311+ isPublished : body . isPublished ,
312+ } )
313+ return { success : true }
314+ }
295315}
0 commit comments