admin.js 3.67 KB
var express = require('express');
var router = express.Router();
var mysql = require('mysql');
var conf = require('../conf/db');
var pool = mysql.createPool(conf.local)
var moment = require('moment');
require('moment-timezone');

router.post('/attend', function(req, res, next) {
	var request_id = req.body.request_id;
	console.log(request_id)
	var lecture_id = req.body.lecture_id;
	console.log(lecture_id)
	var student_id = req.body.student_id;
	console.log(student_id);
	var student_name = req.body.student_name;
	console.log(student_name)
	var status = req.body.status;
	console.log(status)

	moment.tz.setDefault("Asia/Seoul");
	var now = moment().format('YYYY-MM-DD HH:mm:ss');

	pool.getConnection(function(err, conn){
		conn.query('INSERT INTO student_attendance(lecture_id,student_id,status,timestamp)VALUES(?,?,?,?)', [lecture_id, student_id, status, now], function (err, rows){
			if (err){
				console.log(err);
				return res.status(500).json({error: err});
			}
			if (!rows) return res.status(400).json({error: 'not found'});
			var msg = {
				status: 'success',
				err: '',
				result: rows,
				length: rows.length
			};
			res.json(msg);
		});
		conn.release();
		
	});

	console.log("ddd")
});

router.post('/solve', function(req, res, next) {
	var request_id = req.body.request_id;
	console.log(request_id)
	var lecture_id = req.body.lecture_id;
	console.log(lecture_id)
	var student_id = req.body.student_id;
	console.log(student_id);
	var student_name = req.body.student_name;
	console.log(student_name)
	var status = req.body.status;
	console.log(status)

	moment.tz.setDefault("Asia/Seoul");
	var now = moment().format('YYYY-MM-DD HH:mm:ss');

	pool.getConnection(function(err, conn){
		conn.query('Update student_request Set status=? where id=?', [status, request_id], function (err, rows){
			if (err){
				console.log(err);
				return res.status(500).json({error: err});
			}
			if (!rows) return res.status(400).json({error: 'not found'});
			var msg = {
				status: 'success',
				err: '',
				result: rows,
				length: rows.length
			};
			res.json(msg);
		});
		conn.release();
	});
});

router.get('/:lecture_id', function(req, res, next) {
	var lecture_id = req.params.lecture_id;

	pool.getConnection(function(err, conn){
	conn.query('select r.id, r.student_id AS student_id, r.student_name AS student_name, r.status AS status, CONVERT_TZ(r.timestamp, "+00:00", "+09:00") AS timestamp FROM student_request r WHERE (r.lecture_id=?) AND (DATE(timestamp) = CURDATE()) ORDER BY timestamp DESC;', [lecture_id], function (err, rows){
		if (err){
			console.log(err);
			return res.status(500).json({error: err});
		}
		if (!rows) return res.status(400).json({error: 'not found'});
		var msg = {
			status: 'success',
			err: '',
			result: rows,
			length: rows.length
		};
		res.json(msg);
		});
	conn.release();	
	});
});

router.post('/request/:lecture_id', function(req, res, next) {
	var lecture_id = req.params.lecture_id;
	console.log(lecture_id)
	var student_id = req.body.student_id;
	console.log(student_id);
	var student_name = req.body.student_name;
	console.log(student_name)
	moment.tz.setDefault("Asia/Seoul");
	var now = moment().format('YYYY-MM-DD HH:mm:ss');

	pool.getConnection(function(err, conn){
		conn.query('INSERT INTO student_request(lecture_id,student_id,student_name,status,timestamp)VALUES(?,?,?,?,?)', [lecture_id, student_id, student_name, "pending", now], function (err, rows){
		if (err){
			console.log(err);
			return res.status(500).json({error: err});
		}
		if (!rows) return res.status(400).json({error: 'not found'});
		var msg = {
			status: 'success',
			err: '',
			result: rows,
			length: rows.length
		};
		res.redirect('/');
		});
	conn.release();	
	});
});



module.exports = router;