Skip to content

Added documentation #14

Merged
merged 1 commit into from Dec 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 27 additions & 1 deletion modules/user.js
Expand Up @@ -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)
Expand All @@ -16,6 +25,12 @@ module.exports = class User {
})()
}

/**
* Registers a user.
* @param {string} user - The username
* @param {string} pass - The password
* @returns {Promise<true>} A confirmation of registration.
*/
async register(user, pass) {
try {
if(user.length === 0) throw new Error('missing username')
Expand All @@ -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<number>} The user's ID
*/
async login(username, password) {
try {
let sql = `SELECT count(id) AS count FROM users WHERE user="${username}";`
Expand All @@ -47,6 +68,11 @@ module.exports = class User {
}
}

/**
* Gets a username based on an ID.
* @param {string} id - The user's ID
* @returns {Promise<string>} The ID username
*/
async get(id) {
const sql = `SELECT user FROM users WHERE id=${id}`
const data = await this.db.get(sql)
Expand Down
6 changes: 6 additions & 0 deletions routes/browse.get.js
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions routes/comment.post.js
Expand Up @@ -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')
Expand Down
6 changes: 6 additions & 0 deletions routes/deleteComment.get.js
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions routes/deleteSong.get.js
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions routes/home.get.js
Expand Up @@ -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 = {}
Expand Down
6 changes: 6 additions & 0 deletions routes/library.get.js
Expand Up @@ -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')
Expand Down
21 changes: 21 additions & 0 deletions routes/libraryDetails.get.js
Expand Up @@ -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<number>} songIDs - A list of comment IDs
* @param {string} dbName - The database name
* @returns {Promise<Array<dbData>>} An array of details.
* @memberof routes
*/
const songIDsToDetails = async(songIDs, dbName) => {
const song = await new Song(dbName), songs = []
for (const id of songIDs) {
Expand All @@ -17,6 +24,14 @@ const songIDsToDetails = async(songIDs, dbName) => {
return songs
}

/**
* Converts comment IDs to a list of details.
* @param {Array<number>} commentIDs - A list of comment IDs
* @param {ctx} ctx - The context passed from the route
* @param {string} dbName - The database name
* @returns {Promise<Array<{number, string}>>} An array of details.
* @memberof routes
*/
const commentIDsToDetails = async(commentIDs, ctx, dbName) => {
const userComment = await new UserComment(dbName)
const comment = await new Comment(dbName)
Expand All @@ -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 */
Expand Down
5 changes: 5 additions & 0 deletions 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 = {}
Expand Down
6 changes: 6 additions & 0 deletions routes/login.post.js
Expand Up @@ -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')
Expand Down
5 changes: 5 additions & 0 deletions 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
Expand Down
6 changes: 6 additions & 0 deletions routes/playlistAdd.post.js
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions routes/playlists.get.js
Expand Up @@ -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<number>} songIDs - A list of comment IDs
* @param {string} dbName - The database name
* @returns {Promise<Array<dbData>>} An array of details.
*/
const songIDsToDetails = async(songIDs, dbName) => {
const song = await new Song(dbName), songs = []
for (const id of songIDs) {
Expand All @@ -14,6 +20,14 @@ const songIDsToDetails = async(songIDs, dbName) => {
return songs
}

/**
* Converts comment IDs to a list of details.
* @param {Array<number>} commentIDs - A list of comment IDs
* @param {ctx} ctx - The context passed from the route
* @param {string} dbName - The database name
* @returns {Promise<Array<{number, string}>>} An array of details.
* @memberof routes
*/
const playlistIDsToDetails = async(playlistIDs, dbName) => {
const playlist = await new Playlists(dbName), playlists = []
for (const id of playlistIDs) {
Expand All @@ -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')
Expand Down
6 changes: 6 additions & 0 deletions routes/playlists.post.js
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions routes/register.post.js
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions routes/songDetails.get.js
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions routes/songs.get.js
Expand Up @@ -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()
Expand Down
6 changes: 6 additions & 0 deletions routes/upload.get.js
Expand Up @@ -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 = []
Expand Down
7 changes: 7 additions & 0 deletions routes/upload.post.js
Expand Up @@ -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')
Expand Down