@@ -8,7 +8,13 @@ import {
88 ReturnedArticleResponseSchema ,
99 UpdateArticleSchema ,
1010} from '@articles/articles.schema' ;
11- import { Elysia } from 'elysia' ;
11+ import { Elysia , t } from 'elysia' ;
12+ import {
13+ AddCommentSchema ,
14+ DeleteCommentResponse ,
15+ ReturnedCommentResponse ,
16+ ReturnedCommentsResponse ,
17+ } from './comments/comments.schema' ;
1218
1319export const articlesPlugin = new Elysia ( ) . use ( setupArticles ) . group (
1420 '/articles' ,
@@ -67,14 +73,19 @@ export const articlesPlugin = new Elysia().use(setupArticles).group(
6773 query : ArticleFeedQuerySchema ,
6874 response : ReturnedArticleListSchema ,
6975 detail : {
70- summary : 'Artifle Feed' ,
76+ summary : 'Article Feed' ,
7177 } ,
7278 } ,
7379 )
7480 . get (
7581 '/:slug' ,
76- async ( { params, store } ) =>
77- store . articlesService . findBySlug ( params . slug ) ,
82+ async ( { params, store, request } ) =>
83+ store . articlesService . findBySlug (
84+ params . slug ,
85+ await store . authService . getOptionalUserIdFromHeader (
86+ request . headers ,
87+ ) ,
88+ ) ,
7889 {
7990 response : ReturnedArticleResponseSchema ,
8091 detail : {
@@ -113,5 +124,72 @@ export const articlesPlugin = new Elysia().use(setupArticles).group(
113124 summary : 'Delete Article' ,
114125 } ,
115126 } ,
127+ )
128+ . post (
129+ '/:slug/comments' ,
130+ async ( { body, params, store, request } ) => {
131+ const comment = await store . commentsService . createComment (
132+ params . slug ,
133+ body . comment ,
134+ await store . authService . getUserIdFromHeader ( request . headers ) ,
135+ ) ;
136+ return { comment } ;
137+ } ,
138+ {
139+ beforeHandle : app . store . authService . requireLogin ,
140+ params : t . Object ( {
141+ slug : t . String ( ) ,
142+ } ) ,
143+ body : AddCommentSchema ,
144+ response : ReturnedCommentResponse ,
145+ detail : {
146+ summary : 'Add Comment to Article' ,
147+ } ,
148+ } ,
149+ )
150+ . get (
151+ '/:slug/comments' ,
152+ async ( { params, store, request } ) => {
153+ const userId = await store . authService . getOptionalUserIdFromHeader (
154+ request . headers ,
155+ ) ;
156+ return {
157+ comments : await store . commentsService . getComments (
158+ params . slug ,
159+ userId === null ? undefined : userId ,
160+ ) ,
161+ } ;
162+ } ,
163+ {
164+ params : t . Object ( {
165+ slug : t . String ( ) ,
166+ } ) ,
167+ response : ReturnedCommentsResponse ,
168+ detail : {
169+ summary : 'Get Comments from Article' ,
170+ } ,
171+ } ,
172+ )
173+ . delete (
174+ '/:slug/comments/:id' ,
175+ async ( { params, store, request } ) => {
176+ await store . commentsService . deleteComment (
177+ params . slug ,
178+ Number . parseInt ( params . id , 10 ) ,
179+ await store . authService . getUserIdFromHeader ( request . headers ) ,
180+ ) ;
181+ return { } ;
182+ } ,
183+ {
184+ beforeHandle : app . store . authService . requireLogin ,
185+ params : t . Object ( {
186+ slug : t . String ( ) ,
187+ id : t . String ( ) ,
188+ } ) ,
189+ response : DeleteCommentResponse ,
190+ detail : {
191+ summary : 'Delete Comment' ,
192+ } ,
193+ } ,
116194 ) ,
117195) ;
0 commit comments