Commit 662b4c70 authored by Choi Ga Young's avatar Choi Ga Young
Browse files

관리자페이지 기능완성

parent 400397ec
......@@ -9,6 +9,7 @@ const usersRouter = require('./routes/users');
const loginRouter = require('./routes/login');
const reservesRouter = require('./routes/reserves');
const noticeRouter = require('./routes/notices');
const connect = require('./schemas');
const app = express();
......@@ -31,11 +32,11 @@ app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/users', usersRouter, reservesRouter);
app.use('/login', loginRouter);
app.use('/reserves', reservesRouter);
app.use('/notices', noticeRouter);
app.use('/admin', usersRouter, reservesRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
......
import React, { useState, useEffect } from 'react';
import Menu from '../Components/Menu';
import axios from 'axios';
import { Link, Redirect } from 'react-router-dom';
import 'bootstrap/dist/css/bootstrap.css';
function ACheck(props) {
const [state, setState] = useState()
const [reserve, setReserve] = useState([]);
useEffect(() => {
getReserve();
}, [])
function getReserve() {
axios.get(`/admin/`, {
// headers: { authorization: localStorage.getItem('token') },
axios.get(`/users/admin/${props.match.params.id}`, {
headers: { authorization: localStorage.getItem('token') },
})
.then(res => {
if (res.status !== 201) {
alert(res.data.error);
if (res.status === 404) {
alert(res.data.error)
setState(true);
}
console.log(res.data);
setReserve(res.data);
......@@ -24,6 +27,8 @@ function ACheck(props) {
alert(err.error)
});
}
if (state) return <Redirect to="/home" />;
function remove(index) {
axios.delete(`/reserves/${reserve[index]._id}`)
.then(res => {
......@@ -37,7 +42,7 @@ function ACheck(props) {
};
function admit(index) {
axios.put(`/reserves/${reserve[index].approve}`)
axios.put(`/reserves/${reserve[index]._id}`)
.then(res => {
if (res.status === 404) return alert(res.data.error)
alert("승인되었습니다!");
......@@ -67,7 +72,7 @@ function ACheck(props) {
{reserve.map((reserve, index) => {
return (
<tr key={index}>
<td>{reserve.user}</td>
<td>{reserve.user.name}</td>
<td>{reserve.date}</td>
<td>{reserve.time}</td>
<td>{reserve.room}</td>
......
......@@ -59,7 +59,7 @@ function Check(props) {
<td>{reserve.starttime}~{(Number(reserve.starttime) + reserve.usetime)}</td>
<td>{reserve.room}</td>
<td>{reserve.num}</td>
<td>{reserve.approve ? "사용허가" : "글쎄..."}</td>
<td>{reserve.approve ? "승인" : "미승인"}</td>
<td>
<button onClick={() => remove(index)} className="btn btn-danger">
취소
......
......@@ -33,7 +33,7 @@ ReactDOM.render(
<Route path="/apply/:id" component={Apply} />
<Route path="/check/:id" component={Check} />
<Route path="/notice" component={Notice} />
<Route path="/acheck" component={ACheck} />
<Route path="/acheck/:id" component={ACheck} />
<Redirect path="/" to="/" />
<Redirect path="/home" to="/" />
<Redirect path="/change/:id" to="/change"/>
......
......@@ -46,8 +46,7 @@ router.get('/:_id', function (req, res, next) {
});
router.get('/admin/:_id', function (req, res, next) {
console.log('/reserves get req.params', req.params)
console.log('/reserves/admin get req.params', req.params)
Reserve.find({ approve: false }).populate('user').exec(function (err, reserve) {
if (err) return res.status(500).json({ error: err });
console.log('reserve list', reserve)
......@@ -74,7 +73,7 @@ router.delete('/:_id', function (req, res, next) {
router.put('/:id', function (req, res, next) {
console.log('/reserves put req.body', req.params)
User.findOne({ _id: req.params.id }, 'password', function (err, reserve) {
Reserve.findOne({ _id: req.params.id }, 'approve', function (err, reserve) {
if (err) return res.status(500).json({ error: err });
reserve.approve = true;
reserve.save()
......
......@@ -42,6 +42,16 @@ router.get('/:_id', function (req, res, next) {
})
});
router.get('/admin/:_id', function (req, res, next) {
console.log('/admin get req.params', req.params)
User.findOne({ _id: req.params._id }, 'role', function (err, user) {
if (err) return res.status(500).json({ error: err });
if (user.role == 'admin') {
next();
}
else return res.status(404).json({ error: '권한이 없습니다.' })
})
});
router.put('/change/:id', function (req, res, next) {
console.log('/change put req.body', req.params)
......
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