Skip to content

Commit

Permalink
add test api and start logic cinema in app b00tc4mp#257
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeyusin committed Jul 7, 2024
1 parent adbfdb0 commit 48cb3e4
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 14 deletions.
18 changes: 17 additions & 1 deletion staff/sergio-ocaña/project/api/data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,27 @@ const user = new Schema({
}

})
const room = new Schema({
name: {
type: String,
required: true,
},
cinema: {
type: ObjectId,
ref: 'Cinema'
},
temperature: {
type: String,
required: true,
}
})

const Room = model('Room', room)
const Cinema = model('Cinema', cinema)
const User = model('User', user)

export {
Cinema,
User
User,
Room
}
11 changes: 5 additions & 6 deletions staff/sergio-ocaña/project/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,15 @@ mongoose.connect(MONGO_URL)
}
})

server.delete('cinema/delete/:cinemaId', (req, res) => {
server.delete('/cinema/: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
const { cinemaId } = req.params

logic.deleteCinema(userId, cinemaId)
.then(() => res.status(204).send())
Expand Down Expand Up @@ -210,13 +210,13 @@ mongoose.connect(MONGO_URL)
}
})

server.patch('users/:cinemaId', (req, res) => {
server.patch('/users/:cinemaId', (req, res) => {
try {
const { authorization } = req.headers

const token = authorization.slice(7)

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

const { cinemaId } = req.params

Expand Down Expand Up @@ -244,7 +244,7 @@ mongoose.connect(MONGO_URL)
}
})

server.delete('users/:cinemaId/delete', (req, res) => {
server.delete('/users/:cinemaId/delete', (req, res) => {
try {
const { authorization } = req.headers

Expand Down Expand Up @@ -277,7 +277,6 @@ mongoose.connect(MONGO_URL)
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))
42 changes: 42 additions & 0 deletions staff/sergio-ocaña/project/api/logic/createRoom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { User, Cinema, Room } from '../data/index.js'

import { validate, errors } from 'com'

const { SystemError, MatchError, DuplicityError } = errors

function createRoom(userId, cinemaId, name, temperature) {
validate.id(userId)
validate.id(cinemaId)
validate.name(name)
validate.temperature(temperature)

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 need asign a cinema first, so you can edit it')

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 own cinema')

return Room.findOne({ $and: [{ name }, { _id: cinemaId }] })
.catch(error => { throw new SystemError(error.message) })
.then(room => {
if (room) throw new DuplicityError('You have another room with the same name')

const newRoom = { name, temperature, cinema: cinemaId }

return Room.create(newRoom)
.catch(error => { throw new SystemError(error.message) })
.then(room => { })
})

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

import mongoose from 'mongoose'

mongoose.connect('mongodb://localhost:27017/project')
.then(() => {
try {
createRoom('667f1a6c57cb4db689a3418e', '668412c453ab9bc323fa24a7', '1', '35')
.then(() => console.log('room created'))
.catch(error => console.error(error.message))
} catch (error) {
console.error(error)
}
})
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
curl -X PATCH -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NjdkNjkyNWZjOWUyNjQyZWJmNTI1ODEiLCJyb2xlIjoibWFuYWdlciIsImlhdCI6MTcyMDE5OTI5Mn0.M6YzEWvz79TTmzgo1jGGEuGjSPygihLoFMGR5Dqpx1g' http://localhost:8080/users/668828bba6a8e381be8810b3 -v
2 changes: 1 addition & 1 deletion staff/sergio-ocaña/project/api/test/authenticate-user.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
curl -X POST -H 'Content-type: application/json' -d '{"email":"perico@palotes.com","password":"12341234"}' http://127.0.0.1:8080/users/auth -v
curl -X POST -H 'Content-type: application/json' -d '{"email":"the@boss.com","password":"12341234"}' http://127.0.0.1:8080/users/auth -v
1 change: 1 addition & 0 deletions staff/sergio-ocaña/project/api/test/create-cinema.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
curl -X POST -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NjdkNjkyNWZjOWUyNjQyZWJmNTI1ODEiLCJyb2xlIjoibWFuYWdlciIsImlhdCI6MTcyMDE5OTI5Mn0.M6YzEWvz79TTmzgo1jGGEuGjSPygihLoFMGR5Dqpx1g' -H 'Content-Type: application/json' -d '{"name":"cucarachacha","address":"c/bolivar numero 3"}' http://localhost:8080/cinema -v
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
curl -X DELETE -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NjdkNjkyNWZjOWUyNjQyZWJmNTI1ODEiLCJyb2xlIjoibWFuYWdlciIsImlhdCI6MTcyMDE5OTI5Mn0.M6YzEWvz79TTmzgo1jGGEuGjSPygihLoFMGR5Dqpx1g' http://localhost:8080/users/668828bba6a8e381be8810b3/delete -v
1 change: 1 addition & 0 deletions staff/sergio-ocaña/project/api/test/delete-cinema.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
curl -X DELETE -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NjdkNjkyNWZjOWUyNjQyZWJmNTI1ODEiLCJyb2xlIjoibWFuYWdlciIsImlhdCI6MTcyMDE5OTI5Mn0.M6YzEWvz79TTmzgo1jGGEuGjSPygihLoFMGR5Dqpx1g' http://localhost:8080/cinema/668828bba6a8e381be8810b3/delete -v
30 changes: 30 additions & 0 deletions staff/sergio-ocaña/project/app/src/logic/createCinema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { validate, errors } from 'com'

const { SystemError } = errors

function createCinema(name, address) {
validate.token(sessionStorage.token)
validate.name(name)
validate.name(address)

return fetch(`${import.meta.env.VITE_API_URL}/cinema`, {
method: 'POST',
headers: { 'Content/type': 'application/json', authorization: `Bearer ${sessionStorage.token}` },
body: JSON.stringify({ name, address })
})
.catch(error => { throw new SystemError(error.message) })
.then(res => {
if (res.status === 201) return

return res.json()
.catch(error => { throw new SystemError(error.message) })
.then(body => {
const { error, message } = body

const constructor = errors[error]

throw new constructor(message)
})
})
}
export default createCinema
26 changes: 20 additions & 6 deletions staff/sergio-ocaña/project/com/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,30 +78,44 @@ function validateText(text) {
if (!text.length) throw new ContentError('text is empty')
}

function validateToken(token, explain = 'token') {
if (typeof token !== 'string') throw new TypeError(`${explain} is not a string`)
function validateToken(token, explain = 'customer') {
if (typeof token !== 'string') throw new TypeError('token is not a string')

if (!token.length) throw new ContentError(`${explain} is empty`)
if (!token.length) throw new ContentError('token is empty')

const [, payload64,] = token.split('.')
const payloadJSON = atob(payload64)
const payload = JSON.parse(payloadJSON)

const { exp } = payload
const { exp, role } = payload

const now = Date.now() / 1000

if (exp < now) throw new MatchError(`${explain} expired`)
if (exp < now) throw new MatchError('token expired')

if (role !== explain) throw new MatchError(`wrong role only ${explain} could do`)

}

function validateTemperature(temperature, explain = 'temperature') {
if (typeof temperature !== 'string') throw new TypeError(`${explain} is not a string`)

const temp = Number(temperature)

if (temperature > 55) throw new RangeError(`${explain} your room is not viable to show films, cold it first`)

if (temperature < 0) throw new RangeError(`${explain} your room is not viable to show films, heater it first`)
}


const validate = {
name: validateName,
birthdate: validateBirthdate,
email: validateEmail,
password: validatePassword,
id: validateId,
text: validateText,
token: validateToken
token: validateToken,
temperature: validateTemperature
}
export default validate

0 comments on commit 48cb3e4

Please sign in to comment.