Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
CaCaoCritics
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Bilel El Yaagoubi
CaCaoCritics
Commits
ca07bd62
Commit
ca07bd62
authored
3 years ago
by
Bilel El Yaagoubi
Browse files
Options
Downloads
Plain Diff
Merge branch 'authorize_movie_like' into 'master'
You can now like movies See merge request
!15
parents
f701b26f
3ea1b46c
No related branches found
No related tags found
1 merge request
!15
You can now like movies
Pipeline
#42572
passed
3 years ago
Stage: lint
Stage: build
Changes
1
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
backend/routes/users.js
+41
-0
41 additions, 0 deletions
backend/routes/users.js
with
41 additions
and
0 deletions
backend/routes/users.js
+
41
−
0
View file @
ca07bd62
const
express
=
require
(
"
express
"
);
const
UserModel
=
require
(
"
../models/user
"
);
const
MovieModel
=
require
(
"
../models/movie
"
);
const
router
=
express
.
Router
();
router
.
get
(
"
/
"
,
function
(
req
,
res
)
{
...
...
@@ -32,6 +33,46 @@ router.post("/new", function (req, res) {
});
});
router
.
put
(
"
/like
"
,
async
function
(
req
,
res
)
{
try
{
const
userId
=
await
req
.
body
.
userId
;
const
movieId
=
await
req
.
body
.
movieId
;
const
movieOid
=
await
MovieModel
.
findOne
({
id
:
movieId
});
const
user
=
await
UserModel
.
findById
(
userId
);
const
likedMovies
=
user
.
liked_movies
.
concat
([
movieOid
.
_id
]);
await
UserModel
.
findByIdAndUpdate
(
userId
,
{
liked_movies
:
likedMovies
,
});
res
.
send
(
"
Done
"
);
}
catch
(
error
)
{
console
.
log
(
error
);
res
.
send
(
"
Internal problem
"
);
}
});
router
.
put
(
"
/unlike
"
,
async
function
(
req
,
res
)
{
try
{
const
userId
=
await
req
.
body
.
userId
;
const
movieId
=
await
req
.
body
.
movieId
;
const
movieOid
=
await
MovieModel
.
findOne
({
id
:
movieId
});
const
user
=
await
UserModel
.
findById
(
userId
);
const
likedMovies
=
user
.
liked_movies
;
const
myIndex
=
likedMovies
.
indexOf
(
movieOid
.
_id
);
if
(
myIndex
!==
-
1
)
{
likedMovies
.
splice
(
myIndex
,
1
);
await
UserModel
.
findByIdAndUpdate
(
userId
,
{
liked_movies
:
likedMovies
,
});
res
.
send
(
"
Done
"
);
}
else
{
res
.
send
(
"
This movie wasn't liked
"
);
}
}
catch
(
error
)
{
console
.
log
(
error
);
res
.
send
(
"
Internal problem
"
);
}
});
router
.
delete
(
"
/:userId
"
,
function
(
req
,
res
)
{
UserModel
.
deleteOne
({
_id
:
req
.
params
.
userId
})
.
then
(
function
()
{
...
...
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