Skip to content

Commit

Permalink
implement apì routes and fixed errors in logic b00tc4mp#257
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeyusin committed Jun 26, 2024
1 parent 082612d commit 8676a32
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 23 deletions.
4 changes: 2 additions & 2 deletions staff/sergio-ocaña/project/api/data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ const user = new Schema({
role: {
type: String,
requiered: true,
enum: ['student', 'teacher'],
default: 'student'
enum: ['customer', 'manager'],
default: 'customer'
}

})
Expand Down
83 changes: 80 additions & 3 deletions staff/sergio-ocaña/project/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import cors from 'cors'
import jwt from 'jsonwebtoken'
import dotenv from 'dotenv'
import { errors } from 'com'
import logic from './logic/index.js';

dotenv.config()

Expand All @@ -21,7 +22,83 @@ mongoose.connect(MONGO_URL)

server.use(cors())

server.post('/customer', jsonBodyParser, (req, res) => {
const { name, birthdate, email, username, password } = req.body
server.post('/users/customer', jsonBodyParser, (req, res) => {
try {
const { name, birthdate, email, username, password } = req.body

logic.registerCustomer(name, birthdate, email, username, password)
.then(() => res.status(201).send())
.catch(error => {
let status = 500

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 = 409
res.status(status).json({ error: error.constructor.name, message: error.message })
}

server.post('/users/manager', jsonBodyParser, req, res => {
try {
const { name, birthdate, email, password } = req.body

logic.registerManager(name, birthdate, email, password)
.then(() => res.status(201).send)
.catch(error => {
let status = 500

if (error instanceof DuplicityError)
status = 409
res.status(status).json({ error: error.constructor.name, message: error.message })

status = 400
})

} catch (error) {
let status = 500

if (error instanceof TypeError || error instanceof RangeError || error instanceof ContentError)
status = 400

res.status(status).json({ error: error.constructor.name, message: error.message })
}

server.post('/users/auth', jsonBodyParser, (req, res) => {
try {
const { email, password } = req.body

logic.authenticateUser(email, password)
.then((userId, role) => {
const token = jwt.sign({ sub: userId, role }, JWT_SECRET, { expiresIn: '1h' })

res.status(200).json(token)
})
.catch(error => {
let status = 500

if (error instanceof MatchError)
status = 401

res.status(status).json({ error: error.constructor.name, message: error.message })
})

} catch (error) {
let status = 500

if (error instanceof ContentError || error instanceof RangeError || error instanceof TypeError)
status = 400
res.status(status).json({ error: error.constructor.name, message: error.message })
}
})
})

})
})
server.listen(PORT, () => console.log('API started'))
})
.catch(error => console.error(error))
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import mongoose from 'mongoose'
mongoose.connect('mongodb://localhost:27017/project')
.then(() => {
try {
authenticateUser('pepitogrillo@gmail.com', '12341234')
authenticateUser('carolitogym@gmail.com', '12341234')
.then((id, role) => console.log('user logged in', id, role))
.catch(error => console.error(error))
} catch (error) {
Expand Down
16 changes: 9 additions & 7 deletions staff/sergio-ocaña/project/api/logic/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import authenticateUser from './authenticateUser';
import registerCustomer from './registerCustomer';
import registerManager from './registerManager';
import authenticateUser from './authenticateUser.js';
import registerCustomer from './registerCustomer.js';
import registerManager from './registerManager.js';

const logic = {
authenticateUser: authenticateUser
}
authenticateUser,
registerCustomer,
registerManager,


export default logic = {
}

}
export default logic
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ mongoose.connect('mongodb://localhost:27017/project')
} catch (error) {
console.error(error)
}
})
})
4 changes: 2 additions & 2 deletions staff/sergio-ocaña/project/api/logic/registerManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ function registerManager(name, birthdate, email, password) {
return User.findOne({ email })
.catch(error => { throw new SystemError(error.message) })
.then(user => {
if (user) throw new DuplicityError
if (user) throw new DuplicityError('email already exist')

user = { name, birthdate, email, password, role: manager }
user = { name, birthdate, email, password, role: 'manager' }

return User.create(user)
.catch(error => { throw new SystemError(error.message) })
Expand Down
13 changes: 13 additions & 0 deletions staff/sergio-ocaña/project/api/logic/registerManager.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import mongoose from 'mongoose';
import registerManager from './registerManager.js';

mongoose.connect('mongodb://localhost:27017/project')
.then(() => {
try {
registerManager('Bebe Jefazo', '2000-01-01', '[email protected]', '12341234')
.catch(error => console.error(error))
.then(() => console.log('manager created'))
} catch (error) {
console.error(error)
}
})
1 change: 1 addition & 0 deletions staff/sergio-ocaña/project/api/test/registerCustomer.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
curl -X POST -H 'Content-type: application/json' -d '{"name":"perico el de los palotes","1995-01-02","email":"[email protected]","password":"12341234"}' http://localhost:8080/users/customer -v
14 changes: 7 additions & 7 deletions staff/sergio-ocaña/project/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8676a32

Please sign in to comment.