Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
115 lines (108 sloc) 3.08 KB
import {NextFunction, Request, Response} from "express";
import * as mysql from 'promise-mysql';
var express = require('express');
var router = express.Router();
import {Mysql} from "../include/Mysql"
let myDb = new Mysql()
/* GET */
router.get('/', function(req: Request, res: Response, next: NextFunction) {
res.send('respond with a resource-1234');
});
/********************************
*
*********************************/
router.get('/tasks_index', function(req: Request, res: Response, next: NextFunction ) {
let connection :mysql.Connection;
myDb.get_connection()
.then((conn) => {
connection = conn;
return connection.query('SELECT * FROM tasks order by id desc');
}).then((results: any[]) => {
// console.log(results );
results.forEach(result => {
console.log(result.id );
});
connection.end();
var param = {"docs": results };
res.json(param);
});
});
/********************************
*
*********************************/
router.post('/tasks_new', (req: Request, res: Response, next: NextFunction) => {
let data = req.body
let sql = `
INSERT INTO tasks (title , content, createdAt ) VALUES
('${ data.title }',
'${ data.content }',
now()
)
`;
let connection :mysql.Connection;
myDb.get_connection()
.then((conn) => {
connection = conn;
connection.query( sql )
connection.end();
res.json(data);
});
});
/********************************
*
*********************************/
router.get('/tasks_show/:id', function(req: Request, res: Response, next: NextFunction) {
let sql = `
SELECT * FROM tasks where id=${ req.params.id }
`;
let connection :mysql.Connection;
myDb.get_connection()
.then((conn) => {
connection = conn;
return connection.query(sql);
}).then((results: any[]) => {
// console.log(results );
results.forEach(result => {
console.log(result.id );
});
connection.end();
var param = {"docs": results };
res.json(param);
});
});
/********************************
*
*********************************/
router.post('/tasks_update', (req: Request, res: Response, next: NextFunction) => {
let data = req.body
let sql = `
update tasks set title = '${ data.title }'
, content = '${ data.content }'
where id = ${data.id}
`;
let connection :mysql.Connection;
myDb.get_connection()
.then((conn) => {
connection = conn;
connection.query( sql )
connection.end();
res.json(data);
});
});
/********************************
*
*********************************/
router.get('/tasks_delete/:id', function(req: Request, res: Response, next: NextFunction) {
let sql = `
delete from tasks where id = ${req.params.id};
`;
let connection :mysql.Connection;
myDb.get_connection()
.then((conn) => {
connection = conn;
connection.query( sql )
connection.end();
res.json(req.params.id);
});
});
export {router as apiRouter}
You can’t perform that action at this time.