-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
304 lines (262 loc) · 8.98 KB
/
index.js
File metadata and controls
304 lines (262 loc) · 8.98 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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
const {PrismaClient}=require('@prisma/client');
const cors = require('cors');
const express = require ('express');
const passport = require('passport');
const session = require('express-session');
const path = require ('path');
const app = express ();
const prisma = new PrismaClient();
require('dotenv').config(); // Load environment variables
// Log to ensure the environment variables are loaded
console.log('GOOGLE_CLIENT_ID:', process.env.GOOGLE_CLIENT_ID);
console.log('GOOGLE_CLIENT_SECRET:', process.env.GOOGLE_CLIENT_SECRET);
console.log('GOOGLE_CALLBACK_URL:', process.env.GOOGLE_CALLBACK_URL);
console.log(process.env.GOOGLE_CLIENT_SECRET);
require('./auth');
app.use (express.json ());
app.use (express.static (path.join (__dirname, 'client')));
function isloggedIn(req, res, next) {
req.user ? next() : res.sendStatus(401);
}
app.use(cors({
origin: ['http://localhost:4000'],
credentials: true,
methods: ['GET'], // need to allow get
allowedHeaders: ['Content-Type', 'Authorization']
}));
app.get ('/', (req, res) => {
res.sendFile ('index.html');
});
app.use(session({
secret:'keyboard cat',
resave: false,
saveUninitialized: true,
cookie: { secure: false }
}));
app.use (passport.initialize ());
app.use(passport.session());
app.get('/auth/google',
passport.authenticate('google', { scope:
[ 'email', 'profile' ] }
));
app.get( '/auth/google/callback',
passport.authenticate( 'google', {
successRedirect: 'http://localhost:4000/WelcomeNewUserPage',
failureRedirect: '/auth/google/failure'
}));
app.get('/auth/google/failure', (req,res) => {
res.send("Something went wrong!");
});
app.get('/auth/protected', isloggedIn, (req,res)=>{
res.json({ username: req.user.displayName , userid: req.user.id,email:req.user.email} );
});
app.get('/user/:id',async(req,res) =>{
const {id} =req.params; //api request parameter which is user id in our case
const user=await prisma.user.findUnique({
where: {id:id}
});
if (user){res.json(user)}; //send user object to the front end where front end can decode the user info from
//the database
});
app.post('/user/:id',async(req,res)=>{
const {id} =req.params; //api request parameter which is user id in our case
const{name, program, university,location,institution,gender}=req.body;
const user=await prisma.user.findUnique({
where: {id:id}
});
if(user){
const setUser=await prisma.user.update({
where: {id:id},
data:{
name,
program,
university,
location,
institution,
gender
}
});
console.log(setUser)
res.json({ data: setUser }).ae
}
}
)
app.post('/companies/:id/reviews', async (req, res) => {
//find company using its id then, the company object has a review array, push a review object to THAT reviews array
const { id } = req.params;
const { reviewerId, companyId, review_text, rating} = req.body;
try {
const company = await prisma.companies.findUnique({
where: { id: id }
});
console.log(req.body);
//we create a new review object from the incoming data
const newReview=await prisma.reviews.create({
data:{
reviewerId: String(reviewerId),
companyId: companyId,
review: review_text,
rating: rating,
createdAt: new Date()
}
})
res.status(200).send("review received");
//I dont think we need to send the newReview back a reload should update it? prompt a reload maybe?
//adding a review with a companyID should automatically associate it to the company in the database
} catch (error) {
console.error('Failed:', error);
res.status(500).send('Internal server error');
}
});
app.get('/companies/:id',async(req,res) =>{
const {id} =req.params; //api request parameter which is company id in our case
const companies=await prisma.companies.findUnique({
where: {id:id},
include: {reviews: true, salaries:true }
});
if (companies){res.json(companies)}; //send company object to the front end where front end can decode the user info from
// the database
});
app.get('/companies',async(req,res) =>{
try{
const companies = await prisma.companies.findMany();
res.status(200).json(companies);
}
catch (error){
res.status(500).json({error: "Unable to fetch companies"});
}
});
// Create a new Job
// app.post('/jobs', async (req, res) => {
// const { companyImage, companyName, title, description, location, employmentType, workType, internType, jobLink, linkedin, skillsRequired, basicQualifications, preferredQualifications, keyResponsibilities, additionalInfo} = req.body;
// try {
// const newJob = await prisma.job.create({
// data: {
// companyImage,
// companyName,
// title,
// careerPages,
// description,
// location,
// employmentType,
// workType,
// internType,
// jobLink,
// linkedin,
// skillsRequired,
// basicQualifications,
// preferredQualifications,
// keyResponsibilities,
// additionalInfo
// },
// });
// res.status(201).json(newJob);
// } catch (error) {
// console.error(error);
// res.status(500).json({ error: 'Failed to create job' });
// }
// });
// Get all Jobs
app.get('/jobs', async (req, res) => {
try {
const jobs = await prisma.jobs.findMany({
//include: {
//reviews: true,
//salaries: true
// }
});
res.json(jobs);
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Failed to fetch jobs' });
}
});
// Get a specific Job by ID
app.get('/jobs/:id', async (req, res) => {
const { id } = req.params;
try {
const job = await prisma.jobs.findUnique({
where: { id },
include: {
//reviews: true,
//salaries: true
}
});
if (job) {
res.status(200).json(job);
} else {
res.status(404).json({ error: 'Job not found' });
}
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Failed to fetch job' });
}
});
// app.get('/user/logout', (req,res) => {
// req.logout()
// req.session.destroy()
// res.redirect('/')
// })
// Logout route
app.get('/logout', (req, res) => {
req.logout((err) => {
if (err) {
console.error('Error during logout:', err);
return res.redirect('/error');
}
req.session.destroy((err) => {
if (err) {
console.error('Error destroying session:', err);
}
});
res.sendStatus(200);
});
});
// Update a Job by ID
app.put('/jobs/:id', async (req, res) => {
const { id } = req.params;
const { companyImage, companyName, title, description, location, employmentType, workType, internType, jobLink, linkedin, skillsRequired, basicQualifications, preferredQualifications, keyResponsibilities, additionalInfo } = req.body;
try {
const updatedJob = await prisma.job.update({
where: { id },
data: {
companyImage,
companyName,
title,
careerPages,
description,
location,
employmentType,
workType,
internType,
jobLink,
linkedin,
skillsRequired,
basicQualifications,
preferredQualifications,
keyResponsibilities,
additionalInfo
}
});
res.json(updatedJob);
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Failed to update job' });
}
});
// Delete a Job by ID
// app.delete('/jobs/:id', async (req, res) => {
// const { id } = req.params;
// try {
// await prisma.job.delete({
// where: { id }
// });
// res.status(204).send();
// } catch (error) {
// console.error(error);
// res.status(500).json({ error: 'Failed to delete job' });
// }
// });
app.listen (3000, () => {
console.log ('Listening on port 3000');
});