Implement score table functionality with ScoreTable, ScoreEntry, and ScoreTableService; add Timeframe and ScoreType enums; enhance User and StatsList models for better stats management
LocationBasedServices/sumoc-backend/pipeline/head This commit looks good
LocationBasedServices/sumoc-backend/pipeline/head This commit looks good
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
package SuMoC.backend.gamification;
|
||||
|
||||
public enum Timeframe {
|
||||
TOTAL,
|
||||
MONTHLY,
|
||||
WEEKLY,
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package SuMoC.backend.gamification.badges;
|
||||
|
||||
import SuMoC.backend.gamification.Timeframe;
|
||||
import SuMoC.backend.gamification.badges.models.Badge;
|
||||
import SuMoC.backend.gamification.badges.models.UserBadge;
|
||||
import SuMoC.backend.user.models.User;
|
||||
@@ -43,7 +44,7 @@ public class BadgeService {
|
||||
// Prüfe Kriterien
|
||||
boolean qualifies = badge.getCriteria().entrySet().stream()
|
||||
.allMatch(entry ->
|
||||
user.getStats().getOrDefault(entry.getKey(), 0) >= entry.getValue()
|
||||
user.getStats().getStatValue(Timeframe.TOTAL, entry.getKey()) >= entry.getValue()
|
||||
);
|
||||
|
||||
if (qualifies) {
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
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 io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
||||
import io.swagger.v3.oas.annotations.responses.ApiResponses;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/scores")
|
||||
class ScoreTableController {
|
||||
private ScoreTableService scoreTableService;
|
||||
|
||||
@Autowired
|
||||
public ScoreTableController(ScoreTableService scoreTableService) {
|
||||
this.scoreTableService = scoreTableService;
|
||||
}
|
||||
|
||||
@Operation(summary = "Get the global score table")
|
||||
@ApiResponses(value = {
|
||||
@ApiResponse(responseCode = "200", description = "Successfully retrieved global score table"),
|
||||
@ApiResponse(responseCode = "400", description = "Bad request")
|
||||
|
||||
})
|
||||
@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);
|
||||
}
|
||||
|
||||
@Operation(summary = "Get the friend score table")
|
||||
@ApiResponses(value = {
|
||||
@ApiResponse(responseCode = "200", description = "Successfully retrieved friend score table"),
|
||||
@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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
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,30 @@
|
||||
package SuMoC.backend.gamification.scoretable.models;
|
||||
|
||||
public class ScoreEntry {
|
||||
private Long userId;
|
||||
private String username;
|
||||
private Boolean isFriend;
|
||||
private Integer score;
|
||||
|
||||
public ScoreEntry(Long userId, String username, Boolean isFriend, Integer score) {
|
||||
this.userId = userId;
|
||||
this.username = username;
|
||||
this.isFriend = isFriend;
|
||||
this.score = score;
|
||||
}
|
||||
|
||||
public ScoreEntry() {}
|
||||
|
||||
// Getter & Setter
|
||||
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 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; }
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
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; }
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package SuMoC.backend.gamification.scoretable.models;
|
||||
|
||||
public enum ScoreType {
|
||||
XP,
|
||||
CO2,
|
||||
KM
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import SuMoC.backend.gamification.badges.models.UserBadge;
|
||||
import SuMoC.backend.trip.models.Trip;
|
||||
import SuMoC.backend.user.enums.RoleName;
|
||||
import SuMoC.backend.user.enums.VisibilityPrivacy;
|
||||
import SuMoC.backend.user.stats.StatsList;
|
||||
import jakarta.persistence.*;
|
||||
|
||||
import java.util.*;
|
||||
@@ -55,11 +56,8 @@ public class User {
|
||||
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
|
||||
private List<UserBadge> badges = new ArrayList<>();
|
||||
|
||||
@ElementCollection
|
||||
@CollectionTable(name = "user_stats", joinColumns = @JoinColumn(name = "user_id"))
|
||||
@MapKeyColumn(name = "stat_name")
|
||||
@Column(name = "stat_value")
|
||||
private Map<String, Integer> stats = new HashMap<>();
|
||||
@OneToOne(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
|
||||
private StatsList statsList;
|
||||
|
||||
public User() {}
|
||||
|
||||
@@ -85,8 +83,7 @@ public class User {
|
||||
this.friendCode = java.util.UUID.randomUUID().toString(); // generate unique friend code
|
||||
this.roles = new HashSet<>();
|
||||
this.roles.add(RoleName.USER);
|
||||
this.badges = new ArrayList<>();
|
||||
this.stats = new HashMap<>();
|
||||
this.statsList = new StatsList(this);
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
@@ -173,7 +170,7 @@ public class User {
|
||||
|
||||
public void setBadges(List<UserBadge> badges) { this.badges = badges; }
|
||||
|
||||
public Map<String, Integer> getStats() { return stats;}
|
||||
public StatsList getStats() { return statsList;}
|
||||
|
||||
public void setStats(Map<String, Integer> stats) { this.stats = stats; }
|
||||
public void setStats(StatsList statsList) { this.statsList = statsList; }
|
||||
}
|
||||
|
||||
@@ -17,4 +17,6 @@ public interface FriendshipRepository extends JpaRepository<Friendship, Long> {
|
||||
List<Friendship> findByFriendIdAndStatus(Long friendId, FriendShipStatus status);
|
||||
|
||||
Friendship findByUserAndFriend(User user, User friend);
|
||||
|
||||
Friendship findByUserIdAndFriendIdAndStatus(Long id, Long otherUserId, FriendShipStatus friendShipStatus);
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
@Transactional
|
||||
@@ -270,4 +271,45 @@ public class FriendshipService {
|
||||
|
||||
return dtoList;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public record NameWithFriendship(
|
||||
Long id,
|
||||
String name,
|
||||
boolean friendship
|
||||
) {}
|
||||
|
||||
public List<NameWithFriendship> getPrivateNameIfFriendshipAccepted(User user, List<Long> otherUserIds) {
|
||||
List<NameWithFriendship> result = new ArrayList<>();
|
||||
|
||||
for (Long otherUserId : otherUserIds) {
|
||||
|
||||
Friendship friendship =
|
||||
friendshipRepository.findByUserIdAndFriendIdAndStatus(
|
||||
user.getId(),
|
||||
otherUserId,
|
||||
FriendShipStatus.ACCEPTED
|
||||
);
|
||||
|
||||
if (friendship != null) {
|
||||
User target = friendship.getFriend();
|
||||
result.add(new NameWithFriendship(
|
||||
target.getId(),
|
||||
target.getPrivateName(),
|
||||
true
|
||||
));
|
||||
} else {
|
||||
User target = userRepository.findById(otherUserId)
|
||||
.orElseThrow(() -> new RuntimeException("User not found"));
|
||||
result.add(new NameWithFriendship(
|
||||
target.getId(),
|
||||
target.getPublicName(),
|
||||
false
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
package SuMoC.backend.user.stats;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
|
||||
@Entity
|
||||
public class Stats {
|
||||
|
||||
@Schema(description = "Unique identifier of the Stats.", example = "1", required = true)
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
@Schema(description = "The StatsList to which these stats belong.", required = true)
|
||||
@ManyToOne
|
||||
private StatsList statsList;
|
||||
|
||||
|
||||
@Schema(description = "Experience points earned by the user.", example = "1500", required = true)
|
||||
private Long XP;
|
||||
|
||||
@Schema(description = "Amount of CO2 saved by the user in grams.", example = "2500", required = true)
|
||||
private Long CO2_SAVED;
|
||||
|
||||
@Schema(description = "Total kilometers cycled by the user in timeframe.", example = "120", required = true)
|
||||
private Long KM_CYCLED;
|
||||
|
||||
@Schema(description = "Total kilometers walked by the user in timeframe.", example = "80", required = true)
|
||||
private Long KM_WALKED;
|
||||
|
||||
@Schema(description = "Total kilometers traveled by public transport in timeframe.", example = "200", required = true)
|
||||
private Long KM_PUBLIC_TRANSPORT;
|
||||
|
||||
@Schema(description = "Total kilometers driven by the user in timeframe.", example = "300", required = true)
|
||||
private Long KM_DRIVEN;
|
||||
|
||||
|
||||
// Constructors
|
||||
|
||||
public Stats() {
|
||||
|
||||
}
|
||||
|
||||
public Stats(StatsList statsList) {
|
||||
this.statsList = statsList;
|
||||
this.XP = 0L;
|
||||
this.CO2_SAVED = 0L;
|
||||
this.KM_CYCLED = 0L;
|
||||
this.KM_WALKED = 0L;
|
||||
this.KM_PUBLIC_TRANSPORT = 0L;
|
||||
this.KM_DRIVEN = 0L;
|
||||
}
|
||||
|
||||
|
||||
// Getters and Setters
|
||||
|
||||
|
||||
public StatsList getStatsList() {
|
||||
return statsList;
|
||||
}
|
||||
|
||||
public void setStatsList(StatsList statsList) {
|
||||
this.statsList = statsList;
|
||||
}
|
||||
|
||||
public Long getXP() {
|
||||
return XP;
|
||||
}
|
||||
|
||||
public void setXP(Long XP) {
|
||||
this.XP = XP;
|
||||
}
|
||||
|
||||
public Long getCO2_SAVED() {
|
||||
return CO2_SAVED;
|
||||
}
|
||||
|
||||
public void setCO2_SAVED(Long CO2_SAVED) {
|
||||
this.CO2_SAVED = CO2_SAVED;
|
||||
}
|
||||
|
||||
public Long getKM_CYCLED() {
|
||||
return KM_CYCLED;
|
||||
}
|
||||
|
||||
public void setKM_CYCLED(Long KM_CYCLED) {
|
||||
this.KM_CYCLED = KM_CYCLED;
|
||||
}
|
||||
|
||||
public Long getKM_WALKED() {
|
||||
return KM_WALKED;
|
||||
}
|
||||
|
||||
public void setKM_WALKED(Long KM_WALKED) {
|
||||
this.KM_WALKED = KM_WALKED;
|
||||
}
|
||||
|
||||
public Long getKM_PUBLIC_TRANSPORT() {
|
||||
return KM_PUBLIC_TRANSPORT;
|
||||
}
|
||||
|
||||
public void setKM_PUBLIC_TRANSPORT(Long KM_PUBLIC_TRANSPORT) {
|
||||
this.KM_PUBLIC_TRANSPORT = KM_PUBLIC_TRANSPORT;
|
||||
}
|
||||
|
||||
public Long getKM_DRIVEN() {
|
||||
return KM_DRIVEN;
|
||||
}
|
||||
|
||||
public void setKM_DRIVEN(Long KM_DRIVEN) {
|
||||
this.KM_DRIVEN = KM_DRIVEN;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,215 @@
|
||||
package SuMoC.backend.user.stats;
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
|
||||
import SuMoC.backend.gamification.Timeframe;
|
||||
import SuMoC.backend.user.models.User;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.persistence.*;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.mapstruct.Context;
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
@Entity
|
||||
public class StatsList {
|
||||
@Schema(description = "Unique identifier of the StatsList.", example = "1")
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
@Schema(description = "The User to whom this StatsList belongs.")
|
||||
@OneToOne
|
||||
@MapsId
|
||||
@JoinColumn(name = "user_id")
|
||||
private User user;
|
||||
|
||||
@Schema(description = "Total stats of the user.")
|
||||
@OneToOne
|
||||
private Stats totalStats;
|
||||
|
||||
@Schema(description = "Monthly stats of the user.")
|
||||
@OneToOne
|
||||
private Stats monthlyStats;
|
||||
|
||||
@Schema(description = "Weekly stats of the user.")
|
||||
@OneToOne
|
||||
private Stats weeklyStats;
|
||||
|
||||
|
||||
// Constructors
|
||||
|
||||
public StatsList() {}
|
||||
|
||||
public StatsList(User user) {
|
||||
this.user = user;
|
||||
this.totalStats = new Stats(this);
|
||||
this.monthlyStats = new Stats(this);
|
||||
this.weeklyStats = new Stats(this);
|
||||
}
|
||||
|
||||
|
||||
// Getter & Setter
|
||||
|
||||
|
||||
public User getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public void setUser(User user) {
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
public Stats getTotalStats() {
|
||||
return totalStats;
|
||||
}
|
||||
|
||||
public void setTotalStats(Stats totalStats) {
|
||||
this.totalStats = totalStats;
|
||||
}
|
||||
|
||||
public Stats getMonthlyStats() {
|
||||
return monthlyStats;
|
||||
}
|
||||
|
||||
public void setMonthlyStats(Stats monthlyStats) {
|
||||
this.monthlyStats = monthlyStats;
|
||||
}
|
||||
|
||||
public Stats getWeeklyStats() {
|
||||
return weeklyStats;
|
||||
}
|
||||
|
||||
public void setWeeklyStats(Stats weeklyStats) {
|
||||
this.weeklyStats = weeklyStats;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Other Methods
|
||||
|
||||
/**
|
||||
* Get the value of a specific stat field for a given timeframe.
|
||||
* @param timeframe
|
||||
* @param fieldName
|
||||
* @return
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
Field field = Stats.class.getDeclaredField(fieldName);
|
||||
field.setAccessible(true);
|
||||
return (Long) field.get(stats);
|
||||
|
||||
} catch (NoSuchFieldException | IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
return 0L;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the Field object of a specific stat field for a given timeframe.
|
||||
* @param timeframe
|
||||
* @param fieldName
|
||||
* @return
|
||||
*/
|
||||
public Field getStat(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);
|
||||
}
|
||||
|
||||
Field field = Stats.class.getDeclaredField(fieldName);
|
||||
field.setAccessible(true);
|
||||
return field;
|
||||
|
||||
} catch (NoSuchFieldException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Adds a value to a specific stat field for a given timeframe.
|
||||
* @param timeframe
|
||||
* @param fieldName
|
||||
* @param value
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
Field field = Stats.class.getDeclaredField(fieldName);
|
||||
field.setAccessible(true);
|
||||
|
||||
Long currentValue = (Long) field.get(stats);
|
||||
if (currentValue == null) currentValue = 0L;
|
||||
|
||||
field.set(stats, currentValue + value);
|
||||
|
||||
} catch (NoSuchFieldException | IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException("Failed to update stat " + fieldName, e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Adds a value to a specific stat field for all timeframes.
|
||||
* @param fieldName
|
||||
* @param value
|
||||
*/
|
||||
public void addToAllStats(String fieldName, long value) {
|
||||
addToStat(Timeframe.TOTAL, fieldName, value);
|
||||
addToStat(Timeframe.MONTHLY, fieldName, value);
|
||||
addToStat(Timeframe.WEEKLY, fieldName, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets all stats for a given timeframe to zero.
|
||||
* @param timeframe
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
Field[] fields = Stats.class.getDeclaredFields();
|
||||
for (Field field : fields) {
|
||||
if (field.getType().equals(Long.class)) {
|
||||
field.setAccessible(true);
|
||||
field.set(stats, 0L);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException("Failed to reset stats for timeframe " + timeframe, e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package SuMoC.backend.user.stats;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Repository
|
||||
public interface StatsListRepository extends JpaRepository<StatsList, Long> {
|
||||
Optional<StatsList> findByUserId(Long userId);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package SuMoC.backend.user.stats;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface StatsRepository extends JpaRepository<Stats, Long> {
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package SuMoC.backend.user.stats;
|
||||
|
||||
import SuMoC.backend.gamification.Timeframe;
|
||||
import SuMoC.backend.user.repository.UserRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class StatsService {
|
||||
|
||||
@Autowired
|
||||
private StatsListRepository statsListRepository;
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
public void addToWeeklyStats(Long userId, String name, int value) {
|
||||
StatsList statsList = this.statsListRepository.findByUserId(userId)
|
||||
.orElseGet(() -> new StatsList(userRepository.getReferenceById(userId)));
|
||||
|
||||
statsList.getStat(Timeframe.WEEKLY, name).get().addValue(value);
|
||||
this.statsListRepository.save(statsList);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user