-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8ecf8cc
commit a12c2dc
Showing
2 changed files
with
121 additions
and
0 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
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) | ||
} | ||
|
||
} | ||
} |
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,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) |