Skip to content

Commit

Permalink
Merge pull request #25 from tofiksa/24-list-of-games-played
Browse files Browse the repository at this point in the history
utvided endepunkt til å inkludere scoredetaljer pr spilløkt
  • Loading branch information
tofiksa authored Aug 7, 2024
2 parents 0a69142 + b6d78cc commit a0e2c90
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package no.josefushighscore.controller;

import no.josefushighscore.dto.GameDto;
import no.josefushighscore.exception.InvalidJwtAuthenticationException;
import no.josefushighscore.model.Game;
import no.josefushighscore.service.GameService;
import no.josefushighscore.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -34,9 +34,9 @@ public ResponseEntity currentUser(@AuthenticationPrincipal UserDetails userDetai

@PreAuthorize("hasRole('ROLE_USER')")
@GetMapping("/games")
public ResponseEntity<Page<Game>> totalGames(@AuthenticationPrincipal UserDetails userDetails, @RequestParam(defaultValue = "0") int page,
public ResponseEntity<Page<GameDto>> totalGames(@AuthenticationPrincipal UserDetails userDetails, @RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "10") int size) {
Page<Game> games = gameService.getAllGames(userDetails.getUsername(), page, size);
Page<GameDto> games = gameService.getAllGames(userDetails.getUsername(), page, size);
return ResponseEntity.ok(games);
}

Expand Down
76 changes: 76 additions & 0 deletions src/main/java/no/josefushighscore/dto/GameDto.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,82 @@
package no.josefushighscore.dto;

import no.josefushighscore.model.Score;

import java.time.LocalDate;

public class GameDto {

private Long gameId;
private Long userId;
private String username;
private LocalDate createdAt;
private LocalDate updatedAt;
private LocalDate gameEndTime;
private Score score;

public GameDto(Score score, Long gameId, Long userId, String username, LocalDate createdAt, LocalDate updatedAt, LocalDate gameEndTime) {
this.gameId = gameId;
this.userId = userId;
this.username = username;
this.createdAt = createdAt;
this.updatedAt = updatedAt;
this.gameEndTime = gameEndTime;
this.score = score;
}

public Score getScore() {
return score;
}

public void setScore(Score score) {
this.score = score;
}

public Long getGameId() {
return gameId;
}

public void setGameId(Long gameId) {
this.gameId = gameId;
}

public Long getUserId() {
return userId;
}

public void setUserId(Long userId) {
this.userId = userId;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public LocalDate getCreatedAt() {
return createdAt;
}

public void setCreatedAt(LocalDate createdAt) {
this.createdAt = createdAt;
}

public LocalDate getUpdatedAt() {
return updatedAt;
}

public void setUpdatedAt(LocalDate updatedAt) {
this.updatedAt = updatedAt;
}

public LocalDate getGameEndTime() {
return gameEndTime;
}

public void setGameEndTime(LocalDate gameEndTime) {
this.gameEndTime = gameEndTime;
}
}
19 changes: 17 additions & 2 deletions src/main/java/no/josefushighscore/service/GameService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package no.josefushighscore.service;

import no.josefushighscore.dto.GameDto;
import no.josefushighscore.dto.ScoreDto;
import no.josefushighscore.model.Game;
import no.josefushighscore.model.Score;
Expand Down Expand Up @@ -46,17 +47,19 @@ public Game getGameDetails(String username) {
return game;
}

public Page<Game> getAllGames(String username, int page, int size) {
public Page<GameDto> getAllGames(String username, int page, int size) {

Optional<User> currentUser = userRegister.findByUsername(username);
Long userId = currentUser.orElseThrow( () -> new UsernameNotFoundException("Username " + username + " not found")).getUserId();

PageRequest pageRequest = PageRequest.of(page, size, Sort.by("gameId").descending());
Page<Game> games = this.gameRegister.findByUser_UserId(userId,pageRequest);

Page<GameDto> gameDTOPage = games.map(this::convertToGameDTO);

LOG.info("size of listofGames {}", games.getSize());

return games;
return gameDTOPage;
}

public Score updateScore(String username, ScoreDto scoreDto) {
Expand Down Expand Up @@ -88,5 +91,17 @@ public Score updateScore(String username, ScoreDto scoreDto) {
return score;
}

private GameDto convertToGameDTO(Game game) {
return new GameDto(
game.getScoreId(),
game.getGameId(),
game.getUser().getUserId(),
game.getUser().getUsername(),
game.getCreatedAt(),
game.getUpdatedAt(),
game.getGameEndTime()
);
}


}

0 comments on commit a0e2c90

Please sign in to comment.