-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathavatar.js
159 lines (132 loc) · 5.46 KB
/
avatar.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import path from 'path';
import express from 'express';
import sqlite3 from 'sqlite3';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
const router = express.Router();
const db = new sqlite3.Database('branchEvent.db');
//dynamicly get the path to the folder
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const directoryPath = __dirname;
const studentAvatarFolderPath = path.join(directoryPath, 'Student_avatars');
const companyAvatarFolderPath = path.join(directoryPath, 'Company_avatars');
//get student avatar
router.get('/studentAvatars/:studentId', (req, res) => {
console.log('hello');
//client provide the id of student wich avatar we want to fetch
const { studentId } = req.params;
//query to find the student first, and their avatar id.
const query = `SELECT avatar_id FROM Student WHERE id = ?`;
console.log(studentId, 'before');
db.get(query, [studentId], (err, row) => {
if (err) {
console.error('Error querying database:', err);
return res.status(500).json({ error: 'Internal Server Error' });
}
if (!row) {
return res.status(404).json({ error: 'Student not found' });
}
console.log(studentId, 'before studid');
//we have the avatar id
const { avatar_id } = row;
console.log(avatar_id, 'avatarid');
//we query the avatar table
const avatarQuery = `SELECT name FROM Student_avatar WHERE id = ?`;
db.get(avatarQuery, [avatar_id], (avatarErr, avatarRow) => {
if (avatarErr) {
console.error('Error querying database:', avatarErr);
return res.status(500).json({ error: 'Internal Server Error' });
}
if (!avatarRow) {
return res.status(404).json({ error: 'Avatar not found' });
}
//the name is the same as the filename in the avatarfolder
const avatarName = avatarRow.name.toString();
/* const { filename } = avatarName; */
const imagePath = path.join(studentAvatarFolderPath, avatarName);
//send the image file to the client
res.sendFile(imagePath);
console.log('avatar sent to client')
});
});
});
//get company avatar
router.get('/companyAvatars/:companyId', (req, res) => {
console.log('hello');
const { companyId } = req.params;
const query = `SELECT avatar_id FROM Company WHERE id = ?`;
db.get(query, [companyId], (err, row) => {
if (err) {
console.error('Error querying database:', err);
return res.status(500).json({ error: 'Internal Server Error' });
}
if (!row) {
return res.status(404).json({ error: 'company id not found' });
}
//we have the avatar id
const { avatar_id } = row;
//we query the avatar table
const avatarQuery = `SELECT name FROM Company_avatar WHERE id = ?`;
db.get(avatarQuery, [avatar_id], (avatarErr, avatarRow) => {
if (avatarErr) {
console.error('Error querying database:', avatarErr);
return res.status(500).json({ error: 'Internal Server Error' });
}
if (!avatarRow) {
return res.status(404).json({ error: 'Avatar not found' });
}
//the name is the same as the filename in the avatarfolder
const avatarName = avatarRow.name.toString();
/* const { filename } = avatarName; */
const companyimagePath = path.join(companyAvatarFolderPath, avatarName);
//send the image file to the client
res.sendFile(companyimagePath);
console.log('avatar sent to client')
});
});
});
//get avatar by id from avatar tabel
router.get('/cavatars/:avatar_id', (req, res) => {
const { avatar_id } = req.params;
//we query the avatar table
const avatarQuery = `SELECT name FROM Company_avatar WHERE id = ?`;
db.get(avatarQuery, [avatar_id], (avatarErr, avatarRow) => {
if (avatarErr) {
console.error('Error querying database:', avatarErr);
return res.status(500).json({ error: 'Internal Server Error' });
}
if (!avatarRow) {
return res.status(404).json({ error: 'Avatar not found' });
}
//the name is the same as the filename in the avatarfolder
const avatarName = avatarRow.name.toString();
/* const { filename } = avatarName; */
const companyimagePath = path.join(companyAvatarFolderPath, avatarName);
//send the image file to the client
res.sendFile(companyimagePath);
console.log('avatar sent to client')
})
});
//get students avatar from avatarid
router.get('/savatars/:avatar_id', (req, res) => {
const { avatar_id } = req.params;
//we query the avatar table
const avatarQuery = `SELECT name FROM Student_avatar WHERE id = ?`;
db.get(avatarQuery, [avatar_id], (avatarErr, avatarRow) => {
if (avatarErr) {
console.error('Error querying database:', avatarErr);
return res.status(500).json({ error: 'Internal Server Error' });
}
if (!avatarRow) {
return res.status(404).json({ error: 'Avatar not found' });
}
//the name is the same as the filename in the avatarfolder
const avatarName = avatarRow.name.toString();
const companyimagePath = path.join(studentAvatarFolderPath, avatarName);
//send the image file to the client
res.sendFile(companyimagePath);
console.log('avatar sent to client')
})
});
export default router;