diff --git a/package.json b/package.json index b25406d..9bff123 100644 --- a/package.json +++ b/package.json @@ -22,9 +22,11 @@ "@types/cors": "^2.8.10", "@types/express": "^4.17.11", "@types/node": "^14.14.37", + "@types/uuid": "^8.3.0", "cors": "^2.8.5", "dotenv": "^8.2.0", "express": "^4.17.1", - "typescript": "^4.2.4" + "typescript": "^4.2.4", + "uuid": "^8.3.2" } } diff --git a/src/api.ts b/src/api.ts index b58799b..df07f44 100644 --- a/src/api.ts +++ b/src/api.ts @@ -10,7 +10,22 @@ import { export const router = Router(); -router.get('/user', (req, res) => { +router.get('/user/:userID', (req, res) => { + if (typeof req.query.userID != 'string') { + return; + } + + let userID = req.query.userID; + let user = getUserByID(userID); + + if (user) { + res.json({ status: 'success', data: user }); + } else { + res.json({ status: 'error', error: 'not_found' }); + } +}); + +router.post('/user', (req, res) => { if (typeof req.query.userID != 'string') { return; } @@ -31,6 +46,7 @@ router.post('/user', (req, res) => { } else { users[req.body.userID] = { id: req.body.userID, + username: req.body.username, first_name: req.body.first_name, last_name: req.body.last_name, }; diff --git a/src/data.ts b/src/data.ts index e49acac..d203d7a 100644 --- a/src/data.ts +++ b/src/data.ts @@ -3,7 +3,8 @@ */ export const users: Record = { myfatemi04: { - id: 'myfatemi04', + id: '3baeaed6-05cb-4c03-9b43-1d74beafdbb7', + username: 'myfatemi04', first_name: 'Michael', last_name: 'Fatemi', }, diff --git a/src/index.ts b/src/index.ts index 01a2f2a..6ce018a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,10 +1,16 @@ -import cors = require('cors'); -import express = require('express'); -import * as api from './api'; +import express from 'express'; +import dotenv from 'dotenv'; +import cors from 'cors'; +import { users, groups } from './data'; + +dotenv.config({ + path: '.env', +}); const app = express(); -app.use(cors()); -app.use('/api', api.router); +app.use(cors); -app.listen(80, () => void console.log('Listening on port 80')); +((port = process.env.APP_PORT || 5000) => { + app.listen(port, () => void console.log(`Listening on port ${port}`)); +})(); diff --git a/src/types.ts b/src/types.ts index 04aab7a..ae10ed2 100644 --- a/src/types.ts +++ b/src/types.ts @@ -6,6 +6,7 @@ namespace Carpool { export interface User { id: string; + username: string; first_name: string; last_name: string; } diff --git a/yarn.lock b/yarn.lock index 10556bc..58ffb39 100644 --- a/yarn.lock +++ b/yarn.lock @@ -69,6 +69,11 @@ "@types/mime" "^1" "@types/node" "*" +"@types/uuid@^8.3.0": + version "8.3.0" + resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-8.3.0.tgz#215c231dff736d5ba92410e6d602050cce7e273f" + integrity sha512-eQ9qFW/fhfGJF8WKHGEHZEyVWfZxrT+6CLIJGBcZPfxUh/+BnEj+UCGYMlr9qZuX/2AltsvwrGqp0LhEW8D0zQ== + accepts@~1.3.7: version "1.3.7" resolved "https://registry.yarnpkg.com/accepts/-/accepts-1.3.7.tgz#531bc726517a3b2b41f850021c6cc15eaab507cd" @@ -452,6 +457,11 @@ utils-merge@1.0.1: resolved "https://registry.yarnpkg.com/utils-merge/-/utils-merge-1.0.1.tgz#9f95710f50a267947b2ccc124741c1028427e713" integrity sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM= +uuid@^8.3.2: + version "8.3.2" + resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.2.tgz#80d5b5ced271bb9af6c445f21a1a04c606cefbe2" + integrity sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg== + vary@^1, vary@~1.1.2: version "1.1.2" resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc"