Skip to content
This repository was archived by the owner on Mar 9, 2021. It is now read-only.

Commit ee15717

Browse files
committed
Add Validation
1 parent 14232f2 commit ee15717

File tree

4 files changed

+64
-18
lines changed

4 files changed

+64
-18
lines changed

models/User.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@ const UserSchema = new Schema({
1515
required: true,
1616
minlength: 10
1717
},
18-
mobile: Number,
18+
mobile: {
19+
type: Number,
20+
default: 0
21+
},
1922
bio: {
2023
type: String,
21-
maxlength: 1000
24+
maxlength: 1000,
25+
default: ''
2226
},
2327
working: Boolean,
2428
forhire: Boolean,
@@ -36,7 +40,10 @@ const UserSchema = new Schema({
3640
techFamiliarWith: [String],
3741
techInterestedIn: [String],
3842
projects: [Schema.Types.ObjectId],
39-
hits: Number
43+
hits: {
44+
type: Number,
45+
default: 0
46+
}
4047
})
4148

4249
const User = mongoose.model('User', UserSchema)

models/UserProject.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ const UserProjectSchema = new Schema({
1414
minlength: 100,
1515
maxlength: 1000
1616
},
17-
thumbnailUrl: String,
17+
thumbnailUrl: {
18+
type: String,
19+
default: 'Some url to default thumbnail'
20+
},
1821
creators: {
1922
type: [Schema.Types.ObjectId],
2023
required: true
@@ -23,7 +26,10 @@ const UserProjectSchema = new Schema({
2326
type: String,
2427
required: true
2528
},
26-
hits: Number
29+
hits: {
30+
type: Number,
31+
default: 0
32+
}
2733
})
2834

2935
module.exports = UserProjectSchema

resolvers/index.js

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,47 @@
11
const User = require('../models/User')
22

3+
const updateHits = require('../utils/updateHits')
4+
35
const resolvers = {
46
Query: {
57
getUserById: async (_, { userId }) => {
6-
const user = User.findOne({ _id: userId })
7-
if (!user) {
8-
return null
9-
}
8+
try {
9+
const user = await User.findOne({ _id: userId }).exec()
10+
if (!user) {
11+
throw new Error(`User with userId ${userId} not found!`)
12+
}
1013

11-
return user
14+
return updateHits(user)
15+
} catch (e) {
16+
throw e
17+
}
1218
},
1319
getUserByUsername: async (_, { username }) => {
14-
const user = await User.findOne({ username })
15-
console.log(user)
16-
if (!user) {
17-
return null
18-
}
20+
try {
21+
const user = await User.findOne({ username }).exec()
22+
if (!user) {
23+
throw new Error(`User with username ${username} not found!`)
24+
}
1925

20-
return user
26+
return updateHits(user)
27+
} catch (e) {
28+
throw e
29+
}
2130
}
2231
},
2332
Mutation: {
2433
addUser: async (_, { user }) => {
25-
const newUser = new User(user)
26-
return newUser.save()
34+
try {
35+
const { username } = user
36+
const userExists = await User.countDocuments({ username }).exec()
37+
if (userExists) {
38+
throw new Error(`A user with the username ${username} already exists`)
39+
}
40+
41+
return new User(user).save()
42+
} catch (e) {
43+
throw e
44+
}
2745
}
2846
}
2947
}

utils/updateHits.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
async function updateHits (doc) {
2+
try {
3+
if (!doc.hits) {
4+
doc.hits = 1
5+
} else {
6+
doc.hits += 1
7+
}
8+
9+
return doc.save()
10+
} catch (e) {
11+
throw e
12+
}
13+
}
14+
15+
module.exports = updateHits

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy