Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
DojoBackendAPI
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
External 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
Dojo Project (HES-SO)
Projects
Backend
DojoBackendAPI
Commits
0dc8e76d
Commit
0dc8e76d
authored
2 years ago
by
michael.minelli
Browse files
Options
Downloads
Patches
Plain Diff
Add Enonce routes
parent
92126200
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
ExpressAPI/src/routes/ApiRoutesManager.ts
+2
-0
2 additions, 0 deletions
ExpressAPI/src/routes/ApiRoutesManager.ts
ExpressAPI/src/routes/EnonceRoutes.ts
+87
-0
87 additions, 0 deletions
ExpressAPI/src/routes/EnonceRoutes.ts
with
89 additions
and
0 deletions
ExpressAPI/src/routes/ApiRoutesManager.ts
+
2
−
0
View file @
0dc8e76d
...
...
@@ -2,6 +2,7 @@ import { Express } from 'express-serve-static-core';
import
RoutesManager
from
'
../express/RoutesManager
'
;
import
BaseRoutes
from
'
./BaseRoutes
'
;
import
SessionRoutes
from
'
./SessionRoutes
'
;
import
EnonceRoutes
from
'
./EnonceRoutes
'
;
class
AdminRoutesManager
implements
RoutesManager
{
...
...
@@ -20,6 +21,7 @@ class AdminRoutesManager implements RoutesManager {
registerOnBackend
(
backend
:
Express
)
{
BaseRoutes
.
registerOnBackend
(
backend
);
SessionRoutes
.
registerOnBackend
(
backend
);
EnonceRoutes
.
registerOnBackend
(
backend
);
}
}
...
...
This diff is collapsed.
Click to expand it.
ExpressAPI/src/routes/EnonceRoutes.ts
0 → 100644
+
87
−
0
View file @
0dc8e76d
import
{
Express
}
from
'
express-serve-static-core
'
;
import
express
from
'
express
'
;
import
*
as
ExpressValidator
from
'
express-validator
'
;
import
{
StatusCodes
}
from
'
http-status-codes
'
;
import
RoutesManager
from
'
../express/RoutesManager
'
;
import
ParamsValidatorMiddleware
from
'
../middlewares/ParamsValidatorMiddleware
'
;
import
ApiRequest
from
'
../models/ApiRequest
'
;
import
SecurityMiddleware
from
'
../middlewares/SecurityMiddleware
'
;
import
SecurityCheckType
from
'
../types/SecurityCheckType
'
;
import
GitlabUser
from
'
../shared/types/Gitlab/GitlabUser
'
;
import
GitlabHelper
from
'
../helpers/GitlabHelper
'
;
import
Config
from
'
../config/Config
'
;
import
GitlabMember
from
'
../shared/types/Gitlab/GitlabMember
'
;
import
GitlabAccessLevel
from
'
../shared/types/Gitlab/GitlabAccessLevel
'
;
import
GitlabRepository
from
'
../shared/types/Gitlab/GitlabRepository
'
;
import
UserManager
from
'
../managers/UserManager
'
;
import
User
from
'
../models/User
'
;
import
Enonce
from
'
../models/Enonce
'
;
import
EnonceStaff
from
'
../models/EnonceStaff
'
;
class
EnonceRoutes
implements
RoutesManager
{
private
static
_instance
:
EnonceRoutes
;
private
constructor
()
{
}
public
static
get
instance
():
EnonceRoutes
{
if
(
!
EnonceRoutes
.
_instance
)
{
EnonceRoutes
.
_instance
=
new
EnonceRoutes
();
}
return
EnonceRoutes
.
_instance
;
}
private
readonly
enonceValidator
:
ExpressValidator
.
Schema
=
{
name
:
{
trim
:
true
,
notEmpty
:
true
},
members
:
{
trim
:
true
,
notEmpty
:
true
}
};
registerOnBackend
(
backend
:
Express
)
{
backend
.
post
(
'
/enonces
'
,
SecurityMiddleware
.
check
(
true
,
SecurityCheckType
.
TEACHING_STAFF
),
ParamsValidatorMiddleware
.
validate
(
this
.
enonceValidator
),
this
.
createEnonce
);
}
private
async
createEnonce
(
req
:
ApiRequest
,
res
:
express
.
Response
)
{
const
params
:
{
name
:
string
,
members
:
string
}
=
req
.
body
;
const
gitlabMembers
:
Array
<
GitlabUser
>
=
JSON
.
parse
(
params
.
members
)
as
Array
<
GitlabUser
>
;
const
repository
:
GitlabRepository
=
await
GitlabHelper
.
createRepository
(
params
.
name
,
Config
.
enonce
.
default
.
description
.
replace
(
'
{{ENONCE_NAME}}
'
,
params
.
name
),
Config
.
enonce
.
default
.
visibility
,
Config
.
enonce
.
default
.
initReadme
,
Config
.
gitlab
.
group
.
enonces
,
Config
.
enonce
.
default
.
sharedRunnersEnabled
,
Config
.
enonce
.
default
.
wikiEnabled
,
Config
.
enonce
.
default
.
template
);
const
members
:
Array
<
GitlabMember
|
false
>
=
await
Promise
.
all
([
req
.
session
.
profile
.
userGitlabId
,
...
gitlabMembers
.
map
(
member
=>
member
.
id
)
].
map
(
async
(
memberId
:
number
):
Promise
<
GitlabMember
|
false
>
=>
{
try
{
return
await
GitlabHelper
.
addRepositoryMember
(
repository
.
id
,
memberId
,
GitlabAccessLevel
.
Maintainer
);
}
catch
(
e
)
{
return
false
;
}
}));
const
enonce
:
Enonce
=
Enonce
.
createFromSql
({
enonceName
:
repository
.
name
,
enonceGitlabId
:
repository
.
id
,
enonceGitlabLink
:
repository
.
web_url
,
enonceGitlabCreationInfo
:
JSON
.
stringify
(
repository
),
enonceGitlabLastInfo
:
JSON
.
stringify
(
repository
),
enonceGitlabLastInfoTs
:
new
Date
().
getTime
()
});
await
enonce
.
create
();
let
dojoUsers
:
Array
<
User
>
=
[
req
.
session
.
profile
,
...(
await
UserManager
.
getByGitlabIds
(
gitlabMembers
.
map
(
member
=>
member
.
id
))).
filter
(
user
=>
user
)
];
// TODO: Remplacer le filter par une map qui créer l'utilisateur inconnu.
dojoUsers
=
dojoUsers
.
reduce
((
unique
,
user
)
=>
(
unique
.
findIndex
(
uniqueUser
=>
uniqueUser
.
userID
===
user
.
userID
)
!==
-
1
?
unique
:
[
...
unique
,
user
]),
Array
<
User
>
());
await
Promise
.
all
(
dojoUsers
.
map
(
async
(
dojoUser
:
User
)
=>
{
return
EnonceStaff
.
createFromSql
({
enonceID
:
enonce
.
enonceID
,
userID
:
dojoUser
.
userID
});
}));
req
.
session
.
sendResponse
(
res
,
StatusCodes
.
OK
,
enonce
);
}
}
export
default
EnonceRoutes
.
instance
;
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