Skip to content

Commit

Permalink
EF
Browse files Browse the repository at this point in the history
  • Loading branch information
GrantErickson committed May 2, 2024
1 parent 94dda59 commit 3c13713
Show file tree
Hide file tree
Showing 10 changed files with 5,962 additions and 5,761 deletions.
9 changes: 9 additions & 0 deletions Wordle.Api/Wordle.Api.Tests/WordOfTheDayServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,13 @@ public void GetWordOfTheDay_ReturnsString()
{
CollectionAssert.Contains(WordOfTheDayService.LoadWordList(), "yules");
}

[TestMethod]
public void GetWordOfTheDay_SameWord()
{
WordOfTheDayService service = new();
var word = service.GetRandomWord();
CollectionAssert.Equals(word, service.GetRandomWord());
}

}

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

36 changes: 36 additions & 0 deletions Wordle.Api/Wordle.Api/Migrations/20240430231124_WordOfTheDay.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Wordle.Api.Migrations
{
/// <inheritdoc />
public partial class WordOfTheDay : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "WordOfTheDay",
columns: table => new
{
WordOfTheDayId = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Word = table.Column<string>(type: "nvarchar(max)", nullable: false),
Date = table.Column<DateOnly>(type: "date", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_WordOfTheDay", x => x.WordOfTheDayId);
});
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "WordOfTheDay");
}
}
}
47 changes: 47 additions & 0 deletions Wordle.Api/Wordle.Api/Migrations/WordleDbContextModelSnapshot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Wordle.Api.Models;

#nullable disable

namespace Wordle.Api.Migrations
{
[DbContext(typeof(WordleDbContext))]
partial class WordleDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "8.0.4")
.HasAnnotation("Relational:MaxIdentifierLength", 128);

SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);

modelBuilder.Entity("Wordle.Api.Models.WordOfTheDay", b =>
{
b.Property<int>("WordOfTheDayId")
.ValueGeneratedOnAdd()
.HasColumnType("int");

SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("WordOfTheDayId"));

b.Property<DateOnly>("Date")
.HasColumnType("date");

b.Property<string>("Word")
.IsRequired()
.HasColumnType("nvarchar(max)");

b.HasKey("WordOfTheDayId");

b.ToTable("WordOfTheDay");
});
#pragma warning restore 612, 618
}
}
}
14 changes: 14 additions & 0 deletions Wordle.Api/Wordle.Api/Models/WordOfTheDay.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace Wordle.Api.Models
{
[Table("WordOfTheDay")]
public class WordOfTheDay
{
public int WordOfTheDayId { get; set; }
public string Word { get; set; } = null!;
public DateOnly Date { get; set; }

}
}
15 changes: 15 additions & 0 deletions Wordle.Api/Wordle.Api/Models/WordleDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Microsoft.EntityFrameworkCore;

namespace Wordle.Api.Models
{

public class WordleDbContext:DbContext
{
public DbSet<WordOfTheDay> WordsOfTheDays { get; set; }

public WordleDbContext(DbContextOptions<WordleDbContext> options)
: base(options)
{
}
}
}
15 changes: 15 additions & 0 deletions Wordle.Api/Wordle.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using System;
using Wordle.Api.Models;
using Wordle.Api.Services;

var AllOrigins = "AllOrigins";

var builder = WebApplication.CreateBuilder(args);

var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
builder.Services.AddDbContext<WordleDbContext>(options =>
options.UseSqlServer(connectionString));
//builder.Services.AddDatabaseDeveloperPageExceptionFilter();

// Add services to the container.
builder.Services.AddCors(options =>
{
Expand All @@ -24,6 +33,12 @@

var app = builder.Build();

using (var scope = app.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<WordleDbContext>();
db.Database.Migrate();
}

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
Expand Down
Loading

0 comments on commit 3c13713

Please sign in to comment.