---
# System prepended metadata

title: 'COMP1531 - Tut05: H16A'

---

```typescript=1

import express, { json, Request, Response } from 'express';
import cors from 'cors';
import morgan from 'morgan';
import { port, url } from './config.json';
import {
  clear,
  peopleAdd,
  personView,
  personEdit,
  peopleList,
  personRemove,
  peopleStats,
} from './people';

const app = express();

// Use middleware that allows for access from other domains (needed for frontend to connect)
app.use(cors());
// Use middleware that allows us to access the JSON body of requests
app.use(json());
// Use middleware to log (print to terminal) incoming HTTP requests (OPTIONAL)
app.use(morgan('dev'));

app.get('/', (req: Request, res: Response) => {
  res.json({ message: 'Welcome to the root URL of names ages!' });
});

// TODO: Clear
    // 1. HTTP Method (POST/GET/PUT/DELETE) = delete 
    // 2. Input Parameter Type (Query/Body/Param) = query
app.delete('/clear', (req: Request, res: Response) => {
    res.json(clear());
})


// TODO: Add a person
    // 1. HTTP Method (POST/GET/PUT/DELETE) = ???
    // 2. Input Parameter Type (Query/Body/Param) = ???
app.post('/people/add', (req: Request, res: Response) => {
    const {name, age} = req.body;
    // const name = req.body.name;
    // const age = req.body.age;

    const result = peopleAdd(name, age);
    if ('error' in result) {
        return res.status(400).json(result);
    }
    res.json(result);
})


// TODO GROUP AERO: List all people
    // 1. HTTP Method (POST/GET/PUT/DELETE) = ???
    // 2. Input Parameter Type (Query/Body/Param) = ???
app.get('/people', req: Request, res: Response) => {
    const minAge = req.query;
    if (parseInt(minAge) < 0) {
        return res.status(400).json({error})
    }
    
    const result = peopleList(parseInt(minAge));
    if ('error' in result) {
        return res.status(400).json(result);
    }
    
    res.json(result);
}


// TODO GROUP BOOST: Read a person's details
    // 1. HTTP Method (POST/GET/PUT/DELETE) = ???
    // 2. Input Parameter Type (Query/Body/Param) = ???
app.get('/person/:peopleid', (req: Request, res: Response)) => {
    
    const id = req.params.peopleid;
    
    const result = personView(id);
    
    if ('error' in result) {
        return res.status(400).json(result);
    }
    res.json(result);
    
}

    



// TODO GROUP CRUNCHIE: Update a person
    // 1. HTTP Method (POST/GET/PUT/DELETE) = ???
    // 2. Input Parameter Type (Query/Body/Param) = ???
app.put('/person/:personId', (req:Request, res:Response)) => {
    const {name, age} = req.body;
    const personId = req.params.personId;
    
    cosnt result = personEdit(personId, name, age);
    
    if('error' in result){
        return res.status(400).json(result);
    }
    
    res.json(result);
});



// TODO GROUP DREAM: Remove a person
    // 1. HTTP Method (POST/GET/PUT/DELETE) = ???
    // 2. Input Parameter Type (Query/Body/Param) = ???




// TODO GROUP EGGS: Get stats
    // 1. HTTP Method (POST/GET/PUT/DELETE) = ???
    // 2. Input Parameter Type (Query/Body/Param) = ???
app.get('/person/:peopleid', (req: Request, res: Response) => {
    // Extract stats from request parameters
    
    // is this needed?
    // const minAge = req.params.minAge;
    // const maxAge = req.params.maxAge;
    // const averageAge = req.params.averageAge;
    
    
    const result = peopleStats();

    // check for error
    if ('error' in result) {
        return res.status(400).json(result);
    }
    
    // Send the result as response
    res.json(result);  
    
});




app.listen(port, () => {
  console.log(`Express Server started and awaiting requests at the URL: '${url}:${port}'`);
});


```

