Skip to content
Permalink
4a0927eae0
Switch branches/tags

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?
Go to file
 
 
Cannot retrieve contributors at this time
43 lines (38 sloc) 1.32 KB
// 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;
}