Skip to content

Commit

Permalink
add logic to the collection cinema b00tc4mp#257
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeyusin committed Jul 2, 2024
1 parent 6921a18 commit 1729410
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 0 deletions.
30 changes: 30 additions & 0 deletions staff/sergio-ocaña/project/api/logic/addCinemaToManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Cinema, User } from '../data/index.js'

import { validate, errors } from 'com'

const { SystemError, MatchError } = errors

function addCinemaToManager(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.role !== 'manager') throw new MatchError('Only Managers are allowed could be assigned to a cinema')

return Cinema.findById(cinemaId)
.catch(error => { throw new SystemError(error.message) })
.then(cinema => {
if (!cinema) { throw new MatchError("cinema doesn't exist, you could create and try again") }

user.cinema = cinemaId

return user.save()
})

})
}
export default addCinemaToManager
14 changes: 14 additions & 0 deletions staff/sergio-ocaña/project/api/logic/addCinemaToManager.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import addCinemaToManager from './addCinemaToManager.js'

import mongoose from 'mongoose'

mongoose.connect('mongodb://localhost:27017/project')
.then(() => {
try {
addCinemaToManager('667f1a6c57cb4db689a3418e', '668412c453ab9bc323fa24a7')
.then(() => console.log('cinema added'))
.catch(error => console.error(error))
} catch (error) {
console.error(error)
}
})
31 changes: 31 additions & 0 deletions staff/sergio-ocaña/project/api/logic/createCinema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Cinema, User } from '../data/index.js'
import { validate, errors } from 'com'

const { MatchError, SystemError, DuplicityError } = errors


function createCinema(userId, name, address) {
validate.id(userId)
validate.name(name)


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

if (user.role !== 'manager') throw new MatchError('Only Managers are allowed to create Cinemas')

return Cinema.findOne({ $or: [{ name }, { address }] })
.catch(error => { throw new SystemError(error.message) })
.then(cinema => {
if (cinema) { throw new DuplicityError('cinema already exist') }

cinema = { name, address }

return Cinema.create(cinema)
.then(cinema => { })
})
})
}
export default createCinema
14 changes: 14 additions & 0 deletions staff/sergio-ocaña/project/api/logic/createCinema.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import createCinema from './createCinema.js'

import mongoose from 'mongoose'

mongoose.connect('mongodb://localhost:27017/project')
.then(() => {
try {
createCinema('667d6925fc9e2642ebf52581', 'Prueba borrado', 'calle nosera nº15')
.then(() => console.log('cinema created'))
.catch(error => console.error(error))
} catch (error) {
console.error(error)
}
})
32 changes: 32 additions & 0 deletions staff/sergio-ocaña/project/api/logic/deleteCinema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Cinema, User } from '../data/index.js'
import { validate, errors } from 'com'

const { MatchError, SystemError } = errors


function deleteCinema(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.role !== 'manager') throw new MatchError('Only Managers are allowed to delete a cinema')

return Cinema.findById(cinemaId)
.catch(error => { throw new SystemError(error.message) })
.then(cinema => {
if (!cinema) throw new MatchError('Cinema doesn´t exist')

return Cinema.findByIdAndDelete(cinemaId)
.catch(error => { throw SystemError(error.message) })
.then(() => {
return User.updateMany({ cinema: cinemaId }, { $unset: { cinema } })
})
})
})
}

export default deleteCinema
15 changes: 15 additions & 0 deletions staff/sergio-ocaña/project/api/logic/deleteCinema.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import deleteCinema from './deleteCinema.js'

import mongoose from 'mongoose'

mongoose.connect('mongodb://localhost:27017/project')
.then(() => {
try {
deleteCinema('667f1a6c57cb4db689a3418e', '668412c453ab9bc323fa24a7')
.then(() => console.log('cinema deleted'))
.catch((error) => console.error(error))
} catch (error) {
console.error(error)
}
})
.catch(error => console.error(error))
Empty file.

0 comments on commit 1729410

Please sign in to comment.