diff --git a/api/jsonApi.js b/api/jsonApi.js index 7393205..296e053 100644 --- a/api/jsonApi.js +++ b/api/jsonApi.js @@ -6,6 +6,8 @@ const { Card, User, DropHistory, Character, Group } = require("../models"); const { isAuthorized } = require('./middleware/apiKeyAuth'); const { Op } = require('sequelize'); +const groupRoutes = require('./routes/groups'); + const ACCESS_TOKEN = process.env.API_ACCESS_TOKEN; const app = express(); const router = express.Router(); @@ -80,69 +82,6 @@ router.get('/most-recent-drop', async (req, res) => { } }); -/** - * 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); -}); - -router.post('/groups', async (req, res) => { - try { - const newGroupData = req.body; - - const newGroup = await Group.create(newGroupData); - - res.status(201).json({ message: 'Group created successfully.', group: newGroup }); - } catch (error) { - res.status(500).json({ message: 'Error creating group.', error }); - } -}); - -router.put('/groups/:group_id', async (req, res) => { - if(!isAuthorized(req, res)){return;} - - try { - const groupId = req.params.group_id; - const updatedGroupData = req.body; - - const [updatedRowCount] = await Group.update(updatedGroupData, { - where: { id: groupId } - }); - - if (updatedRowCount === 0) { - return res.status(404).json({ message: 'Group not found.' }); - } - - res.status(200).json({ message: 'Group updated successfully.' }); - } catch (error) { - res.status(500).json({ message: 'Error updating Group.', error }); - } -}); - /** * Characters */ @@ -207,4 +146,5 @@ router.put('/characters/:character_id', async (req, res) => { }); app.use(PREFIX, router); +app.use(PREFIX, groupRoutes); module.exports = app; diff --git a/api/routes/groups.js b/api/routes/groups.js new file mode 100644 index 0000000..de55e5a --- /dev/null +++ b/api/routes/groups.js @@ -0,0 +1,71 @@ +const express = require('express'); +const Sequelize = require('sequelize'); +const { Group } = require('../../models'); +const { isAuthorized } = require('../middleware/apiKeyAuth'); + +const router = express.Router(); + +/** + * 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); +}); + +router.post('/groups', async (req, res) => { + try { + const newGroupData = req.body; + + const newGroup = await Group.create(newGroupData); + + res.status(201).json({ message: 'Group created successfully.', group: newGroup }); + } catch (error) { + res.status(500).json({ message: 'Error creating group.', error }); + } +}); + +router.put('/groups/:group_id', async (req, res) => { + if (!isAuthorized(req, res)) { return; } + + try { + const groupId = req.params.group_id; + const updatedGroupData = req.body; + + const [updatedRowCount] = await Group.update(updatedGroupData, { + where: { id: groupId } + }); + + if (updatedRowCount === 0) { + return res.status(404).json({ message: 'Group not found.' }); + } + + res.status(200).json({ message: 'Group updated successfully.' }); + } catch (error) { + res.status(500).json({ message: 'Error updating Group.', error }); + } +}); + +module.exports = router; \ No newline at end of file