diff --git a/index.js b/index.js index 3a9f481..a4d7235 100644 --- a/index.js +++ b/index.js @@ -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 = ""; diff --git a/modules/accounts.js b/modules/accounts.js index 05d1056..86b07f0 100644 --- a/modules/accounts.js +++ b/modules/accounts.js @@ -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 })() } diff --git a/views/content_playlist.handlebars b/views/content_playlist.handlebars index 012893b..a0ed0c5 100644 --- a/views/content_playlist.handlebars +++ b/views/content_playlist.handlebars @@ -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 @@ User Comment Delete comment + Rating {{#each playlistcomments}} @@ -168,6 +196,17 @@ + +
+

+   +   +   +   +   + {{this.rating}} +

+ {{/each}}