Skip to content

Commit

Permalink
feat(#3): apply serializer on put route
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusbsilva committed Jun 29, 2019
1 parent 42e8b6d commit c9483c2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
2 changes: 0 additions & 2 deletions lib/exceptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

class SerializeError extends Error {
constructor(field, message) {
console.log('C')
super(message)
this.field = field
}
Expand All @@ -17,7 +16,6 @@ class SerializeUndefinedError extends SerializeError {

class SerializeNotValidatedError extends Error {
constructor() {
console.log('Q')
super('Must run validate() before getting validatedData')
}
}
Expand Down
28 changes: 15 additions & 13 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ app.get('/', (req, res) => {
app.post('/monitoring_event', (req, res) => {

let monitoringEvent = new MonitoringEventSerializer(req.body)

let valid = monitoringEvent.validate()

if(valid) {
Expand Down Expand Up @@ -67,18 +66,21 @@ app.put('/monitoring_event/:id', (req, res) => {
})
}

event.place = req.body.place
event.datetime = req.body.datetime

event.save((err) => {
if(err){
return res.status(400).send(err)
}
return res.status(201).send(event)
})
})
.catch((err) => {
return res.status(500).send(err)
let monitoringEvent = new MonitoringEventSerializer(req.body, event)
let valid = monitoringEvent.validate()

if(valid) {
monitoringEvent.save()
.then(() => {
return res.status(201).send({ success: true })
})
.catch((err) => {
return res.status(500).send(err.message)
})
}
else{
return res.status(400).send({errors: monitoringEvent.errors()})
}
})
})

Expand Down

0 comments on commit c9483c2

Please sign in to comment.