-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lagt til hente totalscore, høyeste og laveste
- Loading branch information
Showing
6 changed files
with
118 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
src/main/java/no/josefushighscore/service/ScoreService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package no.josefushighscore.service; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import no.josefushighscore.model.Game; | ||
import no.josefushighscore.model.Score; | ||
import no.josefushighscore.model.User; | ||
import no.josefushighscore.register.GameRegister; | ||
import no.josefushighscore.register.UserRegister; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
import java.util.LongSummaryStatistics; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.stream.Stream; | ||
|
||
@Service | ||
public class ScoreService { | ||
|
||
private final GameRegister gameRegister; | ||
private final UserRegister userRegister; | ||
|
||
private final Logger LOG = LoggerFactory.getLogger(ScoreService.class); | ||
|
||
public ScoreService (GameRegister gameRegister, UserRegister userRegister) { | ||
this.gameRegister = gameRegister; | ||
this.userRegister = userRegister; | ||
} | ||
|
||
public ObjectNode getTotalScoreForUser (String username) { | ||
|
||
List<Game> getAllGamesForUser = this.gameRegister.findByUser_UserId(getUserId(username)); | ||
Stream<Score> allGamesUserHasScoreOn = getAllGamesForUser.stream() | ||
.map(Game::getScoreId) | ||
.filter(Objects::nonNull) // Filter out null Score objects | ||
.distinct(); | ||
|
||
Long totalScore = allGamesUserHasScoreOn | ||
.mapToLong(score -> score.getScore() != null ? score.getScore() : 0L) // Handle null scores | ||
.sum(); | ||
LOG.info("This player scored {} points for player {}", totalScore, username); | ||
|
||
ObjectMapper mapper = new ObjectMapper(); | ||
ObjectNode response = mapper.createObjectNode(); | ||
response.put("totalScore", totalScore); | ||
response.put("gameStats",getHighestAndLowestScores(getAllGamesForUser)); | ||
|
||
return response; | ||
} | ||
|
||
private Long getUserId (String username) { | ||
Optional<User> getUserId = this.userRegister.findByUsername(username); | ||
return getUserId.orElseThrow().getUserId(); | ||
} | ||
|
||
private ObjectNode getHighestAndLowestScores(List<Game> getAllGamesForUser) { | ||
|
||
Stream<Score> scores = getAllGamesForUser.stream() | ||
.map(Game::getScoreId) | ||
.filter(Objects::nonNull) // Filter out null Score objects | ||
.distinct(); | ||
|
||
LongSummaryStatistics stats = scores | ||
.filter(score -> score != null) | ||
.map(Score::getScore) | ||
.filter(score -> score != null) | ||
.mapToLong(Long::longValue) | ||
.filter(score -> score >= 0) | ||
.summaryStatistics(); | ||
|
||
ObjectMapper mapper = new ObjectMapper(); | ||
ObjectNode response = mapper.createObjectNode(); | ||
response.put("highestScore", stats.getCount() > 0 ? stats.getMax() : 0L); | ||
response.put("lowestScore", stats.getCount() > 0 ? stats.getMin() : 0L); | ||
|
||
return response; | ||
} | ||
|
||
|
||
} |