Skip to content

Commit fce2549

Browse files
committed
chore
1 parent 66a017a commit fce2549

File tree

9 files changed

+196
-8
lines changed

9 files changed

+196
-8
lines changed

exercises/models/__test__/crud.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const User = require('../user')
22
const crud = require('../crud')
33

4-
describe('user crud', () => {
4+
describe('User crud', () => {
55
describe('getUserById', () => {
66
test('get user by object id', async () => {
77
const user = await User.create({

exercises/models/__test__/user.spec.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const User = require('../user')
22

3-
describe('user', () => {
3+
describe('User model', () => {
44
test('first name must be required', async () => {
55
expect.assertions(1)
66

@@ -83,7 +83,8 @@ describe('user', () => {
8383
zip: 91917,
8484
city: 'SF',
8585
State: 'CA'
86-
}
86+
},
87+
pets: ['tido', 'miguel']
8788
})).toObject()
8889

8990
expect(user).toEqual({
@@ -98,7 +99,8 @@ describe('user', () => {
9899
zip: 91917,
99100
city: 'SF',
100101
State: 'CA'
101-
}
102+
},
103+
pets: ['tido', 'miguel']
102104
})
103105
})
104106
})

exercises/models/user.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const userSchema = new mongoose.Schema({
1919
default: false
2020
},
2121
birthDate: Date,
22+
pets: [{type: String}],
2223
address: {
2324
other: Boolean,
2425
street: String,
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const Post = require('../post')
2+
const Author = require('../author')
3+
4+
describe('Post model', () => {
5+
test('author is required and id type', async () => {
6+
expect.assertions(2)
7+
8+
try {
9+
await Post.create({
10+
title: 'Top ten movie books',
11+
content: 'There are a bunch books out there with movies. But which ones are legit?',
12+
})
13+
} catch (e) {
14+
expect(e).toBeTruthy()
15+
}
16+
17+
try {
18+
await Post.create({
19+
title: 'Top ten movie books',
20+
content: 'There are a bunch books out there with movies. But which ones are legit?',
21+
author: 'hello'
22+
})
23+
} catch (e) {
24+
expect(e).toBeTruthy()
25+
}
26+
})
27+
test('author must ref author collection', async () => {
28+
const name = 'JK Moose'
29+
const author = await Author.create({
30+
name,
31+
bio: 'I have so many awards'
32+
})
33+
34+
const post = await Post.create({
35+
title: 'Top ten movie books',
36+
content: 'There are a bunch books out there with movies. But which ones are legit?',
37+
author: author.id
38+
})
39+
40+
const p = await post.populate('author').execPopulate()
41+
expect(p.author.name).toBe(name)
42+
})
43+
})
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const {
2+
postByTitle,
3+
postByContentLength,
4+
postsForAuthor
5+
} = require('../queries')
6+
const Post = require('../post')
7+
const Author = require('../post')
8+
const mongoose = require('mongoose')
9+
const createConent = (length, fill = 'b') => new Array(length).fill(fill).join('')
10+
11+
describe('queries', () => {
12+
describe('postByTitle', () => {
13+
test('get post by title', async () => {
14+
const title = 'cat bedtime'
15+
const post = await Post.create({
16+
title,
17+
author: mongoose.Types.ObjectId(),
18+
content: createConent(50)
19+
})
20+
21+
const match = await postByTitle(title)
22+
expect(match.id).toBe(post.id)
23+
})
24+
})
25+
26+
describe('postsForAuthor', () => {
27+
test('get post by author', async () => {
28+
const author = mongoose.Types.ObjectId()
29+
const post = await Post.create({
30+
author,
31+
title: 'Carter v',
32+
content: createConent(50)
33+
})
34+
const match = await postsForAuthor(author)
35+
expect(match.author.toString()).toBe(post.author.toString())
36+
})
37+
})
38+
39+
describe('postByContentLength', () => {
40+
test('find posts in between content lengths', async () => {
41+
const author = mongoose.Types.ObjectId()
42+
43+
const posts = await Post.create([
44+
{title: 'Super Duper', author, content: createConent(1000)},
45+
{title: 'Amazing', author, content: createConent(100)},
46+
{title: 'Other', author, content: createConent(800)}
47+
])
48+
49+
const matches = await postByContentLength(1000, 100)
50+
expect(matches).toHaveLength(1)
51+
})
52+
})
53+
})

exercises/queries/author.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const mongoose = require('mongoose')
2+
3+
const authorSchema = new mongoose.Schema({
4+
name: {
5+
type: String,
6+
required: true,
7+
trim: true
8+
},
9+
bio: {
10+
type: String,
11+
required: true
12+
},
13+
social: {
14+
twitter: {
15+
type: String,
16+
unique: true,
17+
sparse: true
18+
},
19+
linkedin: {
20+
type: String,
21+
unique: true,
22+
sparse: true
23+
}
24+
}
25+
}, {timestamps: true})
26+
27+
module.exports = mongoose.model('author', authorSchema)

exercises/queries/index.js

Whitespace-only changes.

exercises/queries/post.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,27 @@ const postSchema = new mongoose.Schema({
44
title: {
55
type: String,
66
required: true,
7-
unique: true,
8-
sparse: true
7+
unique: true
98
},
10-
11-
9+
content: {
10+
type: String,
11+
required: true,
12+
minlength: 50,
13+
maxlength: 1200
14+
},
15+
author: {
16+
type: mongoose.Schema.Types.ObjectId,
17+
ref: 'author',
18+
required: true
19+
},
20+
isFeatured: {
21+
type: Boolean,
22+
deafult: false
23+
},
24+
similarPosts: [{
25+
type: mongoose.Schema.Types.ObjectId,
26+
ref: 'post',
27+
}]
1228
}, {timestamps: true})
29+
30+
module.exports = mongoose.model('post', postSchema)

exercises/queries/queries.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const Post = require('./post')
2+
3+
const postByTitle = (title) => {
4+
return Post.findOne({title}).exec()
5+
}
6+
7+
const postsForAuthor = (authorId) => {
8+
return Post.find({author: authorId}).exec()
9+
}
10+
11+
const fullPostById = (id) => {
12+
return Post.findById(id)
13+
.populate('author')
14+
.populate('similarPosts')
15+
.exec()
16+
}
17+
18+
const selectivePostsByLatest = (fieldsToSelect) => {
19+
return Post.find({})
20+
.select(fieldsToSelect)
21+
.sort('-createdAt')
22+
.exec()
23+
}
24+
25+
const postByContentLength = (maxContentLength, minContentLength) => {
26+
return Post.find({
27+
content: {$lt: maxContentLength, $gt: minContentLength}
28+
})
29+
.exec()
30+
}
31+
32+
const addRelatedPosts = (postId, relatedPosts) => {
33+
return Post.findById(postId, {$pushAll: {relatedPosts}}, {new: true})
34+
}
35+
36+
module.exports = {
37+
postByTitle,
38+
postsForAuthor,
39+
postByContentLength,
40+
fullPostById,
41+
selectivePostsByLatest,
42+
postByContentLength,
43+
addRelatedPosts
44+
}

0 commit comments

Comments
 (0)