Commit 3fa9e1fd authored by Geovanny's avatar Geovanny

Added house api

parent 6a557536
const db = require('../database/database');
const model = {};
const h_columns = ['house_name', 'house_level', 'camp_location'];
model.getAll = async () => {
const sql_text = `SELECT h.*, u.username as liege_username
FROM houses as h
LEFT JOIN users as u on h.liege_id = u.id;`;
const data = await db.con.query(sql_text);
return data;
}
model.getHouse = async() => {
// implement getting house details
throw Error('Not Implemented');
}
model.insertHouse = async(body, liege_id) => {
let column_text = 'liege_id';
let value_text = `${db.con.escape(liege_id)}`
if(body){
for (let i = 0; i < h_columns.length; i++) {
const element = h_columns[i];
if(body[element]!==undefined){
column_text += ', ' + element;
value_text += ', ' + db.con.escape(body[element]);
}
}
}
const sql_text = `INSERT INTO houses (${column_text}) VALUES (${value_text});`
const sql_text2 = `SELECT @house_id:=h.id
FROM houses as h
WHERE h.liege_id = ? LIMIT 1; `
const sql_text3 = `UPDATE users
SET house_id = @house_id, lk_house_role = 'lg'
WHERE id = ?;`;
const aaa = await db.con.query('START TRANSACTION');
await db.con.query(sql_text);
await db.con.query(sql_text2, [liege_id])
await db.con.query(sql_text3, [liege_id]);
await db.con.query('COMMIT');
}
model.modifyHouse = async(house_id, body) => {
let set_text = '';
for (let i = 0; i < h_columns.length; i++) {
const element = h_columns[i];
if(body[element]!==undefined){
if(set_text===''){
set_text += `${element} = ${db.con.escape(body[element])}`;
}else{
set_text += `, ${element} = ${db.con.escape(body[element])}`;
}
}
}
if(set_text===''){
throw Error('No Params to Update')
}
const sql_text = `UPDATE houses SET ${set_text} WHERE id = ?`
await db.con.query(sql_text, [house_id]);
};
model.deleteHouse = async(house_id, liege_id) => {
const sql_text = 'DELETE FROM houses WHERE id = ?;';
const sql_text2 = 'UPDATE users SET lk_house_role = NULL where id = ?;'
await db.con.query('START TRANSACTION;');
await db.con.query(sql_text, [house_id]);
await db.con.query(sql_text2, [liege_id]);
await db.con.query('COMMIT;')
}
model.sendRequest = async(house_id, user_id) => {
const sql_text = 'DELETE FROM house_requests WHERE user_id = ?';
const sql_text2 = 'INSERT INTO house_requests (house_id, user_id) VALUES (?, ?)';
await db.con.query('START TRANSACTION;');
await db.con.query(sql_text, [user_id]);
await db.con.query(sql_text2, [house_id, user_id]);
await db.con.query('COMMIT;');
};
model.cancelRequests = async(user_id) => {
const sql_text = 'DELETE FROM house_requests WHERE user_id = ?';
await db.con.query(sql_text, [user_id]);
}
model.getHouseRequests = async(house_id) => {
const sql_text = `SELECT u.id, u.username
FROM house_requests as hr
LEFT JOIN users as u ON u.id = hr.user_id
WHERE hr.house_id = ?;`
const data = await db.con.query(sql_text, [house_id]);
return data;
}
model.acceptRequest = async(user_id, house_id) => {
const sql_text = 'DELETE FROM house_requests WHERE user_id = ?;';
const sql_text2 = 'UPDATE users SET house_id = ?, lk_house_role = \'kng\' WHERE id = ?;';
await db.con.query('START TRANSACTION;');
await db.con.query(sql_text, [user_id]);
await db.con.query(sql_text2, [house_id, user_id]);
await db.con.query('COMMIT;');
}
model.refuseRequest = async(user_id) => {
const sql_text = 'DELETE FROM house_requests WHERE user_id = ?;';
await db.con.query(sql_text, [user_id]);
}
model.deleteMember = async(user_id) => {
const sql_text = 'UPDATE users SET house_id = NULL, lk_house_role = NULL WHERE id = ? AND lk_house_role != \'lg\';';
await db.con.query(sql_text, [user_id]);
}
model.leaveHouse = async(user_id) => {
const sql_text = 'UPDATE users SET house_id = NULL, lk_house_role = NULL WHERE id = ?;';
await db.con.query(sql_text, [user_id]);
}
module.exports = model;
\ No newline at end of file
const Koa = require('koa');
const Router = require('@koa/router');
const router = new Router();
const authRouter = new Router();
const houseModel = require('./model');
const HOUSE_ROLES = {
LIEGE: 0,
SEN: 1,
MAR: 2,
NOB: 3,
TRE: 4,
KNG: 5
}
function checkPermissions(context, ROLE){
if(ROLE < context.user.lk_permission_level){
context.throw(403, "No Permissions")
}
}
function checkHouse(context){
const house_id = context.user.house_id ? context.user.house_id : context.request.body.house_id;
if(!house_id || house_id!==context.user.house_id){
context.throw(403, "Not your House");
}
}
router.get('/all', async (context, next) => {
try{
const data = await houseModel.getAll();
context.response.status = 200;
context.response.body = data;
}catch(error){
console.log(error);
context.throw(400, 'Unable to get houses');
}
});
authRouter.post('/request', async (context, next) => {
if(context.user.house_id){
context.throw(400, 'Already in a house')
}
try{
const body = context.request.body;
if(!body || !body.house_id){
throw Error('No house ID');
}
await houseModel.sendRequest(body.house_id, context.session.user_id);
context.response.status = 204;
}catch(error){
console.log(error);
context.throw(400, 'Failed to send request');
}
});
authRouter.delete('/request', async (context, next) => {
if(context.user.house_id){
context.throw(400, 'Already in a house')
}
try{
await houseModel.cancelRequests(context.session.user_id);
context.response.status = 204;
}catch(error){
console.log(error);
context.throw(400, 'Failed to send request');
}
});
authRouter.get('/requests/:house_id', async (context, next) => {
checkHouse(context);
checkPermissions(context, HOUSE_ROLES.SEN);
try{
const data = await houseModel.getHouseRequests(context.user.house_id);
context.response.status = 200;
context.response.body = data;
}catch(error){
console.log(error);
context.throw(400, 'Unable to get requests');
}
});
authRouter.post('/accept-request', async (context, next) => {
checkHouse(context);
checkPermissions(context, HOUSE_ROLES.SEN);
try{
const body = context.request.body;
if(!body || !body.user_id){
throw Error("No user to accept");
}
await houseModel.acceptRequest(body.user_id, context.user.house_id);
context.response.status = 204;
}catch(error){
console.log(error);
context.throw(400, 'Unable to Accept Request')
}
});
authRouter.delete('/refuse-request', async (context, next) => {
checkHouse(context);
checkPermissions(context, HOUSE_ROLES.SEN);
try{
const body = context.request.body;
if(!body || !body.user_id){
throw Error("No user to refuse");
}
await houseModel.refuseRequest(body.user_id);
context.response.status = 204;
}catch(error){
console.log(error);
context.throw(400, 'Unable to Accept Request')
}
});
authRouter.delete('/delete-member/:user_id', async (context, next) => {
checkPermissions(context, HOUSE_ROLES.SEN);
if(context.params.user_id===context.session.user_id){
context.throw(400, "Can't delete yourself");
}
try{
await houseModel.deleteMember(context.params.user_id);
context.response.status = 204;
}catch(error){
console.log(error);
context.throw(400, 'Unable to Delete Member')
}
});
authRouter.delete('/leave-house', async (context, next) => {
if(context.user.lk_house_role==='lg'){
try{
console.log('asd')
await houseModel.deleteHouse(context.user.house_id, context.session.user_id);
context.response.status = 204;
}catch(error){
console.log(error);
context.throw(400, 'Unable to Delete House');
}
}else{
try{
await houseModel.leaveHouse(context.session.user_id);
context.response.status = 204;
}catch(error){
console.log(error);
context.throw(400, 'Unable to Leave');
}
}
});
router.get('/', async (context, next) => {
try{
const data = await houseModel.getHouse();
context.response.status = 200;
context.response.body = data;
}catch(error){
console.log(error);
context.throw(400, 'Unable to get house');
}
});
authRouter.post('/', async (context, next) => {
try{
if(context.user.house_id!==null){
context.throw(400, "Can't create house when belonging to one")
}
const body = context.request.body;
if(!body || !body.house_name){
throw Error('No house_name')
}
await houseModel.insertHouse(body, context.session.user_id)
context.response.status = 204;
}catch(error){
console.log(error);
context.throw(422, "Couldn't Insert House")
}
});
authRouter.put('/:house_id', async (context, next) => {
checkHouse(context);
checkPermissions(context, HOUSE_ROLES.LIEGE)
try{
const body = context.request.body;
if(!body){
throw Error('No params')
}
await houseModel.modifyHouse(context.user.house_id, body);
context.response.status = 204;
}catch(error){
console.log(error);
context.throw(400, "ERROR")
}
});
authRouter.delete('/:house_id', async (context, next) => {
checkHouse(context);
checkPermissions(context, HOUSE_ROLES.LIEGE);
try{
await houseModel.deleteHouse(context.params.house_id, context.user.id);
context.response.status = 204;
}catch(error){
console.log(error);
context.throw(400, "Unable to Delete");
}
});
module.exports = [router, authRouter];
\ No newline at end of file
......@@ -9,6 +9,7 @@ const logger = require('koa-logger')
const getUser = require('./user/model');
const [userRouter, userAuthRouter] = require('./user/route');
const [houseRouter, houseAuthRouter] = require('./house/route');
const unitRouter = require('./unit/route');
const SESS_CONFIG = require('./session_config');
const ENV = require('./settings')
......@@ -25,8 +26,10 @@ app.use(session(SESS_CONFIG, app));
router.use('/api/unit', unitRouter.routes(), unitRouter.allowedMethods());
router.use('/api/user', userRouter.routes(), userRouter.allowedMethods());
router.use('/api/house', houseRouter.routes(), houseRouter.allowedMethods());
authRouter.use('/api/user', userAuthRouter.routes(), userAuthRouter.allowedMethods());
authRouter.use('/api/house', houseAuthRouter.routes(), houseAuthRouter.allowedMethods());
app.use(router.routes()).use(router.allowedMethods());
......@@ -41,7 +44,7 @@ app.use(async (context, next) => {
});
// Add user to context
app.use(async (context, next) => {
const user = getUser.getUserFromId(context.session.user_id);
const user = await getUser.getUserFullFromId(context.session.user_id);
if(user){
context.user = user;
await next();
......
......@@ -28,6 +28,16 @@ userModel.getUser = async (context, next) => {
}
};
userModel.getUserFullFromId = async (id) => {
const sql_text = `SELECT u.id, u.discord_id, u.house_id, u.leadership, u.lk_house_role, r.lk_permission_level
FROM users as u
LEFT JOIN house_role_lk as r ON r.lk_key = u.lk_house_role
WHERE u.id = ? LIMIT 1;`;
const data = await db.con.query(sql_text, [id]);
return data[0];
}
userModel.getUserUnits = async(id) => {
const sql_txt = `SELECT u.*, uu.unit_level, uu.elite_flg
FROM users as us
......
......@@ -147,7 +147,6 @@ authRouter.post('/unit', async (context, next) => {
if(!body.unit_id){
throw Error('No Unit Id To assign')
}
console.log(body)
await userModel.assignUserUnit(context.session.user_id, body.unit_id, body);
context.status = 204;
}catch(error){
......
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