Skip to content
Permalink
Browse files
Added rating system for comments
  • Loading branch information
goldberr committed Nov 24, 2019
1 parent 64751a6 commit a714b96a9b4a5f78f6e9e41bff8fb6434d188574
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
@@ -158,6 +158,21 @@ router.post("/playlistRating", async ctx => {
await ctx.render("error", { message: err.message });
}
});
router.post("/commentRating", async ctx => {
try {
if (ctx.session.authorised !== true) return ctx.redirect("/login");
const body = ctx.request.body;
const newRating = body.num;
const commentId = body.commentId;

const db = await sqlite.open(dbName);
await db.run(`INSERT INTO comment_ratings(value, user, commentId) VALUES(${newRating},'${ctx.session.user}',${commentId});`);
await db.close();
} catch (err) {
console.log(err);
await ctx.render("error", { message: err.message });
}
});
router.post("/audioRating", async ctx => {
try {
if (ctx.session.authorised !== true) return ctx.redirect("/login");
@@ -272,6 +287,17 @@ router.post("/loadcontent", async ctx => {
upload.rating = Math.round(totalRating * 100) / 100;
});

playlistComments.forEach(async upload => {
const audioRatings = await db.all(`SELECT "value" FROM comment_ratings WHERE commentID='${upload.commentID}';`);
let totalRating = sum(audioRatings);

if (totalRating > 0)
totalRating = totalRating / audioRatings.length;

upload.safeRating = Math.floor(totalRating);
upload.rating = Math.round(totalRating * 100) / 100;
});

await db.close()
if (!body.genreTitle) {
body.genreTitle = "";
@@ -55,6 +55,11 @@ module.exports = class User {
FOREIGN KEY("playlist") REFERENCES "playlists"("name"),
PRIMARY KEY("commentID" AUTOINCREMENT)
);`)
await this.db.run(`CREATE TABLE IF NOT EXISTS "comment_ratings" (
"value" TINYINT,
"user" TEXT,
"commentID" INTEGER
);`)
return this
})()
}
@@ -54,6 +54,33 @@
parent.parent()[0].setAttribute('data-rating', num);
}
function setCommentRating(ev) {
let span = ev;
let parent = $(ev).parent();
let stars = parent.children();
let match = false;
let num = 0;
stars.each(function (index, star) {
if (match) {
star.classList.remove('rated');
} else {
star.classList.add('rated');
}
if (star === span) {
match = true;
num = index + 1;
}
});
$.ajax({
url: "/commentRating",
type: "post",
data: {
"num": num,
"commentId": parent.parent()[0].classList[0]
}
});
parent.parent()[0].setAttribute('data-rating', num);
}
function setRating(ev) {
let span = ev;
let parent = $(ev).parent();
@@ -159,6 +186,7 @@
<th>User</th>
<th>Comment</th>
<th>Delete comment</th>
<th>Rating</th>
</tr>
{{#each playlistcomments}}
<tr>
@@ -168,6 +196,17 @@
<button class="btnDeleteComment" data-genre-title="{{genretitle}}"
onclick=deleteComment({{this.commentID}})>Delete comment</button>
</td>
<td>
<section class="{{this.commentID}}">
<p>
<span class="star" onclick=setCommentRating(this)>&nbsp;</span>
<span class="star" onclick=setCommentRating(this)>&nbsp;</span>
<span class="star" onclick=setCommentRating(this)>&nbsp;</span>
<span class="star" onclick=setCommentRating(this)>&nbsp;</span>
<span class="star" onclick=setCommentRating(this)>&nbsp;</span>
{{this.rating}}
</p>
</td>
</tr>
{{/each}}
</table>

0 comments on commit a714b96

Please sign in to comment.