-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
184 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 2 additions & 7 deletions
9
src/main/java/com/faforever/api/achievements/AchievementUpdateRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/main/java/com/faforever/api/event/EventRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.faforever.api.event; | ||
|
||
import com.faforever.api.data.domain.Event; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface EventRepository extends JpaRepository<Event, String> { | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/faforever/api/event/EventUpdateRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.faforever.api.event; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
class EventUpdateRequest { | ||
|
||
private int playerId; | ||
private String eventId; | ||
private int count; | ||
|
||
} |
53 changes: 53 additions & 0 deletions
53
src/main/java/com/faforever/api/event/EventsController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.faforever.api.event; | ||
|
||
import com.faforever.api.security.OAuthScope; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.google.common.collect.ImmutableMap.Builder; | ||
import com.yahoo.elide.jsonapi.models.Data; | ||
import com.yahoo.elide.jsonapi.models.JsonApiDocument; | ||
import com.yahoo.elide.jsonapi.models.Resource; | ||
import io.swagger.annotations.ApiOperation; | ||
import org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import javax.inject.Inject; | ||
import java.util.Arrays; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import java.util.stream.Collectors; | ||
|
||
@RestController | ||
@RequestMapping(path = "/events") | ||
public class EventsController { | ||
|
||
private static final String JSON_API_MEDIA_TYPE = "application/vnd.api+json"; | ||
private final EventsService eventsService; | ||
private AtomicInteger nextUpdateId; | ||
|
||
@Inject | ||
public EventsController(EventsService eventsService) { | ||
this.eventsService = eventsService; | ||
nextUpdateId = new AtomicInteger(); | ||
} | ||
|
||
@ApiOperation(value = "Updates the state and progress of one or multiple events.") | ||
@PreAuthorize("#oauth2.hasScope('" + OAuthScope._WRITE_EVENTS + "')") | ||
@RequestMapping(value = "/update", method = RequestMethod.PATCH, produces = JSON_API_MEDIA_TYPE) | ||
public JsonApiDocument update(@RequestBody EventUpdateRequest[] updateRequests) { | ||
return new JsonApiDocument(new Data<>(Arrays.stream(updateRequests) | ||
.map(request -> eventsService.increment(request.getPlayerId(), request.getEventId(), request.getCount())) | ||
.map(this::toResource) | ||
.collect(Collectors.toList()))); | ||
} | ||
|
||
private Resource toResource(UpdatedEventResponse updatedEventResponse) { | ||
Builder<String, Object> attributesBuilder = ImmutableMap.<String, Object>builder() | ||
.put("eventId", updatedEventResponse.getEventId()) | ||
.put("currentCount", updatedEventResponse.getCurrentCount()); | ||
|
||
return new Resource("updatedEvent", String.valueOf(nextUpdateId.getAndIncrement()), | ||
attributesBuilder.build(), null, null, null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.faforever.api.event; | ||
|
||
import com.faforever.api.data.domain.Event; | ||
import com.faforever.api.data.domain.PlayerEvent; | ||
import com.google.common.base.MoreObjects; | ||
import org.springframework.stereotype.Service; | ||
|
||
import javax.inject.Inject; | ||
import java.util.function.BiFunction; | ||
|
||
@Service | ||
public class EventsService { | ||
|
||
private final EventRepository eventRepository; | ||
private final PlayerEventRepository playerEventRepository; | ||
|
||
@Inject | ||
public EventsService(EventRepository eventRepository, PlayerEventRepository playerEventRepository) { | ||
this.eventRepository = eventRepository; | ||
this.playerEventRepository = playerEventRepository; | ||
} | ||
|
||
UpdatedEventResponse increment(int playerId, String eventId, int steps) { | ||
BiFunction<Integer, Integer, Integer> stepsFunction = (currentSteps, newSteps) -> currentSteps + newSteps; | ||
Event event = eventRepository.getOne(eventId); | ||
|
||
PlayerEvent playerEvent = getOrCreatePlayerEvent(playerId, event); | ||
|
||
int currentSteps1 = MoreObjects.firstNonNull(playerEvent.getCurrentCount(), 0); | ||
int newCurrentCount = stepsFunction.apply(currentSteps1, steps); | ||
|
||
playerEvent.setCurrentCount(newCurrentCount); | ||
playerEventRepository.save(playerEvent); | ||
|
||
return new UpdatedEventResponse(eventId, newCurrentCount); | ||
} | ||
|
||
private PlayerEvent getOrCreatePlayerEvent(int playerId, Event event) { | ||
return playerEventRepository.findOneByEventIdAndPlayerId(event.getId(), playerId) | ||
.orElseGet(() -> new PlayerEvent() | ||
.setPlayerId(playerId) | ||
.setEvent(event)); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/faforever/api/event/PlayerEventRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.faforever.api.event; | ||
|
||
import com.faforever.api.data.domain.PlayerEvent; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.Optional; | ||
|
||
@Repository | ||
public interface PlayerEventRepository extends JpaRepository<PlayerEvent, String> { | ||
|
||
Optional<PlayerEvent> findOneByEventIdAndPlayerId(String eventId, int playerId); | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/faforever/api/event/UpdatedEventResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.faforever.api.event; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
class UpdatedEventResponse { | ||
|
||
private final String eventId; | ||
private final Integer currentCount; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
spring: | ||
profiles: | ||
include: int | ||
datasource: | ||
url: jdbc:h2:mem:faf | ||
username: root | ||
|