diff --git "a/staff/sergio-oca\303\261a/project/api/index.js" "b/staff/sergio-oca\303\261a/project/api/index.js" index 31fb431b..b74da375 100644 --- "a/staff/sergio-oca\303\261a/project/api/index.js" +++ "b/staff/sergio-oca\303\261a/project/api/index.js" @@ -169,8 +169,112 @@ mongoose.connect(MONGO_URL) error = new MatchError(error.message) - res.status(status).json({ error: error.constructor.name, message: error.message }) } + res.status(status).json({ error: error.constructor.name, message: error.message }) + } + }) + + server.delete('cinema/delete/:cinemaId', (req, res) => { + try { + const { authorization } = req.headers + + const token = authorization.slice(7) + + const { sub: userId } = jwt.verify(token, JWT_SECRET) + + const cinemaId = req.params + + logic.deleteCinema(userId, cinemaId) + .then(() => res.status(204).send()) + .catch(error => { + let status = 500 + + if (error instanceof MatchError) + status = 401 + res.status(status).json({ error: error.constructor.name, message: error.message }) + }) + + } catch (error) { + let status = 500 + + if (error instanceof TypeError || error instanceof RangeError || error instanceof ContentError) + status = 400 + + else if (error instanceof JsonWebTokenError || error instanceof TokenExpiredError) { + status = 401 + + error = new MatchError(error.message) + + } + res.status(status).json({ error: error.constructor.name, message: error.message }) + } + }) + + server.patch('users/:cinemaId', (req, res) => { + try { + const { authorization } = req.headers + + const token = authorization.slice(7) + + const { sub: userId } = jwt.verify(token) + + const { cinemaId } = req.params + + logic.addCinemaToManager(userId, cinemaId) + .then(() => { res.status(200).send() }) + .catch(error => { + let status = 500 + + if (error instanceof MatchError) + status = 401 + + res.status(status).json({ error: error.constructor.name, message: error.message }) + }) + } catch (error) { + let status = 500 + + if (error instanceof TypeError || error instanceof RangeError || error instanceof ContentError) + status = 400 + else if (error instanceof JsonWebTokenError || error instanceof TokenExpiredError) { + status = 401 + + error = new MatchError(error.message) + } + res.status(status).json({ error: error.constructor.name, message: error.message }) + } + }) + + server.delete('users/:cinemaId/delete', (req, res) => { + try { + const { authorization } = req.headers + + const token = authorization.slice(7) + + const { sub: userId } = jwt.verify(token, JWT_SECRET) + + const { cinemaId } = req.params + + logic.deleteCinemaToManager(userId, cinemaId) + .then(() => res.status(204).send()) + .catch(error => { + let status = 500 + + if (error instanceof MatchError) + status = 401 + + res.status(status).json({ error: error.constructor.name, message: error.message }) + }) + } catch (error) { + let status = 500 + + if (error instanceof TypeError || error instanceof RangeError || error instanceof ContentError) + status = 400 + else if (error instanceof JsonWebTokenError || error instanceof TokenExpiredError) { + status = 401 + + error = new MatchError(error.message) + } + res.status(status).json({ error: error.constructor.name, message: error.message }) } }) diff --git "a/staff/sergio-oca\303\261a/project/api/logic/deleteCinemaToManager.js" "b/staff/sergio-oca\303\261a/project/api/logic/deleteCinemaToManager.js" index 56f0d784..014c9c54 100644 --- "a/staff/sergio-oca\303\261a/project/api/logic/deleteCinemaToManager.js" +++ "b/staff/sergio-oca\303\261a/project/api/logic/deleteCinemaToManager.js" @@ -25,10 +25,6 @@ function deleteCinemaToManager(userId, cinemaId) { return User.findByIdAndUpdate(userId, { $unset: { cinema } }) }) - - }) - - } export default deleteCinemaToManager \ No newline at end of file