-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
198 lines (159 loc) · 6.19 KB
/
Copy pathserver.js
File metadata and controls
198 lines (159 loc) · 6.19 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
require('dotenv').config();
const express = require('express');
const postgres = require('postgres');
const GreetingRequest= require('./Models/GreetingRequest');
const GreetingResponse= require('./Models/GreetingResponse');
const { PGHOST, PGDATABASE, PGUSER, PGPASSWORD, PG_PORT } = process.env;
const sql = postgres({
host: PGHOST,
database: PGDATABASE,
username: PGUSER,
password: PGPASSWORD,
port: PG_PORT,
ssl: 'require',
});
const app = express();
// for parsing JSON
app.use(express.json());
//-----------------------------------
async function getPgVersion() {
const result = await sql`select version()`;
console.log(result[0]);
}
getPgVersion();
//---------------------------------------------
// Initialize PostgreSQL database
(async () => {
try {
// Create the 'Greetings' table if it doesn't exist
await sql`
CREATE TABLE IF NOT EXISTS Greetings (
id SERIAL PRIMARY KEY,
TimeOfDay TEXT NOT NULL,
Language TEXT NOT NULL,
GreetingMessage TEXT NOT NULL,
Tone TEXT NOT NULL
)
`;
// Seed data
const seedData = [
['Morning', 'English', 'Good Morning!', 'Formal'],
['Morning', 'English', 'Hey, Good Morning!', 'Casual'],
['Afternoon', 'English', 'Good Afternoon!', 'Formal'],
['Afternoon', 'English', 'Hey, Good Afternoon!', 'Casual'],
['Evening', 'English', 'Good Evening!', 'Formal'],
['Evening', 'English', 'Hey, Good Evening!', 'Casual'],
['Morning', 'Italian', 'Buongiorno!', 'Formal'],
['Morning', 'Italian', 'Ehi, Buongiorno!', 'Casual'],
['Afternoon', 'Italian', 'Buon pomeriggio!', 'Formal'],
['Afternoon', 'Italian', 'Ehi, Buon pomeriggio!', 'Casual'],
['Evening', 'Italian', 'Buonasera!', 'Formal'],
['Evening', 'Italian', 'Ehi, Buonasera!', 'Casual'],
['Morning', 'Spanish', 'Buenos días!', 'Formal'],
['Morning', 'Spanish', 'Hola, Buenos días!', 'Casual'],
['Afternoon', 'Spanish', 'Buenas tardes!', 'Formal'],
['Afternoon', 'Spanish', 'Hola, Buenas tardes!', 'Casual'],
['Evening', 'Spanish', 'Buenas noches!', 'Formal'],
['Evening', 'Spanish', 'Hola, Buenas noches!', 'Casual'],
];
for (const [TimeOfDay, Language, GreetingMessage, Tone] of seedData) {
const exists = await sql`
SELECT 1
FROM Greetings
WHERE TimeOfDay = ${TimeOfDay} AND Language = ${Language} AND Tone = ${Tone}
`;
// if the record doesn't exist seed it
if (exists.length === 0) {
await sql`
INSERT INTO Greetings (TimeOfDay, Language, GreetingMessage, Tone)
VALUES (${TimeOfDay}, ${Language}, ${GreetingMessage}, ${Tone})
`;
}
}
console.log('Database initialized and seeded.');
} catch (error) {
console.error('Error initializing database:', error);
}
})();
// GreetUser Endpoint
app.post('/api/Greetings/GreetUser', async (req, res) => {
// make the default Tone = Formal if it is empty
if (req.body.Tone.toLowerCase() === 'formal' || !req.body.Tone) {
req.body.Tone="Formal";
}
else if(req.body.Tone.toLowerCase() === 'casual'){
req.body.Tone="Casual";
}
const greetingRequest = new GreetingRequest(req.body.TimeOfDay, req.body.Language, req.body.Tone);
const { TimeOfDay, Language, Tone } = greetingRequest;
if (!TimeOfDay || !Language ||!Tone) {
return res.status(400).json({ error: 'TimeOfDay, Language, and Tone are required' });
}
try {
const result = await sql`
SELECT GreetingMessage
FROM Greetings
WHERE TimeOfDay = ${TimeOfDay} AND Language = ${Language} AND Tone = ${Tone}
`;
if (!result) {
return res.status(404).json({ error: 'Greeting not found' });
}
else{
const greetingResponse = new GreetingResponse(result[0].greetingmessage); // Note that result=[{"greetingmessage": "Good Morning!"}]
res.json({ greetingMessage: greetingResponse.GreetingMessage }); //results should have object key. So we have to use key for the JSON object 'greetingMessage:'
}
} catch (error) {
res.status(500).json({ error: 'Greeting not found' });
}
});
// GetAllTimesOfDay Endpoint
app.get('/api/Greetings/GetAllTimesOfDay', async (req, res) => {
try {
//The result from db.all will be an array of objects. Each object will have a timeOfDay property.
const result = await sql`
SELECT DISTINCT TimeOfDay
FROM Greetings
`;
if (!result) {
return res.status(404).json({ error: 'TimeOfDay not found' });
}
else{
// extract only the 'timeOfDay' values from each object and put them into a new array. (note the Case Sensitivity 'timeofday' not 'TimeOfDay')
const timesOfDay = result.map(row => row.timeofday);
//wrap the timesOfDay array in an object before sending it as the JSON response
res.status(200).json(timesOfDay);
}
} catch (error) {
res.status(500).json({ error: 'Error retrieving times of day' });
}
});
// GetSupportedLanguages Endpoint
app.get('/api/Greetings/GetSupportedLanguages', async (req, res) => {
try {
//The result from db.all will be an array of objects. Each object will have a timeOfDay property.
const result = await sql`
SELECT DISTINCT Language
FROM Greetings
`;
if (!result) {
return res.status(404).json({ error: 'language not found' });
}
else{
// extract only the 'language' values from each object and put them into a new array. (note the Case Sensitivity 'language' not 'Language')
const language = result.map(row => row.language);
//wrap the timesOfDay array in an object before sending it as the JSON response
res.status(200).json(language);
}
} catch (error) {
res.status(500).json({ error: 'Error retrieving supported languages' });
}
});
app.get('/',(req,res)=>{
res.send('Assignment 3 By Eman Abdeen');
});
// Start the server
app.listen(process.env.PG_PORT, () => {
console.log(`Server running on http://localhost:${process.env.PG_PORT}`);
});
// Export the app for Vercel
module.exports = app;