{"size": 3892, "ext": "js", "max_stars_repo_path": "users/users.router.js", "max_stars_repo_name": "BW-Better-Professor-2/back-end", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "users/users.router.js", "max_issues_repo_name": "BW-Better-Professor-2/back-end", "max_issues_repo_issues_event_min_datetime": "2021-05-11T04:48:13.000Z", "max_issues_repo_issues_event_max_datetime": "2021-09-02T07:18:30.000Z", "max_forks_repo_path": "users/users-router.js", "max_forks_repo_name": "bw-better-professor/backend", "max_forks_repo_forks_event_min_datetime": "2020-04-29T22:34:00.000Z", "max_forks_repo_forks_event_max_datetime": "2020-04-29T22:34:00.000Z"}
{"max_stars_count": null, "max_issues_count": 2, "max_forks_count": 1, "avg_line_length": 23.1666666667, "max_line_length": 98, "alphanum_fraction": 0.5611510791}
const express = require('express');

const Users = require('./users-model');

const router = express.Router();

/** 
* @api {get} api/users GET a list of all users
* @apiName getUsers
* @apiGroup Users
*/

router.get('/', (req, res) => {
    Users.getUsers()
    .then(users => {
        res.json(users)
    })
    .catch(err => {
        console.log(err)
        res.status(500).json({errorMessage: "Database failed to get users. Contact your backend"})
    })
 });

 /** 
* @api {get} api/users/:id GET a User by Id
* @apiName getUserById
* @apiGroup Users
* @apiParam {Number} id User id
* @apiSuccess {Number} id User id
* @apiSuccess {String} username User email
* @apiSuccess {String} password User password
* @apiSuccessExample Successful Response
* HTTP/1.1 200 OK
*{
*   "id": 2,
*   "username": "test2@test.com",
*   "password": "pass"
*}
*/

router.get('/:id', (req, res) => {
    const {id} = req.params;

    Users.findUserById(id)
    .then(user => {
        if (user) {
            res.json(user)
        } else {
            res.status(404).json({message: 'There is no user with that id'})
        }
    })
    .catch(err => {
        res.status(500).json({errorMessage: 'Failed to get user. Contact your backend'})
    })
});

 /** 
* @api {put} api/users/:id EDIT a User by Id
* @apiName editUser
* @apiGroup Users
* @apiParam {Number} id User id
* @apiParam {String} username User email is used for username
* @apiParam {String} password user password
*
* @apiSuccessExample Successful Response
* HTTP/1.1 200 OK
*/

router.put('/:id', (req, res) => {
    const {id} = req.params;
    const changes = req.body;

    Users.findUserById(id)
    .then(user => {
        if (user) {
            Users.updateUser(changes, id)
            .then(updatedUser => {
                res.json(updatedUser);
            })
        } else {
            res.status(404).json({message: "No user with that id exists"})
        }
    })
    .catch(err => {
        res.status(500).json({message: "Failed to update user. Contact your backend"})
    })
});

 /** 
* @api {delete} api/users/:id DELETE a User
* @apiName deleteUser
* @apiGroup Users
* @apiParam {Number} id User id
*
* @apiSuccessExample Successful Response
* HTTP/1.1 200 OK
* {
*    "removed": 1
* }
*/

router.delete('/:id', (req, res) => {
    const {id} = req.params;

    Users.removeUser(id)
    .then(deleted => {
        if (deleted) {
            res.json({removed: deleted})
        } else {
            res.status(404).json({message: 'No user with that id exists'})
        }
    })
    .catch(err => {
        res.status(500).json({errorMessage: "Failed to delete user. Contact your backend"})
    })
});

 /** 
* @api {get} api/users/:id/students GET a list of Students belonging to a professor/user
* @apiName getStudentList
* @apiGroup Users
*
* @apiParam {Number} id User id
*
* @apiSuccessExample Successful Response
* HTTP/1.1 200 OK
*[
*    {
*        "studentId": 1,
*        "name": "Calvin Riley",
*        "email": "calvin@gmail.com",
*        "image_url": "https://ibb.co/D517kWp"
*    },
*    {
*        "studentId": 2,
*        "name": "Cindy Lou",
*        "email": "cindy@gmail.com",
*        "image_url": "https://ibb.co/gjnrsxT"
*    },
*    {
*        "studentId": 3,
*        "name": "John Smith",
*        "email": "john@gmail.com",
*        "image_url": "https://ibb.co/Pr9g04c"
*    },
*   {
*        "studentId": 4,
*        "name": "Julian Mills",
*        "email": "julian@gmail.com",
*        "image_url": "https://ibb.co/R6kSgDG"
*    },
* ]
*/

router.get('/:id/students', (req, res) => {
    const {id} = req.params

    Users.getStudentList(id)
    .then(students => {
        res.json(students)
    })
    .catch(err => {
        console.log(err)
        res.status(500).json({errorMessage: "Database failed to get users. Contact your backend"})
    })
 });

module.exports = router;