@@ -26,6 +26,8 @@ import { BlogpostMapper } from "./blogpost.mapper";
26
26
import { WithPagination } from "../../utils/decorator/pagination" ;
27
27
import { NullableIntPipe } from "../../utils/pipes" ;
28
28
import { makePage } from "../../gateway/util/make-page" ;
29
+ import { ForumApi } from "../../generated-api/forum" ;
30
+ import { ThreadType } from "../../gateway/shared-types/thread-type" ;
29
31
30
32
@Controller ( "blog" )
31
33
@ApiTags ( "blog" )
@@ -34,6 +36,7 @@ export class BlogpostController {
34
36
@InjectRepository ( BlogpostEntity )
35
37
private readonly blogpostEntityRepository : Repository < BlogpostEntity > ,
36
38
private readonly mapper : BlogpostMapper ,
39
+ private readonly forumApi : ForumApi ,
37
40
) { }
38
41
39
42
@ModeratorGuard ( )
@@ -45,16 +48,20 @@ export class BlogpostController {
45
48
) : Promise < BlogpostDto > {
46
49
let blogpost : BlogpostEntity ;
47
50
if ( dto . id ) {
48
- blogpost = await this . blogpostEntityRepository . findOne ( {
51
+ blogpost = await this . blogpostEntityRepository . findOneOrFail ( {
49
52
where : { id : dto . id } ,
50
53
} ) ;
51
54
} else {
52
55
blogpost = new BlogpostEntity ( ) ;
53
56
blogpost . author = user . steam_id ;
57
+ blogpost . imageKey = `upload/dotaold.jpg` ; // Very bad hack but wcyd
54
58
}
55
59
blogpost . content = dto . content ;
56
- blogpost . imageKey = dto . imageKey ;
60
+ if ( dto . imageKey ) {
61
+ blogpost . imageKey = dto . imageKey ;
62
+ }
57
63
blogpost . title = dto . title ;
64
+ blogpost . shortDescription = dto . shortDescription ;
58
65
59
66
return this . blogpostEntityRepository
60
67
. save ( blogpost )
@@ -68,10 +75,19 @@ export class BlogpostController {
68
75
@CurrentUser ( ) user : CurrentUserDto ,
69
76
@Param ( "id" , ParseIntPipe ) id : number ,
70
77
) : Promise < BlogpostDto > {
71
- const blog = await this . blogpostEntityRepository . findOne ( { where : { id } } ) ;
78
+ const blog = await this . blogpostEntityRepository . findOneOrFail ( {
79
+ where : { id } ,
80
+ } ) ;
81
+
82
+ if ( blog . published ) return this . mapper . mapPost ( blog ) ;
72
83
73
84
blog . published = true ;
74
85
blog . publishedAt = new Date ( ) ;
86
+ await this . forumApi . forumControllerGetThreadForKey ( {
87
+ threadType : ThreadType . BLOGPOST ,
88
+ externalId : blog . id . toString ( ) ,
89
+ title : blog . title ,
90
+ } ) ;
75
91
return this . blogpostEntityRepository . save ( blog ) . then ( this . mapper . mapPost ) ;
76
92
}
77
93
@@ -80,7 +96,7 @@ export class BlogpostController {
80
96
@Param ( "id" , ParseIntPipe ) id : number ,
81
97
) : Promise < BlogpostDto > {
82
98
return this . blogpostEntityRepository
83
- . findOne ( {
99
+ . findOneOrFail ( {
84
100
where : { id, published : true } ,
85
101
} )
86
102
. then ( this . mapper . mapPost ) ;
@@ -94,7 +110,7 @@ export class BlogpostController {
94
110
) : Promise < BlogpostDto > {
95
111
return this . blogpostEntityRepository
96
112
. findOne ( {
97
- where : { id, published : false } ,
113
+ where : { id } ,
98
114
} )
99
115
. then ( this . mapper . mapPost ) ;
100
116
}
0 commit comments