-
Notifications
You must be signed in to change notification settings - Fork 20
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
10 changed files
with
361 additions
and
54 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,27 @@ | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Wordle.Api.Dtos; | ||
using Wordle.Api.Models; | ||
using Wordle.Api.Services; | ||
|
||
namespace Wordle.Api.Controllers; | ||
|
||
[Route("api/[controller]")] | ||
[Route("[controller]")] | ||
[ApiController] | ||
public class GameController : ControllerBase | ||
{ | ||
public WordleDbContext Db { get; set; } | ||
public GameService GameService { get; set; } | ||
|
||
public GameController(WordleDbContext db) | ||
public GameController(GameService gameService) | ||
{ | ||
Db = db; | ||
GameService = gameService; | ||
} | ||
|
||
[HttpPost("GameWordOfTheDay")] | ||
public async Task<bool> PostGame(GameDto gameDto) | ||
[HttpPost("Result")] | ||
public async Task<GameStatsDto> PostGame(GameDto gameDto) | ||
{ | ||
// Get todays date | ||
var today = DateOnly.FromDateTime(DateTime.UtcNow); | ||
Game game = await GameService.PostGameResult(gameDto); | ||
var stats = await GameService.GetGameStats(game); | ||
|
||
// Get all the words that match our game word and load their WOTDs | ||
var word = Db.Words | ||
.Include(word => word.WordsOfTheDays) | ||
.Where(word => word.Text == gameDto.Word) | ||
.First(); | ||
|
||
// Create a new game object to save to the DB | ||
Game game = new() | ||
{ | ||
Attempts = gameDto.Attempts, | ||
IsWin = gameDto.IsWin, | ||
// Attempt to find the WOTD that best matches todays date | ||
WordOfTheDay = word.WordsOfTheDays | ||
.OrderByDescending(wotd => wotd.Date) | ||
.FirstOrDefault(wotd => wotd.Date < today.AddDays(-1)), | ||
Word = word | ||
}; | ||
|
||
Db.Games.Add(game); | ||
await Db.SaveChangesAsync(); | ||
return true; | ||
return stats; | ||
} | ||
} |
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,15 @@ | ||
namespace Wordle.Api.Dtos | ||
{ | ||
public class GameStatsDto | ||
{ | ||
public required string Word { get; set; } | ||
|
||
public double AverageGuesses { get; set; } | ||
|
||
public int TotalTimesPlayed { get; set; } | ||
|
||
public int TotalWins { get; set; } | ||
|
||
public int TotalLosses { get => TotalTimesPlayed - TotalWins; } | ||
} | ||
} |
140 changes: 140 additions & 0 deletions
140
Wordle.Api/Wordle.Api/Migrations/20240514214408_NullableWordOfTheDay.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
100 changes: 100 additions & 0 deletions
100
Wordle.Api/Wordle.Api/Migrations/20240514214408_NullableWordOfTheDay.cs
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,100 @@ | ||
using Microsoft.EntityFrameworkCore.Migrations; | ||
|
||
#nullable disable | ||
|
||
namespace Wordle.Api.Migrations | ||
{ | ||
/// <inheritdoc /> | ||
public partial class NullableWordOfTheDay : Migration | ||
{ | ||
/// <inheritdoc /> | ||
protected override void Up(MigrationBuilder migrationBuilder) | ||
{ | ||
migrationBuilder.DropForeignKey( | ||
name: "FK_Games_WordOfTheDay_WordOfTheDayId", | ||
table: "Games"); | ||
|
||
migrationBuilder.DropForeignKey( | ||
name: "FK_Games_Words_WordId", | ||
table: "Games"); | ||
|
||
migrationBuilder.AlterColumn<int>( | ||
name: "WordOfTheDayId", | ||
table: "Games", | ||
type: "int", | ||
nullable: true, | ||
oldClrType: typeof(int), | ||
oldType: "int"); | ||
|
||
migrationBuilder.AlterColumn<int>( | ||
name: "WordId", | ||
table: "Games", | ||
type: "int", | ||
nullable: false, | ||
defaultValue: 0, | ||
oldClrType: typeof(int), | ||
oldType: "int", | ||
oldNullable: true); | ||
|
||
migrationBuilder.AddForeignKey( | ||
name: "FK_Games_WordOfTheDay_WordOfTheDayId", | ||
table: "Games", | ||
column: "WordOfTheDayId", | ||
principalTable: "WordOfTheDay", | ||
principalColumn: "WordOfTheDayId"); | ||
|
||
migrationBuilder.AddForeignKey( | ||
name: "FK_Games_Words_WordId", | ||
table: "Games", | ||
column: "WordId", | ||
principalTable: "Words", | ||
principalColumn: "WordId", | ||
onDelete: ReferentialAction.Cascade); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override void Down(MigrationBuilder migrationBuilder) | ||
{ | ||
migrationBuilder.DropForeignKey( | ||
name: "FK_Games_WordOfTheDay_WordOfTheDayId", | ||
table: "Games"); | ||
|
||
migrationBuilder.DropForeignKey( | ||
name: "FK_Games_Words_WordId", | ||
table: "Games"); | ||
|
||
migrationBuilder.AlterColumn<int>( | ||
name: "WordOfTheDayId", | ||
table: "Games", | ||
type: "int", | ||
nullable: false, | ||
defaultValue: 0, | ||
oldClrType: typeof(int), | ||
oldType: "int", | ||
oldNullable: true); | ||
|
||
migrationBuilder.AlterColumn<int>( | ||
name: "WordId", | ||
table: "Games", | ||
type: "int", | ||
nullable: true, | ||
oldClrType: typeof(int), | ||
oldType: "int"); | ||
|
||
migrationBuilder.AddForeignKey( | ||
name: "FK_Games_WordOfTheDay_WordOfTheDayId", | ||
table: "Games", | ||
column: "WordOfTheDayId", | ||
principalTable: "WordOfTheDay", | ||
principalColumn: "WordOfTheDayId", | ||
onDelete: ReferentialAction.Cascade); | ||
|
||
migrationBuilder.AddForeignKey( | ||
name: "FK_Games_Words_WordId", | ||
table: "Games", | ||
column: "WordId", | ||
principalTable: "Words", | ||
principalColumn: "WordId"); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.