mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-02-28 17:10:48 +08:00
feat: track milk tea redemption count
This commit is contained in:
@@ -27,26 +27,30 @@ public class ActivityController {
|
||||
@GetMapping("/milk-tea")
|
||||
public MilkTeaInfo milkTea() {
|
||||
Activity a = activityService.getByType(ActivityType.MILK_TEA);
|
||||
long count = activityService.countLevel1Users();
|
||||
if (!a.isEnded() && count > 50) {
|
||||
long count = activityService.countParticipants(a);
|
||||
if (!a.isEnded() && count >= 50) {
|
||||
activityService.end(a);
|
||||
}
|
||||
MilkTeaInfo info = new MilkTeaInfo();
|
||||
info.setLevel1Count(count);
|
||||
info.setRedeemCount(count);
|
||||
info.setEnded(a.isEnded());
|
||||
return info;
|
||||
}
|
||||
|
||||
@PostMapping("/milk-tea/redeem")
|
||||
public void redeemMilkTea(@RequestBody RedeemRequest req, Authentication auth) {
|
||||
public java.util.Map<String, String> redeemMilkTea(@RequestBody RedeemRequest req, Authentication auth) {
|
||||
User user = userService.findByIdentifier(auth.getName()).orElseThrow();
|
||||
Activity a = activityService.getByType(ActivityType.MILK_TEA);
|
||||
activityService.redeem(a, user, req.getContact());
|
||||
boolean first = activityService.redeem(a, user, req.getContact());
|
||||
if (first) {
|
||||
return java.util.Map.of("message", "redeemed");
|
||||
}
|
||||
return java.util.Map.of("message", "updated");
|
||||
}
|
||||
|
||||
@Data
|
||||
private static class MilkTeaInfo {
|
||||
private long level1Count;
|
||||
private long redeemCount;
|
||||
private boolean ended;
|
||||
}
|
||||
|
||||
|
||||
@@ -37,9 +37,20 @@ public class ActivityService {
|
||||
activityRepository.save(activity);
|
||||
}
|
||||
|
||||
public void redeem(Activity activity, User user, String contact) {
|
||||
public long countParticipants(Activity activity) {
|
||||
return activity.getParticipants().size();
|
||||
}
|
||||
|
||||
/**
|
||||
* Redeem an activity for the given user.
|
||||
*
|
||||
* @return true if the user redeemed for the first time, false if the
|
||||
* information was simply updated
|
||||
*/
|
||||
public boolean redeem(Activity activity, User user, String contact) {
|
||||
notificationService.createActivityRedeemNotifications(user, contact);
|
||||
activity.getParticipants().add(user);
|
||||
boolean added = activity.getParticipants().add(user);
|
||||
activityRepository.save(activity);
|
||||
return added;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user