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
382b9695
Commit
382b9695
authored
3 years ago
by
elit04
Browse files
Options
Downloads
Patches
Plain Diff
sport routes + controllers
parent
a8a94c3b
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
server/src/controllers/sport.controllers.ts
+159
-12
159 additions, 12 deletions
server/src/controllers/sport.controllers.ts
server/src/routes/sport.routes.ts
+28
-4
28 additions, 4 deletions
server/src/routes/sport.routes.ts
server/src/server.ts
+3
-0
3 additions, 0 deletions
server/src/server.ts
with
190 additions
and
16 deletions
server/src/controllers/sport.controllers.ts
+
159
−
12
View file @
382b9695
/*import { Request, Response } from "express";
import sport from "../db/models/Sport";
import
{
Request
,
Response
}
from
"
express
"
;
import
Boat
from
"
../db/models/Boat
"
;
import BoatType from "../db/models/BoatType";
import
BoatHasSport
from
"
../db/models/BoatHasSport
"
;
import
Sport
from
"
../db/models/Sport
"
;
//createSportController
//create
SportController
const
createSportController
=
async
(
req
:
Request
,
res
:
Response
)
=>
{
try
{
if
(
!
(
res
.
locals
.
user
.
role
==
"
coordinator
"
))
{
...
...
@@ -13,8 +13,17 @@ const createSportController = async (req: Request, res: Response) => {
}
const
newSportInput
=
req
.
body
;
//check if Sport already exists in DB to avoid duplicates
const
findIfSportExists
=
await
Sport
.
findOne
({
where
:
{
name
:
newSportInput
.
name
}
})
if
(
findIfSportExists
)
{
return
res
.
status
(
404
).
json
({
success
:
false
,
error
:
"
sportAlreadyExists
"
});
}
const newSport = await
s
port.create(newSportInput);
const
newSport
=
await
S
port
.
create
(
newSportInput
);
if
(
newSport
)
{
return
res
.
status
(
201
).
json
({
...
...
@@ -31,7 +40,7 @@ const createSportController = async (req: Request, res: Response) => {
}
};
//show all Sports
//show all Sports
const
showAllSports
=
async
(
req
:
Request
,
res
:
Response
)
=>
{
try
{
if
(
!
(
res
.
locals
.
user
.
role
===
"
coordinator
"
))
{
...
...
@@ -39,11 +48,11 @@ const showAllSports = async (req: Request, res: Response) => {
.
status
(
403
)
.
json
({
success
:
false
,
error
:
"
MustBeCoordinator
"
});
}
const allSports = await
s
port.findAll();
const
allSports
=
await
S
port
.
findAll
();
return
res
.
status
(
200
).
send
({
success
:
true
,
result: allSports.map((
s
port) => {
return { id:
s
port.id, name:
s
port.name };
result
:
allSports
.
map
((
S
port
)
=>
{
return
{
id
:
S
port
.
id
,
name
:
S
port
.
name
};
}),
});
}
catch
(
error
)
{
...
...
@@ -52,10 +61,148 @@ const showAllSports = async (req: Request, res: Response) => {
}
};
// delete a Sport by using given sport ID
const
deleteSportById
=
async
(
req
:
Request
,
res
:
Response
)
=>
{
try
{
//check authority
if
(
!
(
res
.
locals
.
user
.
role
===
"
coordinator
"
))
{
return
res
.
status
(
403
)
.
json
({
success
:
false
,
error
:
"
MustBeCoordinator
"
});
}
//delete using given id
const
givenId
=
req
.
params
.
id
;
//first we need to destroy entries where the to-be-deleted sport is assigned to boat in BoatHasSport table, otherwise it violates the foreign key constraint
await
BoatHasSport
.
destroy
(
{
where
:
{
sportid
:
givenId
,
}
}
)
const
sportToDelete
=
await
Sport
.
destroy
({
where
:
{
id
:
givenId
,
},
});
if
(
sportToDelete
==
0
)
{
return
res
.
status
(
404
)
.
json
({
success
:
false
,
error
:
"
accountIdDoesNotExist
"
});
}
return
res
.
status
(
204
).
json
({
success
:
true
});
}
catch
(
error
)
{
console
.
error
(
"
server error:
"
,
error
.
message
);
return
res
.
status
(
500
).
json
({
success
:
false
,
error
:
"
serverError
"
});
}
};
//update boat by given sport id
const
updateSportById
=
async
(
req
:
Request
,
res
:
Response
)
=>
{
try
{
//check authority
if
(
!
(
res
.
locals
.
user
.
role
===
"
coordinator
"
))
{
return
res
.
status
(
403
)
.
json
({
success
:
false
,
error
:
"
MustBeCoordinator
"
});
}
//get new input
const
input
=
req
.
body
;
//return 200 with empty response if no data was given
if
(
Object
.
keys
(
input
).
length
===
0
)
{
return
res
.
status
(
200
)
.
json
({
success
:
true
,
result
:
{},
message
:
"
noInputFound
"
});
}
//get id
const
givenId
=
req
.
params
.
id
;
//check if sport can be found using givenId
const
foundSport
=
await
Sport
.
findByPk
(
givenId
);
if
(
!
foundSport
)
{
return
res
.
status
(
404
).
json
({
success
:
false
,
error
:
"
sportIdNotFound
"
});
}
//try to update
const
updatedSport
=
await
Sport
.
update
(
input
,
{
where
:
{
id
:
givenId
,
},
returning
:
true
,
});
//return after updating
const
sportDataAfterUpdate
=
updatedSport
[
1
][
0
];
return
res
.
status
(
200
).
json
({
success
:
true
,
result
:
{
id
:
sportDataAfterUpdate
.
id
,
name
:
sportDataAfterUpdate
.
name
,
color
:
sportDataAfterUpdate
.
color
,
},
});
}
catch
(
error
)
{
console
.
error
(
"
server error:
"
,
error
.
message
);
return
res
.
status
(
500
).
json
({
success
:
false
,
error
:
"
serverError
"
});
}
};
//show all sports assigned to boat by given boatid
const
showSportByBoatId
=
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
boat
=
await
Boat
.
findByPk
(
givenId
);
if
(
boat
)
{
//if boat exists check in BoatHasSport table if there are any sports assigned to Boat
const
boatIdInBoatHasSport
=
await
BoatHasSport
.
findByPk
(
givenId
);
if
(
!
boatIdInBoatHasSport
)
{
return
res
.
status
(
404
).
json
({
success
:
false
,
error
:
"
noAssignedSportToBoatId
"
});
}
const
findAllBoatId
=
await
BoatHasSport
.
findAll
({
where
:
{
boatid
:
givenId
,
}
});
for
(
let
i
=
0
;
i
<
findAllBoatId
.
length
;
i
++
)
{
const
found
=
await
Sport
.
findByPk
(
findAllBoatId
[
i
].
sportid
)
return
res
.
status
(
200
)
.
json
({
success
:
true
,
result
:
{
sport
:
found
.
name
}
});
}
}
return
res
.
status
(
404
).
json
({
success
:
false
,
error
:
"
boatIdNotFound
"
});
}
catch
(
error
)
{
console
.
error
(
"
server error:
"
,
error
.
message
);
return
res
.
status
(
500
).
json
({
success
:
false
,
error
:
"
serverError
"
});
}
};
const
sportControllers
=
{
createSportController
,
showAllSports
};
showAllSports
,
deleteSportById
,
updateSportById
,
showSportByBoatId
};
export
default
sportControllers
;
*/
\ No newline at end of file
\ No newline at end of file
This diff is collapsed.
Click to expand it.
server/src/routes/sport.routes.ts
+
28
−
4
View file @
382b9695
/*
import { Router } from "express";
import
{
Router
}
from
"
express
"
;
import
{
body
}
from
"
express-validator
"
;
import
sportControllers
from
"
../controllers/sport.controllers
"
;
import
handleValidationResult
from
"
../middleware/handleValidationResult
"
;
...
...
@@ -13,14 +13,38 @@ sportRouter.get(
sportControllers
.
showAllSports
);
//create sport
//create
a
sport
sportRouter
.
post
(
"
/api/sport/
"
,
body
(
"
name
"
).
not
().
isEmpty
(),
body
(
"
color
"
).
if
(
body
(
"
color
"
).
exists
()).
not
().
isEmpty
(),
//optional tags field
handleValidationResult
,
validateToken
,
sportControllers
.
createSportController
);
export default sportRouter;
*/
\ No newline at end of file
//delete Sport by given id
sportRouter
.
delete
(
"
/api/sport/:id
"
,
validateToken
,
sportControllers
.
deleteSportById
,
);
//update a sport
sportRouter
.
patch
(
"
/api/sport/:id/
"
,
body
(
"
name
"
).
if
(
body
(
"
color
"
).
exists
()).
not
().
isEmpty
(),
body
(
"
color
"
).
if
(
body
(
"
color
"
).
exists
()).
not
().
isEmpty
(),
handleValidationResult
,
validateToken
,
sportControllers
.
updateSportById
);
//show sports assigned to boat id
sportRouter
.
get
(
"
/api/sport/:id/
"
,
validateToken
,
sportControllers
.
showSportByBoatId
);
export
default
sportRouter
;
\ No newline at end of file
This diff is collapsed.
Click to expand it.
server/src/server.ts
+
3
−
0
View file @
382b9695
...
...
@@ -12,6 +12,7 @@ import boatsRouter from "./routes/boat.routes";
import
userRouter
from
"
./routes/user.routes
"
;
import
boatTypeRouter
from
"
./routes/boatType.routes
"
;
import
entryRouter
from
"
./routes/createLogEntry.routes
"
;
import
sportRouter
from
"
./routes/sport.routes
"
;
let
init
=
async
()
=>
{
//DB
...
...
@@ -30,6 +31,8 @@ let init = async () => {
app
.
use
(
"
/
"
,
userRouter
);
app
.
use
(
"
/
"
,
boatTypeRouter
);
app
.
use
(
"
/
"
,
entryRouter
);
app
.
use
(
"
/
"
,
sportRouter
);
//DB-information section
await
showAllDBs
(
sequelize
);
await
showTables
(
sequelize
);
...
...
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