-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
126 lines (113 loc) · 3.9 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
const spicedPg = require("spiced-pg");
let db;
if (process.env.DATABASE_URL) {
db = spicedPg(process.env.DATABASE_URL);
} else {
const { user, pw } = require("./secrets.json");
db = spicedPg(`postgres:${user}:${pw}@localhost:5432/myprivatecook`);
}
exports.insertRegistration = (first, last, email, pw) => {
return db.query(
`INSERT INTO users (first, last, email, password) VALUES ($1, $2, $3, $4) RETURNING *`,
[first, last, email, pw]
);
};
exports.checkLogin = (email) => {
return db.query(`SELECT * FROM users WHERE email = $1`, [email]);
};
exports.selectProfile = (Id) => {
return db.query(`SELECT * FROM users WHERE id = $1`, [Id]);
};
exports.insertCook = (
national_cuisine,
hourly_wage,
specialties,
experiences,
cook_on_site,
shopping_food,
delivery,
id
) => {
return db.query(
`INSERT INTO cooks (national_cuisine, hourly_wage, specialties, experiences, cook_on_site, shopping_food, delivery, cooks_id)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
ON CONFLICT (cooks_id)
DO UPDATE SET national_cuisine = $1, hourly_wage = $2, specialties=$3, experiences=$4, cook_on_site=$5, shopping_food=$6, delivery=$7 RETURNING *`,
[
national_cuisine,
hourly_wage,
specialties,
experiences,
cook_on_site,
shopping_food,
delivery,
id,
]
);
};
exports.selectCooks = () => {
return db.query(`SELECT users.id, first, last, email, profilepic, national_cuisine,
hourly_wage,
specialties,
experiences,
cook_on_site,
shopping_food,
delivery, cooks.created_at, image1, image2, image3, image4, image5 FROM users JOIN cooks ON users.id = cooks.cooks_id
LEFT JOIN images ON users.id = images.users_id
ORDER BY cooks.created_at DESC `);
};
exports.getCookModal = (id) => {
return db.query(
`SELECT users.id, first, last, email, profilepic, national_cuisine,
hourly_wage,
specialties,
experiences,
cook_on_site,
shopping_food,
delivery, cooks.created_at, image1, image2, image3, image4, image5 FROM users JOIN cooks ON users.id = cooks.cooks_id
LEFT JOIN images ON users.id = images.users_id
WHERE users.id = $1`,
[id]
);
};
exports.insertImage = (id, image1, image2, image3, image4, image5) => {
return db.query(
`INSERT INTO images (users_id, image1, image2, image3, image4, image5)
VALUES ($1, $2, $3, $4, $5, $6)
ON CONFLICT (users_id)
DO UPDATE SET image1 = $2, image2 = $3, image3=$4, image4=$5, image5=$6 RETURNING *`,
[
id,
image1 || null,
image2 || null,
image3 || null,
image4 || null,
image5 || null,
]
);
};
exports.selectCommentProfile = (userId) => {
return db.query(`SELECT first, last, email FROM users WHERE id = $1`, [
userId,
]);
};
exports.insertComment = (id, firstName, lastName, email, comment) => {
return db.query(
`INSERT INTO comments (users_id, comment_first, comment_last, comment_email, comment)
VALUES ($1, $2, $3, $4, $5)
RETURNING *`,
[id, firstName, lastName, email, comment]
);
};
exports.getComments = (id) => {
return db.query(
`SELECT comment_first, comment_last, comment_email, comment, created_at FROM comments WHERE users_id = $1`,
[id]
);
};
exports.getLogedInName = (userId) => {
return db.query(`SELECT * FROM users WHERE id = $1`, [userId]);
};
exports.checkExistingProfile = (userId) => {
return db.query(`SELECT * FROM cooks WHERE cooks_id = $1`, [userId]);
};