Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
node-backendapi/models/likes.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
43 lines (38 sloc)
1.32 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// pg DB connection from config file /helpers/db.js | |
const db = require('../helpers/db.js') | |
exports.addLike = async function addLike(articleId, userId) { | |
let sql = 'INSERT INTO likes (article_id, user_id) \ | |
SELECT articles.id, users.id \ | |
FROM (SELECT id FROM articles WHERE id='+articleId+') articles \ | |
, (SELECT id FROM users WHERE id='+userId+') users;' | |
var obj = await db.query(sql) | |
.then(obj => { | |
return obj; | |
}); | |
return obj; | |
} | |
exports.checkLike = async function checkLike(articleId,userId) { | |
let sql = 'SELECT user_id,article_id FROM likes WHERE (article_id='+articleId+') AND user_id='+userId+';'; | |
var obj = await db.query(sql) | |
.then(obj => { | |
return obj; | |
}); | |
return obj; | |
} | |
// temp function to check user exists (later if is logged) | |
exports.checkUser = async function checkUser(userId) { | |
let sql = 'SELECT id FROM users WHERE id='+userId+';'; | |
var obj = await db.query(sql) | |
.then(obj => { | |
return obj; | |
}); | |
return obj; | |
} | |
exports.removeLike = async function removeLike(articleId, userId) { | |
let sql = 'DELETE FROM likes WHERE article_id='+articleId+' AND user_id='+userId+' RETURNING true;' | |
var obj = await db.query(sql) | |
.then(obj => { | |
return obj; | |
}); | |
return obj; | |
} |