From e2f18e1df510d29d97295b7163a4f073673abc8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Gro=C3=9F?= Date: Mon, 17 Apr 2023 15:07:29 +0200 Subject: [PATCH] API: Add routes to create and update groups/characters --- api/jsonApi.js | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/api/jsonApi.js b/api/jsonApi.js index 20f08c2..3d3cd17 100644 --- a/api/jsonApi.js +++ b/api/jsonApi.js @@ -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;