@@ -2,7 +2,24 @@ import axios from "axios";
22import { apiURL } from "../scripts/api" ;
33import { Comment } from "../models/Comment" ;
44
5+ /**
6+ * CommentService class
7+ *
8+ * @description
9+ * This class provides methods for managing comments, including publishing,
10+ * fetching, updating, and deleting comments.
11+ */
512export default class CommentService {
13+ /**
14+ * publishComment() -> Promise<{ message: string; comment: Comment } | Error>
15+ *
16+ * Publishes a comment for a specific content ID.
17+ *
18+ * @param contentId - The ID of the content to comment on.
19+ * @param commentText - The text of the comment.
20+ * @param ownerId - The ID of the user who owns the comment.
21+ * @returns A promise that resolves to a success message and the created comment or an error.
22+ */
623 static async publishComment (
724 contentId : string ,
825 commentText : string ,
@@ -32,34 +49,51 @@ export default class CommentService {
3249 }
3350 }
3451
35- static async deleteComment (
36- commentId : string
37- ) : Promise < { message : string } | Error > {
52+ /**
53+ * getPostComments() -> Promise<Comment[] | Error>
54+ *
55+ * Fetches all comments for a specific content ID.
56+ *
57+ * @param contentId - The ID of the content to fetch comments for.
58+ * @returns A promise that resolves to an array of comments or an error.
59+ */
60+ static async getPostComments ( contentId : string ) : Promise < Comment [ ] | Error > {
3861 try {
39- const response = await axios . delete ( `${ apiURL } /comment/${ commentId } ` , {
40- withCredentials : true ,
41- timeout : 5000 ,
42- } ) ;
62+ const response = await axios . get (
63+ `${ apiURL } /comment/content/${ contentId } ` ,
64+ {
65+ withCredentials : true ,
66+ timeout : 5000 ,
67+ }
68+ ) ;
4369
4470 return response . data ;
4571 } catch ( error ) {
4672 const message =
4773 axios . isAxiosError ( error ) && error . response ?. data ?. error
4874 ? error . response . data . error
49- : "Failed to delete comment " ;
75+ : "Failed to get comments " ;
5076
5177 return new Error ( message ) ;
5278 }
5379 }
5480
55- static async updateComment (
81+ /**
82+ * getComment() -> Promise<Comment | Error>
83+ *
84+ * Fetches a specific comment by its ID and the content ID it belongs to.
85+ *
86+ * @param commentId - The ID of the comment to fetch.
87+ * @param contentId - The ID of the content the comment belongs to.
88+ * @returns A promise that resolves to the comment or an error.
89+ */
90+ static async getComment (
5691 commentId : string ,
57- commentText : string
58- ) : Promise < { message : string } | Error > {
92+ contentId : string
93+ ) : Promise < Comment | Error > {
5994 try {
60- const response = await axios . put (
61- `${ apiURL } /comment/${ commentId } ` ,
62- { text : commentText } ,
95+ const response = await axios . get (
96+ `${ apiURL } /comment/${ commentId } /content/${ contentId } ` ,
6397 {
6498 withCredentials : true ,
6599 timeout : 5000 ,
@@ -71,16 +105,31 @@ export default class CommentService {
71105 const message =
72106 axios . isAxiosError ( error ) && error . response ?. data ?. error
73107 ? error . response . data . error
74- : "Failed to update comment" ;
108+ : "Failed to get comment" ;
75109
76110 return new Error ( message ) ;
77111 }
78112 }
79113
80- static async getPostComments ( contentId : string ) : Promise < Comment [ ] | Error > {
114+ /**
115+ * updateComment() -> Promise<{ message: string } | Error>
116+ *
117+ * Updates a comment by its ID and the content ID it belongs to.
118+ *
119+ * @param commentId - The ID of the comment to update.
120+ * @param commentText - The new text for the comment.
121+ * @param contentId - The ID of the content the comment belongs to.
122+ * @returns A promise that resolves to a success message or an error.
123+ */
124+ static async updateComment (
125+ commentId : string ,
126+ commentText : string ,
127+ contentId : string
128+ ) : Promise < { message : string } | Error > {
81129 try {
82- const response = await axios . get (
83- `${ apiURL } /comment/content/${ contentId } ` ,
130+ const response = await axios . put (
131+ `${ apiURL } /comment/${ commentId } /content/${ contentId } ` ,
132+ { text : commentText } ,
84133 {
85134 withCredentials : true ,
86135 timeout : 5000 ,
@@ -92,25 +141,40 @@ export default class CommentService {
92141 const message =
93142 axios . isAxiosError ( error ) && error . response ?. data ?. error
94143 ? error . response . data . error
95- : "Failed to get comments " ;
144+ : "Failed to update comment " ;
96145
97146 return new Error ( message ) ;
98147 }
99148 }
100149
101- static async getComment ( commentId : string ) : Promise < Comment | Error > {
150+ /**
151+ * deleteComment() -> Promise<{ message: string } | Error>
152+ *
153+ * Deletes a comment by its ID and the content ID it belongs to.
154+ *
155+ * @param commentId - The ID of the comment to delete.
156+ * @param contentId - The ID of the content the comment belongs to.
157+ * @returns A promise that resolves to a success message or an error.
158+ */
159+ static async deleteComment (
160+ commentId : string ,
161+ contentId : string
162+ ) : Promise < { message : string } | Error > {
102163 try {
103- const response = await axios . get ( `${ apiURL } /comment/${ commentId } ` , {
104- withCredentials : true ,
105- timeout : 5000 ,
106- } ) ;
164+ const response = await axios . delete (
165+ `${ apiURL } /comment/${ commentId } /content/${ contentId } ` ,
166+ {
167+ withCredentials : true ,
168+ timeout : 5000 ,
169+ }
170+ ) ;
107171
108172 return response . data ;
109173 } catch ( error ) {
110174 const message =
111175 axios . isAxiosError ( error ) && error . response ?. data ?. error
112176 ? error . response . data . error
113- : "Failed to get comment" ;
177+ : "Failed to delete comment" ;
114178
115179 return new Error ( message ) ;
116180 }
0 commit comments