This repository was archived by the owner on Mar 9, 2021. It is now read-only.
File tree 4 files changed +64
-18
lines changed
4 files changed +64
-18
lines changed Original file line number Diff line number Diff line change @@ -15,10 +15,14 @@ const UserSchema = new Schema({
15
15
required : true ,
16
16
minlength : 10
17
17
} ,
18
- mobile : Number ,
18
+ mobile : {
19
+ type : Number ,
20
+ default : 0
21
+ } ,
19
22
bio : {
20
23
type : String ,
21
- maxlength : 1000
24
+ maxlength : 1000 ,
25
+ default : ''
22
26
} ,
23
27
working : Boolean ,
24
28
forhire : Boolean ,
@@ -36,7 +40,10 @@ const UserSchema = new Schema({
36
40
techFamiliarWith : [ String ] ,
37
41
techInterestedIn : [ String ] ,
38
42
projects : [ Schema . Types . ObjectId ] ,
39
- hits : Number
43
+ hits : {
44
+ type : Number ,
45
+ default : 0
46
+ }
40
47
} )
41
48
42
49
const User = mongoose . model ( 'User' , UserSchema )
Original file line number Diff line number Diff line change @@ -14,7 +14,10 @@ const UserProjectSchema = new Schema({
14
14
minlength : 100 ,
15
15
maxlength : 1000
16
16
} ,
17
- thumbnailUrl : String ,
17
+ thumbnailUrl : {
18
+ type : String ,
19
+ default : 'Some url to default thumbnail'
20
+ } ,
18
21
creators : {
19
22
type : [ Schema . Types . ObjectId ] ,
20
23
required : true
@@ -23,7 +26,10 @@ const UserProjectSchema = new Schema({
23
26
type : String ,
24
27
required : true
25
28
} ,
26
- hits : Number
29
+ hits : {
30
+ type : Number ,
31
+ default : 0
32
+ }
27
33
} )
28
34
29
35
module . exports = UserProjectSchema
Original file line number Diff line number Diff line change 1
1
const User = require ( '../models/User' )
2
2
3
+ const updateHits = require ( '../utils/updateHits' )
4
+
3
5
const resolvers = {
4
6
Query : {
5
7
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
+ }
10
13
11
- return user
14
+ return updateHits ( user )
15
+ } catch ( e ) {
16
+ throw e
17
+ }
12
18
} ,
13
19
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
+ }
19
25
20
- return user
26
+ return updateHits ( user )
27
+ } catch ( e ) {
28
+ throw e
29
+ }
21
30
}
22
31
} ,
23
32
Mutation : {
24
33
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
+ }
27
45
}
28
46
}
29
47
}
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments