Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
KSZK
DevTeam
kszkepzes
Backend
Commits
294e544d
Commit
294e544d
authored
Aug 11, 2020
by
rlacko
💬
Browse files
separate application controller
parent
16e6aa8d
Pipeline
#4785
passed with stages
in 2 minutes and 46 seconds
Changes
6
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
src/resources/application/applicationRouter.js
View file @
294e544d
const
{
Router
}
=
require
(
'
express
'
)
const
controllers
=
require
(
'
./
applicationC
ontrollers
'
)
const
controllers
=
require
(
'
./
c
ontrollers
'
)
const
{
isLoggedIn
,
isMentor
}
=
require
(
'
../../middlewares/auth
'
)
const
router
=
Router
()
...
...
@@ -7,15 +7,13 @@ const router = Router()
// /api/v1/application
router
.
route
(
'
/
'
)
.
get
(
isLoggedIn
,
isMentor
,
controllers
.
default
.
getMany
)
.
post
(
isLoggedIn
,
controllers
.
default
.
createOne
)
.
get
(
isLoggedIn
,
isMentor
,
controllers
.
getMany
)
.
post
(
isLoggedIn
,
controllers
.
createOne
)
// /api/v1/application/id/:id
router
.
route
(
'
/id/:id
'
).
get
(
isLoggedIn
,
controllers
.
default
.
getOneByID
)
router
.
route
(
'
/id/:id
'
).
get
(
isLoggedIn
,
controllers
.
getOneByID
)
// /api/v1/application/schacc/:schacc
router
.
route
(
'
/schacc/:schacc
'
)
.
get
(
isLoggedIn
,
controllers
.
default
.
getOneBySchacc
)
router
.
route
(
'
/schacc/:schacc
'
).
get
(
isLoggedIn
,
controllers
.
getOneBySchacc
)
exports
.
default
=
router
src/resources/application/
applicationControllers
.js
→
src/resources/application/
controllers/createOne
.js
View file @
294e544d
const
{
crudControllers
}
=
require
(
'
../../utils/crud
'
)
const
{
Application
}
=
require
(
'
./applicationModel
'
)
const
{
Groups
}
=
require
(
'
../groups/groupsModel
'
)
const
{
Settings
}
=
require
(
'
../settings/settingsModel
'
)
const
{
pick
,
merge
}
=
require
(
'
lodash
'
)
const
{
isValidObjectId
}
=
require
(
'
mongoose
'
)
exports
.
default
=
crudControllers
(
Application
,
[
'
_id
'
,
'
motivation
'
,
'
expectation
'
,
'
solution
'
,
'
groups
'
,
])
const
{
Application
}
=
require
(
'
../applicationModel
'
)
const
{
Groups
}
=
require
(
'
../../groups/groupsModel
'
)
const
{
Settings
}
=
require
(
'
../../settings/settingsModel
'
)
const
{
pick
}
=
require
(
'
lodash
'
)
const
{
pickedKeys
}
=
require
(
'
.
'
)
const
findGroupIds
=
async
(
group
)
=>
{
groupObject
=
await
Groups
.
findOne
({
name
:
group
}).
lean
().
exec
()
...
...
@@ -20,7 +11,7 @@ const findGroupIds = async (group) => {
return
groupObject
.
_id
}
exports
.
default
.
createOne
=
async
(
req
,
res
)
=>
{
exports
.
createOne
=
async
(
req
,
res
)
=>
{
try
{
// Convert group names to Ids
let
groupIds
=
await
Promise
.
all
(
...
...
@@ -37,7 +28,7 @@ exports.default.createOne = async (req, res) => {
creator
:
req
.
body
.
creator
,
},
{
upsert
:
true
,
// Create new One if there is no match
upsert
:
true
,
returnOriginal
:
false
,
setDefaultsOnInsert
:
true
,
runValidators
:
true
,
...
...
@@ -56,7 +47,7 @@ exports.default.createOne = async (req, res) => {
state
:
req
.
body
.
state
,
},
{
upsert
:
true
,
// Create new One if there is no match
upsert
:
true
,
returnOriginal
:
false
,
setDefaultsOnInsert
:
true
,
runValidators
:
true
,
...
...
@@ -89,7 +80,7 @@ exports.default.createOne = async (req, res) => {
groups
:
groupIds
,
},
{
upsert
:
true
,
// Create new One if there is no match
upsert
:
true
,
returnOriginal
:
false
,
setDefaultsOnInsert
:
true
,
runValidators
:
true
,
...
...
@@ -107,15 +98,7 @@ exports.default.createOne = async (req, res) => {
.
exec
()
retDoc
.
creator
=
retDoc
.
_creator
return
res
.
status
(
201
).
json
({
data
:
pick
(
retDoc
,
[
'
_id
'
,
'
creator
'
,
'
motivation
'
,
'
expectation
'
,
'
solution
'
,
'
groups
'
,
'
state
'
,
]),
data
:
pick
(
retDoc
,
pickedKeys
),
})
}
catch
(
err
)
{
if
(
err
.
name
==
'
ValidationError
'
)
{
...
...
@@ -133,112 +116,3 @@ exports.default.createOne = async (req, res) => {
return
res
.
status
(
400
).
end
()
}
}
exports
.
default
.
getMany
=
async
(
req
,
res
)
=>
{
try
{
const
docs
=
await
Application
.
find
()
.
populate
(
'
groups
'
,
[
'
name
'
])
.
populate
(
'
_creator
'
,
[
'
schacc
'
,
'
fullName
'
,
'
secondaryEmail
'
])
.
lean
()
.
exec
()
res
.
status
(
200
).
json
({
data
:
docs
.
map
((
e
)
=>
{
e
.
creator
=
e
.
_creator
return
pick
(
e
,
[
'
_id
'
,
'
creator
'
,
'
motivation
'
,
'
expectation
'
,
'
solution
'
,
'
groups
'
,
'
state
'
,
])
}),
})
}
catch
(
e
)
{
console
.
error
(
e
)
res
.
status
(
400
).
end
()
}
}
exports
.
default
.
getOneByID
=
async
(
req
,
res
)
=>
{
try
{
let
doc
if
(
req
.
params
.
id
===
'
own
'
)
doc
=
Application
.
findOne
({
creator
:
req
.
user
.
schacc
})
else
doc
=
Application
.
findOne
({
_id
:
req
.
params
.
id
})
doc
=
await
doc
.
populate
(
'
groups
'
,
[
'
name
'
])
.
populate
(
'
_creator
'
,
[
'
schacc
'
,
'
fullName
'
,
'
secondaryEmail
'
])
.
lean
()
.
exec
()
if
(
!
doc
)
{
return
res
.
status
(
404
).
end
()
}
if
(
doc
.
creator
!=
req
.
user
.
schacc
&&
req
.
user
.
role
!=
'
mentor
'
)
return
res
.
status
(
403
).
end
()
doc
.
creator
=
doc
.
_creator
res
.
status
(
200
).
json
({
data
:
pick
(
doc
,
[
'
_id
'
,
'
creator
'
,
'
motivation
'
,
'
expectation
'
,
'
solution
'
,
'
groups
'
,
'
state
'
,
]),
})
}
catch
(
err
)
{
if
(
err
.
name
==
'
CastError
'
)
{
// Throwed by Mongoose
return
res
.
status
(
422
).
json
(
'
Invalid ID provided
'
)
}
else
{
console
.
error
(
err
)
res
.
status
(
400
).
end
()
}
}
}
exports
.
default
.
getOneBySchacc
=
async
(
req
,
res
)
=>
{
try
{
let
doc
=
await
Application
.
findOne
({
creator
:
req
.
params
.
schacc
})
.
populate
(
'
groups
'
,
[
'
name
'
])
.
populate
(
'
_creator
'
,
[
'
schacc
'
,
'
fullName
'
,
'
secondaryEmail
'
])
.
lean
()
.
exec
()
if
(
!
doc
)
{
return
res
.
status
(
404
).
end
()
}
if
(
doc
.
creator
!=
req
.
user
.
schacc
&&
req
.
user
.
role
!=
'
mentor
'
)
return
res
.
status
(
403
).
end
()
doc
.
creator
=
doc
.
_creator
res
.
status
(
200
).
json
({
data
:
pick
(
doc
,
[
'
_id
'
,
'
creator
'
,
'
motivation
'
,
'
expectation
'
,
'
solution
'
,
'
groups
'
,
'
state
'
,
]),
})
}
catch
(
err
)
{
if
(
err
.
name
==
'
CastError
'
)
{
// Throwed by Mongoose
return
res
.
status
(
422
).
json
(
'
Invalid ID provided
'
)
}
else
{
console
.
error
(
err
)
res
.
status
(
400
).
end
()
}
}
}
src/resources/application/controllers/getMany.js
0 → 100644
View file @
294e544d
const
{
Application
}
=
require
(
'
../applicationModel
'
)
const
{
pick
}
=
require
(
'
lodash
'
)
const
{
pickedKeys
}
=
require
(
'
.
'
)
exports
.
getMany
=
async
(
req
,
res
)
=>
{
try
{
const
docs
=
await
Application
.
find
()
.
populate
(
'
groups
'
,
[
'
name
'
])
.
populate
(
'
_creator
'
,
[
'
schacc
'
,
'
fullName
'
,
'
secondaryEmail
'
])
.
lean
()
.
exec
()
res
.
status
(
200
).
json
({
data
:
docs
.
map
((
e
)
=>
{
e
.
creator
=
e
.
_creator
return
pick
(
e
,
pickedKeys
)
}),
})
}
catch
(
e
)
{
console
.
error
(
e
)
res
.
status
(
400
).
end
()
}
}
src/resources/application/controllers/getOneByID.js
0 → 100644
View file @
294e544d
const
{
Application
}
=
require
(
'
../applicationModel
'
)
const
{
pick
}
=
require
(
'
lodash
'
)
const
{
pickedKeys
}
=
require
(
'
.
'
)
exports
.
getOneByID
=
async
(
req
,
res
)
=>
{
try
{
let
doc
if
(
req
.
params
.
id
===
'
own
'
)
doc
=
Application
.
findOne
({
creator
:
req
.
user
.
schacc
})
else
doc
=
Application
.
findOne
({
_id
:
req
.
params
.
id
})
doc
=
await
doc
.
populate
(
'
groups
'
,
[
'
name
'
])
.
populate
(
'
_creator
'
,
[
'
schacc
'
,
'
fullName
'
,
'
secondaryEmail
'
])
.
lean
()
.
exec
()
if
(
!
doc
)
{
return
res
.
status
(
404
).
end
()
}
if
(
doc
.
creator
!=
req
.
user
.
schacc
&&
req
.
user
.
role
!=
'
mentor
'
)
return
res
.
status
(
403
).
end
()
doc
.
creator
=
doc
.
_creator
res
.
status
(
200
).
json
({
data
:
pick
(
doc
,
pickedKeys
),
})
}
catch
(
err
)
{
if
(
err
.
name
==
'
CastError
'
)
{
// Throwed by Mongoose
return
res
.
status
(
422
).
json
(
'
Invalid ID provided
'
)
}
else
{
console
.
error
(
err
)
res
.
status
(
400
).
end
()
}
}
}
src/resources/application/controllers/getOneBySchacc.js
0 → 100644
View file @
294e544d
const
{
Application
}
=
require
(
'
../applicationModel
'
)
const
{
pick
}
=
require
(
'
lodash
'
)
const
{
pickedKeys
}
=
require
(
'
.
'
)
exports
.
getOneBySchacc
=
async
(
req
,
res
)
=>
{
try
{
let
doc
=
await
Application
.
findOne
({
creator
:
req
.
params
.
schacc
})
.
populate
(
'
groups
'
,
[
'
name
'
])
.
populate
(
'
_creator
'
,
[
'
schacc
'
,
'
fullName
'
,
'
secondaryEmail
'
])
.
lean
()
.
exec
()
if
(
!
doc
)
{
return
res
.
status
(
404
).
end
()
}
if
(
doc
.
creator
!=
req
.
user
.
schacc
&&
req
.
user
.
role
!=
'
mentor
'
)
return
res
.
status
(
403
).
end
()
doc
.
creator
=
doc
.
_creator
res
.
status
(
200
).
json
({
data
:
pick
(
doc
,
pickedKeys
),
})
}
catch
(
err
)
{
if
(
err
.
name
==
'
CastError
'
)
{
// Throwed by Mongoose
return
res
.
status
(
422
).
json
(
'
Invalid ID provided
'
)
}
else
{
console
.
error
(
err
)
res
.
status
(
400
).
end
()
}
}
}
src/resources/application/controllers/index.js
0 → 100644
View file @
294e544d
exports
.
pickedKeys
=
[
'
_id
'
,
'
creator
'
,
'
motivation
'
,
'
expectation
'
,
'
solution
'
,
'
groups
'
,
'
state
'
,
]
exports
.
createOne
=
require
(
'
./createOne
'
).
createOne
exports
.
getMany
=
require
(
'
./getMany
'
).
getMany
exports
.
getOneByID
=
require
(
'
./getOneByID
'
).
getOneByID
exports
.
getOneBySchacc
=
require
(
'
./getOneBySchacc
'
).
getOneBySchacc
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment