Skip to content

Commit

Permalink
add all cinema logics and test b00tc4mp#257
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeyusin committed Jul 4, 2024
1 parent 1729410 commit c1b41f2
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 3 deletions.
43 changes: 42 additions & 1 deletion staff/sergio-ocaña/project/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ mongoose.connect(MONGO_URL)
const jsonBodyParser = express.json()

server.use(cors())

/* User Routes */
server.post('/users/customer', jsonBodyParser, (req, res) => {
try {
const { name, birthdate, email, password } = req.body
Expand Down Expand Up @@ -133,6 +133,47 @@ mongoose.connect(MONGO_URL)
}
})

/* CinemaRoutes */

server.post('/cinema', jsonBodyParser, (req, res) => {
try {
const { authorization } = req.headers

const token = authorization.slice(7)

const { sub: userId } = jwt.verify(token, JWT_SECRET)

const { name, address } = req.body

logic.createCinema(userId, name, address)
.then(() => res.status(201).send())
.catch(error => {
let status = 500

if (error instanceof MatchError)
status = 401

if (error instanceof DuplicityError)
status = 409
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.listen(PORT, () => console.log(`API started on port ${PORT}`))
})
.catch(error => console.error(error))
2 changes: 2 additions & 0 deletions staff/sergio-ocaña/project/api/logic/addCinemaToManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ function addCinemaToManager(userId, cinemaId) {

if (user.role !== 'manager') throw new MatchError('Only Managers are allowed could be assigned to a cinema')

if (user.cinema) throw new MatchError('You already have a Cinema. If you want to change it, delete it first')

return Cinema.findById(cinemaId)
.catch(error => { throw new SystemError(error.message) })
.then(cinema => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import mongoose from 'mongoose'
mongoose.connect('mongodb://localhost:27017/project')
.then(() => {
try {
addCinemaToManager('667f1a6c57cb4db689a3418e', '668412c453ab9bc323fa24a7')
addCinemaToManager('667d6925fc9e2642ebf52581', '6682f4966b94b0dd9a25382a')
.then(() => console.log('cinema added'))
.catch(error => console.error(error))
} catch (error) {
Expand Down
34 changes: 34 additions & 0 deletions staff/sergio-ocaña/project/api/logic/deleteCinemaToManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Cinema, User } from '../data/index.js'

import { validate, errors } from 'com'

const { SystemError, MatchError } = errors

function deleteCinemaToManager(userId, cinemaId) {
validate.id(userId)
validate.id(cinemaId, 'cinemaId')

return User.findById(userId)
.catch(error => { throw new SystemError(error.message) })
.then(user => {
if (!user) throw new MatchError('User not found')

if (!user.cinema) throw new MatchError('You don´t have any cinema to delete')

return Cinema.findById(cinemaId)
.catch(error => { throw new SystemError(error.message) })
.then(cinema => {
if (!cinema) throw new MatchError('Cinema not found')

if (cinemaId !== user.cinema.toString()) throw new MatchError('You can only modify your cinema')

return User.findByIdAndUpdate(userId, { $unset: { cinema } })

})


})


}
export default deleteCinemaToManager
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import deleteCinemaToManager from './deleteCinemaToManager.js'

import mongoose from 'mongoose'

mongoose.connect('mongodb://localhost:27017/project')
.catch(error => console.error(error))
.then(() => {
try {
deleteCinemaToManager('667d6925fc9e2642ebf52581', '6682f4966b94b0dd9a25382a')
.then(() => console.log('cinema deleted from manager'))
.catch(error => console.error(error))
} catch (error) {
console.error(error)
}
})
9 changes: 8 additions & 1 deletion staff/sergio-ocaña/project/api/logic/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import addCinemaToManager from './addCinemaToManager.js'
import createCinema from './createCinema.js'
import authenticateUser from './authenticateUser.js'
import registerCustomer from './registerCustomer.js'
import registerManager from './registerManager.js'
import retrieveUser from './retrieveUser.js'
import deleteCinema from './deleteCinema.js'
import deleteCinemaToManager from './deleteCinemaToManager.js'

const logic = {
authenticateUser,
registerCustomer,
registerManager,
retrieveUser,

addCinemaToManager,
createCinema,
deleteCinema,
deleteCinemaToManager,
}

export default logic

0 comments on commit c1b41f2

Please sign in to comment.