Skip to content

Commit

Permalink
Merge pull request #60 from theImmortalCoders/issue-53
Browse files Browse the repository at this point in the history
feat: #53 empty values for gameRecord allowed
  • Loading branch information
marcinbator authored Nov 19, 2024
2 parents 94e1948 + 31674e2 commit 3d138bf
Show file tree
Hide file tree
Showing 14 changed files with 367 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public static GameRecordResponse Map(GameRecord gameRecord)
EndState = gameRecord.EndState,
OutputSpec = gameRecord.OutputSpec,
SizeMb = gameRecord.SizeMb,
User = UserMapper.Map(gameRecord.User)
User = UserMapper.Map(gameRecord.User),
IsEmptyRecord = gameRecord.IsEmptyRecord
};
}

Expand Down
5 changes: 3 additions & 2 deletions rag-2-backend/Infrastructure/Dao/GameRecordDao.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public virtual void PerformGameRecordTransaction(Game game, GameRecord gameRecor
try
{
dbContext.Database.ExecuteSqlRaw(
"SELECT InsertRecordedGame(@GameId, @Values, @UserId, @Players, @OutputSpec, @EndState, @Started, @Ended, @SizeMb)",
"SELECT InsertRecordedGame(@GameId, @Values, @UserId, @Players, @OutputSpec, @EndState, @Started, @Ended, @SizeMb, @IsEmptyRecord)",
new NpgsqlParameter("@GameId", game.Id),
new NpgsqlParameter("@Values", JsonSerializer.Serialize(gameRecord.Values)),
new NpgsqlParameter("@UserId", user.Id),
Expand All @@ -81,7 +81,8 @@ public virtual void PerformGameRecordTransaction(Game game, GameRecord gameRecor
new NpgsqlParameter("@EndState", gameRecord.EndState),
new NpgsqlParameter("@Started", gameRecord.Started),
new NpgsqlParameter("@Ended", gameRecord.Ended),
new NpgsqlParameter("@SizeMb", gameRecord.SizeMb)
new NpgsqlParameter("@SizeMb", gameRecord.SizeMb),
new NpgsqlParameter("@IsEmptyRecord", gameRecord.IsEmptyRecord)
);
transaction.Commit();
}
Expand Down
1 change: 1 addition & 0 deletions rag-2-backend/Infrastructure/Database/Entity/GameRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ public class GameRecord
[MaxLength(1000)] public string? OutputSpec { get; init; }
[MaxLength(500)] public string? EndState { get; init; }
public double SizeMb { get; init; }
public bool IsEmptyRecord { get; init; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ public void ChangeRole([Required] int userId, [Required] Role role)
administrationService.ChangeRole(userId, role);
}

/// <summary>Get details of any user by user ID, only yours if not admin or teacher (Auth)</summary>
/// <summary>Get details of any user by user ID (Admin, Teacher)</summary>
/// <response code="403">Cannot view details</response>
[HttpGet("{userId:int}/details")]
[Authorize]
[Authorize(Roles = "Admin,Teacher")]
public UserResponse GetUserDetails([Required] int userId)
{
return administrationService.GetUserDetails(AuthDao.GetPrincipalEmail(User), userId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ public class GameRecordRequest
public required string GameName { get; init; }
public required string OutputSpec { get; init; }
public required List<GameRecordValue> Values { get; init; }
public required List<Player> Players { get; init; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ public class GameRecordResponse
public string? OutputSpec { get; set; }
public object? EndState { get; set; }
public double SizeMb { get; init; }
public bool IsEmptyRecord { get; init; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public List<GameRecordResponse> GetRecordsByGame([Required] int gameId, [Require
/// <summary>Download JSON file from specific game, admin and teacher can download everyone's data (Auth)</summary>
/// <response code="404">User or game record not found</response>
/// <response code="403">Permission denied</response>
/// <response code="400">Record is empty</response>
[HttpGet("{recordedGameId:int}")]
[Authorize]
public FileContentResult DownloadRecordData([Required] int recordedGameId)
Expand Down
27 changes: 17 additions & 10 deletions rag-2-backend/Infrastructure/Module/GameRecord/GameRecordService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,18 @@ public byte[] DownloadRecordData(int recordedGameId, string email)
var recordedGame = gameRecordDao.GetRecordedGameById(recordedGameId);

if (user.Id != recordedGame.User.Id && user.Role.Equals(Role.Student))
throw new BadRequestException("Permission denied");
throw new ForbiddenException("Permission denied");
if (recordedGame.IsEmptyRecord)
throw new BadRequestException("Record is empty");

return Encoding.UTF8.GetBytes(JsonSerializer.Serialize(GameRecordMapper.JsonMap(recordedGame)));
}

public void AddGameRecord(GameRecordRequest recordRequest, string email)
{
if (recordRequest.Values.Count == 0 || recordRequest.Values[^1].State == null)
throw new BadRequestException("Value state cannot be empty");

var user = userDao.GetUserByEmailOrThrow(email);
CheckUserDataLimit(recordRequest, user);
if (recordRequest.Values.Count > 0)
CheckUserDataLimit(recordRequest, user);

var game = gameDao.GetGameByNameOrThrow(recordRequest.GameName);

Expand All @@ -59,10 +59,13 @@ public void AddGameRecord(GameRecordRequest recordRequest, string email)
Game = game,
Values = recordRequest.Values,
User = user,
Players = recordRequest.Values[0].Players,
Players = recordRequest.Players,
OutputSpec = recordRequest.OutputSpec,
EndState = recordRequest.Values[^1].State?.ToString(),
SizeMb = JsonSerializer.Serialize(recordRequest.Values).Length / (1024.0 * 1024.0)
EndState = recordRequest.Values.Count > 0 ? recordRequest.Values[^1].State?.ToString() : "{}",
SizeMb = recordRequest.Values.Count > 0
? JsonSerializer.Serialize(recordRequest.Values).Length / (1024.0 * 1024.0)
: 0,
IsEmptyRecord = recordRequest.Values.Count == 0
};

UpdateTimestamps(recordRequest, recordedGame);
Expand Down Expand Up @@ -120,8 +123,12 @@ private static void UpdateTimestamps(GameRecordRequest recordRequest, Database.E
{
try
{
var startTimestamp = recordRequest.Values[0].Timestamp;
var endTimestamp = recordRequest.Values[^1].Timestamp;
var startTimestamp = recordRequest.Values.Count > 0
? recordRequest.Values[0].Timestamp
: DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
var endTimestamp = recordRequest.Values.Count > 0
? recordRequest.Values[^1].Timestamp
: DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
if (startTimestamp is not null)
gameRecord.Started = DateTime.Parse(startTimestamp, null, DateTimeStyles.RoundtripKind);
if (endTimestamp is not null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ CREATE OR REPLACE FUNCTION InsertRecordedGame(
p_end_state TEXT,
p_started TIMESTAMP,
p_ended TIMESTAMP,
p_sizeMb DOUBLE PRECISION
p_sizeMb DOUBLE PRECISION,
p_isEmptyRecord BOOLEAN
)
RETURNS VOID AS
$$
BEGIN
--'Procedura składowana'
INSERT INTO ""game_record_table"" (""GameId"", ""Values"", ""UserId"", ""Players"", ""OutputSpec"", ""EndState"", ""Started"", ""Ended"", ""SizeMb"")
VALUES (p_game_id, p_values, p_user_id, p_players, p_output_spec, p_end_state, p_started, p_ended, p_sizeMb);
INSERT INTO ""game_record_table"" (""GameId"", ""Values"", ""UserId"", ""Players"", ""OutputSpec"", ""EndState"", ""Started"", ""Ended"", ""SizeMb"", ""IsEmptyRecord"")
VALUES (p_game_id, p_values, p_user_id, p_players, p_output_spec, p_end_state, p_started, p_ended, p_sizeMb, p_isEmptyRecord);
UPDATE ""user_table""
SET ""LastPlayed"" = p_ended
Expand Down
Loading

0 comments on commit 3d138bf

Please sign in to comment.