API: Add routes to fetch groups and characters
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
require("dotenv").config();
|
require("dotenv").config();
|
||||||
|
const Sequelize = require('sequelize');
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const bodyParser = require('body-parser');
|
const bodyParser = require('body-parser');
|
||||||
const { Card, User, DropHistory, Character, Group } = require("../models");
|
const { Card, User, DropHistory, Character, Group } = require("../models");
|
||||||
@@ -88,7 +89,67 @@ router.get('/most-recent-drop', async (req, res) => {
|
|||||||
console.error(error);
|
console.error(error);
|
||||||
res.status(500).send('Error fetching most recent drop');
|
res.status(500).send('Error fetching most recent drop');
|
||||||
}
|
}
|
||||||
}).needsAuth= true;
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Groups
|
||||||
|
*/
|
||||||
|
router.get('/groups', async (req, res) => {
|
||||||
|
let condition = {
|
||||||
|
where: {
|
||||||
|
[Sequelize.Op.or]: [
|
||||||
|
{enabled: 1}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(isAuthorized && req.query.include_disabled) {
|
||||||
|
condition['where'][Sequelize.Op.or].push({enabled: 0});
|
||||||
|
}
|
||||||
|
|
||||||
|
let groups = await Group.findAll(condition);
|
||||||
|
res.json(groups);
|
||||||
|
});
|
||||||
|
|
||||||
|
router.get('/groups/:group_id', async (req, res) => {
|
||||||
|
let group = await Group.findByPk(req.params.group_id);
|
||||||
|
|
||||||
|
if (!group.enabled && !isAuthorized(req)) {
|
||||||
|
res.status(404).json({ error: 'Group not found' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
res.json(group);
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Characters
|
||||||
|
*/
|
||||||
|
router.get('/characters', async (req, res) => {
|
||||||
|
let condition = {
|
||||||
|
where: {
|
||||||
|
[Sequelize.Op.or]: [
|
||||||
|
{enabled: 1}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(isAuthorized && req.query.include_disabled) {
|
||||||
|
condition['where'][Sequelize.Op.or].push({enabled: 0});
|
||||||
|
}
|
||||||
|
|
||||||
|
let characters = await Character.findAll(condition);
|
||||||
|
res.json(characters);
|
||||||
|
});
|
||||||
|
|
||||||
|
router.get('/characters/:character_id', async (req, res) => {
|
||||||
|
let character = await Character.findByPk(req.params.character_id);
|
||||||
|
|
||||||
|
if (!character.enabled && !isAuthorized(req)) {
|
||||||
|
res.status(404).json({ error: 'Character not found' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
res.json(character);
|
||||||
|
});
|
||||||
|
|
||||||
app.use(PREFIX, router);
|
app.use(PREFIX, router);
|
||||||
module.exports = app;
|
module.exports = app;
|
||||||
|
|||||||
Reference in New Issue
Block a user