@@ -5,7 +5,6 @@ import { EntityManager } from '@mikro-orm/sqlite'
55import { CommentSchema } from '#entities/comment'
66import { AuthError } from '#repositories/user_repository'
77import { ArticleRepository } from '#repositories/article_repository'
8- import { getUserFromCtx } from '#utils/auth'
98import { type IArticle } from '#entities/article'
109
1110function verifyArticlePermissions ( user : { id : number } , article : IArticle ) {
@@ -38,9 +37,9 @@ export default class ArticlesController {
3837 } )
3938 }
4039
41- async store ( ctx : HttpContext ) {
42- const author = getUserFromCtx ( ctx )
43- const { title, description, text } = ctx . request . body ( )
40+ async store ( { auth , request } : HttpContext ) {
41+ const author = auth . getUserOrFail ( )
42+ const { title, description, text } = request . body ( )
4443
4544 const article = this . articleRepo . create ( {
4645 title, description, text,
@@ -52,11 +51,11 @@ export default class ArticlesController {
5251 return article
5352 }
5453
55- async update ( ctx : HttpContext ) {
56- const user = getUserFromCtx ( ctx )
57- const article = await this . articleRepo . findOneOrFail ( + ctx . params . id )
54+ async update ( { auth , params , request } : HttpContext ) {
55+ const user = auth . getUserOrFail ( )
56+ const article = await this . articleRepo . findOneOrFail ( + params . id )
5857 verifyArticlePermissions ( user , article )
59- const body = ctx . request . body ( )
58+ const body = request . body ( )
6059 const data : Record < string , unknown > = { }
6160
6261 for ( const key of [ 'title' , 'description' , 'text' ] as const ) {
@@ -71,19 +70,19 @@ export default class ArticlesController {
7170 return article
7271 }
7372
74- async destroy ( ctx : HttpContext ) {
75- const user = getUserFromCtx ( ctx )
76- const article = await this . articleRepo . findOneOrFail ( + ctx . params . id )
73+ async destroy ( { auth , params } : HttpContext ) {
74+ const user = auth . getUserOrFail ( )
75+ const article = await this . articleRepo . findOneOrFail ( + params . id )
7776 verifyArticlePermissions ( user , article )
7877 await this . em . remove ( article ) . flush ( )
7978
8079 return { success : true }
8180 }
8281
83- async addComment ( ctx : HttpContext ) {
84- const author = getUserFromCtx ( ctx )
85- const article = await this . articleRepo . findOneOrFail ( { slug : ctx . params . slug } )
86- const { text } = ctx . request . body ( )
82+ async addComment ( { auth , params , request } : HttpContext ) {
83+ const author = auth . getUserOrFail ( )
84+ const article = await this . articleRepo . findOneOrFail ( { slug : params . slug } )
85+ const { text } = request . body ( )
8786 const comment = this . em . create ( CommentSchema , { author, article, text } )
8887 await this . em . flush ( )
8988
0 commit comments