Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create quiz repo #12

Merged
merged 3 commits into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/pullrequest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ jobs:
- name: dotnet build
run: dotnet build --configuration Release

- name: dotnet test
run: dotnet test --verbosity normal

build_react:
runs-on: ubuntu-latest
steps:
Expand Down
226 changes: 0 additions & 226 deletions QuizApp.Database/Migrations/QuizContextModelSnapshot.cs

This file was deleted.

3 changes: 3 additions & 0 deletions QuizApp.Database/Models/BaseModel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System.ComponentModel.DataAnnotations;

namespace QuizApp.Database.Models;

public class BaseModel
{
[Key]
public int Id { get; set; }
}
1 change: 0 additions & 1 deletion QuizApp.Database/QuizApp.Database.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

<ItemGroup>
<Folder Include="Migrations\" />
<Folder Include="Repositories\" />
</ItemGroup>

<ItemGroup>
Expand Down
7 changes: 7 additions & 0 deletions QuizApp.Database/Repositories/IQuizRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace QuizApp.Database.Repositories;

public interface IQuizRepository
{
Task<int> CreateQuiz(string name);
Task<int> CreateQuestion(int quizId, string question);
}
47 changes: 47 additions & 0 deletions QuizApp.Database/Repositories/QuizRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using QuizApp.Database.Models;

namespace QuizApp.Database.Repositories;

public class QuizRepository : IQuizRepository
{
private readonly QuizContext db;

public QuizRepository(QuizContext db)
{
this.db = db;
}

public async Task<int> CreateQuiz(string name)
{
var model = db.Add(new Quiz
{
QuizName = name,
});

await db.SaveChangesAsync();

return model.Entity.Id;
}

public async Task<int> CreateQuestion(int quizId, string question)
{
var quizModel = db.Quizzes.SingleOrDefault(x => x.Id == quizId);
if (quizModel is null)
{
throw new InvalidOperationException($"Cannot found quiz id = {quizId}");
}

var questionModel = new Question
{
QuizId = quizModel.Id,
Quiz = quizModel,
Title = question,
};

var result = db.Questions.Add(questionModel);

await db.SaveChangesAsync();

return result.Entity.Id;
}
}
52 changes: 52 additions & 0 deletions QuizApp.Test/BaseRepoTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using QuizApp.Database;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;

namespace QuizApp.Test;

public class BaseRepoTest : IDisposable
{
internal SqliteConnection Connection { get; }
internal DbContextOptions<QuizContext> Options { get; }

public BaseRepoTest()
{
// Create and open a shared SqliteConnection
Connection = new SqliteConnection("DataSource=:memory:");
Connection.Open();

// Configure DbContextOptions to use this connection
Options = new DbContextOptionsBuilder<QuizContext>()
.UseSqlite(Connection)
.Options;

// Ensure the database schema is created
using var context = new QuizContext(Options);
context.Database.EnsureCreated();
}

public void Dispose()
{
// Clean up the connection after all tests are done
Connection.Close();
}

#region IDisposable Support
private bool disposedValue = false;

protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
Connection.Close();
Connection.Dispose();
}

disposedValue = true;
}
}

#endregion
}
30 changes: 30 additions & 0 deletions QuizApp.Test/QuizApp.Test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2"/>
<PackageReference Include="FluentAssertions" Version="7.0.0-alpha.6" />
<PackageReference Include="FluentValidation" Version="11.11.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="9.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1"/>
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="NUnit" Version="4.2.2" />
<PackageReference Include="xunit" Version="2.9.2" />
</ItemGroup>

<ItemGroup>
<Using Include="NUnit.Framework"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\QuizApp.Database\QuizApp.Database.csproj" />
</ItemGroup>

</Project>
Loading
Loading