@@ -17,6 +17,8 @@ import {
17
17
PostsTags ,
18
18
PostsCategories ,
19
19
Category ,
20
+ Series ,
21
+ SeriesPosts ,
20
22
} from 'database/models' ;
21
23
import redisClient from 'lib/redisClient' ;
22
24
import UrlSlugHistory from 'database/models/UrlSlugHistory' ;
@@ -72,6 +74,7 @@ export const updatePost = async (ctx: Context): Promise<*> => {
72
74
is_temp : boolean ,
73
75
meta : any ,
74
76
is_private : boolean ,
77
+ series_id : ?string ,
75
78
} ;
76
79
77
80
const schema = Joi . object ( ) . keys ( {
@@ -92,6 +95,7 @@ export const updatePost = async (ctx: Context): Promise<*> => {
92
95
. max ( 130 ) ,
93
96
meta : Joi . object ( ) ,
94
97
is_private : Joi . boolean ( ) ,
98
+ series_id : Joi . string ( ) . allow ( null ) ,
95
99
} ) ;
96
100
97
101
if ( ! validateSchema ( ctx , schema ) ) {
@@ -108,6 +112,7 @@ export const updatePost = async (ctx: Context): Promise<*> => {
108
112
is_temp : isTemp ,
109
113
meta,
110
114
is_private,
115
+ series_id,
111
116
} : BodySchema = ( ctx . request . body : any ) ;
112
117
113
118
const stringsToCheck = [ title , body , ...tags ] ;
@@ -180,8 +185,8 @@ export const updatePost = async (ctx: Context): Promise<*> => {
180
185
thumbnail ,
181
186
is_temp : isTemp ,
182
187
meta ,
183
- is_private : ( is_private || false ) ,
184
- released_at : ( ! isTemp && ctx . post . is_temp ) ? new Date ( ) : undefined ,
188
+ is_private : is_private || false ,
189
+ released_at : ! isTemp && ctx . post . is_temp ? new Date ( ) : undefined ,
185
190
} ;
186
191
187
192
Object . keys ( updateQuery ) . forEach ( ( key ) => {
@@ -190,6 +195,48 @@ export const updatePost = async (ctx: Context): Promise<*> => {
190
195
}
191
196
} ) ;
192
197
198
+ // Update Series
199
+ let series = null ;
200
+ try {
201
+ const seriesPost = await SeriesPosts . findOne ( {
202
+ where : { fk_post_id : id } ,
203
+ include : [ Series ] ,
204
+ } ) ;
205
+ // Check Series Validity
206
+ if ( series_id ) {
207
+ const nextSeries = await Series . findById ( series_id ) ;
208
+ if ( ! nextSeries ) {
209
+ ctx . status = 404 ;
210
+ ctx . body = {
211
+ name : 'INVALID_SERIES' ,
212
+ } ;
213
+ }
214
+ if ( nextSeries . fk_user_id !== ctx . user . id ) {
215
+ ctx . status = 403 ;
216
+ return ;
217
+ }
218
+ series = {
219
+ id : nextSeries . id ,
220
+ name : nextSeries . name ,
221
+ } ;
222
+ }
223
+
224
+ if ( seriesPost ) {
225
+ if ( seriesPost . series . id !== series_id ) {
226
+ seriesPost . destroy ( ) ;
227
+ if ( ! series_id ) {
228
+ series = null ;
229
+ } else {
230
+ await SeriesPosts . append ( series_id , id , ctx . user . id ) ;
231
+ }
232
+ }
233
+ } else {
234
+ await SeriesPosts . append ( series_id , id , ctx . user . id ) ;
235
+ }
236
+ } catch ( e ) {
237
+ ctx . throw ( 500 , e ) ;
238
+ }
239
+
193
240
// Update Tags
194
241
if ( tags ) {
195
242
// Check which tags to remove or add
@@ -254,7 +301,7 @@ export const updatePost = async (ctx: Context): Promise<*> => {
254
301
await ctx . post . update ( updateQuery ) ;
255
302
const post = await Post . readPostById ( id ) ;
256
303
const serialized = serializePost ( post ) ;
257
- ctx . body = serialized ;
304
+ ctx . body = { ... serialized , series } ;
258
305
} catch ( e ) {
259
306
ctx . throw ( 500 , e ) ;
260
307
}
@@ -264,9 +311,26 @@ export const updatePost = async (ctx: Context): Promise<*> => {
264
311
export const readPost = async ( ctx : Context ) : Promise < * > => {
265
312
const { id } = ctx . params ;
266
313
try {
267
- const post = await Post . readPostById ( id ) ;
314
+ const [ post , seriesPost ] = await Promise . all ( [
315
+ Post . readPostById ( id ) ,
316
+ SeriesPosts . findOne ( {
317
+ include : [ Series ] ,
318
+ where : {
319
+ fk_post_id : id ,
320
+ } ,
321
+ } ) ,
322
+ ] ) ;
323
+
268
324
const serialized = serializePost ( post ) ;
269
- ctx . body = serialized ;
325
+ ctx . body = {
326
+ ...serialized ,
327
+ series : seriesPost
328
+ ? {
329
+ id : seriesPost . series . id ,
330
+ name : seriesPost . series . name ,
331
+ }
332
+ : null ,
333
+ } ;
270
334
} catch ( e ) {
271
335
ctx . throw ( 500 , e ) ;
272
336
}
0 commit comments