API: Move group routes to separate file

This commit is contained in:
2023-04-17 15:46:28 +02:00
parent 5712ea38c1
commit 2e04a85eb8
2 changed files with 74 additions and 63 deletions

View File

@@ -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;

71
api/routes/groups.js Normal file
View File

@@ -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;