Refactor score table to leaderboard, update related models and services for consistency
LocationBasedServices/sumoc-backend/pipeline/head This commit looks good
LocationBasedServices/sumoc-backend/pipeline/head This commit looks good
This commit is contained in:
@@ -1,7 +1,18 @@
|
||||
package SuMoC.backend.gamification;
|
||||
|
||||
public enum Timeframe {
|
||||
TOTAL,
|
||||
MONTHLY,
|
||||
WEEKLY,
|
||||
TOTAL("totalStats"),
|
||||
MONTHLY("monthlyStats"),
|
||||
WEEKLY("weeklyStats");
|
||||
|
||||
private final String statsField;
|
||||
|
||||
Timeframe(String statsField) {
|
||||
this.statsField = statsField;
|
||||
}
|
||||
|
||||
public String getStatsField() {
|
||||
return statsField;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+16
-16
@@ -1,7 +1,7 @@
|
||||
package SuMoC.backend.gamification.scoretable;
|
||||
|
||||
import SuMoC.backend.gamification.scoretable.models.ScoreTable;
|
||||
import SuMoC.backend.gamification.scoretable.models.ScoreType;
|
||||
import SuMoC.backend.gamification.scoretable.models.Leaderboard;
|
||||
import SuMoC.backend.gamification.scoretable.models.LeaderboardType;
|
||||
import SuMoC.backend.gamification.Timeframe;
|
||||
import SuMoC.backend.user.models.User;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
@@ -18,12 +18,12 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/scores")
|
||||
class ScoreTableController {
|
||||
private ScoreTableService scoreTableService;
|
||||
class LeaderboardController {
|
||||
private LeaderboardService leaderboardService;
|
||||
|
||||
@Autowired
|
||||
public ScoreTableController(ScoreTableService scoreTableService) {
|
||||
this.scoreTableService = scoreTableService;
|
||||
public LeaderboardController(LeaderboardService leaderboardService) {
|
||||
this.leaderboardService = leaderboardService;
|
||||
}
|
||||
|
||||
@Operation(summary = "Get the global score table")
|
||||
@@ -33,11 +33,11 @@ class ScoreTableController {
|
||||
|
||||
})
|
||||
@GetMapping("/getScoreTable")
|
||||
public ResponseEntity<ScoreTable> getScoreTable(@AuthenticationPrincipal User user,
|
||||
@RequestParam Timeframe timeframe,
|
||||
@RequestParam ScoreType scoreType) {
|
||||
ScoreTable scoreTable = this.scoreTableService.getScoreTable(user, timeframe, scoreType);
|
||||
return ResponseEntity.status(HttpStatus.OK).body(scoreTable);
|
||||
public ResponseEntity<Leaderboard> getScoreTable(@AuthenticationPrincipal User user,
|
||||
@RequestParam Timeframe timeframe,
|
||||
@RequestParam String field) {
|
||||
Leaderboard leaderboard = this.leaderboardService.getScoreTable(user, timeframe, field);
|
||||
return ResponseEntity.status(HttpStatus.OK).body(leaderboard);
|
||||
}
|
||||
|
||||
@Operation(summary = "Get the friend score table")
|
||||
@@ -46,10 +46,10 @@ class ScoreTableController {
|
||||
@ApiResponse(responseCode = "400", description = "Bad request")
|
||||
})
|
||||
@GetMapping("/getFriendScoreTable")
|
||||
public ResponseEntity<ScoreTable> getFriendScoreTable(@AuthenticationPrincipal User user,
|
||||
@RequestParam Timeframe timeframe,
|
||||
@RequestParam ScoreType scoreType) {
|
||||
ScoreTable scoreTable = this.scoreTableService.getFriendScoreTable(user, timeframe, scoreType);
|
||||
return ResponseEntity.status(HttpStatus.OK).body(scoreTable);
|
||||
public ResponseEntity<Leaderboard> getFriendScoreTable(@AuthenticationPrincipal User user,
|
||||
@RequestParam Timeframe timeframe,
|
||||
@RequestParam String field) {
|
||||
Leaderboard leaderboard = this.leaderboardService.getFriendScoreTable(user, timeframe, field);
|
||||
return ResponseEntity.status(HttpStatus.OK).body(leaderboard);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,202 @@
|
||||
package SuMoC.backend.gamification.scoretable;
|
||||
|
||||
import SuMoC.backend.gamification.scoretable.models.Leaderboard;
|
||||
import SuMoC.backend.gamification.Timeframe;
|
||||
import SuMoC.backend.gamification.scoretable.models.LeaderboardEntry;
|
||||
import SuMoC.backend.user.models.User;
|
||||
import SuMoC.backend.user.repository.UserRepository;
|
||||
import SuMoC.backend.user.service.FriendshipService;
|
||||
import SuMoC.backend.user.service.UserService;
|
||||
import SuMoC.backend.user.stats.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
public class LeaderboardService {
|
||||
private final FriendshipService friendshipService;
|
||||
private final StatsListRepository statsListRepository;
|
||||
|
||||
public LeaderboardService(
|
||||
FriendshipService friendshipService,
|
||||
StatsListRepository statsListRepository
|
||||
) {
|
||||
|
||||
this.friendshipService = friendshipService;
|
||||
this.statsListRepository = statsListRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method Overload to get the score table with a default limit of 30.
|
||||
* @param user
|
||||
* @param timeframe
|
||||
* @param field
|
||||
* @return
|
||||
*/
|
||||
public Leaderboard getScoreTable(User user, Timeframe timeframe, String field) {
|
||||
return getScoreTable(user, timeframe, field, 30);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the global score table for a given user, timeframe, score type, and limit.
|
||||
* @param user
|
||||
* @param timeframe
|
||||
* @param field
|
||||
* @param limit
|
||||
* @return
|
||||
*/
|
||||
public Leaderboard getScoreTable(User user, Timeframe timeframe, String field, int limit) {
|
||||
List<StatsList> topStats = getTopStats(timeframe, field, limit);
|
||||
|
||||
List<Long> topUserIds = topStats.stream()
|
||||
.map(stats -> stats.getUser().getId())
|
||||
.toList();
|
||||
|
||||
Map<Long, FriendshipService.NameWithFriendship> nameMap =
|
||||
friendshipService.getPrivateNameIfFriendshipAccepted(user, topUserIds)
|
||||
.stream()
|
||||
.collect(Collectors.toMap(FriendshipService.NameWithFriendship::id, n -> n));
|
||||
|
||||
Leaderboard leaderboard = new Leaderboard();
|
||||
leaderboard.setEntries(new ArrayList<>());
|
||||
|
||||
for (StatsList statsList : topStats) {
|
||||
User topUser = statsList.getUser();
|
||||
FriendshipService.NameWithFriendship nwf = nameMap.get(topUser.getId());
|
||||
|
||||
String displayName = (nwf != null) ? nwf.name() : topUser.getPublicName();
|
||||
boolean friendship = (nwf != null) && nwf.friendship();
|
||||
|
||||
LeaderboardEntry entry = new LeaderboardEntry(
|
||||
topUser.getId(),
|
||||
displayName,
|
||||
friendship,
|
||||
statsList.getStatValue(timeframe, field)
|
||||
);
|
||||
|
||||
leaderboard.getEntries().add(entry);
|
||||
}
|
||||
|
||||
if (leaderboard.getEntries().stream().noneMatch(e -> e.getUserId().equals(user.getId()))) {
|
||||
LeaderboardEntry userEntry = new LeaderboardEntry(
|
||||
user.getId(),
|
||||
user.getPrivateName(),
|
||||
true,
|
||||
user.getStats().getStatValue(timeframe, field)
|
||||
);
|
||||
leaderboard.getEntries().add(userEntry);
|
||||
}
|
||||
|
||||
leaderboard.getEntries().sort(
|
||||
Comparator.comparingLong(LeaderboardEntry::getScore).reversed()
|
||||
);
|
||||
|
||||
return leaderboard;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Method Overload to get the friend score table with a default limit of 30.
|
||||
* @param user
|
||||
* @param timeframe
|
||||
* @param field
|
||||
* @return
|
||||
*/
|
||||
public Leaderboard getFriendScoreTable(User user, Timeframe timeframe, String field) {
|
||||
return getFriendScoreTable(user, timeframe, field, 30);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the friend score table for a given user, timeframe, score type, and limit.
|
||||
* @param user
|
||||
* @param timeframe
|
||||
* @param field
|
||||
* @param limit
|
||||
* @return
|
||||
*/
|
||||
public Leaderboard getFriendScoreTable(User user, Timeframe timeframe, String field, int limit) {
|
||||
List<User> friends = friendshipService.getAcceptedFriends(user);
|
||||
|
||||
Leaderboard leaderboard = new Leaderboard();
|
||||
if (friends.isEmpty()) { return leaderboard; }
|
||||
|
||||
for (User friend : friends) {
|
||||
StatsList statsList = friend.getStats();
|
||||
if (statsList != null) {
|
||||
Long statValue = statsList.getStatValue(timeframe, field);
|
||||
if (statValue != null) {
|
||||
LeaderboardEntry entry = new LeaderboardEntry(
|
||||
friend.getId(),
|
||||
friend.getPrivateName(),
|
||||
true,
|
||||
statValue
|
||||
);
|
||||
leaderboard.getEntries().add(entry);
|
||||
}
|
||||
}
|
||||
}
|
||||
leaderboard.getEntries().add(new LeaderboardEntry(
|
||||
user.getId(),
|
||||
user.getPrivateName(),
|
||||
true,
|
||||
user.getStats().getStatValue(timeframe, field)
|
||||
));
|
||||
|
||||
leaderboard.getEntries().sort(
|
||||
Comparator.comparingLong(LeaderboardEntry::getScore).reversed()
|
||||
);
|
||||
return leaderboard;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the top stats for a given timeframe and stat field.
|
||||
* @param timeframe
|
||||
* @param statField
|
||||
* @param limit
|
||||
* @return
|
||||
*/
|
||||
public List<StatsList> getTopStats(
|
||||
Timeframe timeframe,
|
||||
String statField,
|
||||
int limit
|
||||
) {
|
||||
Pageable pageable = PageRequest.of(0, limit);
|
||||
|
||||
return statsListRepository
|
||||
.findAll(
|
||||
StatsListSpecifications.orderByStat(timeframe, statField),
|
||||
pageable
|
||||
)
|
||||
.getContent();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the rank of a specific value for a given timeframe and stat field.
|
||||
* @param timeframe
|
||||
* @param statField
|
||||
* @param value
|
||||
* @return
|
||||
*/
|
||||
public long getRank(
|
||||
Timeframe timeframe,
|
||||
String statField,
|
||||
long value
|
||||
) {
|
||||
return statsListRepository.count(
|
||||
StatsListSpecifications
|
||||
.higherThan(timeframe, statField, value)
|
||||
) + 1;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
package SuMoC.backend.gamification.scoretable;
|
||||
|
||||
import SuMoC.backend.gamification.scoretable.models.ScoreTable;
|
||||
import SuMoC.backend.gamification.scoretable.models.ScoreType;
|
||||
import SuMoC.backend.gamification.Timeframe;
|
||||
import SuMoC.backend.user.models.User;
|
||||
import SuMoC.backend.user.repository.UserRepository;
|
||||
import SuMoC.backend.user.service.FriendshipService;
|
||||
import SuMoC.backend.user.service.UserService;
|
||||
import SuMoC.backend.user.stats.StatsService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
class ScoreTableService {
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
@Autowired
|
||||
private StatsService statsService;
|
||||
|
||||
@Autowired
|
||||
private FriendshipService friendshipService;
|
||||
|
||||
|
||||
|
||||
public ScoreTable getScoreTable(User user, Timeframe timeframe, ScoreType scoreType) {
|
||||
// TODO: Implement logic to generate and return the global score table
|
||||
return null;
|
||||
}
|
||||
|
||||
public ScoreTable getFriendScoreTable(User user, Timeframe timeframe, ScoreType scoreType) {
|
||||
// TODO: Implement logic to generate and return the friend score table
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package SuMoC.backend.gamification.scoretable.models;
|
||||
|
||||
import SuMoC.backend.gamification.Timeframe;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Leaderboard {
|
||||
|
||||
private Timeframe timeframe;
|
||||
|
||||
private LeaderboardType leaderboardType;
|
||||
|
||||
private List<LeaderboardEntry> entries;
|
||||
|
||||
|
||||
// Constructor
|
||||
|
||||
public Leaderboard(Timeframe timeframe, LeaderboardType leaderboardType, List<LeaderboardEntry> entries) {
|
||||
this.timeframe = timeframe;
|
||||
this.leaderboardType = leaderboardType;
|
||||
this.entries = entries;
|
||||
}
|
||||
|
||||
public Leaderboard() {}
|
||||
|
||||
|
||||
// Getter & Setter
|
||||
|
||||
public Timeframe getTimeframe() { return timeframe; }
|
||||
public void setTimeframe(Timeframe timeframe) { this.timeframe = timeframe; }
|
||||
|
||||
public LeaderboardType getScoreType() { return leaderboardType; }
|
||||
public void setScoreType(LeaderboardType leaderboardType) { this.leaderboardType = leaderboardType; }
|
||||
|
||||
public List<LeaderboardEntry> getEntries() { return entries; }
|
||||
public void setEntries(List<LeaderboardEntry> entries) { this.entries = entries; }
|
||||
}
|
||||
+6
-6
@@ -1,19 +1,19 @@
|
||||
package SuMoC.backend.gamification.scoretable.models;
|
||||
|
||||
public class ScoreEntry {
|
||||
public class LeaderboardEntry {
|
||||
private Long userId;
|
||||
private String username;
|
||||
private Boolean isFriend;
|
||||
private Integer score;
|
||||
private Long score;
|
||||
|
||||
public ScoreEntry(Long userId, String username, Boolean isFriend, Integer score) {
|
||||
public LeaderboardEntry(Long userId, String username, Boolean isFriend, Long score) {
|
||||
this.userId = userId;
|
||||
this.username = username;
|
||||
this.isFriend = isFriend;
|
||||
this.score = score;
|
||||
}
|
||||
|
||||
public ScoreEntry() {}
|
||||
public LeaderboardEntry() {}
|
||||
|
||||
// Getter & Setter
|
||||
public Long getUserId() { return userId; }
|
||||
@@ -25,6 +25,6 @@ public class ScoreEntry {
|
||||
public Boolean getIsFriend() { return isFriend; }
|
||||
public void setIsFriend(Boolean isFriend) { this.isFriend = isFriend; }
|
||||
|
||||
public Integer getScore() { return score; }
|
||||
public void setScore(Integer score) { this.score = score; }
|
||||
public Long getScore() { return score; }
|
||||
public void setScore(Long score) { this.score = score; }
|
||||
}
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
package SuMoC.backend.gamification.scoretable.models;
|
||||
|
||||
public enum ScoreType {
|
||||
public enum LeaderboardType {
|
||||
XP,
|
||||
CO2,
|
||||
KM
|
||||
@@ -1,37 +0,0 @@
|
||||
package SuMoC.backend.gamification.scoretable.models;
|
||||
|
||||
import SuMoC.backend.gamification.Timeframe;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ScoreTable {
|
||||
|
||||
private Timeframe timeframe;
|
||||
|
||||
private ScoreType scoreType;
|
||||
|
||||
private List<ScoreEntry> entries;
|
||||
|
||||
|
||||
// Constructor
|
||||
|
||||
public ScoreTable(Timeframe timeframe, ScoreType scoreType, List<ScoreEntry> entries) {
|
||||
this.timeframe = timeframe;
|
||||
this.scoreType = scoreType;
|
||||
this.entries = entries;
|
||||
}
|
||||
|
||||
public ScoreTable() {}
|
||||
|
||||
|
||||
// Getter & Setter
|
||||
|
||||
public Timeframe getTimeframe() { return timeframe; }
|
||||
public void setTimeframe(Timeframe timeframe) { this.timeframe = timeframe; }
|
||||
|
||||
public ScoreType getScoreType() { return scoreType; }
|
||||
public void setScoreType(ScoreType scoreType) { this.scoreType = scoreType; }
|
||||
|
||||
public List<ScoreEntry> getEntries() { return entries; }
|
||||
public void setEntries(List<ScoreEntry> entries) { this.entries = entries; }
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package SuMoC.backend.user.mapper;
|
||||
|
||||
|
||||
import SuMoC.backend.user.dtos.MaximalUserDTO;
|
||||
import SuMoC.backend.user.dtos.MinimalUserDTO;
|
||||
import SuMoC.backend.user.dtos.UserDTO;
|
||||
import SuMoC.backend.user.models.User;
|
||||
import org.mapstruct.Mapper;
|
||||
@@ -13,6 +14,8 @@ public interface UserMapper {
|
||||
User toUser(UserDTO userDTO);
|
||||
|
||||
MaximalUserDTO toMaximalUserDTO(User user);
|
||||
|
||||
MinimalUserDTO toMinimalUserDTO(User user);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -312,4 +312,15 @@ public class FriendshipService {
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public List<User> getAcceptedFriends(User user) {
|
||||
List<Friendship> friendships = friendshipRepository.findByUserIdAndStatus(user.getId(), FriendShipStatus.ACCEPTED);
|
||||
List<User> friends = new ArrayList<>();
|
||||
|
||||
for (Friendship friendship : friendships) {
|
||||
friends.add(friendship.getFriend());
|
||||
}
|
||||
|
||||
return friends;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.persistence.*;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.mapstruct.Context;
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
@@ -88,6 +87,17 @@ public class StatsList {
|
||||
|
||||
// Other Methods
|
||||
|
||||
public Stats getStats(Timeframe timeframe) {
|
||||
Stats stats;
|
||||
switch (timeframe) {
|
||||
case TOTAL -> stats = this.totalStats;
|
||||
case MONTHLY -> stats = this.monthlyStats;
|
||||
case WEEKLY -> stats = this.weeklyStats;
|
||||
default -> throw new IllegalArgumentException("Unknown timeframe: " + timeframe);
|
||||
}
|
||||
return stats;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of a specific stat field for a given timeframe.
|
||||
* @param timeframe
|
||||
@@ -96,13 +106,7 @@ public class StatsList {
|
||||
*/
|
||||
public Long getStatValue(Timeframe timeframe, String fieldName) {
|
||||
try {
|
||||
Stats stats;
|
||||
switch (timeframe) {
|
||||
case TOTAL -> stats = this.totalStats;
|
||||
case MONTHLY -> stats = this.monthlyStats;
|
||||
case WEEKLY -> stats = this.weeklyStats;
|
||||
default -> throw new IllegalArgumentException("Unknown timeframe: " + timeframe);
|
||||
}
|
||||
Stats stats = getStats(timeframe);
|
||||
|
||||
Field field = Stats.class.getDeclaredField(fieldName);
|
||||
field.setAccessible(true);
|
||||
@@ -121,15 +125,9 @@ public class StatsList {
|
||||
* @param fieldName
|
||||
* @return
|
||||
*/
|
||||
public Field getStat(Timeframe timeframe, String fieldName) {
|
||||
public Field getStatField(Timeframe timeframe, String fieldName) {
|
||||
try {
|
||||
Stats stats;
|
||||
switch (timeframe) {
|
||||
case TOTAL -> stats = this.totalStats;
|
||||
case MONTHLY -> stats = this.monthlyStats;
|
||||
case WEEKLY -> stats = this.weeklyStats;
|
||||
default -> throw new IllegalArgumentException("Unknown timeframe: " + timeframe);
|
||||
}
|
||||
Stats stats = getStats(timeframe);
|
||||
|
||||
Field field = Stats.class.getDeclaredField(fieldName);
|
||||
field.setAccessible(true);
|
||||
@@ -150,15 +148,9 @@ public class StatsList {
|
||||
*/
|
||||
public void addToStat(Timeframe timeframe, String fieldName, long value) {
|
||||
try {
|
||||
Stats stats;
|
||||
switch (timeframe) {
|
||||
case TOTAL -> stats = this.totalStats;
|
||||
case MONTHLY -> stats = this.monthlyStats;
|
||||
case WEEKLY -> stats = this.weeklyStats;
|
||||
default -> throw new IllegalArgumentException("Unknown timeframe: " + timeframe);
|
||||
}
|
||||
Stats stats = getStats(timeframe);
|
||||
|
||||
Field field = Stats.class.getDeclaredField(fieldName);
|
||||
Field field = getStatField(timeframe, fieldName);
|
||||
field.setAccessible(true);
|
||||
|
||||
Long currentValue = (Long) field.get(stats);
|
||||
@@ -166,7 +158,7 @@ public class StatsList {
|
||||
|
||||
field.set(stats, currentValue + value);
|
||||
|
||||
} catch (NoSuchFieldException | IllegalAccessException e) {
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException("Failed to update stat " + fieldName, e);
|
||||
}
|
||||
@@ -190,13 +182,7 @@ public class StatsList {
|
||||
*/
|
||||
public void resetStats(Timeframe timeframe) {
|
||||
try {
|
||||
Stats stats;
|
||||
switch (timeframe) {
|
||||
case TOTAL -> stats = this.totalStats;
|
||||
case MONTHLY -> stats = this.monthlyStats;
|
||||
case WEEKLY -> stats = this.weeklyStats;
|
||||
default -> throw new IllegalArgumentException("Unknown timeframe: " + timeframe);
|
||||
}
|
||||
Stats stats = getStats(timeframe);
|
||||
|
||||
Field[] fields = Stats.class.getDeclaredFields();
|
||||
for (Field field : fields) {
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
package SuMoC.backend.user.stats;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Repository
|
||||
public interface StatsListRepository extends JpaRepository<StatsList, Long> {
|
||||
public interface StatsListRepository
|
||||
extends JpaRepository<StatsList, Long>,
|
||||
JpaSpecificationExecutor<StatsList> {
|
||||
|
||||
Optional<StatsList> findByUserId(Long userId);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package SuMoC.backend.user.stats;
|
||||
|
||||
import SuMoC.backend.gamification.Timeframe;
|
||||
import jakarta.persistence.criteria.Join;
|
||||
import jakarta.persistence.criteria.JoinType;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
|
||||
public final class StatsListSpecifications {
|
||||
|
||||
private StatsListSpecifications() {}
|
||||
|
||||
public static Specification<StatsList> orderByStat(
|
||||
Timeframe timeframe,
|
||||
String statField
|
||||
) {
|
||||
return (root, query, cb) -> {
|
||||
|
||||
Join<StatsList, Stats> stats =
|
||||
root.join(timeframe.getStatsField(), JoinType.LEFT);
|
||||
|
||||
query.orderBy(cb.desc(stats.get(statField)));
|
||||
|
||||
return cb.conjunction();
|
||||
};
|
||||
}
|
||||
|
||||
public static Specification<StatsList> higherThan(
|
||||
Timeframe timeframe,
|
||||
String statField,
|
||||
long value
|
||||
) {
|
||||
return (root, query, cb) -> {
|
||||
Join<StatsList, Stats> stats =
|
||||
root.join(timeframe.getStatsField(), JoinType.LEFT);
|
||||
|
||||
return cb.greaterThan(
|
||||
stats.get(statField),
|
||||
value
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,11 +15,13 @@ public class StatsService {
|
||||
private UserRepository userRepository;
|
||||
|
||||
public void addToWeeklyStats(Long userId, String name, int value) {
|
||||
StatsList statsList = this.statsListRepository.findByUserId(userId)
|
||||
/*StatsList statsList = this.statsListRepository.findByUserId(userId)
|
||||
.orElseGet(() -> new StatsList(userRepository.getReferenceById(userId)));
|
||||
|
||||
statsList.getStat(Timeframe.WEEKLY, name).get().addValue(value);
|
||||
statsList.getStat(Timeframe.WEEKLY, name).;
|
||||
this.statsListRepository.save(statsList);
|
||||
|
||||
*/
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user