Commit 19f69801 authored by Geovera's avatar Geovera

Unit routing cleanup

parent 827cd3d9
...@@ -4,23 +4,22 @@ const units = {}; ...@@ -4,23 +4,22 @@ const units = {};
const unit_columns = ['name', 'type', 'stars', 'hp', 'pap', 'pd', 'sap', 'sd', 'bap', 'bp', 'pdf', 'sdf', 'bdf', 'leadership', 'troop_count', 'hero_level']; const unit_columns = ['name', 'type', 'stars', 'hp', 'pap', 'pd', 'sap', 'sd', 'bap', 'bp', 'pdf', 'sdf', 'bdf', 'leadership', 'troop_count', 'hero_level'];
units.getAll = async (context) =>{ units.getAll = async (context, next) =>{
let sql_text = 'SELECT * FROM units'; let sql_text = 'SELECT * FROM units';
try{ try{
let data = await db.con.query(sql_text); let data = await db.con.query(sql_text);
return {units: data}; context.response.body = {units: data};
}catch(error){ }catch(error){
console.log(error); console.log(error);
context.throw(400, 'INVALID_DATA'); context.throw(400, 'INVALID_DATA');
} }
} }
units.getUnit = async (context, id) =>{ units.getUnit = async (context, next) =>{
let sql_text = `SELECT * FROM units WHERE id=${context.params.id}`;
let sql_text = `SELECT * FROM units WHERE id=${id}`;
try{ try{
let data = await db.con.query(sql_text); let data = await db.con.query(sql_text);
return {unit: data}; context.response.body = {unit: data};
}catch(error){ }catch(error){
console.log(error); console.log(error);
context.throw(400, 'INVALID_DATA'); context.throw(400, 'INVALID_DATA');
...@@ -28,12 +27,13 @@ units.getUnit = async (context, id) =>{ ...@@ -28,12 +27,13 @@ units.getUnit = async (context, id) =>{
} }
units.insertUnit = async (context, body) =>{ units.insertUnit = async (context, next) =>{
let body = context.request.body;
let column_text = 'name, type'; let column_text = 'name, type';
let value_text = `'${body.name}', '${body.type}'`; let value_text = `'${body.name}', '${body.type}'`;
for (let i = 2; i < unit_columns.length; i++) { for (let i = 2; i < unit_columns.length; i++) {
const element = unit_columns[i]; const element = unit_columns[i];
if(body[element]){ if(body[element]!==undefined){
column_text += ', ' + element; column_text += ', ' + element;
value_text += ', ' +body[element]; value_text += ', ' +body[element];
} }
...@@ -42,7 +42,7 @@ units.insertUnit = async (context, body) =>{ ...@@ -42,7 +42,7 @@ units.insertUnit = async (context, body) =>{
try{ try{
let data = await db.con.query(sql_query); let data = await db.con.query(sql_query);
return {status: 'success'}; context.response.body = {status: 'success'};
}catch(error){ }catch(error){
console.log(error); console.log(error);
context.throw(400, 'INVALID_DATA'); context.throw(400, 'INVALID_DATA');
...@@ -50,10 +50,11 @@ units.insertUnit = async (context, body) =>{ ...@@ -50,10 +50,11 @@ units.insertUnit = async (context, body) =>{
} }
units.modifyUnit = async (context, body) => { units.modifyUnit = async (context, next) => {
let body = context.request.body;
let set_text = ''; let set_text = '';
if(body.name && body.type){ if(body.name && body.type){
set_text += `name = '${body.name}' type = '${body.type}'`; set_text += `name = '${body.name}', type = '${body.type}'`;
}else if(body.name){ }else if(body.name){
set_text += `name = '${body.name}'`; set_text += `name = '${body.name}'`;
}else if(body.type){ }else if(body.type){
...@@ -61,17 +62,19 @@ units.modifyUnit = async (context, body) => { ...@@ -61,17 +62,19 @@ units.modifyUnit = async (context, body) => {
} }
for (let i = 2; i < unit_columns.length; i++) { for (let i = 2; i < unit_columns.length; i++) {
const element = unit_columns[i]; const element = unit_columns[i];
if(body[element]!==undefined){
if(set_text===''){ if(set_text===''){
set_text = `${element} = ${body[element]}`; set_text += `${element} = ${body[element]}`;
}else if(body[element]){ }else{
set_text = `, ${element} = ${body[element]}`; set_text += `, ${element} = ${body[element]}`;
}
} }
} }
let sql_query = `UPDATE units SET ${set_text} WHERE id = ${body.id}`; let sql_query = `UPDATE units SET ${set_text} WHERE id = ${body.id}`;
try{ try{
let data = await db.con.query(sql_query); let data = await db.con.query(sql_query);
return {status: 'success'}; context.response.body = {status: 'success'};
}catch(error){ }catch(error){
console.log(error); console.log(error);
context.throw(400, 'INVALID_DATA'); context.throw(400, 'INVALID_DATA');
......
...@@ -3,28 +3,13 @@ const Router = require('@koa/router'); ...@@ -3,28 +3,13 @@ const Router = require('@koa/router');
const router = new Router(); const router = new Router();
const unitsModel = require('../models/units'); const unitsModel = require('../models/units');
router.get('/unit/all', async (context, next) => { router.get('/unit/all', unitsModel.getAll);
let data = await unitsModel.getAll(context); router.get('/unit/:id', unitsModel.getUnit);
context.response.body = data;
}); router.post('/unit/insert', unitsModel.insertUnit);
router.get('/unit/:id', async (context, next) => {
let data = await unitsModel.getUnit(context, context.params.id); router.post('/unit/modify', unitsModel.modifyUnit);
context.response.body = data;
});
router.post('/unit/insert', async (context, next)=>{
let data = await unitsModel.insertUnit(context, context.request.body);
context.response.body = data;
});
router.post('/unit/modify', async (context, next)=>{
let data = await unitsModel.modifyUnit(context, context.request.body);
context.response.body = data;
});
module.exports = router; module.exports = router;
\ No newline at end of file
...@@ -4,6 +4,8 @@ const Router = require('@koa/router'); ...@@ -4,6 +4,8 @@ const Router = require('@koa/router');
const router = new Router(); const router = new Router();
const usersModel = require('../models/users'); const usersModel = require('../models/users');
router.get('/user/:id', (context, next)=>{
})
module.exports = router; module.exports = router;
\ No newline at end of file
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