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 logic to the collection cinema b00tc4mp#257
- Loading branch information
Showing
7 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
staff/sergio-ocaña/project/api/logic/addCinemaToManager.js
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 { 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
14
staff/sergio-ocaña/project/api/logic/addCinemaToManager.test.js
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 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) | ||
} | ||
}) |
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,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 |
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 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) | ||
} | ||
}) |
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,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 |
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,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.