Skip to content

Commit 33fdea1

Browse files
authored
Merge pull request #133 from LeadOn/features/fifa/tournament
Added way to declare score on unplayed FIFA games
2 parents 05cac76 + 89c9e1d commit 33fdea1

File tree

8 files changed

+295
-2
lines changed

8 files changed

+295
-2
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// <copyright file="DeclareFifaGamePlayedScoreCommand.cs" company="LeadOn's Corp'">
2+
// Copyright (c) LeadOn's Corp'. All rights reserved.
3+
// </copyright>
4+
5+
namespace GameOn.Application.FIFA.FifaGamePlayed.Commands.DeclareFifaGamePlayedScore
6+
{
7+
using GameOn.Common.DTOs;
8+
using GameOn.Domain;
9+
using MediatR;
10+
11+
/// <summary>
12+
/// DeclareFifaGamePlayedScoreCommand class.
13+
/// </summary>
14+
public class DeclareFifaGamePlayedScoreCommand : IRequest<FifaGamePlayed?>
15+
{
16+
/// <summary>
17+
/// Gets or sets Declare Score DTO.
18+
/// </summary>
19+
public DeclareScoreDto ScoreDto { get; set; } = new DeclareScoreDto();
20+
}
21+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// <copyright file="DeclareFifaGamePlayedScoreCommandHandler.cs" company="LeadOn's Corp'">
2+
// Copyright (c) LeadOn's Corp'. All rights reserved.
3+
// </copyright>
4+
5+
namespace GameOn.Application.FIFA.FifaGamePlayed.Commands.DeclareFifaGamePlayedScore
6+
{
7+
using GameOn.Common.Interfaces;
8+
using GameOn.Domain;
9+
using MediatR;
10+
using Microsoft.EntityFrameworkCore;
11+
12+
/// <summary>
13+
/// DeclareFifaGamePlayedScoreCommandHandler class.
14+
/// </summary>
15+
public class DeclareFifaGamePlayedScoreCommandHandler : IRequestHandler<DeclareFifaGamePlayedScoreCommand, FifaGamePlayed?>
16+
{
17+
private readonly IApplicationDbContext context;
18+
19+
/// <summary>
20+
/// Initializes a new instance of the <see cref="DeclareFifaGamePlayedScoreCommandHandler"/> class.
21+
/// </summary>
22+
/// <param name="context">DbContext, injected.</param>
23+
public DeclareFifaGamePlayedScoreCommandHandler(IApplicationDbContext context)
24+
{
25+
this.context = context;
26+
}
27+
28+
/// <inheritdoc />
29+
public async Task<FifaGamePlayed?> Handle(DeclareFifaGamePlayedScoreCommand request, CancellationToken cancellationToken)
30+
{
31+
// First, getting game from database
32+
var gameInDb = await this.context.FifaGamesPlayed
33+
.FirstOrDefaultAsync(
34+
x =>
35+
x.Id == request.ScoreDto.GameId
36+
&& x.IsPlayed == false
37+
&& x.TeamPlayers.FirstOrDefault(
38+
y => y.PlayerId == request.ScoreDto.CurrentPlayerId) != null,
39+
cancellationToken);
40+
41+
if (gameInDb is null)
42+
{
43+
return null;
44+
}
45+
46+
// Updating game
47+
gameInDb.IsPlayed = true;
48+
gameInDb.TeamScore1 = request.ScoreDto.ScoreTeam1;
49+
gameInDb.TeamScore2 = request.ScoreDto.ScoreTeam2;
50+
gameInDb.PlayedOn = DateTime.Now;
51+
52+
this.context.FifaGamesPlayed.Update(gameInDb);
53+
await this.context.SaveChangesAsync(cancellationToken);
54+
return gameInDb;
55+
}
56+
}
57+
}

GameOn.Application/FIFA/FifaGamePlayed/Queries/GetLastFifaGamesPlayed/GetLastFifaGamesPlayedQueryHandler.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public async Task<List<FifaGamePlayedDto>> Handle(GetLastFifaGamesPlayedQuery re
4040
.Include(x => x.Platform)
4141
.Include(x => x.Highlights)
4242
.OrderByDescending(x => x.PlayedOn)
43+
.Where(x => x.IsPlayed == true)
4344
.Take(request.Limit)
4445
.ToListAsync(cancellationToken);
4546

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// <copyright file="DeclareScoreDto.cs" company="LeadOn's Corp'">
2+
// Copyright (c) LeadOn's Corp'. All rights reserved.
3+
// </copyright>
4+
5+
namespace GameOn.Common.DTOs
6+
{
7+
/// <summary>
8+
/// DeclareScoreDto class.
9+
/// </summary>
10+
public class DeclareScoreDto
11+
{
12+
/// <summary>
13+
/// Gets or sets Game Id.
14+
/// </summary>
15+
public int GameId { get; set; }
16+
17+
/// <summary>
18+
/// Gets or sets Score Team 1.
19+
/// </summary>
20+
public int ScoreTeam1 { get; set; }
21+
22+
/// <summary>
23+
/// Gets or sets Score Team 2.
24+
/// </summary>
25+
public int ScoreTeam2 { get; set; }
26+
27+
/// <summary>
28+
/// Gets or sets Current Player ID.
29+
/// </summary>
30+
public int CurrentPlayerId { get; set; }
31+
}
32+
}

GameOn.Documentation/GameOn! API.postman_collection.json

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"info": {
33
"_postman_id": "1b7d2b8f-7c79-47c3-aa79-d5f86660b91e",
4-
"name": "GameOn! 5.0.0",
4+
"name": "GameOn! 5.0.1",
55
"description": "This API goal is to monitor players performance across real-world and virtual games.",
66
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
77
"_exporter_id": "6882080"
@@ -622,6 +622,36 @@
622622
}
623623
},
624624
"response": []
625+
},
626+
{
627+
"name": "Declare score",
628+
"request": {
629+
"method": "POST",
630+
"header": [],
631+
"body": {
632+
"mode": "raw",
633+
"raw": "{\r\n \"Id\": 89,\r\n \"PlatformId\": 2,\r\n \"TeamCode1\": \"FCB\",\r\n \"FifaTeam1\": 21,\r\n \"TeamScore1\": 2,\r\n \"TeamCode2\": \"MCI\",\r\n \"FifaTeam2\": 10,\r\n \"TeamScore2\": 1,\r\n \"IsPlayed\": false\r\n}",
634+
"options": {
635+
"raw": {
636+
"language": "json"
637+
}
638+
}
639+
},
640+
"url": {
641+
"raw": "{{baseUrl}}/fifa/fifagame/1/2/3",
642+
"host": [
643+
"{{baseUrl}}"
644+
],
645+
"path": [
646+
"fifa",
647+
"fifagame",
648+
"1",
649+
"2",
650+
"3"
651+
]
652+
}
653+
},
654+
"response": []
625655
}
626656
]
627657
},

GameOn.Documentation/GameOn!.swagger.json

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,62 @@
514514
}
515515
}
516516
},
517+
"/fifa/FifaGame/{gameId}/{score1}/{score2}": {
518+
"post": {
519+
"tags": ["FifaGame"],
520+
"summary": "Declares score of a game in database.",
521+
"parameters": [
522+
{
523+
"name": "gameId",
524+
"in": "path",
525+
"required": true,
526+
"schema": {
527+
"type": "integer",
528+
"format": "int32"
529+
}
530+
},
531+
{
532+
"name": "score1",
533+
"in": "path",
534+
"required": true,
535+
"schema": {
536+
"type": "integer",
537+
"format": "int32"
538+
}
539+
},
540+
{
541+
"name": "score2",
542+
"in": "path",
543+
"required": true,
544+
"schema": {
545+
"type": "integer",
546+
"format": "int32"
547+
}
548+
}
549+
],
550+
"responses": {
551+
"200": {
552+
"description": "Updated game.",
553+
"content": {
554+
"application/json": {
555+
"schema": {
556+
"$ref": "#/components/schemas/FifaGamePlayed"
557+
}
558+
}
559+
}
560+
},
561+
"401": {
562+
"description": "User is not logged in."
563+
},
564+
"403": {
565+
"description": "User isn't authorized."
566+
},
567+
"500": {
568+
"description": "Unknown error."
569+
}
570+
}
571+
}
572+
},
517573
"/fifa/FifaTeam": {
518574
"get": {
519575
"tags": ["FifaTeam"],
@@ -2842,6 +2898,54 @@
28422898
"participantPUUID": {
28432899
"type": "string",
28442900
"nullable": true
2901+
},
2902+
"magicDamageDone": {
2903+
"type": "integer",
2904+
"format": "int32"
2905+
},
2906+
"magicDamageDoneToChampions": {
2907+
"type": "integer",
2908+
"format": "int32"
2909+
},
2910+
"magicDamageTaken": {
2911+
"type": "integer",
2912+
"format": "int32"
2913+
},
2914+
"physicalDamageDone": {
2915+
"type": "integer",
2916+
"format": "int32"
2917+
},
2918+
"physicalDamageDoneToChampions": {
2919+
"type": "integer",
2920+
"format": "int32"
2921+
},
2922+
"physicalDamageTaken": {
2923+
"type": "integer",
2924+
"format": "int32"
2925+
},
2926+
"totalDamageDone": {
2927+
"type": "integer",
2928+
"format": "int32"
2929+
},
2930+
"totalDamageDoneToChampions": {
2931+
"type": "integer",
2932+
"format": "int32"
2933+
},
2934+
"totalDamageTaken": {
2935+
"type": "integer",
2936+
"format": "int32"
2937+
},
2938+
"trueDamageDone": {
2939+
"type": "integer",
2940+
"format": "int32"
2941+
},
2942+
"trueDamageDoneToChampions": {
2943+
"type": "integer",
2944+
"format": "int32"
2945+
},
2946+
"trueDamageTaken": {
2947+
"type": "integer",
2948+
"format": "int32"
28452949
}
28462950
},
28472951
"additionalProperties": false

GameOn.Presentation/Controllers/FIFA/FifaGameController.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
namespace GameOn.Presentation.Controllers.FIFA
66
{
7+
using GameOn.Application.Common.Players.Queries.GetPlayerByKeycloakId;
78
using GameOn.Application.FIFA.FifaGamePlayed.Commands.CreateFifaGamePlayed;
9+
using GameOn.Application.FIFA.FifaGamePlayed.Commands.DeclareFifaGamePlayedScore;
810
using GameOn.Application.FIFA.FifaGamePlayed.Commands.DeleteFifaGamePlayed;
911
using GameOn.Application.FIFA.FifaGamePlayed.Commands.UpdateFifaGamePlayed;
1012
using GameOn.Application.FIFA.FifaGamePlayed.Queries.GetFifaGamePlayedById;
@@ -229,5 +231,51 @@ public async Task<IActionResult> Update([FromBody] UpdateGameDto game)
229231
return this.Ok(updatedGame);
230232
}
231233
}
234+
235+
/// <summary>
236+
/// Declare score in database.
237+
/// </summary>
238+
/// <param name="gameId">Game ID.</param>
239+
/// <param name="score1">Score 1.</param>
240+
/// <param name="score2">Score 2.</param>
241+
/// <returns>IActionResult object.</returns>
242+
[HttpPost]
243+
[Authorize]
244+
[Route("{gameId:int}/{score1:int}/{score2:int}")]
245+
[Produces("application/json")]
246+
[SwaggerOperation(Summary = "Declares score of a game in database.")]
247+
[SwaggerResponse(200, "Updated game.", typeof(FifaGamePlayed))]
248+
[SwaggerResponse(401, "User is not logged in.")]
249+
[SwaggerResponse(403, "User isn't authorized.")]
250+
[SwaggerResponse(500, "Unknown error.")]
251+
public async Task<IActionResult> DeclareScore(int gameId, int score1, int score2)
252+
{
253+
var currentUserId = this.User.GetConnectedPlayer().KeycloakId;
254+
var currentUser = await this.mediator.Send(new GetPlayerByKeycloakIdQuery { KeycloakId = currentUserId });
255+
256+
if (currentUser is null)
257+
{
258+
return this.Problem();
259+
}
260+
261+
var updatedGame = await this.mediator.Send(new DeclareFifaGamePlayedScoreCommand {
262+
ScoreDto = new DeclareScoreDto
263+
{
264+
GameId = gameId,
265+
ScoreTeam1 = score1,
266+
ScoreTeam2 = score2,
267+
CurrentPlayerId = currentUser.Id,
268+
},
269+
});
270+
271+
if (updatedGame is null)
272+
{
273+
return this.Problem();
274+
}
275+
else
276+
{
277+
return this.Ok(updatedGame);
278+
}
279+
}
232280
}
233281
}

GameOn.Presentation/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
c.EnableAnnotations();
2424
c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo
2525
{
26-
Version = "5.0.0",
26+
Version = "5.0.1",
2727
Title = "LeadOn's Corp - GameOn! API",
2828
Description = "This API goal is to monitor players performance across multiple games.",
2929
Contact = new Microsoft.OpenApi.Models.OpenApiContact

0 commit comments

Comments
 (0)