|
|
|
@ -5,8 +5,6 @@ import {User} from "./User";
|
|
|
|
|
|
|
|
|
|
export class Post extends DataObject {
|
|
|
|
|
public readonly id: number;
|
|
|
|
|
private $upvotes: number;
|
|
|
|
|
private $downvotes: number;
|
|
|
|
|
private $createdAt: string;
|
|
|
|
|
private $content: string;
|
|
|
|
|
private $author: number;
|
|
|
|
@ -16,23 +14,29 @@ export class Post extends DataObject {
|
|
|
|
|
* Returns the upvotes of a post.
|
|
|
|
|
*/
|
|
|
|
|
public async upvotes(): Promise<number> {
|
|
|
|
|
this.loadDataIfNotExists();
|
|
|
|
|
return this.$upvotes;
|
|
|
|
|
const result = await queryHelper.first({
|
|
|
|
|
text: "SELECT COUNT(*) count FROM votes WHERE item_id = $1 AND vote_type = 'UPVOTE'",
|
|
|
|
|
values: [this.id],
|
|
|
|
|
});
|
|
|
|
|
return result.count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the downvotes of the post
|
|
|
|
|
*/
|
|
|
|
|
public async downvotes(): Promise<number> {
|
|
|
|
|
this.loadDataIfNotExists();
|
|
|
|
|
return this.$downvotes;
|
|
|
|
|
const result = await queryHelper.first({
|
|
|
|
|
text: "SELECT COUNT(*) count FROM votes WHERE item_id = $1 AND vote_type = 'DOWNVOTE'",
|
|
|
|
|
values: [this.id],
|
|
|
|
|
});
|
|
|
|
|
return result.count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The content of the post (markdown)
|
|
|
|
|
*/
|
|
|
|
|
public async content(): Promise<string> {
|
|
|
|
|
this.loadDataIfNotExists();
|
|
|
|
|
await this.loadDataIfNotExists();
|
|
|
|
|
return this.$content;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -40,7 +44,7 @@ export class Post extends DataObject {
|
|
|
|
|
* The date the post was created at.
|
|
|
|
|
*/
|
|
|
|
|
public async createdAt(): Promise<string> {
|
|
|
|
|
this.loadDataIfNotExists();
|
|
|
|
|
await this.loadDataIfNotExists();
|
|
|
|
|
return this.$createdAt;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -48,7 +52,7 @@ export class Post extends DataObject {
|
|
|
|
|
* The autor of the post.
|
|
|
|
|
*/
|
|
|
|
|
public async author(): Promise<User> {
|
|
|
|
|
this.loadDataIfNotExists();
|
|
|
|
|
await this.loadDataIfNotExists();
|
|
|
|
|
return new User(this.$author);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -77,6 +81,34 @@ export class Post extends DataObject {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Performs a vote on a post.
|
|
|
|
|
* @param userId
|
|
|
|
|
* @param type
|
|
|
|
|
*/
|
|
|
|
|
public async vote(userId: number, type: dataaccess.VoteType): Promise<dataaccess.VoteType> {
|
|
|
|
|
const uVote = await this.userVote(userId);
|
|
|
|
|
if (uVote === type) {
|
|
|
|
|
await queryHelper.first({
|
|
|
|
|
text: "DELETE FROM votes WHERE item_id = $1 AND user_id = $2",
|
|
|
|
|
values: [this.id, userId],
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
if (uVote) {
|
|
|
|
|
await queryHelper.first({
|
|
|
|
|
text: "UPDATE votes SET vote_type = $1 WHERE user_id = $1 AND item_id = $3",
|
|
|
|
|
values: [type, userId, this.id],
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
await queryHelper.first({
|
|
|
|
|
text: "INSERT INTO votes (user_id, item_id, vote_type) values ($1, $2, $3)",
|
|
|
|
|
values: [userId, this.id, type],
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return type;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Loads the data from the database if needed.
|
|
|
|
|
*/
|
|
|
|
@ -93,8 +125,6 @@ export class Post extends DataObject {
|
|
|
|
|
if (result) {
|
|
|
|
|
this.$author = result.author;
|
|
|
|
|
this.$content = result.content;
|
|
|
|
|
this.$downvotes = result.downvotes;
|
|
|
|
|
this.$upvotes = result.upvotes;
|
|
|
|
|
this.$createdAt = result.created_at;
|
|
|
|
|
this.$type = result.type;
|
|
|
|
|
this.dataLoaded = true;
|
|
|
|
|