Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
student-tournament-hub
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Schneider Stefan
student-tournament-hub
Commits
113b53a3
Commit
113b53a3
authored
1 year ago
by
Jonas
Browse files
Options
Downloads
Patches
Plain Diff
18.12.2023 - create service class and methods
#24
parent
90d12c1e
No related branches found
Branches containing commit
No related tags found
1 merge request
!7
Dev in Main Merge
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
sth-backend/src/main/java/hdm/mi/sthbackend/service/TournamentService.java
+101
-0
101 additions, 0 deletions
...ain/java/hdm/mi/sthbackend/service/TournamentService.java
with
101 additions
and
0 deletions
sth-backend/src/main/java/hdm/mi/sthbackend/service/TournamentService.java
0 → 100644
+
101
−
0
View file @
113b53a3
package
hdm.mi.sthbackend.service
;
import
hdm.mi.sthbackend.exeptions.PlayerIdNotFoundException
;
import
hdm.mi.sthbackend.exeptions.TournamentIdNotFoundException
;
import
hdm.mi.sthbackend.model.Player
;
import
hdm.mi.sthbackend.model.Team
;
import
hdm.mi.sthbackend.model.Tournament
;
import
hdm.mi.sthbackend.repository.ITournamentRepository
;
import
lombok.AllArgsConstructor
;
import
org.springframework.stereotype.Service
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.UUID
;
@Service
@AllArgsConstructor
public
class
TournamentService
{
ITournamentRepository
tournamentRepository
;
public
Player
addPlayerToTeam
(
UUID
tournamentId
,
UUID
teamId
,
String
playerName
)
throws
Exception
{
Tournament
tournament
=
tournamentRepository
.
findById
(
tournamentId
)
.
orElseThrow
(()
->
new
Exception
(
"TournamentId "
+
tournamentId
+
" not found"
));
Player
newPlayer
=
new
Player
(
UUID
.
randomUUID
(),
playerName
);
tournament
.
getTeams
()
.
get
(
teamId
)
.
getTeamMembers
()
.
add
(
newPlayer
);
tournamentRepository
.
save
(
tournament
);
return
newPlayer
;
}
public
Player
removePlayerFromTeam
(
UUID
tournamentId
,
UUID
teamId
,
UUID
playerId
)
throws
TournamentIdNotFoundException
,
PlayerIdNotFoundException
{
Tournament
tournament
=
tournamentRepository
.
findById
(
tournamentId
)
.
orElseThrow
(()
->
new
TournamentIdNotFoundException
(
tournamentId
));
List
<
Player
>
teamMembers
=
tournament
.
getTeams
()
.
get
(
teamId
)
.
getTeamMembers
();
Player
playerToRemove
=
teamMembers
.
stream
()
.
filter
(
player
->
player
.
getPlayerId
()
.
equals
(
playerId
))
.
findFirst
()
.
orElseThrow
(()
->
new
PlayerIdNotFoundException
(
playerId
));
tournament
.
getTeams
()
.
get
(
teamId
)
.
getTeamMembers
()
.
remove
(
playerToRemove
);
tournamentRepository
.
save
(
tournament
);
return
playerToRemove
;
}
public
UUID
updatePlayerName
(
UUID
tournamentId
,
UUID
teamId
,
UUID
playerId
,
String
newPlayerName
)
throws
Exception
{
Tournament
tournament
=
tournamentRepository
.
findById
(
tournamentId
)
.
orElseThrow
(()
->
new
TournamentIdNotFoundException
(
tournamentId
));
tournament
.
getTeams
()
.
get
(
teamId
)
.
getTeamMembers
()
.
stream
()
.
filter
(
player
->
player
.
getPlayerId
()
.
equals
(
playerId
))
.
findFirst
()
.
orElseThrow
(()
->
new
PlayerIdNotFoundException
(
playerId
))
.
setName
(
newPlayerName
);
tournamentRepository
.
save
(
tournament
);
return
playerId
;
}
public
Team
createTeam
(
UUID
tournamentId
,
String
teamName
)
throws
TournamentIdNotFoundException
{
Tournament
tournament
=
tournamentRepository
.
findById
(
tournamentId
)
.
orElseThrow
(()
->
new
TournamentIdNotFoundException
(
tournamentId
));
Team
team
=
new
Team
(
UUID
.
randomUUID
(),
teamName
,
new
ArrayList
<>()
{
});
tournament
.
getTeams
()
.
put
(
team
.
getId
(),
team
);
tournamentRepository
.
save
(
tournament
);
return
team
;
}
public
Tournament
createTournament
(
String
tournamentName
)
{
Tournament
tournament
=
new
Tournament
(
UUID
.
randomUUID
(),
tournamentName
,
new
ArrayList
<>(),
new
HashMap
<>());
tournamentRepository
.
insert
(
tournament
);
return
tournament
;
}
}
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