|
|
@ -5,8 +5,6 @@ import {User} from "./User";
|
|
|
|
|
|
|
|
|
|
|
|
export class Post extends DataObject {
|
|
|
|
export class Post extends DataObject {
|
|
|
|
public readonly id: number;
|
|
|
|
public readonly id: number;
|
|
|
|
private $upvotes: number;
|
|
|
|
|
|
|
|
private $downvotes: number;
|
|
|
|
|
|
|
|
private $createdAt: string;
|
|
|
|
private $createdAt: string;
|
|
|
|
private $content: string;
|
|
|
|
private $content: string;
|
|
|
|
private $author: number;
|
|
|
|
private $author: number;
|
|
|
@ -16,23 +14,29 @@ export class Post extends DataObject {
|
|
|
|
* Returns the upvotes of a post.
|
|
|
|
* Returns the upvotes of a post.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public async upvotes(): Promise<number> {
|
|
|
|
public async upvotes(): Promise<number> {
|
|
|
|
this.loadDataIfNotExists();
|
|
|
|
const result = await queryHelper.first({
|
|
|
|
return this.$upvotes;
|
|
|
|
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
|
|
|
|
* Returns the downvotes of the post
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public async downvotes(): Promise<number> {
|
|
|
|
public async downvotes(): Promise<number> {
|
|
|
|
this.loadDataIfNotExists();
|
|
|
|
const result = await queryHelper.first({
|
|
|
|
return this.$downvotes;
|
|
|
|
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)
|
|
|
|
* The content of the post (markdown)
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public async content(): Promise<string> {
|
|
|
|
public async content(): Promise<string> {
|
|
|
|
this.loadDataIfNotExists();
|
|
|
|
await this.loadDataIfNotExists();
|
|
|
|
return this.$content;
|
|
|
|
return this.$content;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -40,7 +44,7 @@ export class Post extends DataObject {
|
|
|
|
* The date the post was created at.
|
|
|
|
* The date the post was created at.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public async createdAt(): Promise<string> {
|
|
|
|
public async createdAt(): Promise<string> {
|
|
|
|
this.loadDataIfNotExists();
|
|
|
|
await this.loadDataIfNotExists();
|
|
|
|
return this.$createdAt;
|
|
|
|
return this.$createdAt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -48,7 +52,7 @@ export class Post extends DataObject {
|
|
|
|
* The autor of the post.
|
|
|
|
* The autor of the post.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public async author(): Promise<User> {
|
|
|
|
public async author(): Promise<User> {
|
|
|
|
this.loadDataIfNotExists();
|
|
|
|
await this.loadDataIfNotExists();
|
|
|
|
return new User(this.$author);
|
|
|
|
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.
|
|
|
|
* Loads the data from the database if needed.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
@ -93,8 +125,6 @@ export class Post extends DataObject {
|
|
|
|
if (result) {
|
|
|
|
if (result) {
|
|
|
|
this.$author = result.author;
|
|
|
|
this.$author = result.author;
|
|
|
|
this.$content = result.content;
|
|
|
|
this.$content = result.content;
|
|
|
|
this.$downvotes = result.downvotes;
|
|
|
|
|
|
|
|
this.$upvotes = result.upvotes;
|
|
|
|
|
|
|
|
this.$createdAt = result.created_at;
|
|
|
|
this.$createdAt = result.created_at;
|
|
|
|
this.$type = result.type;
|
|
|
|
this.$type = result.type;
|
|
|
|
this.dataLoaded = true;
|
|
|
|
this.dataLoaded = true;
|
|
|
|