-
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.
Merge branch 'main' into feature/users-routes
- Loading branch information
Showing
6 changed files
with
75 additions
and
8 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
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,9 +1,11 @@ | ||
import express from "express" | ||
|
||
import infoRouter from "./routes/info"; | ||
import usersRouter from "./routes/users"; | ||
|
||
const router = express.Router(); | ||
|
||
router.use("/info", infoRouter); | ||
router.use("/users", usersRouter); | ||
|
||
export default router; |
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,51 @@ | ||
import express from "express"; | ||
|
||
import { getAllUsers, getUserById } from "../../models/User.model"; | ||
import { RowNotFoundError } from "../../utils/errors"; | ||
|
||
const router = express.Router(); | ||
|
||
// Returns user_id, first_name, last_name, signed_in, last_signed_in, total_time for all users | ||
router.get("/users", async (req, res) => { | ||
const users = await getAllUsers(); | ||
users.map((user: any) => {delete user.password}); // Remove password field | ||
|
||
return res.status(200).json({ | ||
description: "Returning all users", | ||
users: users, | ||
}) | ||
}); | ||
|
||
// Return user_id, first_name, last_name, signed_in, last_signed_in, total_time of user with given valid ID | ||
router.get("/users/:id", async (req, res) => { | ||
// Is user ID valid? | ||
if (isNaN(Number(req.params.id))) { | ||
return res.status(404).json({ | ||
description: `User of id: ${req.params.id} not found`, | ||
}); | ||
} | ||
|
||
let user; | ||
|
||
try { | ||
user = await getUserById(Number(req.params.id)); | ||
} catch (err) { | ||
if (err instanceof RowNotFoundError) { | ||
return res.status(404).json({ | ||
description: err.message, | ||
}); | ||
} | ||
|
||
return res.sendStatus(500); | ||
} | ||
|
||
|
||
delete (user as any).password; // Remove password field | ||
|
||
return res.status(200).json({ | ||
description: "User found", | ||
user: user, | ||
}); | ||
}); | ||
|
||
export default router; |
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
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
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