Skip to content

Commit

Permalink
window api sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
rfresh2 committed Jan 29, 2025
1 parent 5317cf8 commit 8dfd8d9
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 29 deletions.
7 changes: 7 additions & 0 deletions src/main/java/vc/config/WebConfig.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package vc.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.resource.PathResourceResolver;
import vc.util.Sort;

@Configuration
public class WebConfig implements WebMvcConfigurer {
Expand All @@ -33,4 +35,9 @@ public void addCorsMappings(CorsRegistry registry) {
.allowedHeaders("*")
.maxAge(300);
}

@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(String.class, Sort.class, Sort::fromValue);
}
}
44 changes: 35 additions & 9 deletions src/main/java/vc/controller/ChatsController.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.tags.Tags;
import org.jooq.Condition;
import org.jooq.DSLContext;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.format.annotation.DateTimeFormat;
Expand All @@ -15,6 +16,7 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import vc.util.PlayerLookup;
import vc.util.Sort;

import java.time.LocalDate;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -122,7 +124,11 @@ public ResponseEntity<ChatsResponse> chats(
@ApiResponse(
responseCode = "200",
description = """
All 2b2t chats during a window of time, starting from startDate (required) until endDate or pageSize is met
All 2b2t chats during a window of time, starting from startDate until endDate or pageSize is met.
Results are sorted in ascending order by default.
startDate is required if sort is ASC, endDate is required if sort is DESC.
startDate and endDate must be ISO 8601 formatted strings, in this format: yyyy-MM-dd'T'HH:mm:ss.SSSXXX
Expand All @@ -147,25 +153,45 @@ public ResponseEntity<ChatsResponse> chats(
)
})
public ResponseEntity<ChatWindowResponse> chatWindow(
@RequestParam(value = "startDate", required = true) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startDate,
@RequestParam(value = "startDate", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startDate,
@RequestParam(value = "endDate", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endDate,
@RequestParam(value = "sort", required = false) Sort sort,
@RequestParam(value = "pageSize", required = false) Integer pageSize
) {
if (pageSize != null && pageSize > 100) {
return ResponseEntity.badRequest().build();
}
if (sort == null) sort = Sort.ASC;
final int size = pageSize == null ? 25 : pageSize;
var baseQuery = dsl.select(CHATS.PLAYER_NAME, CHATS.PLAYER_UUID, CHATS.TIME, CHATS.CHAT)
.from(CHATS)
.where(CHATS.TIME.greaterOrEqual(startDate.atOffset(ZoneOffset.UTC)));
if (endDate != null) {
if (endDate != null && startDate != null) {
if (endDate.equals(startDate) || endDate.isBefore(startDate)) {
return ResponseEntity.badRequest().build();
}
baseQuery = baseQuery.and(CHATS.TIME.lessOrEqual(endDate.atOffset(ZoneOffset.UTC)));
}
var chats = baseQuery
.orderBy(CHATS.TIME.asc())
Condition c = null;
switch (sort) {
case ASC -> {
if (startDate == null) {
return ResponseEntity.badRequest().build();
}
c = CHATS.TIME.greaterOrEqual(startDate.atOffset(ZoneOffset.UTC));
if (endDate != null)
c = c.and(CHATS.TIME.lessOrEqual(endDate.atOffset(ZoneOffset.UTC)));
}
case DESC -> {
if (endDate == null) {
return ResponseEntity.badRequest().build();
}
c = CHATS.TIME.lessOrEqual(endDate.atOffset(ZoneOffset.UTC));
if (startDate != null)
c = c.and(CHATS.TIME.greaterOrEqual(startDate.atOffset(ZoneOffset.UTC)));
}
default -> throw new IllegalStateException("Unexpected value: " + sort);
}
var chats = dsl.select(CHATS.PLAYER_NAME, CHATS.PLAYER_UUID, CHATS.TIME, CHATS.CHAT)
.from(CHATS)
.where(c)
.orderBy(CHATS.TIME.sort(sort.toJooq()))
.limit(size)
.fetch()
.into(PlayerChat.class);
Expand Down
36 changes: 29 additions & 7 deletions src/main/java/vc/controller/ConnectionsController.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.tags.Tags;
import org.jooq.Condition;
import org.jooq.DSLContext;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.format.annotation.DateTimeFormat;
Expand All @@ -16,6 +17,7 @@
import org.springframework.web.bind.annotation.RestController;
import vc.data.dto.enums.Connectiontype;
import vc.util.PlayerLookup;
import vc.util.Sort;

import java.time.LocalDate;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -148,24 +150,44 @@ public ResponseEntity<ConnectionsResponse> connections(
)
})
public ResponseEntity<ConnectionsWindowResponse> connectionsWindow(
@RequestParam(value = "startDate", required = true) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startDate,
@RequestParam(value = "startDate", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startDate,
@RequestParam(value = "endDate", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endDate,
@RequestParam(value = "sort", required = false) Sort sort,
@RequestParam(value = "pageSize", required = false) Integer pageSize
) {
if (pageSize != null && pageSize > 100) {
return ResponseEntity.badRequest().build();
}
if (sort == null) sort = Sort.ASC;
final int size = pageSize == null ? 25 : pageSize;
var baseQuery = dsl.selectFrom(CONNECTIONS)
.where(CONNECTIONS.TIME.greaterOrEqual(startDate.atOffset(ZoneOffset.UTC)));
if (endDate != null) {
if (endDate != null && startDate != null) {
if (endDate.equals(startDate) || endDate.isBefore(startDate)) {
return ResponseEntity.badRequest().build();
}
baseQuery = baseQuery.and(CONNECTIONS.TIME.lessOrEqual(endDate.atOffset(ZoneOffset.UTC)));
}
List<PlayerConnection> connections = baseQuery
.orderBy(CONNECTIONS.TIME.asc())
Condition c = null;
switch (sort) {
case ASC -> {
if (startDate == null) {
return ResponseEntity.badRequest().build();
}
c = CONNECTIONS.TIME.greaterOrEqual(startDate.atOffset(ZoneOffset.UTC));
if (endDate != null)
c = c.and(CONNECTIONS.TIME.lessOrEqual(endDate.atOffset(ZoneOffset.UTC)));
}
case DESC -> {
if (endDate == null) {
return ResponseEntity.badRequest().build();
}
c = CONNECTIONS.TIME.lessOrEqual(endDate.atOffset(ZoneOffset.UTC));
if (startDate != null)
c = c.and(CONNECTIONS.TIME.greaterOrEqual(startDate.atOffset(ZoneOffset.UTC)));
}
default -> throw new IllegalStateException("Unexpected value: " + sort);
}
List<PlayerConnection> connections = dsl.selectFrom(CONNECTIONS)
.where(c)
.orderBy(CONNECTIONS.TIME.sort(sort.toJooq()))
.limit(size)
.fetch()
.into(PlayerConnection.class);
Expand Down
38 changes: 30 additions & 8 deletions src/main/java/vc/controller/DeathsController.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.tags.Tags;
import org.jooq.Condition;
import org.jooq.DSLContext;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.format.annotation.DateTimeFormat;
Expand All @@ -15,6 +16,7 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import vc.util.PlayerLookup;
import vc.util.Sort;

import java.time.LocalDate;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -159,25 +161,45 @@ public ResponseEntity<DeathsResponse> deaths(
)
})
public ResponseEntity<DeathsWindowResponse> deathsWindow(
@RequestParam(value = "startDate", required = true) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startDate,
@RequestParam(value = "startDate", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startDate,
@RequestParam(value = "endDate", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endDate,
@RequestParam(value = "sort", required = false) Sort sort,
@RequestParam(value = "pageSize", required = false) Integer pageSize
) {
if (pageSize != null && pageSize > 100) {
return ResponseEntity.badRequest().build();
}
if (sort == null) sort = Sort.ASC;
final int size = pageSize == null ? 25 : pageSize;
var baseQuery = dsl
.selectFrom(DEATHS)
.where(DEATHS.TIME.greaterOrEqual(startDate.atOffset(ZoneOffset.UTC)));
if (endDate != null) {
if (endDate != null && startDate != null) {
if (endDate.equals(startDate) || endDate.isBefore(startDate)) {
return ResponseEntity.badRequest().build();
}
baseQuery = baseQuery.and(DEATHS.TIME.lessOrEqual(endDate.atOffset(ZoneOffset.UTC)));
}
List<Death> deathsList = baseQuery
.orderBy(DEATHS.TIME.asc())
Condition c = null;
switch (sort) {
case ASC -> {
if (startDate == null) {
return ResponseEntity.badRequest().build();
}
c = DEATHS.TIME.greaterOrEqual(startDate.atOffset(ZoneOffset.UTC));
if (endDate != null)
c = c.and(DEATHS.TIME.lessOrEqual(endDate.atOffset(ZoneOffset.UTC)));
}
case DESC -> {
if (endDate == null) {
return ResponseEntity.badRequest().build();
}
c = DEATHS.TIME.lessOrEqual(endDate.atOffset(ZoneOffset.UTC));
if (startDate != null)
c = c.and(DEATHS.TIME.greaterOrEqual(startDate.atOffset(ZoneOffset.UTC)));
}
default -> throw new IllegalStateException("Unexpected value: " + sort);
}
List<Death> deathsList = dsl
.selectFrom(DEATHS)
.where(c)
.orderBy(DEATHS.TIME.sort(sort.toJooq()))
.limit(size)
.fetch()
.into(Death.class);
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/vc/util/Sort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package vc.util;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import org.jooq.SortOrder;

public enum Sort {
ASC, DESC;

@JsonCreator
public static Sort fromValue(String value) {
return Sort.valueOf(value.toUpperCase());
}

@JsonValue
public String toValue() {
return this.name().toLowerCase();
}

public SortOrder toJooq() {
return this == ASC ? SortOrder.ASC : SortOrder.DESC;
}
}
Loading

0 comments on commit 8dfd8d9

Please sign in to comment.