Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kirsten Forester #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions controllers/notes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const express = require("express");
const router = express.Router();
const Note = require("../models/Note");

// GET '/' which should redirect to `/notes'
router.get("/", (req, res) => {
Note.find({}).then(note => res.json(note));
});

// GET '/notes' which return a list of notes
router.get("/notes", (req, res) => {
Note.find({ notes: req.params.notes }).then(note =>
res.json(note)
);
});

// GET '/notes/:someParameter' where :someParameter grabs a single Note from the database by id. NOTE You can name :someParameter whatever you want
router.get("/notes:title", (req, res) => {
Note.find({ title: req.params.title }).then(note =>
res.json(note)
);
});

module.exports = router
19 changes: 19 additions & 0 deletions controllers/users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const express = require("express")
const router = express.Router()
const User = require("../models/User");

// GET '/users' which will return a list of all users
router.get("/user", (req, res) => {
User.find({ user: req.params.user }).then(user =>
res.json(user)
);
});

// GET '/users/:someParameter which will return a single user by id.
router.get("/user:_id", (req, res) => {
User.find({ _id: req.params._id }).then(user =>
res.json(user)
);
});

module.exports = router
15 changes: 14 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
const express = require('express');
const app = express();
const parser = require("body-parser");

const notesController = require("./controllers/notes");
const usersController = require("./controllers/users");

app.use(parser.urlencoded({ extended: true }));
app.use(parser.json());

app.use("/api/notes/", notesController);
app.use("/api/users/", usersController);

app.listen(3000, () => console.log('app is running'))

// DO NOT REMOVE THIS LINE:
module.exports = app
module.exports = app
18 changes: 15 additions & 3 deletions models/Note.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
const mongoose = require('../db/connection')
const mongoose = require('../db/connection');

const noteSchema = new mongoose.Schema({})
const noteSchema = new mongoose.Schema({
title: String,
body: String,
author: [
{
ref: "User",
type: mongoose.Schema.Types.ObjectId
}
]
});

module.exports = mongoose.model('Note', noteSchema)

const Note = mongoose.model('Note', noteSchema);

module.exports = Note;
17 changes: 14 additions & 3 deletions models/User.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
const mongoose = require('../db/connection')
const mongoose = require('../db/connection');

const userSchema = new mongoose.Schema({})
const userSchema = new mongoose.Schema({
username: String,
email: String,
notes: [
{
ref: "Note",
type: mongoose.Schema.Types.ObjectId
}
]
});

module.exports = mongoose.model('User', userSchema)
const User = mongoose.model('User', userSchema);

module.exports = User;
Loading