forked from MadhavShroff/IECSE-Genesis-2019-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
127 lines (118 loc) · 4.02 KB
/
db.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
var async = require('async');
var fs = require('fs');
var pg = require('pg');
var pool = new pg.Pool(
{
user: 'maxroach',
host: 'localhost',
database: 'iecse_genesis',
port: 26257
}
);
module.exports.fetchUserData = (async function(username, callback) {
const client = await pool.connect()
await client.query(`SELECT * FROM users WHERE username = '${username}';`, (err, results) => {
if (err) {
console.error("Error fetching rows from table: " + err);
callback(err);
} else {
callback(results.rows);
}
});
client.release();
})
module.exports.fetchPostsByUser = (async function(username, callback) {
const client = await pool.connect()
await client.query(`SELECT * FROM media WHERE published_by = '${username}';`, (err, results) => {
if (err) {
console.error("Error fetching rows from table: " + err);
callback(err);
} else {
callback(results.rows);
}
});
client.release();
})
module.exports.fetchAllUsers = (async function(callback) {
const client = await pool.connect()
await client.query(`SELECT * FROM users;`, (err, results) => {
if(err) {
console.log("Error fetching rows from table: " + err);
callback(err);
} else {
callback(results.rows);
}
})
})
module.exports.fetchAllPosts = (async function(callback) {
const client = await pool.connect()
await client.query(`SELECT * FROM media;`, (err, results) => {
if (err) {
console.error("Error fetching rows from table: " + err);
callback(err);
} else {
callback(results.rows);
}
});
client.release();
})
module.exports.fetchPostsByCategory = (async function(category, callback) {
const client = await pool.connect()
await client.query(`SELECT * FROM media WHERE category = '${category}';`, (err, results) => {
if (err) {
console.error("Error fetching rows from table: " + err);
callback(err);
} else {
callback(results.rows);
}
});
client.release();
})
module.exports.addUser = (async function(username, name, email, best_shot, profile_pic, callback) {
const client = await pool.connect()
await client.query(`INSERT INTO users (username, name, email, best_shot, profile_pic) VALUES('${username}', '${name}', '${email}', '${best_shot}', '${profile_pic}');`, (err, results) => {
if (err) {
console.error("Error Inserting values in table: " + err);
callback(err);
} else {
callback(results);
}
});
client.release();
})
module.exports.addPost = (async function(category, href, username, callback) {
const client = await pool.connect()
await client.query(`INSERT INTO media (category, href, published_by) VALUES('${category}', '${href}', '${username}');`, (err, results) => {
if (err) {
console.error("Error Inserting values in table: " + err);
callback(err);
} else {
callback(results);
}
});
client.release();
})
module.exports.makeFavourite = (async function(user_id, post_id, callback) {
const client = await pool.connect()
await client.query(`INSERT INTO favourites (user_id, media_id) VALUES('${user_id}', '${post_id}');`, (err, results) => {
if (err) {
console.error("Error inserting rows into table: " + err);
callback(err);
} else {
callback(results.rows);
}
});
client.release();
})
module.exports.getFavourites = (async function(user_id, callback) {
const client = await pool.connect()
await client.query(`SELECT * FROM favourites WHERE user_id = '${user_id}';`, (err, results) => {
if (err) {
console.error("Error fetching rows from table: " + err);
callback(err);
} else {
callback(results.rows);
}
});
client.release();
})