-
Notifications
You must be signed in to change notification settings - Fork 4
853: create /leaderboard/current endpoint #863
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -12,10 +12,7 @@ | |||||||||||||||||||||||||||||
| import java.util.ArrayList; | ||||||||||||||||||||||||||||||
| import java.util.List; | ||||||||||||||||||||||||||||||
| import java.util.Optional; | ||||||||||||||||||||||||||||||
| import org.patinanetwork.codebloom.api.admin.body.CreateAnnouncementBody; | ||||||||||||||||||||||||||||||
| import org.patinanetwork.codebloom.api.admin.body.DeleteAnnouncementBody; | ||||||||||||||||||||||||||||||
| import org.patinanetwork.codebloom.api.admin.body.NewLeaderboardBody; | ||||||||||||||||||||||||||||||
| import org.patinanetwork.codebloom.api.admin.body.UpdateAdminBody; | ||||||||||||||||||||||||||||||
| import org.patinanetwork.codebloom.api.admin.body.*; | ||||||||||||||||||||||||||||||
| import org.patinanetwork.codebloom.api.admin.body.jda.DeleteMessageBody; | ||||||||||||||||||||||||||||||
| import org.patinanetwork.codebloom.common.components.DiscordClubManager; | ||||||||||||||||||||||||||||||
| import org.patinanetwork.codebloom.common.components.LeaderboardManager; | ||||||||||||||||||||||||||||||
|
|
@@ -38,12 +35,7 @@ | |||||||||||||||||||||||||||||
| import org.patinanetwork.codebloom.common.time.StandardizedOffsetDateTime; | ||||||||||||||||||||||||||||||
| import org.springframework.http.HttpStatus; | ||||||||||||||||||||||||||||||
| import org.springframework.http.ResponseEntity; | ||||||||||||||||||||||||||||||
| import org.springframework.web.bind.annotation.DeleteMapping; | ||||||||||||||||||||||||||||||
| import org.springframework.web.bind.annotation.GetMapping; | ||||||||||||||||||||||||||||||
| import org.springframework.web.bind.annotation.PostMapping; | ||||||||||||||||||||||||||||||
| import org.springframework.web.bind.annotation.RequestBody; | ||||||||||||||||||||||||||||||
| import org.springframework.web.bind.annotation.RequestMapping; | ||||||||||||||||||||||||||||||
| import org.springframework.web.bind.annotation.RestController; | ||||||||||||||||||||||||||||||
| import org.springframework.web.bind.annotation.*; | ||||||||||||||||||||||||||||||
| import org.springframework.web.server.ResponseStatusException; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| @RestController | ||||||||||||||||||||||||||||||
|
|
@@ -314,4 +306,30 @@ public ResponseEntity<ApiResponder<Empty>> deleteDiscordMessage( | |||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| return ResponseEntity.ok(ApiResponder.success("Discord Message successfully deleted", Empty.of())); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| @PutMapping("/leaderboard/current") | ||||||||||||||||||||||||||||||
| public ResponseEntity<ApiResponder<Empty>> editCurrentLeaderboard( | ||||||||||||||||||||||||||||||
| @RequestBody final EditLeaderboardBody editLeaderboardBody, final HttpServletRequest request) { | ||||||||||||||||||||||||||||||
| protector.validateAdminSession(request); | ||||||||||||||||||||||||||||||
| editLeaderboardBody.validate(); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| Optional<Leaderboard> currentLeaderboard = leaderboardRepository.getRecentLeaderboardMetadata(); | ||||||||||||||||||||||||||||||
| currentLeaderboard.ifPresent(lb -> { | ||||||||||||||||||||||||||||||
| OffsetDateTime shouldExpireBy = | ||||||||||||||||||||||||||||||
| StandardizedOffsetDateTime.normalize(editLeaderboardBody.getShouldExpireBy()); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| Leaderboard updated = Leaderboard.builder() | ||||||||||||||||||||||||||||||
| .name(editLeaderboardBody.getName()) | ||||||||||||||||||||||||||||||
| .deletedAt(lb.getDeletedAt()) | ||||||||||||||||||||||||||||||
| .createdAt(lb.getCreatedAt()) | ||||||||||||||||||||||||||||||
| .shouldExpireBy(Optional.ofNullable(shouldExpireBy).map(d -> d.toLocalDateTime())) | ||||||||||||||||||||||||||||||
| .syntaxHighlightingLanguage(Optional.of(editLeaderboardBody.getSyntaxHighlightingLanguage())) | ||||||||||||||||||||||||||||||
| .id(lb.getId()) | ||||||||||||||||||||||||||||||
| .build(); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| leaderboardRepository.updateLeaderboard(updated); | ||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| return ResponseEntity.ok().body(ApiResponder.success("Leaderboard updated successfully", Empty.of())); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
Comment on lines
+330
to
+334
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: The
Suggested change
|
||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,51 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| package org.patinanetwork.codebloom.api.admin.body; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import com.google.common.base.Strings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import jakarta.validation.constraints.NotBlank; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.time.OffsetDateTime; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import lombok.*; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import lombok.extern.jackson.Jacksonized; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.patinanetwork.codebloom.common.time.StandardizedOffsetDateTime; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.patinanetwork.codebloom.utilities.exception.ValidationException; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Getter | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Builder | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Jacksonized | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @AllArgsConstructor | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @ToString | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public class EditLeaderboardBody { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @NotBlank | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private String name; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private OffsetDateTime shouldExpireBy; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private String syntaxHighlightingLanguage; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public void validate() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var name = getName(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Check warning on line 26 in src/main/java/org/patinanetwork/codebloom/api/admin/body/EditLeaderboardBody.java
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var expire = getShouldExpireBy(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (Strings.isNullOrEmpty(name)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new ValidationException("Leaderboard name cannot be null or empty"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (name.length() == 1) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new ValidationException("Leaderboard name cannot have only 1 character"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (name.length() > 512) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new ValidationException("Leaderboard name cannot have more than 512 characters"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+26
to
+39
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: The manual
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (expire != null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| OffsetDateTime nowWithOffset = StandardizedOffsetDateTime.now(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| OffsetDateTime expiresAtWithOffset = StandardizedOffsetDateTime.normalize(expire); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| boolean isInFuture = nowWithOffset.isBefore(expiresAtWithOffset); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!isInFuture) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new ValidationException("The expiration date must be in the future"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: Converting
OffsetDateTimetoLocalDateTimeforshouldExpireBymight lead to loss of timezone information if the database orLeaderboardmodel expectsOffsetDateTime. Ensure consistency by passingOffsetDateTimedirectly to the builder, or confirm thatLocalDateTimeis the intended and correctly handled type throughout the persistence layer. Additionally, useOptional.ofNullable()forsyntaxHighlightingLanguageto safely handle potentially null values. [possible issue, importance: 8]