diff --git a/modules/user.js b/modules/user.js index 1df0a29..1cda7ee 100644 --- a/modules/user.js +++ b/modules/user.js @@ -5,8 +5,17 @@ const bcrypt = require('bcrypt-promise') const sqlite = require('sqlite-async') const saltRounds = 10 +/** + * Class representing a song. + * Interacts with the database. + */ module.exports = class User { - + /** + * User class constructor. + * Leave parameter empty to create db in memory. + * @constructor + * @param {string} [dbName=:memory:] - The database filename. + */ constructor(dbName = ':memory:') { return (async() => { this.db = await sqlite.open(dbName) @@ -16,6 +25,12 @@ module.exports = class User { })() } + /** + * Registers a user. + * @param {string} user - The username + * @param {string} pass - The password + * @returns {Promise} A confirmation of registration. + */ async register(user, pass) { try { if(user.length === 0) throw new Error('missing username') @@ -32,6 +47,12 @@ module.exports = class User { } } + /** + * Logs in a user if username and password are correct. + * @param {string} username - The username + * @param {string} password - The password + * @returns {Promise} The user's ID + */ async login(username, password) { try { let sql = `SELECT count(id) AS count FROM users WHERE user="${username}";` @@ -47,6 +68,11 @@ module.exports = class User { } } + /** + * Gets a username based on an ID. + * @param {string} id - The user's ID + * @returns {Promise} The ID username + */ async get(id) { const sql = `SELECT user FROM users WHERE id=${id}` const data = await this.db.get(sql) diff --git a/routes/browse.get.js b/routes/browse.get.js index dcffd61..5daeab0 100644 --- a/routes/browse.get.js +++ b/routes/browse.get.js @@ -2,6 +2,12 @@ const Playlists = require('../modules/Playlists') +/** + * The script that handles the get.browse route. + * @param {ctx} ctx - Context from route + * @param {string} dbName - Database name + * @memberof routes + */ const getBrowse = async(ctx, dbName) => { const data = [] if (ctx.query.msg) data.msg = ctx.query.msg diff --git a/routes/comment.post.js b/routes/comment.post.js index acb2cac..d7694ba 100644 --- a/routes/comment.post.js +++ b/routes/comment.post.js @@ -4,6 +4,12 @@ const Comment = require('../modules/comment') const UserComment = require('../modules/userComment') const PlaylistComment = require('../modules/playlistComment') +/** + * The script that handles the post.comment route. + * @param {ctx} ctx - Context from route + * @param {string} dbName - Database name + * @memberof routes + */ const postComment = async(ctx, dbName) => { try { if (!ctx.session.authorised) await ctx.redirect('/login?msg=You need to log in') diff --git a/routes/deleteComment.get.js b/routes/deleteComment.get.js index 37b6348..1717b1e 100644 --- a/routes/deleteComment.get.js +++ b/routes/deleteComment.get.js @@ -4,6 +4,12 @@ const UserComment = require('../modules/userComment') const Comment = require('../modules/comment') const PlaylistComment = require('../modules/playlistComment') +/** + * The script that handles the get.delete-com route. + * @param {ctx} ctx - Context from route + * @param {string} dbName - Database name + * @memberof routes + */ const getDeleteComment = async(ctx, dbName) => { try { const userComment = await new UserComment(dbName) diff --git a/routes/deleteSong.get.js b/routes/deleteSong.get.js index 417f27a..ab7c142 100644 --- a/routes/deleteSong.get.js +++ b/routes/deleteSong.get.js @@ -4,6 +4,12 @@ const UserSong = require('../modules/userSong') const PlaylistSongs = require('../modules/Playlist_songs') const Song = require('../modules/song') +/** + * The script that handes the get.delete-song route. + * @param {ctx} ctx - Context from route + * @param {string} dbName - Database name + * @memberof routes + */ const getDeleteSong = async(ctx, dbName) => { try { const userSong = await new UserSong(dbName) diff --git a/routes/home.get.js b/routes/home.get.js index b40e873..bdaeffb 100644 --- a/routes/home.get.js +++ b/routes/home.get.js @@ -5,6 +5,12 @@ const Playlist = require('../modules/Playlists') const start = 0, count = 3 +/** + * The script that handles the + * @param {ctx} ctx - Context from route + * @param {string} dbName - Database name + * @memberof routes + */ const getHome = async(ctx, dbName) => { try { const data = {} diff --git a/routes/library.get.js b/routes/library.get.js index 73aae4f..ee4fafe 100644 --- a/routes/library.get.js +++ b/routes/library.get.js @@ -3,6 +3,12 @@ const Playlists = require('../modules/Playlists') const UserPlaylist = require('../modules/User_playlists') +/** + * The script that handles the get.library route. + * @param {ctx} ctx - Context from route + * @param {string} dbName - Database name + * @memberof routes + */ const getLibrary = async(ctx, dbName) => { try { if (ctx.session.authorised !== true) await ctx.redirect('/login?msg=you need to login') diff --git a/routes/libraryDetails.get.js b/routes/libraryDetails.get.js index 839a9f9..a893e51 100644 --- a/routes/libraryDetails.get.js +++ b/routes/libraryDetails.get.js @@ -8,6 +8,13 @@ const Playlists = require('../modules/Playlists') const PlaylistSongs = require('../modules/Playlist_songs') const PlaylistComment = require('../modules/playlistComment') +/** + * Converts song IDs to a list of details. + * @param {Array} songIDs - A list of comment IDs + * @param {string} dbName - The database name + * @returns {Promise>} An array of details. + * @memberof routes + */ const songIDsToDetails = async(songIDs, dbName) => { const song = await new Song(dbName), songs = [] for (const id of songIDs) { @@ -17,6 +24,14 @@ const songIDsToDetails = async(songIDs, dbName) => { return songs } +/** + * Converts comment IDs to a list of details. + * @param {Array} commentIDs - A list of comment IDs + * @param {ctx} ctx - The context passed from the route + * @param {string} dbName - The database name + * @returns {Promise>} An array of details. + * @memberof routes + */ const commentIDsToDetails = async(commentIDs, ctx, dbName) => { const userComment = await new UserComment(dbName) const comment = await new Comment(dbName) @@ -34,6 +49,12 @@ const commentIDsToDetails = async(commentIDs, ctx, dbName) => { return commentList } +/** + * The script that handles the get.library/:id route. + * @param {ctx} ctx - Context from route + * @param {string} dbName - Database name + * @memberof routes + */ const getLibraryDetails = async(ctx, dbName) => { try { /* Getting all the necessary objects ready */ diff --git a/routes/login.get.js b/routes/login.get.js index a1eb04e..1782541 100644 --- a/routes/login.get.js +++ b/routes/login.get.js @@ -1,5 +1,10 @@ 'use strict' +/** + * The script that handles the get.login route. + * @param {ctx} ctx - Context from route + * @memberof routes + */ const getLogin = async ctx => { if(ctx.session.authorised === true) await ctx.redirect('/?msg=You are already logged in') const data = {} diff --git a/routes/login.post.js b/routes/login.post.js index 0ae806e..6c7d795 100644 --- a/routes/login.post.js +++ b/routes/login.post.js @@ -2,6 +2,12 @@ const User = require('../modules/user') +/** + * The script that handles the post.login route. + * @param {ctx} ctx - Context from route + * @param {string} dbName - Database name + * @memberof routes + */ const postLogin = async(ctx, dbName) => { try { if(ctx.session.authorised === true) ctx.redirect('/?msg=You are already logged in') diff --git a/routes/logout.get.js b/routes/logout.get.js index 0175bc8..ea18e2d 100644 --- a/routes/logout.get.js +++ b/routes/logout.get.js @@ -1,5 +1,10 @@ 'use strict' +/** + * The script that handles the get.logout route. + * @param {ctx} ctx - Context from route + * @memberof routes + */ const getLogout = async ctx => { ctx.session.authorised = null ctx.session.id = null diff --git a/routes/playlistAdd.post.js b/routes/playlistAdd.post.js index 63bbdb2..f960b49 100644 --- a/routes/playlistAdd.post.js +++ b/routes/playlistAdd.post.js @@ -2,6 +2,12 @@ const PlaylistSongs = require('../modules/Playlist_songs') +/** + * The script that handles the post.playlistAdd route. + * @param {ctx} ctx - Context from route + * @param {string} dbName - Database name + * @memberof routes + */ const postPlaylistAdd = async(ctx, dbName) => { try { const { song, playlist } = ctx.request.body diff --git a/routes/playlists.get.js b/routes/playlists.get.js index 962f2bf..64757ab 100644 --- a/routes/playlists.get.js +++ b/routes/playlists.get.js @@ -5,6 +5,12 @@ const UserPlaylist = require('../modules/User_playlists') const Song = require('../modules/song') const Playlists = require('../modules/playlists') +/** + * Converts song IDs to a list of details. + * @param {Array} songIDs - A list of comment IDs + * @param {string} dbName - The database name + * @returns {Promise>} An array of details. + */ const songIDsToDetails = async(songIDs, dbName) => { const song = await new Song(dbName), songs = [] for (const id of songIDs) { @@ -14,6 +20,14 @@ const songIDsToDetails = async(songIDs, dbName) => { return songs } +/** + * Converts comment IDs to a list of details. + * @param {Array} commentIDs - A list of comment IDs + * @param {ctx} ctx - The context passed from the route + * @param {string} dbName - The database name + * @returns {Promise>} An array of details. + * @memberof routes + */ const playlistIDsToDetails = async(playlistIDs, dbName) => { const playlist = await new Playlists(dbName), playlists = [] for (const id of playlistIDs) { @@ -23,6 +37,11 @@ const playlistIDsToDetails = async(playlistIDs, dbName) => { return playlists } +/** + * The script that handles the get.playlists route. + * @param {ctx} ctx - Context from route + * @param {string} dbName - Database name + */ const getPlaylists = async(ctx, dbName) => { try { if (ctx.session.authorised !== true) await ctx.redirect('/login?msg=you need to login') diff --git a/routes/playlists.post.js b/routes/playlists.post.js index 0ebd8ca..f95f26e 100644 --- a/routes/playlists.post.js +++ b/routes/playlists.post.js @@ -3,6 +3,12 @@ const Playlists = require('../modules/Playlists') const UserPlaylist = require('../modules/User_playlists') +/** + * The script that handles the post.playlists route. + * @param {ctx} ctx - Context from route + * @param {string} dbName - Database name + * @memberof routes + */ const postPlaylists = async(ctx, dbName) => { try { if (ctx.session.authorised !== true) diff --git a/routes/register.post.js b/routes/register.post.js index 18a1733..2497b20 100644 --- a/routes/register.post.js +++ b/routes/register.post.js @@ -2,6 +2,12 @@ const User = require('../modules/user') +/** + * The script that handles the get.register route. + * @param {ctx} ctx - Context from route + * @param {string} dbName - Database name + * @memberof routes + */ const postRegister = async(ctx, dbName) => { try { const body = ctx.request.body diff --git a/routes/songDetails.get.js b/routes/songDetails.get.js index b3a5750..0aeae51 100644 --- a/routes/songDetails.get.js +++ b/routes/songDetails.get.js @@ -3,6 +3,12 @@ const Song = require('../modules/song') const UserSong = require('../modules/userSong') +/** + * The script that handles the get.songs/:id route. + * @param {ctx} ctx - Context from route + * @param {string} dbName - Database name + * @memberof routes + */ const getSongDetails = async(ctx, dbName) => { try { const song = await new Song(dbName) diff --git a/routes/songs.get.js b/routes/songs.get.js index 29b5037..b43a666 100644 --- a/routes/songs.get.js +++ b/routes/songs.get.js @@ -2,6 +2,12 @@ const Song = require('../modules/song') +/** + * The script that handles the get.songs route. + * @param {ctx} ctx - Context from route + * @param {string} dbName - Database name + * @memberof routes + */ const getSongs = async(ctx, dbName) => { const song = await new Song(dbName) const data = await song.getAll() diff --git a/routes/upload.get.js b/routes/upload.get.js index 504a54c..7996701 100644 --- a/routes/upload.get.js +++ b/routes/upload.get.js @@ -3,6 +3,12 @@ const UserPlaylist = require('../modules/User_playlists') const Playlists = require('../modules/Playlists') +/** + * The script that handles the get.upload route. + * @param {ctx} ctx - Context from route + * @param {string} dbName - Database name + * @memberof routes + */ const getUpload = async(ctx, dbName) => { if (ctx.session.authorised !== true) return ctx.redirect('/login?msg=you need to log in') const data = [], lists = [] diff --git a/routes/upload.post.js b/routes/upload.post.js index 03985d4..e2caa9a 100644 --- a/routes/upload.post.js +++ b/routes/upload.post.js @@ -8,6 +8,13 @@ const sharp = require('sharp') const size = 1024 +/** + * The script that handles the post.upload route. + * @param {ctx} ctx - Context from route + * @param {string} dbName - Database name + * @param {string} dirname - The route directory, relative to index.js + * @memberof routes + */ const postUpload = async(ctx, dbName, dirname) => { try { if (!ctx.session.authorised) await ctx.redirect('/login?msg=You need to log in')