-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new types and routes and error handling for api requests
- Loading branch information
Showing
4 changed files
with
90 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { userID, LocationInfo } from './types'; | ||
|
||
export interface CreateUserRequest { | ||
name: string; | ||
} | ||
|
||
export interface AddFriendRequest { | ||
userID: userID; | ||
friend: userID; | ||
} | ||
|
||
export interface SendLocationRequest { | ||
userID: userID; | ||
locationInfo: LocationInfo; | ||
} | ||
|
||
export interface GetLocationRequest { | ||
userID: userID; | ||
} | ||
|
||
export interface GetLocationResponse { | ||
locations: LocationInfo[]; | ||
} | ||
|
||
export interface ErrorResponse { | ||
message: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,63 @@ | ||
import { Request, Response } from 'express'; | ||
// import database from '../database/Database'; | ||
import { CreateUserRequest, AddFriendRequest, SendLocationRequest, GetLocationRequest, GetLocationResponse, ErrorResponse } from '@/../../common/types/requests'; | ||
|
||
import database from '../database/Database'; | ||
|
||
export const createUser = async (req: Request, res: Response) => { | ||
let id = await database.add_user(req.body.name); | ||
res.send({ "id": id }); | ||
} | ||
export const createUser = async (req: Request<{}, {}, CreateUserRequest>, res: Response) => { | ||
try { | ||
const id = await database.addUser(req.body.name); | ||
res.send({ id }); | ||
} catch (error) { | ||
console.error('Error creating user:', error); | ||
res.status(500).send('Internal server error'); | ||
} | ||
}; | ||
|
||
export const addFriend = async (req: Request, res: Response) => { | ||
console.log(`ADD_FRIEND: ${JSON.stringify(req.body)}`); | ||
await database.make_friends(req.body.uuid, req.body.friend); | ||
res.sendStatus(200); | ||
} | ||
export const addFriend = async (req: Request<{}, {}, AddFriendRequest>, res: Response) => { | ||
try { | ||
console.log(`ADD_FRIEND: ${JSON.stringify(req.body)}`); | ||
await database.makeFriends(req.body.userID, req.body.friend); | ||
res.sendStatus(200); | ||
} catch (error) { | ||
console.error('Error adding friend:', error); | ||
if (error instanceof ReferenceError) { | ||
res.status(404).send('User not found'); | ||
} else { | ||
res.status(500).send('Internal server error'); | ||
} | ||
} | ||
}; | ||
|
||
export const sendLocation = async (req: Request, res: Response) => { | ||
console.log(`SEND_LOCATION: ${JSON.stringify(req.body)}`); | ||
await database.add_location(req.body.uuid, req.body.timestamp, req.body.location); | ||
res.sendStatus(200); | ||
} | ||
export const sendLocation = async (req: Request<{}, {}, SendLocationRequest>, res: Response) => { | ||
try { | ||
console.log(`SEND_LOCATION: ${JSON.stringify(req.body)}`); | ||
await database.addLocation(req.body.userID, req.body.locationInfo); | ||
res.sendStatus(200); | ||
} catch (error) { | ||
console.error('Error sending location:', error); | ||
if (error instanceof ReferenceError) { | ||
res.status(404).send('User not found'); | ||
} else { | ||
res.status(500).send('Internal server error'); | ||
} | ||
} | ||
}; | ||
|
||
export const getLocation = async (req: Request, res: Response) => { | ||
console.log(`GET_LOCATION: ${JSON.stringify(req.query)}`); | ||
res.send(await database.get_friends_current_location(req.query.uuid as string)) | ||
} | ||
export const getLocation = async (req: Request<GetLocationRequest>, res: Response<GetLocationResponse | ErrorResponse>) => { | ||
try { | ||
console.log(`GET_LOCATION: ${JSON.stringify(req.query)}`); | ||
if (typeof req.query.userID !== 'string' || req.query.userID === '') { | ||
throw new ReferenceError("Invalid user ID"); | ||
} | ||
const locations = await database.getFriendsCurrentLocation(req.query.userID); | ||
res.status(200).send({ locations }); | ||
} catch (error) { | ||
console.error('Error getting location:', error); | ||
if (error instanceof ReferenceError) { | ||
res.status(404).send({ message: 'User not found' }); | ||
} else { | ||
res.status(500).send({ message: 'Internal server error' }); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Database Schema | ||
|
||
Currently the database is in test mode, so locations are unhashed | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters