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
79f64be8
Commit
79f64be8
authored
1 year ago
by
Jonas
Browse files
Options
Downloads
Patches
Plain Diff
11.01.2024 - adjust service
#24
parent
362e75ed
No related branches found
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
+27
-41
27 additions, 41 deletions
...ain/java/hdm/mi/sthbackend/service/TournamentService.java
with
27 additions
and
41 deletions
sth-backend/src/main/java/hdm/mi/sthbackend/service/TournamentService.java
+
27
−
41
View file @
79f64be8
...
...
@@ -2,7 +2,9 @@ package hdm.mi.sthbackend.service;
import
hdm.mi.sthbackend.dto.PlayerDTO
;
import
hdm.mi.sthbackend.dto.TeamDTO
;
import
hdm.mi.sthbackend.dto.TournamentDTO
;
import
hdm.mi.sthbackend.exeptions.*
;
import
hdm.mi.sthbackend.mapper.ModelToDTOMapper
;
import
hdm.mi.sthbackend.model.Match
;
import
hdm.mi.sthbackend.model.Player
;
import
hdm.mi.sthbackend.model.Team
;
...
...
@@ -14,6 +16,7 @@ import hdm.mi.sthbackend.repository.ITournamentRepository;
import
lombok.AllArgsConstructor
;
import
org.apache.logging.log4j.LogManager
;
import
org.apache.logging.log4j.Logger
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
java.util.*
;
...
...
@@ -22,14 +25,17 @@ import java.util.*;
@AllArgsConstructor
public
class
TournamentService
{
private
final
static
Logger
log
=
LogManager
.
getLogger
(
TournamentService
.
class
);
private
final
Logger
log
=
LogManager
.
getLogger
(
TournamentService
.
class
);
ITournamentRepository
tournamentRepository
;
IPlayerRepository
playerRepository
;
ITeamRepository
teamRepository
;
IMatchRepository
matchRepository
;
public
Player
addPlayerToTeam
(
UUID
teamId
,
@Autowired
ModelToDTOMapper
mapper
;
public
PlayerDTO
addPlayerToTeam
(
UUID
teamId
,
String
playerName
)
throws
TeamIdNotFoundException
{
Team
team
=
teamRepository
.
findById
(
teamId
)
.
orElseThrow
(()
->
new
TeamIdNotFoundException
(
teamId
));
...
...
@@ -41,14 +47,14 @@ public class TournamentService {
playerRepository
.
insert
(
newPlayer
);
teamRepository
.
save
(
team
);
log
.
debug
(
"Player "
+
newPlayer
.
getPlayerId
()
+
" added to Team "
+
teamId
);
return
newPlayer
;
return
mapper
.
mapToPlayerDTO
(
newPlayer
)
;
}
public
PlayerDTO
findPlayerById
(
UUID
playerId
)
throws
PlayerIdNotFoundException
{
Player
player
=
playerRepository
.
findById
(
playerId
)
.
orElseThrow
(()
->
new
PlayerIdNotFoundException
(
playerId
));
return
new
PlayerDTO
(
player
.
getPlayerId
(),
player
.
getName
()
);
return
mapper
.
mapToPlayerDTO
(
player
);
}
/*
...
...
@@ -74,7 +80,7 @@ public class TournamentService {
teamRepository
.
save
(
team
);
log
.
debug
(
"Player "
+
playerId
+
" removed from Team "
+
teamId
);
return
new
PlayerDTO
(
playerToDelete
.
getPlayerId
(),
playerToDelete
.
getName
()
);
return
mapper
.
mapToPlayerDTO
(
playerToDelete
);
}
/*
...
...
@@ -101,10 +107,10 @@ public class TournamentService {
forEach
(
pId
->
playerRepository
.
findById
(
pId
)
.
ifPresent
(
p
->
teamMembers
.
put
(
p
.
getPlayerId
(),
new
PlayerDTO
(
p
.
getPlayerId
(),
p
.
getName
()))));
return
new
TeamDTO
(
team
.
getId
(),
team
.
getTeamName
(),
teamMembers
);
return
mapper
.
mapToTeamDTO
(
team
);
}
public
Team
createAndAddTeamToTournament
(
UUID
tournamentId
,
String
teamName
)
throws
TournamentIdNotFoundException
{
public
Team
DTO
createAndAddTeamToTournament
(
UUID
tournamentId
,
String
teamName
)
throws
TournamentIdNotFoundException
{
Tournament
tournament
=
tournamentRepository
.
findById
(
tournamentId
)
.
orElseThrow
(()
->
new
TournamentIdNotFoundException
(
tournamentId
));
...
...
@@ -115,7 +121,7 @@ public class TournamentService {
teamRepository
.
insert
(
newTeam
);
tournamentRepository
.
save
(
tournament
);
return
newTeam
;
return
mapper
.
mapToTeamDTO
(
newTeam
)
;
}
public
UUID
deleteAndRemoveTeamFromTournament
(
UUID
tournamentId
,
...
...
@@ -137,64 +143,44 @@ public class TournamentService {
return
teamId
;
}
public
Tournament
createTournament
(
String
tournamentName
)
{
public
Tournament
DTO
createTournament
(
String
tournamentName
)
{
Tournament
tournament
=
new
Tournament
(
UUID
.
randomUUID
(),
tournamentName
,
new
ArrayList
<>(),
new
ArrayList
<>());
tournamentRepository
.
insert
(
tournament
);
log
.
debug
(
"created Tournament "
+
tournament
.
getTournamentId
());
return
tournament
;
return
mapper
.
mapToTournamentDTO
(
tournament
)
;
}
public
UUID
determineWinner
(
UUID
tournamentId
,
UUID
matchId
)
throws
TournamentIdNotFoundException
,
MatchIdNotFoundException
,
WinnerNotDeterminedException
{
Tournament
tournament
=
tournamentRepository
.
findById
(
tournamentId
)
.
orElseThrow
(()
->
new
TournamentIdNotFoundException
(
tournamentId
));
Match
match
=
tournament
.
getMatches
().
stream
()
.
filter
(
m
->
m
.
getMatchId
().
equals
(
matchId
))
.
findFirst
()
.
orElseThrow
(()
->
new
MatchIdNotFoundException
(
matchId
));
public
UUID
determineWinner
(
UUID
matchId
)
throws
MatchIdNotFoundException
,
WinnerNotDeterminedException
{
Match
match
=
matchRepository
.
findById
(
matchId
).
orElseThrow
(()
->
new
MatchIdNotFoundException
(
matchId
));
UUID
winnerTeamId
=
match
.
getTeamScores
().
entrySet
().
stream
()
.
max
(
Comparator
.
comparing
(
Map
.
Entry
::
getKey
))
.
map
(
Map
.
Entry
::
getKey
)
.
orElseThrow
(()
->
new
WinnerNotDeterminedException
(
matchId
,
tournamentId
));
.
orElseThrow
(()
->
new
WinnerNotDeterminedException
(
matchId
));
match
.
setWinnerTeamId
(
winnerTeamId
);
tournament
Repository
.
save
(
tournament
);
log
.
debug
(
"The Winner "
+
winnerTeamId
+
" is determined for Match "
+
matchId
+
" in Tournament "
+
tournamentId
);
match
Repository
.
save
(
match
);
log
.
debug
(
"The Winner "
+
winnerTeamId
+
" is determined for Match "
+
matchId
);
return
winnerTeamId
;
}
public
void
assignTeamToMatch
(
UUID
tournamentId
,
UUID
matchId
,
UUID
teamId
,
int
score
)
throws
TournamentIdNotFoundException
,
MatchIdNotFoundException
{
Tournament
tournament
=
tournamentRepository
.
findById
(
tournamentId
)
.
orElseThrow
(()
->
new
TournamentIdNotFoundException
(
tournamentId
));
Match
match
=
tournament
.
getMatches
().
stream
()
.
filter
(
m
->
m
.
getMatchId
().
equals
(
matchId
))
.
findFirst
()
.
orElseThrow
(()
->
new
MatchIdNotFoundException
(
matchId
));
Match
match
=
matchRepository
.
findById
(
matchId
).
orElseThrow
(()
->
new
MatchIdNotFoundException
(
matchId
));
match
.
getTeamScores
().
put
(
teamId
,
score
);
tournament
Repository
.
save
(
tournament
);
match
Repository
.
save
(
match
);
log
.
debug
(
"Team "
+
teamId
+
" assign to Match "
+
matchId
+
" in Tournament "
+
tournamentId
);
}
public
void
updateTeamScore
(
UUID
tournamentId
,
UUID
matchId
,
UUID
teamId
,
int
newScore
)
throws
TournamentIdNotFoundException
,
MatchIdNotFoundException
,
TeamIdNotFoundException
{
Tournament
tournament
=
tournamentRepository
.
findById
(
tournamentId
)
.
orElseThrow
(()
->
new
TournamentIdNotFoundException
(
tournamentId
));
Match
match
=
tournament
.
getMatches
().
stream
()
.
filter
(
m
->
m
.
getMatchId
().
equals
(
matchId
))
.
findFirst
()
.
orElseThrow
(()
->
new
MatchIdNotFoundException
(
matchId
));
Match
match
=
matchRepository
.
findById
(
matchId
).
orElseThrow
(()
->
new
MatchIdNotFoundException
(
matchId
));
if
(!
match
.
getTeamScores
().
containsKey
(
teamId
))
{
throw
new
TeamIdNotFoundException
(
teamId
);
}
match
.
getTeamScores
().
put
(
teamId
,
newScore
);
tournament
Repository
.
save
(
tournament
);
match
Repository
.
save
(
match
);
log
.
debug
(
"Score of Team "
+
teamId
+
" updated to "
+
newScore
);
}
...
...
@@ -217,12 +203,12 @@ public class TournamentService {
log
.
debug
(
"Tournament Name for TournamentID "
+
tournamentId
+
" updated to "
+
newTournamentName
);
return
tournamentId
;
}
public
Tournament
getTournament
(
UUID
tournamentId
)
throws
TournamentIdNotFoundException
{
public
Tournament
DTO
getTournament
(
UUID
tournamentId
)
throws
TournamentIdNotFoundException
{
Tournament
tournament
=
tournamentRepository
.
findById
(
tournamentId
)
.
orElseThrow
(()
->
new
TournamentIdNotFoundException
(
tournamentId
));
log
.
debug
(
tournament
+
"is found"
);
return
tournament
;
return
mapper
.
mapToTournamentDTO
(
tournament
)
;
}
public
void
deleteTournament
(
UUID
tournamentId
)
throws
TournamentIdNotFoundException
{
...
...
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