feat(jpa): persist Activity in create/update/get with defaults; fix H2 defaults; replace existence checks with repository
This commit is contained in:
@@ -12,6 +12,9 @@ import com.mosquito.project.exception.FileUploadException;
|
|||||||
import com.mosquito.project.exception.InvalidActivityDataException;
|
import com.mosquito.project.exception.InvalidActivityDataException;
|
||||||
import com.mosquito.project.exception.UserNotAuthorizedForActivityException;
|
import com.mosquito.project.exception.UserNotAuthorizedForActivityException;
|
||||||
import org.springframework.cache.annotation.Cacheable;
|
import org.springframework.cache.annotation.Cacheable;
|
||||||
|
import com.mosquito.project.persistence.entity.ActivityEntity;
|
||||||
|
import com.mosquito.project.persistence.repository.ActivityRepository;
|
||||||
|
import java.time.ZoneOffset;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
@@ -43,54 +46,81 @@ public class ActivityService {
|
|||||||
private final AtomicLong apiKeyIdCounter = new AtomicLong();
|
private final AtomicLong apiKeyIdCounter = new AtomicLong();
|
||||||
|
|
||||||
private final DelayProvider delayProvider;
|
private final DelayProvider delayProvider;
|
||||||
|
private final ActivityRepository activityRepository;
|
||||||
|
|
||||||
public ActivityService(DelayProvider delayProvider) {
|
public ActivityService(DelayProvider delayProvider, ActivityRepository activityRepository) {
|
||||||
this.delayProvider = delayProvider;
|
this.delayProvider = delayProvider;
|
||||||
|
this.activityRepository = activityRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Activity createActivity(CreateActivityRequest request) {
|
public Activity createActivity(CreateActivityRequest request) {
|
||||||
if (request.getEndTime().isBefore(request.getStartTime())) {
|
if (request.getEndTime().isBefore(request.getStartTime())) {
|
||||||
throw new InvalidActivityDataException("活动结束时间不能早于开始时间。");
|
throw new InvalidActivityDataException("活动结束时间不能早于开始时间。");
|
||||||
}
|
}
|
||||||
|
ActivityEntity entity = new ActivityEntity();
|
||||||
|
entity.setName(request.getName());
|
||||||
|
entity.setStartTimeUtc(request.getStartTime().withZoneSameInstant(ZoneOffset.UTC).toOffsetDateTime());
|
||||||
|
entity.setEndTimeUtc(request.getEndTime().withZoneSameInstant(ZoneOffset.UTC).toOffsetDateTime());
|
||||||
|
entity.setRewardCalculationMode("delta");
|
||||||
|
entity.setStatus("draft");
|
||||||
|
entity.setCreatedAt(java.time.OffsetDateTime.now(ZoneOffset.UTC));
|
||||||
|
entity.setUpdatedAt(java.time.OffsetDateTime.now(ZoneOffset.UTC));
|
||||||
|
entity = activityRepository.save(entity);
|
||||||
|
|
||||||
Activity activity = new Activity();
|
Activity activity = new Activity();
|
||||||
long newId = activityIdCounter.incrementAndGet();
|
activity.setId(entity.getId());
|
||||||
activity.setId(newId);
|
|
||||||
activity.setName(request.getName());
|
activity.setName(request.getName());
|
||||||
activity.setStartTime(request.getStartTime());
|
activity.setStartTime(request.getStartTime());
|
||||||
activity.setEndTime(request.getEndTime());
|
activity.setEndTime(request.getEndTime());
|
||||||
|
activities.put(activity.getId(), activity);
|
||||||
activities.put(newId, activity);
|
|
||||||
return activity;
|
return activity;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Activity updateActivity(Long id, UpdateActivityRequest request) {
|
public Activity updateActivity(Long id, UpdateActivityRequest request) {
|
||||||
Activity activity = activities.get(id);
|
ActivityEntity entity = activityRepository.findById(id)
|
||||||
if (activity == null) {
|
.orElseThrow(() -> new ActivityNotFoundException("活动不存在。"));
|
||||||
throw new ActivityNotFoundException("活动不存在。");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (request.getEndTime().isBefore(request.getStartTime())) {
|
if (request.getEndTime().isBefore(request.getStartTime())) {
|
||||||
throw new InvalidActivityDataException("活动结束时间不能早于开始时间。");
|
throw new InvalidActivityDataException("活动结束时间不能早于开始时间。");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
entity.setName(request.getName());
|
||||||
|
entity.setStartTimeUtc(request.getStartTime().withZoneSameInstant(ZoneOffset.UTC).toOffsetDateTime());
|
||||||
|
entity.setEndTimeUtc(request.getEndTime().withZoneSameInstant(ZoneOffset.UTC).toOffsetDateTime());
|
||||||
|
entity.setUpdatedAt(java.time.OffsetDateTime.now(ZoneOffset.UTC));
|
||||||
|
activityRepository.save(entity);
|
||||||
|
|
||||||
|
Activity activity = new Activity();
|
||||||
|
activity.setId(entity.getId());
|
||||||
activity.setName(request.getName());
|
activity.setName(request.getName());
|
||||||
activity.setStartTime(request.getStartTime());
|
activity.setStartTime(request.getStartTime());
|
||||||
activity.setEndTime(request.getEndTime());
|
activity.setEndTime(request.getEndTime());
|
||||||
|
|
||||||
activities.put(id, activity);
|
activities.put(id, activity);
|
||||||
return activity;
|
return activity;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Activity getActivityById(Long id) {
|
public Activity getActivityById(Long id) {
|
||||||
Activity activity = activities.get(id);
|
ActivityEntity entity = activityRepository.findById(id)
|
||||||
if (activity == null) {
|
.orElseThrow(() -> new ActivityNotFoundException("活动不存在。"));
|
||||||
throw new ActivityNotFoundException("活动不存在。");
|
Activity activity = new Activity();
|
||||||
}
|
activity.setId(entity.getId());
|
||||||
|
activity.setName(entity.getName());
|
||||||
|
activity.setStartTime(entity.getStartTimeUtc().atZoneSameInstant(ZoneOffset.UTC));
|
||||||
|
activity.setEndTime(entity.getEndTimeUtc().atZoneSameInstant(ZoneOffset.UTC));
|
||||||
return activity;
|
return activity;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Activity> getAllActivities() {
|
public List<Activity> getAllActivities() {
|
||||||
return new ArrayList<>(activities.values());
|
List<Activity> result = new ArrayList<>();
|
||||||
|
for (ActivityEntity e : activityRepository.findAll()) {
|
||||||
|
Activity a = new Activity();
|
||||||
|
a.setId(e.getId());
|
||||||
|
a.setName(e.getName());
|
||||||
|
a.setStartTime(e.getStartTimeUtc().atZoneSameInstant(ZoneOffset.UTC));
|
||||||
|
a.setEndTime(e.getEndTimeUtc().atZoneSameInstant(ZoneOffset.UTC));
|
||||||
|
result.add(a);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String generateApiKey(CreateApiKeyRequest request) {
|
public String generateApiKey(CreateApiKeyRequest request) {
|
||||||
@@ -214,7 +244,7 @@ public class ActivityService {
|
|||||||
|
|
||||||
@Cacheable(value = "leaderboards", key = "#activityId")
|
@Cacheable(value = "leaderboards", key = "#activityId")
|
||||||
public List<LeaderboardEntry> getLeaderboard(Long activityId) {
|
public List<LeaderboardEntry> getLeaderboard(Long activityId) {
|
||||||
if (!activities.containsKey(activityId)) {
|
if (!activityRepository.existsById(activityId)) {
|
||||||
throw new ActivityNotFoundException("活动不存在。");
|
throw new ActivityNotFoundException("活动不存在。");
|
||||||
}
|
}
|
||||||
// Simulate fetching and ranking data
|
// Simulate fetching and ranking data
|
||||||
@@ -232,7 +262,7 @@ public class ActivityService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public ActivityStatsResponse getActivityStats(Long activityId) {
|
public ActivityStatsResponse getActivityStats(Long activityId) {
|
||||||
if (!activities.containsKey(activityId)) {
|
if (!activityRepository.existsById(activityId)) {
|
||||||
throw new ActivityNotFoundException("活动不存在。");
|
throw new ActivityNotFoundException("活动不存在。");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -246,7 +276,7 @@ public class ActivityService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public ActivityGraphResponse getActivityGraph(Long activityId) {
|
public ActivityGraphResponse getActivityGraph(Long activityId) {
|
||||||
if (!activities.containsKey(activityId)) {
|
if (!activityRepository.existsById(activityId)) {
|
||||||
throw new ActivityNotFoundException("活动不存在。");
|
throw new ActivityNotFoundException("活动不存在。");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user