Skip to content

Commit

Permalink
Post data & return word stats
Browse files Browse the repository at this point in the history
  • Loading branch information
meghanmae committed May 14, 2024
1 parent 49c4e65 commit aff1881
Show file tree
Hide file tree
Showing 10 changed files with 361 additions and 54 deletions.
43 changes: 11 additions & 32 deletions Wordle.Api/Wordle.Api/Controllers/GameController.cs
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;
}
}
15 changes: 15 additions & 0 deletions Wordle.Api/Wordle.Api/Dtos/GameStatsDto.cs
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; }
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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");
}
}
}
16 changes: 9 additions & 7 deletions Wordle.Api/Wordle.Api/Migrations/WordleDbContextModelSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.Property<bool>("IsWin")
.HasColumnType("bit");

b.Property<int?>("WordId")
b.Property<int>("WordId")
.HasColumnType("int");

b.Property<int>("WordOfTheDayId")
b.Property<int?>("WordOfTheDayId")
.HasColumnType("int");

b.HasKey("GameId");
Expand Down Expand Up @@ -94,15 +94,17 @@ protected override void BuildModel(ModelBuilder modelBuilder)

modelBuilder.Entity("Wordle.Api.Models.Game", b =>
{
b.HasOne("Wordle.Api.Models.Word", null)
b.HasOne("Wordle.Api.Models.Word", "Word")
.WithMany("Games")
.HasForeignKey("WordId");
.HasForeignKey("WordId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();

b.HasOne("Wordle.Api.Models.WordOfTheDay", "WordOfTheDay")
.WithMany("Games")
.HasForeignKey("WordOfTheDayId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
.HasForeignKey("WordOfTheDayId");

b.Navigation("Word");

b.Navigation("WordOfTheDay");
});
Expand Down
1 change: 1 addition & 0 deletions Wordle.Api/Wordle.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
builder.Services.AddSwaggerGen();

builder.Services.AddScoped<WordOfTheDayService>();
builder.Services.AddScoped<GameService>();

var app = builder.Build();

Expand Down
Loading

0 comments on commit aff1881

Please sign in to comment.