forked from b00tc4mp/eurofirms-bootcamp-202402
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add test api and start logic cinema in app b00tc4mp#257
- Loading branch information
Showing
11 changed files
with
133 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
1 change: 1 addition & 0 deletions
1
staff/sergio-ocaña/project/api/test/delete-cinema-to-manager.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters