Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Projet tutoré Developpement mobile
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
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
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
MOREAU Elise
Projet tutoré Developpement mobile
Commits
c1852701
Commit
c1852701
authored
4 years ago
by
Moreau Elise
Browse files
Options
Downloads
Patches
Plain Diff
[api][rooms routes] code typo + use newroom variable instead of room to avoid bug at room creation
parent
84643b94
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
api/routes/rooms_routes.js
+19
-13
19 additions, 13 deletions
api/routes/rooms_routes.js
with
19 additions
and
13 deletions
api/routes/rooms_routes.js
+
19
−
13
View file @
c1852701
...
...
@@ -26,7 +26,7 @@ router.post('/', validator.body(roomSchema), async function(req, res, next){
if
(
!
currentUser
){
return
res
.
status
(
401
).
json
({
'
error
'
:
'
User does
\'
n
t exist.
'
'
error
'
:
'
User does
n
\'
t exist.
'
});
}
...
...
@@ -45,14 +45,15 @@ router.post('/', validator.body(roomSchema), async function(req, res, next){
res
.
json
(
newRoom
);
})
emit
(
'
new-room
'
,
newRoom
,
newRoom
.
users
);
});
// Liste des room contenant l'utilisateur courant
// Pas de room publique
router
.
get
(
'
/
'
,
async
function
(
req
,
res
,
next
){
let
room
=
null
;
try
{
room
=
await
Room
.
find
({
users
:
req
.
user
.
id
}).
populate
(
'
users
'
);
room
=
await
Room
.
find
({
users
:
req
.
user
.
id
}).
populate
(
'
users
'
);
}
catch
(
err
)
{
return
next
(
err
);
}
...
...
@@ -64,29 +65,34 @@ router.get('/', async function (req, res, next){
}
return
res
.
json
(
room
);
})
})
;
// Récupère les messages d'une room
router
.
get
(
'
/:id/messages
'
,
async
function
(
req
,
res
,
next
)
{
let
room
=
null
;
try
{
room
=
await
Room
.
findOne
({
_id
:
req
.
params
.
id
,
users
:
req
.
user
.
id
});
room
=
await
Room
.
findById
(
req
.
params
.
id
);
}
catch
(
err
){
return
next
(
err
);
}
if
(
!
room
)
return
res
.
status
(
404
).
json
({
error
:
'
Room not found
'
});
if
(
!
room
)
return
res
.
status
(
404
).
json
({
error
:
'
Room not found
'
});
let
messages
=
null
;
try
{
messages
=
await
Message
.
find
({
room
:
room
.
_id
})
.
populate
(
'
author
'
)
.
sort
({
date
:
-
1
})
.
limit
(
50
);
messages
=
await
Message
.
find
({
room
:
room
.
_id
}).
sort
({
date
:
-
1
}).
limit
(
50
);
}
catch
(
err
)
{
return
next
(
err
);
}
return
res
.
json
(
messages
);
})
})
;
// Récupération de liste des utilisateurs d'une room dans laquelle est l'utilisateur courant
router
.
get
(
'
/:id/users
'
,
async
function
(
req
,
res
,
next
){
...
...
@@ -104,7 +110,7 @@ router.get('/:id/users', async function(req, res, next){
}
return
res
.
json
(
room
.
users
);
})
})
;
//Suppression d'une room
router
.
delete
(
'
/:id
'
,
async
function
(
req
,
res
,
next
){
...
...
@@ -116,7 +122,7 @@ router.delete('/:id', async function(req, res, next){
}
res
.
status
(
204
).
end
();
})
})
;
const
roomSchemaPut
=
Joi
.
object
({
name
:
Joi
.
string
().
required
().
trim
().
min
(
1
),
...
...
@@ -143,11 +149,11 @@ router.put('/:id', validator.body(roomSchemaPut), async function(req, res, next)
if
(
!
room
)
return
res
.
status
(
404
).
json
({
error
:
'
Room not found
'
});
return
res
.
json
(
room
);
})
})
;
const
roomSchemaUsers
=
Joi
.
object
({
ids
:
Joi
.
array
().
items
(
Joi
.
string
()).
required
()
})
})
;
//Ajouter un ou plusieurs utilisateurs dans une room
router
.
post
(
'
/:id/add_users
'
,
validator
.
body
(
roomSchemaUsers
),
async
function
(
req
,
res
,
next
){
...
...
@@ -179,7 +185,7 @@ router.post('/:id/add_users', validator.body(roomSchemaUsers), async function(re
room
.
save
();
return
res
.
json
(
room
);
})
})
;
//Supprimer des utilisateurs d'une room (excepté le créateur de celle ci)
router
.
post
(
'
/:id/remove_users
'
,
validator
.
body
(
roomSchemaUsers
),
async
function
(
req
,
res
,
next
){
...
...
@@ -197,12 +203,12 @@ router.post('/:id/remove_users', validator.body(roomSchemaUsers), async function
}
let
ids
=
req
.
body
.
ids
;
// On ne peut pas supprimer le créateur de la room de celle-ci
ids
=
ids
.
filter
(
id
=>
id
!==
room
.
createdBy
.
toString
())
ids
=
ids
.
filter
(
id
=>
id
!==
room
.
createdBy
.
toString
())
;
room
.
users
.
pull
(...
ids
);
room
.
save
();
return
res
.
json
(
room
);
})
})
;
export
default
router
;
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