This repository has been archived by the owner on Feb 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrouter.js
111 lines (103 loc) · 2.8 KB
/
router.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const express = require("express");
const accounts = require("./database");
const route = express.Router();
/**
* @route GET /api/accounts
* @desc Get all accounts
* @access Public
* @returns {object} 200 - An array of accounts
* @returns {Error} 500 - Unexpected error
*/
route.get("/accounts", (req, res) => {
try {
res.json({ userData: accounts });
} catch (error) {
res.status(500).send(error);
}
});
/**
* @route POST /api/accounts
* @desc Create a new account
* @access Public
* @returns {object} 201 - All accounts
* @returns {Error} 500 - Unexpected error
*/
route.post("/accounts", (req, res) => {
try {
const newAccount = req.body;
accounts.push(newAccount);
res.json({ userData: accounts });
} catch (error) {
res.status(500).send(error);
}
});
/**
* @route Get /api/accounts/:id
* @desc Get a single account
* @access Public
* @returns {object} 200 - An account
* @returns {Error} 500 - Unexpected error
* @returns {Error} 404 - Account not found
*/
route.get("/accounts/:id", (req, res) => {
try {
const accountId = +req.params.id;
const getAccount = accounts.find((account) => account.id === accountId);
if (getAccount) {
res.json({ userData: getAccount });
} else {
res.status(404).send("Account not found");
}
} catch (error) {
res.status(500).send(error);
}
});
/**
* @route PUT /api/accounts/:id
* @desc Update an account
* @access Public
* @returns {object} 200 - All accounts
* @returns {Error} 500 - Unexpected error
* @returns {Error} 404 - Account not found
*/
route.put("/accounts/:id", (req, res) => {
try {
const accountId = +req.params.id;
const getAccount = accounts.find((account) => account.id === accountId);
if (getAccount) {
const { username, email, gender, status } = req.body;
if (username) getAccount.username = username;
if (email) getAccount.email = email;
if (gender) getAccount.gender = gender;
if (status) getAccount.status = status;
res.json({ userData: accounts });
} else {
res.status(404).send("Account not found");
}
} catch (error) {
res.status(500).send(error);
}
});
/**
* @route DELETE /api/accounts/:id
* @desc Delete an account
* @access Public
* @returns {object} 200 - All accounts
* @returns {Error} 500 - Unexpected error
* @returns {Error} 404 - Account not found
*/
route.delete("/accounts/:id", (req, res) => {
try {
const accountId = +req.params.id;
const getAccount = accounts.find((account) => account.id === accountId);
if (getAccount) {
accounts.splice(accounts.indexOf(getAccount), 1);
res.json({ userData: accounts });
} else {
res.status(404).send("Account not found");
}
} catch (error) {
res.status(500).send(error);
}
});
module.exports = route;