forked from nguyvan/sport-meeting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
217 lines (202 loc) · 6.3 KB
/
index.ts
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
import express from 'express';
import path from 'path';
import log4js from './middleware/log'
import {SportPlaces} from './sport-places-request/sport-places'
import {logMiddleware} from './middleware/log_middleware'
/**
* make a log directory, just in case it isn't there.
*/
try {
require('fs').mkdirSync(path.join(process.cwd(), "_log"));
}
catch (e: any) {
if (e.code != 'EEXIST') {
log.error("Could not set up log directory, error was: ", e);
process.exit(1);
}
}
export const app = express();
app.use(logMiddleware);
import dotenv from 'dotenv';
dotenv.config();
var log = log4js.getLogger("index");
var sport_places_request = new SportPlaces();
app.get('/', (req, res) => res.send('Express + TypeScript Server'));
app.get('/near-me', async (req, res) => {
let long_str = req.query.long
let long: number = 0.0
let lat: number = 0.0
if (long_str !== undefined) {
long = +long_str
}
let lat_str = req.query.lat
if (lat_str !== undefined) {
lat = +lat_str
}
let sport_place_type = req.query.type;
if (sport_place_type == undefined) {
sport_place_type = ""
}
let sport_type = req.query.sport;
let sport_types: string[] = [];
if (sport_type != undefined) {
if (typeof(sport_type) === 'string') {
sport_types.push(sport_type);
}
else if (typeof(sport_type) === 'object'){
sport_types = sport_type.toString().split(',');
}
}
try {
let sport_places = await sport_places_request.getSportPlacesNearMe(long, lat, sport_place_type.toString(), sport_types);
res.status(200);
res.json(sport_places)
}
catch(e: any) {
res.status(400);
res.send("Bad request: " + e)
}
})
app.get('/filter-by-sport-type', async (req, res) => {
let sports = req.query.sport;
let sport_types: string[] = [];
if (sports == undefined) {
res.status(400)
res.send("Cannot find sport places without sport type. Try to find with football for ex");
}
else {
if (typeof(sports) === 'string') {
sport_types.push(sports);
}
else if (typeof(sports) === 'object'){
sport_types = sports.toString().split(',');
}
try {
let sport_places = await sport_places_request.getSportPlacesWithTypeSport(sport_types);
res.status(200);
res.json(sport_places);
}
catch(e: any) {
res.status(400);
res.send("Bad request")
}
}
})
app.get('/filter-by-type-and-id', async (req, res) => {
let sport_type = req.query.type;
let id = req.query.id;
if (sport_type == undefined) {
sport_type = ""
}
if (id == undefined) {
id = ""
}
try {
let sport_places = await sport_places_request.getSportPlaceWithID(sport_type.toString(), id.toString())
res.status(200);
res.json(sport_places)
}
catch(e: any) {
res.status(400);
res.send("Bad request. Error: " + e)
}
})
app.get('/add-new-place', async (req, res) => {
let adress = req.query.adress
let type = req.query.type
let country = req.query.country
let day = req.query.day
let from = req.query.from
let to = req.query.to
let type_sport = req.query.type_sport
let sport_types: string[] = [];
let days: string[] = [];
let open_from: string[] = [];
let closed_at: string[] = []
if (adress == undefined || type == undefined || country == undefined || day == undefined || from == undefined ||
to == undefined || type_sport == undefined) {
res.status(400)
res.send("Missing information to add a new sport place")
}
else {
if (typeof(type_sport) === 'string') {
sport_types.push(type_sport);
}
else if (typeof(type_sport) === 'object'){
sport_types = type_sport.toString().split(',');
}
if (typeof(day) === 'string') {
days.push(day);
}
else if (typeof(day) === 'object') {
days = day.toString().split(',');
}
if (typeof(from) === 'string') {
open_from.push(from);
}
else if (typeof(from) === 'object') {
open_from = from.toString().split(',');
}
if (typeof(to) === 'string') {
closed_at.push(to);
}
else if (typeof(to) === 'object') {
closed_at = to.toString().split(',');
}
if (type.toString() == "field" && sport_types.length > 1) {
res.status(400)
res.send("Field cannot have too much sport, only one sport is accepted in one field")
}
try {
let response = await sport_places_request.addNewSportPlace(adress.toString(), type.toString(),
country.toString(), days, open_from, closed_at, sport_types);
res.status(200)
res.send(response)
}
catch(e: any) {
res.status(400);
res.send("Bad request. Error: " + e)
}
}
})
app.get('/add-new-player', async(req,res) => {
let adress = req.query.adress
let sport = req.query.sport
if (adress == undefined || sport == undefined) {
res.status(400)
res.send("Missing information to add a new player")
}
else {
try {
let response = await sport_places_request.addNewPlayer(adress.toString(), sport.toString());
res.status(200)
res.send(response)
}
catch(e: any) {
res.status(400);
res.send("Bad request. Error: " + e)
}
}
})
app.get('/remove-player', async(req,res) => {
let adress = req.query.adress
let sport = req.query.sport
if (adress == undefined || sport == undefined) {
res.status(400)
res.send("Missing information to add a new player")
}
else {
try {
let response = await sport_places_request.deletePlayer(adress.toString(), sport.toString());
res.status(200)
res.send(response)
}
catch(e: any) {
res.status(400);
res.send("Bad request. Error: " + e)
}
}
})
app.listen(process.env.PORT, () => {
log.info(`⚡️[server]: Server is running at http://localhost:${process.env.PORT}`);
});