Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
D
Daily Thought App
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
COM3014-Coursework
Daily Thought App
Commits
50cac579
Commit
50cac579
authored
2 years ago
by
Talty-Kerr, Patrick (UG - Comp Sci & Elec Eng)
Browse files
Options
Downloads
Patches
Plain Diff
post request response + jwt done
parent
2b30a577
No related branches found
No related tags found
1 merge request
!13
Create Comment service requirements
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
comment-service/src/controllers/Comment.ts
+46
-12
46 additions, 12 deletions
comment-service/src/controllers/Comment.ts
with
46 additions
and
12 deletions
comment-service/src/controllers/Comment.ts
+
46
−
12
View file @
50cac579
...
...
@@ -7,6 +7,9 @@ import { config } from "../config/config";
import
Logging
from
"
../library/logging
"
;
const
createComment
=
async
(
req
:
Request
,
res
:
Response
,
next
:
NextFunction
,)
=>
{
const
{
post_id
,
user_id
,
body
}
=
req
.
body
;
const
comment
=
new
Comment
({
...
...
@@ -16,35 +19,66 @@ const createComment = async ( req: Request, res: Response, next: NextFunction,)
body
});
//Try Catach Block to test for whether Post Exsits
/*
try{
const posts = {post_id : new mongoose.Types.ObjectId(post_id)}
const post_exist = await Comment.countDocuments(posts, { limit: 1 })
if(post_exist == 0) {
return res.status(404).send({message: 'The specified post or user does not exist.'});
}
}
catch{
return res.status(404).send({message: 'The specified post or user does not exist.'});
}
*/
//Try Catch Block to test whether Users Exsits
/*
try{
const users = {post_id : new mongoose.Types.ObjectId(user_id)}
const users_exist = await Comment.countDocuments(users, { limit: 1 })
if(users_exist == 0) {
return res.status(404).send({message: 'The specified post or user does not exist.'});
}
}
catch{
return res.status(404).send({message: 'The specified post or user does not exist.'});
}
*/
le
t
token
=
req
.
headers
.
authorization
?.
split
(
'
'
)[
1
];
cons
t
token
=
req
.
headers
.
authorization
?.
split
(
'
'
)[
1
];
if
(
!
token
)
{
return
res
.
status
(
40
1
).
send
({
message
:
'
Unauthorized
'
});
return
res
.
status
(
40
0
).
send
({
message
:
'
Bad Request
'
});
}
const
decodedToken
=
jwt
.
verify
(
token
,
config
.
server
.
token
.
secret
);
const
decodeTokenToString
=
decodedToken
;
const
tokenString
=
JSON
.
stringify
(
decodeTokenToString
);
const
JSobj
=
JSON
.
parse
(
tokenString
).
user_id
;
try
{
const
decodedToken
=
jwt
.
verify
(
token
,
config
.
server
.
token
.
secret
);
const
decodeTokenToString
=
decodedToken
;
const
tokenString
=
JSON
.
stringify
(
decodeTokenToString
);
const
JSobj
=
JSON
.
parse
(
tokenString
).
user_id
;
if
(
JSobj
==
user_id
){
const
comment_1
=
await
comment
.
save
();
return
res
.
status
(
201
).
json
({
comment_1
});
}
else
{
return
res
.
status
(
401
).
send
({
message
:
'
Unauthorized
'
});
}
}
catch
(
error
)
{
return
res
.
status
(
5
00
).
json
({
error
});
return
res
.
status
(
4
00
).
json
({
message
:
'
Bad Request
'
});
}
};
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment