API: Add routes to create and update groups/characters

This commit is contained in:
2023-04-17 15:07:29 +02:00
parent 1b9693401c
commit e2f18e1df5

View File

@@ -121,6 +121,39 @@ router.get('/groups/:group_id', async (req, res) => {
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
*/
@@ -151,5 +184,38 @@ router.get('/characters/:character_id', async (req, res) => {
res.json(character);
});
router.post('/characters', async (req, res) => {
try {
const newCharacterData = req.body;
const newCharacter = await Character.create(newCharacterData);
res.status(201).json({ message: 'Character created successfully.', character: newCharacter });
} catch (error) {
res.status(500).json({ message: 'Error creating character.', error });
}
});
router.put('/characters/:character_id', async (req, res) => {
if(!isAuthorized(req, res)){return;}
try {
const characterId = req.params.character_id;
const updatedCharacterData = req.body;
const [updatedRowCount] = await Character.update(updatedCharacterData, {
where: { id: characterId }
});
if (updatedRowCount === 0) {
return res.status(404).json({ message: 'Character not found.' });
}
res.status(200).json({ message: 'Character updated successfully.' });
} catch (error) {
res.status(500).json({ message: 'Error updating character.', error });
}
});
app.use(PREFIX, router);
module.exports = app;