-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcontroller.js
More file actions
68 lines (58 loc) · 2.13 KB
/
controller.js
File metadata and controls
68 lines (58 loc) · 2.13 KB
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
const pool = require("../../db.js");
const queries = require("./queries");
const { validate, validateParams } = require("../middleware/validate");
const { profileSchema, profileIdSchema } = require("./validation");
const createProfile = async (req, res) => {
try {
const { first_name, last_name, email, profile_pic } = req.body;
const user_id = req.user.id;
// Check if email already exists
if (email) {
const emailExists = await pool.query("SELECT * FROM profiles WHERE email = $1", [email]);
if (emailExists.rows.length > 0) {
return res.status(400).json({ error: 'Email already exists' });
}
}
await pool.query(queries.createProfile, [user_id, first_name, last_name, email, profile_pic]);
res.status(201).json({ message: 'Profile created successfully' });
} catch (err) {
console.error(err.message);
res.status(500).json({ error: 'Internal Server Error' });
}
};
const getProfile = async (req, res) => {
try {
const user_id = req.user.id;
const profile = await pool.query(queries.getProfileByUserId, [user_id]);
if (profile.rows.length === 0) {
return res.status(404).json({ error: 'Profile not found' });
}
res.json(profile.rows[0]);
} catch (err) {
console.error(err.message);
res.status(500).json({ error: 'Internal Server Error' });
}
};
const updateProfile = async (req, res) => {
try {
const { first_name, last_name, email, profile_pic } = req.body;
const user_id = req.user.id;
// Check if email already exists
if (email) {
const emailExists = await pool.query("SELECT * FROM profiles WHERE email = $1 AND user_id != $2", [email, user_id]);
if (emailExists.rows.length > 0) {
return res.status(400).json({ error: 'Email already exists' });
}
}
await pool.query(queries.updateProfile, [first_name, last_name, email, profile_pic, user_id]);
res.status(200).json({ message: 'Profile updated successfully' });
} catch (err) {
console.error(err.message);
res.status(500).json({ error: 'Internal Server Error' });
}
};
module.exports = {
createProfile,
getProfile,
updateProfile,
};