Skip to content

Commit

Permalink
Criando e gerenciando Pet
Browse files Browse the repository at this point in the history
  • Loading branch information
MatheusCoxxxta committed May 26, 2020
1 parent 8ecf8cc commit a12c2dc
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/Controllers/PetController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const mongoose = require('mongoose')

const Pet = mongoose.model('Pet')


module.exports = {
async index (req, res) {
const pets = await Pet.find().sort({ 'createdAt': -1 })

return res.json(pets)
},

async show (req, res) {
const pet = await Pet.findById(req.params.id)

return res.json(pet)
},

async store (req, res) {
const { name, type, color, age, born, breed, castrationDate, owner, ownerId } = req.body;

try {
const pet = await Pet.create({
name,
type,
color,
age,
born,
breed,
castrationDate,
owner,
ownerId
})
return res.json(pet)
} catch (error) {
return res.json(error)
}
},

async update (req, res) {
const { name, type, color, age, born, breed, castrationDate, owner, ownerId } = req.body;

try {
const pet = await Pet.findByIdAndUpdate(req.params.id, {
name,
type,
color,
age,
born,
breed,
castrationDate,
owner,
ownerId
}, { new: true })

return res.json(pet)
} catch (error) {
return res.json(error)
}

},

async destroy (req, res) {
try {
const pet = await Pet.findByIdAndRemove(req.params.id)

return res.json({ message: "Success" })
} catch (error) {
return res.json(error)
}

}
}
48 changes: 48 additions & 0 deletions src/models/Pet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const mongoose = require('mongoose')
const bcrypt = require('bcryptjs')

const PetSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
type: {
type: String,
required: true
},
color: {
type: String,
required: false
},
age: {
type: String,
required: false
},
born: {
type: String,
required: false
},
breed: {
type: String,
required: false
},
castrationDate: {
type: String,
required: false
},
owner: {
type: String,
required: true
},
ownerId: {
type: String,
required: true
},
createdAt: {
type: Date,
default: Date.now
}
})


mongoose.model('Pet', PetSchema)

0 comments on commit a12c2dc

Please sign in to comment.