@@ -29,6 +29,7 @@ import {
29
29
NidType ,
30
30
NotePasswordQueryDto ,
31
31
NoteQueryDto ,
32
+ SetNotePublishStatusDto ,
32
33
} from './note.dto'
33
34
import { NoteModel , PartialNoteModel } from './note.model'
34
35
import { NoteService } from './note.service'
@@ -68,8 +69,10 @@ export class NoteController {
68
69
}
69
70
70
71
@Get ( ':id' )
71
- @Auth ( )
72
- async getOneNote ( @Param ( ) params : MongoIdDto ) {
72
+ async getOneNote (
73
+ @Param ( ) params : MongoIdDto ,
74
+ @IsAuthenticated ( ) isAuthenticated : boolean ,
75
+ ) {
73
76
const { id } = params
74
77
75
78
const current = await this . noteService . model
@@ -82,6 +85,11 @@ export class NoteController {
82
85
throw new CannotFindException ( )
83
86
}
84
87
88
+ // 非认证用户只能查看已发布的手记
89
+ if ( ! isAuthenticated && ! current . isPublished ) {
90
+ throw new CannotFindException ( )
91
+ }
92
+
85
93
return current
86
94
}
87
95
@@ -95,9 +103,9 @@ export class NoteController {
95
103
const half = size >> 1
96
104
const { id } = params
97
105
const select = isAuthenticated
98
- ? 'nid _id title created hide '
106
+ ? 'nid _id title created isPublished '
99
107
: 'nid _id title created'
100
- const condition = isAuthenticated ? { } : { hide : false }
108
+ const condition = isAuthenticated ? { } : { isPublished : true }
101
109
102
110
// 当前文档直接找,不用加条件,反正里面的东西是看不到的
103
111
const currentDocument = await this . noteService . model
@@ -197,7 +205,7 @@ export class NoteController {
197
205
) {
198
206
const { nid } = params
199
207
const { password, single : isSingle } = query
200
- const condition = isAuthenticated ? { } : { hide : false }
208
+ const condition = isAuthenticated ? { } : { isPublished : true }
201
209
const current : NoteModel | null = await this . noteService . model
202
210
. findOne ( {
203
211
nid,
@@ -278,8 +286,8 @@ export class NoteController {
278
286
sortOrder,
279
287
} = query
280
288
const condition : FilterQuery < NoteModel > = isAuthenticated
281
- ? { $or : [ { hide : false } , { hide : true } ] }
282
- : { hide : false }
289
+ ? { $or : [ { isPublished : false } , { isPublished : true } ] }
290
+ : { isPublished : true }
283
291
284
292
return await this . noteService . getNotePaginationByTopicId (
285
293
id ,
@@ -292,4 +300,16 @@ export class NoteController {
292
300
{ ...condition } ,
293
301
)
294
302
}
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
+ }
295
315
}
0 commit comments