mirror of
https://gitlab.com/comunic/comunicapiv2
synced 2024-11-26 07:19:23 +00:00
Get & return the comments of a post
This commit is contained in:
parent
5a45096fa6
commit
2924b205fd
48
src/controllers/CommentsController.ts
Normal file
48
src/controllers/CommentsController.ts
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
import { RequestHandler } from "../entities/RequestHandler";
|
||||||
|
import { Comment } from "../entities/Comment";
|
||||||
|
import { LikesHelper, LikesType } from "../helpers/LikesHelper";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Comments controller
|
||||||
|
*
|
||||||
|
* @author Pierre HUBERT
|
||||||
|
*/
|
||||||
|
|
||||||
|
export class CommentsController {
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Turn a list of comment object into API entries
|
||||||
|
*
|
||||||
|
* @param h Request handler
|
||||||
|
* @param l List of comments
|
||||||
|
*/
|
||||||
|
public static async CommentsToAPI(h: RequestHandler, l: Comment[]) : Promise<Array<any>> {
|
||||||
|
const list = [];
|
||||||
|
for (const comment of l) {
|
||||||
|
list.push(await this.CommentToAPI(h, comment));
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Turn a comment into an API object
|
||||||
|
*
|
||||||
|
* @param h Request handler
|
||||||
|
* @param c Comment
|
||||||
|
*/
|
||||||
|
public static async CommentToAPI(h: RequestHandler, c: Comment) : Promise<any> {
|
||||||
|
return {
|
||||||
|
ID: c.id,
|
||||||
|
userID: c.userID,
|
||||||
|
postID: c.postID,
|
||||||
|
time_sent: c.timeSent,
|
||||||
|
content: c.content,
|
||||||
|
img_path: c.hasImage ? c.imagePath : null,
|
||||||
|
img_url: c.hasImage ? c.imageURL : null,
|
||||||
|
likes: await LikesHelper.Count(c.id, LikesType.COMMENT),
|
||||||
|
userlike: h.signedIn ? await LikesHelper.IsLiking(h.getUserId(), c.id, LikesType.COMMENT) : false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -7,6 +7,8 @@ import { MoviesHelper } from "../helpers/MoviesHelper";
|
|||||||
import { SurveyHelper } from "../helpers/SurveyHelper";
|
import { SurveyHelper } from "../helpers/SurveyHelper";
|
||||||
import { SurveyController } from "./SurveyController";
|
import { SurveyController } from "./SurveyController";
|
||||||
import { LikesHelper, LikesType } from "../helpers/LikesHelper";
|
import { LikesHelper, LikesType } from "../helpers/LikesHelper";
|
||||||
|
import { CommentsHelper } from "../helpers/CommentsHelper";
|
||||||
|
import { CommentsController } from "./CommentsController";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Posts controller
|
* Posts controller
|
||||||
@ -106,7 +108,7 @@ export class PostsController {
|
|||||||
user_access: ACCESS_LEVELS_API[await PostsHelper.GetAccessLevel(h.optionnalUserID, p)],
|
user_access: ACCESS_LEVELS_API[await PostsHelper.GetAccessLevel(h.optionnalUserID, p)],
|
||||||
|
|
||||||
// Load comments (if possible)
|
// Load comments (if possible)
|
||||||
comments: await PostsHelper.AllowCommentsOnPost(p) ? "to come" : null,
|
comments: await PostsHelper.AllowCommentsOnPost(p) ? await CommentsController.CommentsToAPI(h, await CommentsHelper.Get(p.id)) : null,
|
||||||
};
|
};
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
|
39
src/entities/Comment.ts
Normal file
39
src/entities/Comment.ts
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
import { pathUserData } from "../utils/UserDataUtils";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Single comment information
|
||||||
|
*
|
||||||
|
* @author Pierre HUBERT
|
||||||
|
*/
|
||||||
|
|
||||||
|
export interface CommentBuilder {
|
||||||
|
id: number,
|
||||||
|
timeSent: number,
|
||||||
|
userID: number,
|
||||||
|
postID: number,
|
||||||
|
content: string,
|
||||||
|
imagePath ?: string,
|
||||||
|
}
|
||||||
|
|
||||||
|
export class Comment implements CommentBuilder {
|
||||||
|
id: number; timeSent: number;
|
||||||
|
userID: number;
|
||||||
|
postID: number;
|
||||||
|
content: string;
|
||||||
|
imagePath?: string;
|
||||||
|
|
||||||
|
public constructor(info: CommentBuilder) {
|
||||||
|
for (const key in info) {
|
||||||
|
if (info.hasOwnProperty(key))
|
||||||
|
this[key] = info[key];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
get hasImage() : boolean {
|
||||||
|
return this.imagePath != null && this.imagePath != "" && this.imagePath != "null";
|
||||||
|
}
|
||||||
|
|
||||||
|
get imageURL() : string {
|
||||||
|
return pathUserData(this.imagePath);
|
||||||
|
}
|
||||||
|
}
|
46
src/helpers/CommentsHelper.ts
Normal file
46
src/helpers/CommentsHelper.ts
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
import { Comment } from "../entities/Comment";
|
||||||
|
import { DatabaseHelper } from "./DatabaseHelper";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Comments helper
|
||||||
|
*
|
||||||
|
* @author Pierre HUBERT
|
||||||
|
*/
|
||||||
|
|
||||||
|
const COMMENTS_TABLE = "commentaires";
|
||||||
|
|
||||||
|
export class CommentsHelper {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the comments of a POST
|
||||||
|
*
|
||||||
|
* @param postID Target post ID
|
||||||
|
*/
|
||||||
|
public static async Get(postID: number) : Promise<Array<Comment>> {
|
||||||
|
const results = await DatabaseHelper.Query({
|
||||||
|
table: COMMENTS_TABLE,
|
||||||
|
where: {
|
||||||
|
ID_texte: postID
|
||||||
|
},
|
||||||
|
order: "ID"
|
||||||
|
});
|
||||||
|
|
||||||
|
return results.map(this.DbToComment);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Turn a database entry into a Comment object
|
||||||
|
*
|
||||||
|
* @param row Database entry
|
||||||
|
*/
|
||||||
|
private static DbToComment(row: any) : Comment {
|
||||||
|
return new Comment({
|
||||||
|
id: row.ID,
|
||||||
|
userID: row.ID_personne,
|
||||||
|
postID: row.ID_texte,
|
||||||
|
timeSent: row.time_insert == null ? new Date(row.date_envoi).getTime() / 100 : row.time_insert,
|
||||||
|
content: row.commentaire,
|
||||||
|
imagePath: (row.image_commentaire != null && row.image_commentaire != "") ? row.image_commentaire.replace("file:", "") : null
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user