Skip to content

Main #33

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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open

Main #33

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
9 changes: 2 additions & 7 deletions exercises/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,17 @@ app.use(json())

app.get('/todo/:id', async (req, res) => {
const todoId = req.params.id
const todo = await Todo.findById(id).exec()
res.status(200).json(todo)
})

app.get('/todos', async (req, res) => {
const todos = await Todo.find({}).lean().exec()
res.status(200).json(todos)

})

app.post('/todo', async () => {
const todoToCreate = req.body.todo
const todo = await Todo.create(todoToCreate)
res.status(201).json(todo.toJSON())
})

connect('mongodb://localhost:27017/intro-to-mongodb')
connect(/**add mongo url here */)
.then(() => app.listen(4000, () => {
console.log('server on http://localhost:4000')
}))
Expand Down
2 changes: 1 addition & 1 deletion exercises/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const mongoose = require('mongoose')
mongoose.Promise = global.Promise

const connect = (url) => mongoose.connect(url, {
useNewUrlParser: true
useNewUrlParser: true
})

module.exports = connect
10 changes: 0 additions & 10 deletions exercises/hooks/org.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,4 @@ const orgSchema = new mongoose.Schema({
}
})


orgSchema.post('remove', async (doc, next) => {
await Project.remove({org: doc._id}).exec()
next()
})

orgSchema.virtual('avatar').get(function() {
return `${cdnUrl}/${this._id.toString()}`
})

module.exports = mongoose.model('org', orgSchema)
9 changes: 0 additions & 9 deletions exercises/hooks/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,4 @@ const projectSchema = new mongoose.Schema({
}
})

projectSchema.index({
org: 1,
name: 1
}, {unique: true})


projectSchema.virtual('budgetLeft').get(function() {
return this.budget - this.spent
})
module.exports = mongoose.model('project', projectSchema)
2 changes: 2 additions & 0 deletions exercises/models/crud.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const user = require('./user')
const User = require('./user')

const getUserById = (id) => {
Expand All @@ -21,6 +22,7 @@ const updateUserById = (id, update) => {
return User.findByIdAndUpdate(id, update, {new: true}).exec()
}


module.exports = {
getUserById,
getAllUsers,
Expand Down
1 change: 1 addition & 0 deletions exercises/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const userSchema = new mongoose.Schema({
firstName: {
type: String,
required: true

},
lastName: {
type: String,
Expand Down
11 changes: 1 addition & 10 deletions exercises/queries/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,10 @@ const postSchema = new mongoose.Schema({
type: Number,
required: true
},
author: {
type: mongoose.Schema.Types.ObjectId,
ref: 'author',
required: true
},
isFeatured: {
type: Boolean,
default: false
},
similarPosts: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'post',
}]
}
}, {timestamps: true})

module.exports = mongoose.model('post', postSchema)
23 changes: 6 additions & 17 deletions exercises/queries/queries.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,27 @@
const Post = require('./post')

const postByTitle = (title) => {
return Post.findOne({title}).exec()

}

const postsForAuthor = (authorId) => {
return Post.find({author: authorId}).exec()

}

const fullPostById = (id) => {
return Post.findById(id)
.populate('author')
.populate('similarPosts')
.exec()

}

const allPostsSlim = (fieldsToSelect) => {
return Post.find({})
.select(fieldsToSelect)
.sort('-createdAt')
.exec()

}

const postByContentLength = (maxContentLength, minContentLength) => {
return Post.find({
contentLength: {$lt: maxContentLength, $gt: minContentLength}
})
.exec()

}

const addSimilarPosts = (postId, similarPosts) => {
return Post.findByIdAndUpdate(postId, {
$push: {similarPosts: {$each: similarPosts}}
},{new: true})

}

module.exports = {
Expand Down
Loading