Skip to content

Commit

Permalink
[ADD] - list of games pr user & make it pageable
Browse files Browse the repository at this point in the history
  • Loading branch information
tofiksa committed Aug 6, 2024
1 parent 8d8c0a9 commit 0a69142
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package no.josefushighscore.controller;

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;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import static org.springframework.http.ResponseEntity.ok;
Expand All @@ -18,10 +22,29 @@ public class UserInfoController {
@Autowired
private UserService userService;

@Autowired
private GameService gameService;


@PreAuthorize("hasRole('ROLE_USER')")
@GetMapping("/me")
public ResponseEntity currentUser(@AuthenticationPrincipal UserDetails userDetails) throws InvalidJwtAuthenticationException {
return ok(userService.getUserDetails(userDetails.getUsername()));
}

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

@PreAuthorize("hasRole('ROLE_USER')")
@GetMapping("/score")
public ResponseEntity totalScore(@AuthenticationPrincipal UserDetails userDetails) throws InvalidJwtAuthenticationException {
return ok(userService.getUserDetails(userDetails.getUsername()));
}


}
8 changes: 5 additions & 3 deletions src/main/java/no/josefushighscore/model/Game.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package no.josefushighscore.model;

import jakarta.persistence.*;
import lombok.Data;
import lombok.NoArgsConstructor;

import jakarta.persistence.*;
import java.time.LocalDate;

@Entity
@Table(name="`game`")
@Data
@NoArgsConstructor
public class Game {

@Id
Expand All @@ -34,6 +32,10 @@ public class Game {
@Column(name="`game_end_time`")
LocalDate gameEndTime;

public Game() {

}

public LocalDate getGameEndTime() {
return gameEndTime;
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/no/josefushighscore/model/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ public String getFirstname() {
return firstname;
}

public Long getUserId() {
return userId;
}

public void setFirstname(String firstname) {
this.firstname = firstname;
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/no/josefushighscore/register/GameRegister.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package no.josefushighscore.register;

import no.josefushighscore.model.Game;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

Expand All @@ -11,4 +13,5 @@ public interface GameRegister extends JpaRepository<Game,Long> {

Optional<Game> findLatestByUser_UserId(Long userId);
Optional<Game> findByGameId(Long gameId);
Page<Game> findByUser_UserId(Long userId, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
import org.springframework.data.jpa.repository.JpaRepository;

public interface ScoreRegister extends JpaRepository<Score,Long> {

}
16 changes: 16 additions & 0 deletions src/main/java/no/josefushighscore/service/GameService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import no.josefushighscore.register.UserRegister;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

Expand Down Expand Up @@ -43,6 +46,19 @@ public Game getGameDetails(String username) {
return game;
}

public Page<Game> 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);

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

return games;
}

public Score updateScore(String username, ScoreDto scoreDto) {

Optional<Game> currentGame = this.gameRegister.findByGameId(Long.valueOf(scoreDto.getGameId()));
Expand Down

0 comments on commit 0a69142

Please sign in to comment.