Skip to content

Commit

Permalink
new types and routes and error handling for api requests
Browse files Browse the repository at this point in the history
  • Loading branch information
balbit committed May 2, 2024
1 parent b18441b commit 8a22ca6
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 22 deletions.
27 changes: 27 additions & 0 deletions common/types/requests.ts
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;
}
73 changes: 55 additions & 18 deletions server/src/controllers/basicController.ts
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' });
}
}
};
4 changes: 4 additions & 0 deletions server/src/database/README.md
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

8 changes: 4 additions & 4 deletions server/src/routes/basicRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import {sendLocation, getLocation, createUser, addFriend} from '../controllers/b

const router = express.Router();

router.post('/createuser', createUser);
router.post('/addfriend', addFriend);
router.post('/sendloc', sendLocation);
router.get('/getloc', getLocation)
router.post('/createUser', createUser);
router.post('/addFriend', addFriend);
router.post('/sendLocoation', sendLocation);
router.get('/getLocation', getLocation)
export default router

0 comments on commit 8a22ca6

Please sign in to comment.