Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
fahrtenbuch
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Analyze
Contributor 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
SWP WS21_22 - Fahrtenbuch
Team Einhorn
fahrtenbuch
Commits
8e666717
Commit
8e666717
authored
3 years ago
by
elit04
Browse files
Options
Downloads
Patches
Plain Diff
sport routes+controllers changes/modifications
parent
32bc4236
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
server/src/controllers/sport.controllers.ts
+68
-1
68 additions, 1 deletion
server/src/controllers/sport.controllers.ts
server/src/routes/sport.routes.ts
+8
-1
8 additions, 1 deletion
server/src/routes/sport.routes.ts
with
76 additions
and
2 deletions
server/src/controllers/sport.controllers.ts
+
68
−
1
View file @
8e666717
...
...
@@ -25,6 +25,18 @@ const createSportController = async (req: Request, res: Response) => {
.
json
({
success
:
false
,
error
:
"
sportAlreadyExists
"
});
}
//check if color already assigned to another sport (for the statistics)
const
findIfColorAlreadyAssigned
=
await
Sport
.
findOne
({
where
:
{
color
:
newSportInput
.
color
,
},
});
if
(
findIfColorAlreadyAssigned
)
{
return
res
.
status
(
404
)
.
json
({
success
:
false
,
error
:
"
givenSportColorAlreadyExists
"
});
}
const
newSport
=
await
Sport
.
create
(
newSportInput
);
if
(
newSport
)
{
...
...
@@ -33,6 +45,7 @@ const createSportController = async (req: Request, res: Response) => {
result
:
{
id
:
newSport
.
id
,
name
:
newSport
.
name
,
color
:
newSport
.
color
,
},
});
}
...
...
@@ -91,7 +104,7 @@ const deleteSportById = async (req: Request, res: Response) => {
if
(
sportToDelete
==
0
)
{
return
res
.
status
(
404
)
.
json
({
success
:
false
,
error
:
"
accoun
tIdDoesNotExist
"
});
.
json
({
success
:
false
,
error
:
"
spor
tIdDoesNotExist
"
});
}
return
res
.
status
(
204
).
json
({
success
:
true
});
...
...
@@ -131,6 +144,34 @@ const updateSportById = async (req: Request, res: Response) => {
return
res
.
status
(
404
).
json
({
success
:
false
,
error
:
"
sportIdNotFound
"
});
}
//check if updated name of the new sport already exists in DB
if
(
!
(
input
.
name
===
undefined
))
{
const
findIfNameAlreadyExists
=
await
Sport
.
findOne
({
where
:
{
name
:
input
.
name
,
},
});
if
(
findIfNameAlreadyExists
)
{
return
res
.
status
(
404
)
.
json
({
success
:
false
,
error
:
"
givenSportNameAlreadyExists
"
});
}
}
//check if color already assigned to another sport (for the statistics)
if
(
!
(
input
.
color
===
undefined
))
{
const
findIfColorAlreadyAssigned
=
await
Sport
.
findOne
({
where
:
{
color
:
input
.
color
,
},
});
if
(
findIfColorAlreadyAssigned
)
{
return
res
.
status
(
404
)
.
json
({
success
:
false
,
error
:
"
givenSportColorAlreadyExists
"
});
}
}
//try to update
const
updatedSport
=
await
Sport
.
update
(
input
,
{
where
:
{
...
...
@@ -183,6 +224,7 @@ const showSportByBoatId = async (req: Request, res: Response) => {
//define empty array, where we store founded names of sports assigned to boat's id
var
list
=
new
Array
();
for
(
let
i
=
0
;
i
<
findAllBoatId
.
length
;
i
++
)
{
const
found
=
await
Sport
.
findByPk
(
findAllBoatId
[
i
].
sportid
);
list
.
push
(
found
.
name
);
...
...
@@ -197,12 +239,37 @@ const showSportByBoatId = async (req: Request, res: Response) => {
}
};
const
showSportBySportId
=
async
(
req
:
Request
,
res
:
Response
)
=>
{
try
{
if
(
!
(
res
.
locals
.
user
.
role
===
"
coordinator
"
))
{
return
res
.
status
(
403
)
.
json
({
success
:
false
,
error
:
"
MustBeCoordinator
"
});
}
const
givenId
=
req
.
params
.
id
;
const
sport
=
await
Sport
.
findByPk
(
givenId
);
if
(
sport
)
{
return
res
.
status
(
200
)
.
json
({
success
:
true
,
result
:
{
id
:
givenId
,
name
:
sport
.
name
,
color
:
sport
.
color
},
});
}
return
res
.
status
(
404
).
json
({
success
:
false
,
error
:
"
sportIdNotFound
"
});
}
catch
(
error
)
{
console
.
error
(
"
server error:
"
,
error
.
message
);
return
res
.
status
(
500
).
json
({
success
:
false
,
error
:
"
serverError
"
});
}
};
const
sportControllers
=
{
createSportController
,
showAllSports
,
deleteSportById
,
updateSportById
,
showSportByBoatId
,
showSportBySportId
,
};
export
default
sportControllers
;
This diff is collapsed.
Click to expand it.
server/src/routes/sport.routes.ts
+
8
−
1
View file @
8e666717
...
...
@@ -33,7 +33,7 @@ sportRouter.delete(
//update a sport
sportRouter
.
patch
(
"
/api/sport/:id/
"
,
body
(
"
name
"
).
if
(
body
(
"
color
"
).
exists
()).
not
().
isEmpty
(),
body
(
"
name
"
).
if
(
body
(
"
name
"
).
exists
()).
not
().
isEmpty
(),
body
(
"
color
"
).
if
(
body
(
"
color
"
).
exists
()).
not
().
isEmpty
(),
handleValidationResult
,
validateToken
,
...
...
@@ -47,4 +47,11 @@ sportRouter.get(
sportControllers
.
showSportByBoatId
);
//show specific sport by sport id
sportRouter
.
get
(
"
/api/sport/id/:id/
"
,
validateToken
,
sportControllers
.
showSportBySportId
)
export
default
sportRouter
;
\ No newline at end of file
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