Badges and states added #6
@@ -1,6 +1,9 @@
|
||||
package SuMoC.backend.gamification.badges;
|
||||
|
||||
import SuMoC.backend.gamification.badges.models.Badge;
|
||||
import SuMoC.backend.gamification.badges.models.UserBadgeDTO;
|
||||
import SuMoC.backend.user.models.User;
|
||||
import SuMoC.backend.user.service.UserService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
||||
import io.swagger.v3.oas.annotations.responses.ApiResponses;
|
||||
@@ -24,7 +27,6 @@ public class BadgeController {
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Operation(summary = "Create a new badge")
|
||||
@ApiResponses(value = {
|
||||
@ApiResponse(responseCode = "201", description = "Badge created successfully"),
|
||||
@@ -88,4 +90,25 @@ public class BadgeController {
|
||||
List<Badge> badges = badgeService.getAllBadges();
|
||||
return ResponseEntity.status(HttpStatus.OK).body(badges);
|
||||
}
|
||||
|
||||
@Operation(summary = "Get Badges by user")
|
||||
@ApiResponses(value = {
|
||||
@ApiResponse(responseCode = "200", description = "Badges retrieved successfully"),
|
||||
@ApiResponse(responseCode = "404", description = "No badges found for user")
|
||||
})
|
||||
@GetMapping("/getBadges/{userId}")
|
||||
public ResponseEntity<List<UserBadgeDTO>> getBadgesByUser(@PathVariable("userId") Long userId) {
|
||||
List<UserBadgeDTO> badges = badgeService.getUserBadges(userId);
|
||||
return ResponseEntity.ok(badges);
|
||||
}
|
||||
|
||||
@Operation(summary = "Check badges for current user and award newly unlocked ones")
|
||||
@ApiResponses(value = {
|
||||
@ApiResponse(responseCode = "200", description = "Check successful")
|
||||
})
|
||||
@PostMapping("/checkBadgeAndAward")
|
||||
public ResponseEntity<Void> checkAndAwardBadges() {
|
||||
badgeService.checkAndAwardBadges();
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,7 +44,8 @@ public class BadgeService {
|
||||
}
|
||||
|
||||
|
||||
public void checkAndAwardBadges(User user) {
|
||||
public void checkAndAwardBadges() {
|
||||
User user = currentUser.get();
|
||||
List<Badge> badges = badgeRepository.findAll();
|
||||
|
||||
for (Badge badge : badges) {
|
||||
|
||||
@@ -1,15 +1,20 @@
|
||||
package SuMoC.backend.user.controller;
|
||||
|
||||
import SuMoC.backend.security.dtos.LoginResponseDTO;
|
||||
import SuMoC.backend.user.dtos.AddStatRequestDTO;
|
||||
import SuMoC.backend.user.dtos.MaximalUserDTO;
|
||||
import SuMoC.backend.user.dtos.MinimalUserDTO;
|
||||
import SuMoC.backend.user.dtos.StatScope;
|
||||
import SuMoC.backend.user.enums.VisibilityPrivacy;
|
||||
import SuMoC.backend.user.service.UserService;
|
||||
import SuMoC.backend.user.stats.StatsService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.media.Content;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
||||
import io.swagger.v3.oas.annotations.responses.ApiResponses;
|
||||
import jakarta.annotation.security.RolesAllowed;
|
||||
import jakarta.validation.Valid;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -23,10 +28,12 @@ import org.springframework.web.bind.annotation.*;
|
||||
@RequestMapping("/user")
|
||||
public class UserController {
|
||||
private final UserService userService;
|
||||
private final StatsService statsService;
|
||||
|
||||
@Autowired
|
||||
public UserController(UserService userService) {
|
||||
public UserController(UserService userService, StatsService statsService) {
|
||||
this.userService = userService;
|
||||
this.statsService = statsService;
|
||||
}
|
||||
|
||||
@Operation(summary = "Nutzer fragt ein fremdes Nutzerprofil an", description = "Ein Nutzer kann das Profil eines anderen Nutzers anfragen, um dessen öffentliche Informationen einzusehen.")
|
||||
@@ -141,4 +148,31 @@ public class UserController {
|
||||
this.userService.setVisibilityPrivacy(visibilityPrivacy);
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
@Operation(summary = "Add stats to the current user",
|
||||
description = "Adds a value to a stat field for the current user. Optionally checks and awards badges afterwards.")
|
||||
@ApiResponses(value = {
|
||||
@ApiResponse(responseCode = "200", description = "Stats updated successfully"),
|
||||
@ApiResponse(responseCode = "400", description = "Invalid stat field / invalid input",
|
||||
content = @Content(mediaType = "application/json")),
|
||||
@ApiResponse(responseCode = "403", description = "Not authorized",
|
||||
content = @Content(mediaType = "application/json"))
|
||||
})
|
||||
@RolesAllowed({"USER", "ADMIN"})
|
||||
@PostMapping("/stats/add")
|
||||
public ResponseEntity<Void> addStats(@RequestBody @Valid AddStatRequestDTO body) {
|
||||
|
||||
StatScope scope = body.getScope();
|
||||
boolean awardBadges = body.getAwardBadges() == null || body.getAwardBadges();
|
||||
|
||||
this.statsService.addStatsToCurrentUser(
|
||||
body.getStat(),
|
||||
body.getValue(),
|
||||
scope,
|
||||
awardBadges
|
||||
);
|
||||
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package SuMoC.backend.user.dtos;
|
||||
|
||||
import jakarta.validation.constraints.Min;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
public class AddStatRequestDTO {
|
||||
|
||||
@NotBlank
|
||||
private String stat;
|
||||
|
||||
@NotNull
|
||||
@Min(0)
|
||||
private Long value;
|
||||
|
||||
private StatScope scope;
|
||||
|
||||
private Boolean awardBadges;
|
||||
|
||||
public String getStat() { return stat; }
|
||||
public void setStat(String stat) { this.stat = stat; }
|
||||
|
||||
public Long getValue() { return value; }
|
||||
public void setValue(Long value) { this.value = value; }
|
||||
|
||||
|
||||
public Boolean getAwardBadges() { return awardBadges; }
|
||||
public void setAwardBadges(Boolean awardBadges) { this.awardBadges = awardBadges; }
|
||||
public StatScope getScope() { return scope; }
|
||||
public void setScope(StatScope scope) { this.scope = scope; }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
package SuMoC.backend.user.dtos;
|
||||
|
||||
public enum StatScope {
|
||||
TOTAL,
|
||||
MONTHLY,
|
||||
WEEKLY,
|
||||
ALL
|
||||
}
|
||||
@@ -34,6 +34,8 @@ public class Stats {
|
||||
@Schema(description = "Total kilometers driven by the user in timeframe.", example = "300", required = true)
|
||||
private Long KM_DRIVEN;
|
||||
|
||||
@Schema(description = "Total Mobility score", example = "45", required = true)
|
||||
private Long MOBILITY_SCORE;
|
||||
|
||||
// Constructors
|
||||
|
||||
@@ -41,6 +43,8 @@ public class Stats {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public Stats(StatsList statsList) {
|
||||
this.statsList = statsList;
|
||||
this.XP = 0L;
|
||||
@@ -49,6 +53,7 @@ public class Stats {
|
||||
this.KM_WALKED = 0L;
|
||||
this.KM_PUBLIC_TRANSPORT = 0L;
|
||||
this.KM_DRIVEN = 0L;
|
||||
this.MOBILITY_SCORE = 0L;
|
||||
}
|
||||
|
||||
|
||||
@@ -110,4 +115,12 @@ public class Stats {
|
||||
public void setKM_DRIVEN(Long KM_DRIVEN) {
|
||||
this.KM_DRIVEN = KM_DRIVEN;
|
||||
}
|
||||
|
||||
public Long getMobility_SCORE() {
|
||||
return MOBILITY_SCORE;
|
||||
}
|
||||
|
||||
public void setMobility_SCORE(Long mobility_SCORE) {
|
||||
MOBILITY_SCORE = mobility_SCORE;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,35 @@
|
||||
package SuMoC.backend.user.stats;
|
||||
|
||||
import SuMoC.backend.gamification.Timeframe;
|
||||
import SuMoC.backend.gamification.badges.BadgeService;
|
||||
import SuMoC.backend.user.dtos.StatScope;
|
||||
import SuMoC.backend.user.models.User;
|
||||
import SuMoC.backend.user.repository.UserRepository;
|
||||
import SuMoC.backend.user.service.CurrentUserService;
|
||||
import jakarta.transaction.Transactional;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
@Service
|
||||
public class StatsService {
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
private final BadgeService badgeService;
|
||||
private final CurrentUserService currentUserService;
|
||||
|
||||
public StatsService(CurrentUserService currentUserService,
|
||||
UserRepository userRepository,
|
||||
BadgeService badgeService) {
|
||||
this.currentUserService = currentUserService;
|
||||
this.userRepository = userRepository;
|
||||
this.badgeService = badgeService;
|
||||
}
|
||||
|
||||
@Scheduled(cron = "0 0 0 ? * MON")
|
||||
public void resetWeeklyStats() {
|
||||
@@ -29,4 +48,46 @@ public class StatsService {
|
||||
|
||||
System.out.println("###### Monthly stats have been reset. ######");
|
||||
}
|
||||
|
||||
private static final Set<String> ALLOWED_STATS = Set.of(
|
||||
"XP",
|
||||
"CO2_SAVED",
|
||||
"KM_CYCLED",
|
||||
"KM_WALKED",
|
||||
"KM_PUBLIC_TRANSPORT",
|
||||
"KM_DRIVEN",
|
||||
"MOBILITY_SCORE"
|
||||
);
|
||||
|
||||
@Transactional
|
||||
public void addStatsToCurrentUser(String statField, long value, StatScope scope, boolean awardBadges) {
|
||||
if (!ALLOWED_STATS.contains(statField)) {
|
||||
throw new ResponseStatusException(
|
||||
HttpStatus.BAD_REQUEST,
|
||||
"Unknown stat field: " + statField + " (allowed: " + ALLOWED_STATS + ")"
|
||||
);
|
||||
}
|
||||
|
||||
User user = currentUserService.get();
|
||||
|
||||
|
||||
if (user.getStats() == null) {
|
||||
user.setStats(new StatsList(user));
|
||||
}
|
||||
|
||||
StatScope effectiveScope = (scope == null) ? StatScope.ALL : scope;
|
||||
|
||||
switch (effectiveScope) {
|
||||
case ALL -> user.getStats().addToAllStats(statField, value);
|
||||
case TOTAL -> user.getStats().addToStat(Timeframe.TOTAL, statField, value);
|
||||
case MONTHLY -> user.getStats().addToStat(Timeframe.MONTHLY, statField, value);
|
||||
case WEEKLY -> user.getStats().addToStat(Timeframe.WEEKLY, statField, value);
|
||||
}
|
||||
|
||||
userRepository.save(user);
|
||||
|
||||
if (awardBadges) {
|
||||
badgeService.checkAndAwardBadges();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -203,7 +203,7 @@ public class BadgeServiceTest {
|
||||
|
||||
// Award first Badge
|
||||
sender.getStats().addToAllStats("XP", 50);
|
||||
badgeService.checkAndAwardBadges(sender);
|
||||
badgeService.checkAndAwardBadges();
|
||||
|
||||
badgeList = badgeService.getUserBadges(sender.getId());
|
||||
assertNotNull(badgeList);
|
||||
@@ -211,7 +211,7 @@ public class BadgeServiceTest {
|
||||
|
||||
// Raise XP and award two more
|
||||
sender.getStats().addToAllStats("XP", 20);
|
||||
badgeService.checkAndAwardBadges(sender);
|
||||
badgeService.checkAndAwardBadges();
|
||||
|
||||
badgeList = badgeService.getUserBadges(sender.getId());
|
||||
assertNotNull(badgeList);
|
||||
|
||||
Reference in New Issue
Block a user