|
1 |
| -const { Tests, Projects } = require('../../models/reactypeModels'); |
2 |
| - |
| 1 | +const { UserInputError } = require('apollo-server-express'); |
| 2 | +const { Projects, Users } = require('../../models/reactypeModels'); |
3 | 3 | /*
|
4 | 4 | * resolvers are functions that handles graphQL requests. This file defines the logic for graphQL mutation requests
|
5 | 5 | * Link to Apollo Mutations:
|
6 | 6 | * https://www.apollographql.com/docs/apollo-server/data/resolvers/#defining-a-resolver
|
7 | 7 | */
|
8 | 8 |
|
9 |
| -const Test = { |
10 |
| - addTest: async (parent, args) => { |
11 |
| - const resp = await Tests.create({ name: args.name, likes: args.likes }); |
12 |
| - console.log('Added test', resp); |
13 |
| - if (resp) return { description: resp.name, id: resp._id, likes: args.likes }; |
14 |
| - return { description: 'Error creating test', id: 'unknown', likes: 0 }; |
15 |
| - }, |
16 |
| - |
17 |
| - updateTest: async (parent, args) => { |
18 |
| - // console.log('args >>> ', args); |
19 |
| - const filter = { _id: args.id }; |
20 |
| - const update = { name: args.name, likes: args.likes }; |
21 |
| - const options = { new: true }; |
22 |
| - const resp = await Tests.findOneAndUpdate(filter, update, options); |
23 |
| - |
24 |
| - console.log('Updated database with', resp); |
25 |
| - |
26 |
| - if (resp) return { description: resp.name, id: resp._id, likes: resp.likes }; |
27 |
| - return { description: 'Error updating', id: resp._id, likes: 0 }; |
28 |
| - }, |
29 |
| - |
30 |
| - deleteTest: async (parent, args) => { |
31 |
| - const filter = { _id: args.id }; |
32 |
| - const resp = await Tests.deleteOne(filter); |
33 |
| - if (resp) return { description: resp.name, id: resp._id, likes: 0 }; |
34 |
| - return { description: 'Error updating', likes: 0 }; |
35 |
| - }, |
36 |
| -}; |
37 |
| - |
38 |
| - |
39 | 9 | const Project = {
|
40 | 10 | addLike: async (parent, { projId, likes }) => {
|
41 | 11 | const filter = { _id: projId };
|
42 | 12 | const update = { likes };
|
43 | 13 | const options = { new: true };
|
44 | 14 | const resp = await Projects.findOneAndUpdate(filter, update, options);
|
45 |
| - |
| 15 | + |
46 | 16 | if (resp) {
|
47 | 17 | return ({
|
48 | 18 | name: resp.name,
|
49 | 19 | id: resp._id,
|
50 | 20 | userId: resp.userId,
|
51 |
| - username: resp.username, |
52 | 21 | likes: resp.likes,
|
| 22 | + published: resp.published, |
53 | 23 | });
|
54 | 24 | }
|
55 | 25 |
|
56 |
| - // TODO: Go back to this to see how to handle error later |
57 |
| - return { |
58 |
| - name: 'Error', |
59 |
| - id: 'Error', |
60 |
| - userId: 'Error', |
61 |
| - username: 'Error', |
62 |
| - likes: -1, |
63 |
| - }; |
| 26 | + throw new UserInputError('Project is not found. Please try another project ID', { |
| 27 | + argumentName: 'projId', |
| 28 | + }); |
64 | 29 | },
|
65 | 30 |
|
66 | 31 | makeCopy: async (parent, { projId, userId, username }) => {
|
67 | 32 | const filter = { _id: projId };
|
68 | 33 | const target = await Projects.findOne(filter);
|
69 |
| - |
70 |
| - // make a copy with the passed in userId |
71 |
| - // IMPORTANT: DO NOT CHANGE copy.name, it will create a nother copy of the document with the origional project name. |
72 |
| - const copy = { |
73 |
| - name: target.name, |
74 |
| - project: target.project, |
75 |
| - userId, |
76 |
| - username, |
77 |
| - }; |
78 |
| - |
79 |
| - // IMPORTANT: MUST MAKE A DEEP COPY OF target.project, otherwise, field 'style' is lost during the copy process. See 'minimize' option in projectSchema |
80 |
| - |
81 |
| - const resp = await Projects.create(copy); |
82 | 34 |
|
| 35 | + if (!target) { |
| 36 | + throw new UserInputError('Project is not found. Please try another project ID', { |
| 37 | + argumentName: 'projId', |
| 38 | + }); |
| 39 | + } |
| 40 | + |
| 41 | + // check if user account exists |
| 42 | + const user = await Users.findOne({ _id: userId }); |
| 43 | + |
| 44 | + if (user) { |
| 45 | + // make a copy with the passed in userId |
| 46 | + const copy = { |
| 47 | + name: target.name, |
| 48 | + project: target.project, |
| 49 | + userId, |
| 50 | + username, |
| 51 | + }; |
| 52 | + |
| 53 | + // Make a copy of the requested project |
| 54 | + const resp = await Projects.create(copy); |
| 55 | + if (resp) { |
| 56 | + return ({ |
| 57 | + name: resp.name, |
| 58 | + id: resp._id, |
| 59 | + userId: resp.userId, |
| 60 | + username: resp.username, |
| 61 | + likes: resp.likes, |
| 62 | + published: resp.published, |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + throw new UserInputError('Internal Server Error'); |
| 67 | + } |
| 68 | + |
| 69 | + throw new UserInputError('User is not found. Please try another user ID', { |
| 70 | + argumentName: 'userId', |
| 71 | + }); |
| 72 | + }, |
| 73 | + |
| 74 | + deleteProject: async (parent, { projId }) => { |
| 75 | + const filter = { _id: projId }; |
| 76 | + const options = { strict: true }; |
| 77 | + const resp = await Projects.findOneAndDelete(filter, options); |
| 78 | + |
83 | 79 | if (resp) {
|
84 | 80 | return ({
|
85 | 81 | name: resp.name,
|
86 | 82 | id: resp._id,
|
87 | 83 | userId: resp.userId,
|
88 |
| - username: resp.username, |
89 | 84 | likes: resp.likes,
|
| 85 | + published: resp.published, |
90 | 86 | });
|
91 | 87 | }
|
92 | 88 |
|
93 |
| - // TODO: Go back to this to see how to handle error later |
94 |
| - return { |
95 |
| - name: 'Error', |
96 |
| - id: 'Error', |
97 |
| - userId: 'Error', |
98 |
| - username: 'Error', |
99 |
| - likes: -1, |
100 |
| - }; |
| 89 | + throw new UserInputError('Project is not found. Please try another project ID', { |
| 90 | + argumentName: 'projId', |
| 91 | + }); |
101 | 92 | },
|
102 | 93 |
|
103 |
| - deleteProject: async (parent, { projId }) => { |
| 94 | + publishProject: async (parent, { projId, published }) => { |
| 95 | + |
104 | 96 | const filter = { _id: projId };
|
105 |
| - const options = { strict: true }; |
106 |
| - const resp = await Projects.findOneAndDelete(filter, options); |
107 |
| - // console.log("resp", resp); |
| 97 | + const update = { published }; |
| 98 | + const options = { new: true }; |
| 99 | + const resp = await Projects.findOneAndUpdate(filter, update, options); |
108 | 100 | if (resp) {
|
109 | 101 | return ({
|
110 | 102 | name: resp.name,
|
111 |
| - likes: resp.likes, |
112 | 103 | id: resp._id,
|
113 | 104 | userId: resp.userId,
|
114 |
| - username: resp.username, |
| 105 | + likes: resp.likes, |
| 106 | + published: resp.published, |
115 | 107 | });
|
116 | 108 | }
|
117 |
| - return { |
118 |
| - name: 'Error', |
119 |
| - id: 'Error', |
120 |
| - userId: 'Error', |
121 |
| - username: 'Error', |
122 |
| - likes: -1, |
123 |
| - }; |
| 109 | + |
| 110 | + throw new UserInputError('Project is not found. Please try another project ID', { |
| 111 | + argumentName: 'projId', |
| 112 | + }); |
124 | 113 | },
|
125 |
| -}; |
126 | 114 |
|
127 |
| -module.exports = { |
128 |
| - TestMutation: Test, |
129 |
| - ProjectMutation: Project, |
130 | 115 | };
|
| 116 | + |
| 117 | +module.exports = Project; |
0 commit comments