Commit 587d26bd authored by Nawasan Wisitsingkhon's avatar Nawasan Wisitsingkhon

change rank of user by admin

parent 452287af
......@@ -11,6 +11,25 @@ const AdminUserController = {
db.$disconnect();
res.json(userAll);
},
/**
*
* @param {Request} req
* @param {Response} res
*/
async chageRank(req, res) {
try {
const { id, rank } = req.body;
if (!id) throw 300;
await db.user.update({
where: { id: Number(id) },
data: { rank: !!rank },
});
await db.$disconnect();
res.json({ status: 301, message: "update rank success" });
} catch (err) {
res.json({ status: 300, message: "change error on server" });
}
},
/**
*
* @param {Request} req
......@@ -21,6 +40,7 @@ const AdminUserController = {
const { id } = req.query;
if (!id) throw 400;
await db.user.delete({ where: { id: Number(id) } });
await db.$disconnect();
res.json({
status: 401,
message: "delete success",
......
......@@ -6,6 +6,7 @@ const adminRouter = express.Router();
adminRouter.get("/user", AdminUserController.index);
adminRouter.delete("/user", AdminUserController.delete);
adminRouter.put("/user/rank", AdminUserController.chageRank);
adminRouter.post("/product", AdminProductController.create);
adminRouter.put("/product", AdminProductController.update);
......
......@@ -19,12 +19,19 @@ export default function Member() {
const [userAll, setUserAll] = useState([]);
const [deleteState, setDeleteState] = useState({ open: false, id: -1 });
async function changeRank(id, rank) {
let response = await axios.put(
"/api/admin/user/rank",
{ id, rank },
{ headers: { token: user.value.token } }
);
fetchApi();
}
async function fetchApi() {
try {
let response = await axios.get("/api/admin/user", {
headers: { token: user.value.token },
});
console.log(response.data);
setUserAll(response.data);
} catch (err) {}
}
......@@ -74,7 +81,7 @@ export default function Member() {
<TableCell sx={{ textAlign: "center" }}>
<Switch
checked={!!user.rank}
onChange={() => alert("change rank")}
onChange={() => changeRank(user.id, !user.rank)}
/>
</TableCell>
<TableCell sx={{ textAlign: "center" }}>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment