Skip to content

Commit

Permalink
removed unnecessary relation property from player teams #125
Browse files Browse the repository at this point in the history
  • Loading branch information
isuru89 committed Aug 7, 2022
1 parent 0de358d commit e258082
Show file tree
Hide file tree
Showing 14 changed files with 173 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@

package io.github.oasis.core.model;

import lombok.Getter;
import lombok.Setter;
import lombok.Data;

import java.io.Serializable;
import java.util.Map;
Expand All @@ -29,17 +28,15 @@
/**
* @author Isuru Weerarathna
*/
@Getter
@Setter
@Data
public class UserAssociationInfo implements Serializable {

private String email;
private long id;

private Map<Integer, GameAssociation> games;

@Getter
@Setter
@Data
public static class GameAssociation implements Serializable {
private int team;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ public void removePlayerFromTeam(long playerId, int gameId, int teamId) {
@Override
public void addPlayerToTeam(long playerId, int gameId, int teamId) {
try {
playerTeamDao.insertPlayerToTeam(gameId, playerId, teamId);
playerTeamDao.insertPlayerToTeam(playerId, teamId);
} catch (JdbiException e) {
throw new OasisApiRuntimeException(ErrorCodes.PLAYER_ALREADY_IN_TEAM, HttpStatus.BAD_REQUEST);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*
*/

package io.github.oasis.core.services.api.controllers.admin;
package io.github.oasis.core.services.api.controllers;

import io.github.oasis.core.elements.ModuleDefinition;
import io.github.oasis.core.services.api.controllers.AbstractController;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,10 @@ public TeamObject updateTeam(@PathVariable("teamId") Integer teamId,
tags = {"admin", "curator"}
)
@ForCurator
@PostMapping(value = "/players/{playerId}/teams", consumes = MediaType.APPLICATION_JSON_VALUE)
@PostMapping(value = "/players/{playerId}/teams/{teamId}", consumes = MediaType.APPLICATION_JSON_VALUE)
public void addPlayerToTeam(@PathVariable("playerId") Long playerId,
@Valid @RequestBody PlayerGameAssociationRequest request) {
playerAssignmentService.addPlayerToTeam(playerId, request.getGameId(), request.getTeamId());
@PathVariable("teamId") Integer teamId) {
playerAssignmentService.addPlayerToTeam(playerId, teamId);
}

@Operation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,12 @@ default int updateTeam(int teamId, TeamObject teamObject) {
List<TeamObject> readTeamsByName(@Bind("name") String teamName, @Bind("offset") int offset, @Bind("limit") int size);

@SqlUpdate
void insertPlayerToTeam(@Bind("gameId") int gameId,
@Bind("playerId") long playerId,
void insertPlayerToTeam(@Bind("playerId") long playerId,
@Bind("teamId") int teamId,
@Bind("ts") long timestamp);

default void insertPlayerToTeam(int gameId, long playerId, int teamId) {
insertPlayerToTeam(gameId, playerId, teamId, System.currentTimeMillis());
default void insertPlayerToTeam(long playerId, int teamId) {
insertPlayerToTeam(playerId, teamId, System.currentTimeMillis());
}

@SqlUpdate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public interface IPlayerAssignmentService {

List<TeamObject> getTeamsOfPlayer(long playerId);

void addPlayerToTeam(long playerId, int gameId, int teamId);
void addPlayerToTeam(long playerId, int teamId);

void removePlayerFromTeam(long playerId, int teamId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

package io.github.oasis.core.services.api.services.impl;

import io.github.oasis.core.Game;
import io.github.oasis.core.TeamMetadata;
import io.github.oasis.core.external.OasisRepository;
import io.github.oasis.core.external.PaginatedResult;
Expand Down Expand Up @@ -189,11 +188,10 @@ public List<TeamObject> getTeamsOfPlayer(long playerId) {
}

@Override
public void addPlayerToTeam(long playerId, int gameId, int teamId) {
public void addPlayerToTeam(long playerId, int teamId) {
PlayerObject playerRef = readPlayer(playerId);
TeamObject teamRef = readTeam(teamId);
Game gameRef = backendRepository.readGame(gameId);
backendRepository.addPlayerToTeam(playerRef.getId(), gameRef.getId(), teamRef.getId());
backendRepository.addPlayerToTeam(playerRef.getId(), teamRef.getGameId(), teamRef.getId());

eventPublisher.publishEvent(BasePlayerRelatedEvent.builder()
.changeType(EntityChangeType.MODIFIED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,10 @@
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class PlayerGameAssociationRequest implements Serializable {
public class PlayerTeamAssociationRequest implements Serializable {

private Long userId;

@NotNull(message = "Parameter 'gameId' is mandatory!")
@Positive(message = "Parameter 'gameId' must be a valid game id!")
private Integer gameId;

@NotNull(message = "Parameter 'teamId' is mandatory!")
@Positive(message = "Parameter 'teamId' must be a valid team id!")
private Integer teamId;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
databaseChangeLog:
- changeSet:
id: remove_col_gameid_player_teams
author: isuruw
changes:
- dropPrimaryKey:
tableName: OA_PLAYER_TEAM
dropIndex: true
- dropColumn:
tableName: OA_PLAYER_TEAM
columnName: game_id
- addPrimaryKey:
tableName: OA_PLAYER_TEAM
columnNames: team_id, player_id
constraintName: pk_oa_player_team
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
//// -- LEVEL 1
//// -- Schemas, Tables and References

Table OA_PLAYER {
id int [pk, increment]
version int [default: 1]
display_name varchar(64)
email varchar(64)
avatar_ref varchar(255)
timezone varchar(64)
gender smallint
is_active boolean
created_at bigint
updated_at bigint
}

Table OA_TEAM {
id int [pk, increment]
version int
name varchar(64)
color_code varchar(10)
avatar_ref varchar(255)
game_id int
is_active boolean
created_at bigint
updated_at bigint
}

Table OA_PLAYER_TEAM {
game_id int
player_id int
team_id int
created_at bigint

Indexes {
(game_id, player_id) [pk]
}
}

Table OA_ELEMENT {
id int [pk, increment]
version int
name varchar(128)
type varchar(64)
description varchar(512)
game_id int
def_id varchar(128)
is_active boolean
created_at bigint
updated_at bigint
}

Table OA_ELEMENT_DATA {
element_id int [pk, unique]
def_data blob
is_active boolean
}

Table OA_RANK_DEF {
id int [pk]
name varchar(64)
priority int
color_code varchar(10)
game_id int [not null]
}

Table OA_GAME {
id int [pk, increment]
version int
name varchar(32)
motto varchar(128)
description varchar(512)
logo_ref varchar(255)
is_active boolean
created_at bigint
updated_at bigint
}

Table OA_GAME_STATUS {
game_id int [not null]
status varchar(32)
updated_at bigint
}

Table OA_EVENT_SOURCE {
id int [pk, increment]
token varchar(255)
display_name varchar(255)
download_count smallint
is_active boolean
created_at bigint
updated_at bigint
}

Table OA_EVENT_SOURCE_KEY {
event_source_id int [not null]
public_key text
private_key text
download_count smallint
}

Table OA_EVENT_SOURCE_GAME {
game_id int [ref: > OA_GAME.id]
event_source_id int

Indexes {
(game_id, event_source_id) [pk]
}
}

Table OA_API_KEY {
token varchar(128)
secret_key varchar(255)
roles int
is_active boolean
}

Ref: OA_RANK_DEF.game_id > OA_GAME.id
Ref: OA_TEAM.game_id > OA_GAME.id
REF: OA_PLAYER_TEAM.game_id > OA_GAME.id
Ref: OA_PLAYER_TEAM.player_id > OA_PLAYER.id
Ref: OA_PLAYER_TEAM.team_id > OA_TEAM.id
Ref: OA_ELEMENT.game_id > OA_GAME.id
Ref: OA_ELEMENT_DATA.element_id - OA_ELEMENT.id
Ref: OA_EVENT_SOURCE_KEY.event_source_id - OA_EVENT_SOURCE.id
Ref: OA_GAME_STATUS.game_id > OA_GAME.id
Ref: OA_EVENT_SOURCE_GAME.event_source_id > OA_EVENT_SOURCE.id
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
INSERT INTO OA_PLAYER_TEAM
(game_id, team_id, player_id, created_at)
(team_id, player_id, created_at)
VALUES
(:gameId, :teamId, :playerId, :ts)
(:teamId, :playerId, :ts)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
SELECT
SELECT
ot.id,
ot.game_id AS gameId,
ot.name,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
DELETE FROM
OA_PLAYER_TEAM
WHERE
game_id = :gameId
AND
player_id = :playerId
AND
team_id = :teamId
Original file line number Diff line number Diff line change
Expand Up @@ -451,8 +451,8 @@ void addPlayerToTeam() {

// can't add same user in multiple teams of same game
Mockito.reset(cacheClearanceListener);
doPostError("/players/" + alice.getId() + "/teams",
PlayerGameAssociationRequest.builder().gameId(gameId1).teamId(renegades.getId()).userId(alice.getId()).build(),
doPostError("/players/" + alice.getId() + "/teams/" + renegades.getId(),
null,
HttpStatus.BAD_REQUEST,
ErrorCodes.PLAYER_ALREADY_IN_TEAM);
assertNeverCacheClearanceCalled();
Expand All @@ -466,52 +466,31 @@ void addPlayerToTeamFailures() {

// validations in request
{
doPostError("/players/" + alice.getId() + "/teams",
PlayerGameAssociationRequest.builder().gameId(null).teamId(renegades.getId()).userId(alice.getId()).build(),
HttpStatus.BAD_REQUEST, ErrorCodes.INVALID_PARAMETER);
doPostError("/players/" + alice.getId() + "/teams",
PlayerGameAssociationRequest.builder().gameId(-1).teamId(renegades.getId()).userId(alice.getId()).build(),
HttpStatus.BAD_REQUEST, ErrorCodes.INVALID_PARAMETER);
doPostError("/players/" + alice.getId() + "/teams",
PlayerGameAssociationRequest.builder().gameId(0).teamId(renegades.getId()).userId(alice.getId()).build(),
HttpStatus.BAD_REQUEST, ErrorCodes.INVALID_PARAMETER);

doPostError("/players/" + alice.getId() + "/teams",
PlayerGameAssociationRequest.builder().gameId(gameId).teamId(null).userId(alice.getId()).build(),
HttpStatus.BAD_REQUEST, ErrorCodes.INVALID_PARAMETER);
doPostError("/players/" + alice.getId() + "/teams",
PlayerGameAssociationRequest.builder().gameId(gameId).teamId(-1).userId(alice.getId()).build(),
HttpStatus.BAD_REQUEST, ErrorCodes.INVALID_PARAMETER);
doPostError("/players/" + alice.getId() + "/teams",
PlayerGameAssociationRequest.builder().gameId(gameId).teamId(0).userId(alice.getId()).build(),
HttpStatus.BAD_REQUEST, ErrorCodes.INVALID_PARAMETER);
doPostError("/players/" + alice.getId() + "/teams/-1",
null,
HttpStatus.NOT_FOUND, ErrorCodes.TEAM_NOT_EXISTS);
doPostError("/players/" + alice.getId() + "/teams/0",
null,
HttpStatus.NOT_FOUND, ErrorCodes.TEAM_NOT_EXISTS);
}

callAddPlayerToTeam(alice.getId(), gameId, renegades.getId());

// can't add non-existing user
Mockito.reset(cacheClearanceListener);
doPostError("/players/999999/teams",
PlayerGameAssociationRequest.builder().gameId(gameId).teamId(renegades.getId()).userId(999999L).build(),
doPostError("/players/999999/teams/" + renegades.getId(),
null,
HttpStatus.NOT_FOUND,
ErrorCodes.PLAYER_DOES_NOT_EXISTS);
assertNeverCacheClearanceCalled();

// can't add non-existing team
Mockito.reset(cacheClearanceListener);
doPostError("/players/" + alice.getId() + "/teams",
PlayerGameAssociationRequest.builder().gameId(gameId).teamId(999999).userId(alice.getId()).build(),
doPostError("/players/" + alice.getId() + "/teams/9999999",
null,
HttpStatus.NOT_FOUND,
ErrorCodes.TEAM_NOT_EXISTS);
assertNeverCacheClearanceCalled();

// can't add non-existing game
Mockito.reset(cacheClearanceListener);
doPostError("/players/" + alice.getId() + "/teams",
PlayerGameAssociationRequest.builder().gameId(999999).teamId(renegades.getId()).userId(alice.getId()).build(),
HttpStatus.NOT_FOUND,
ErrorCodes.GAME_NOT_EXISTS);
assertNeverCacheClearanceCalled();
}

@Test
Expand Down Expand Up @@ -672,8 +651,7 @@ private PlayerObject callPlayerDelete(long playerId) {
}

private void callAddPlayerToTeam(long playerId, int gameId, int teamId) {
doPostSuccess("/players/" + playerId + "/teams", PlayerGameAssociationRequest.builder()
.teamId(teamId).gameId(gameId).userId(playerId).build(), null);
doPostSuccess("/players/" + playerId + "/teams/" + teamId, null, null);
}

private void callAddPlayersToTeam(int teamId, List<Long> playerIds) {
Expand Down

0 comments on commit e258082

Please sign in to comment.