-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
aac09dd
commit ebf6ae0
Showing
9 changed files
with
194 additions
and
227 deletions.
There are no files selected for viewing
226 changes: 0 additions & 226 deletions
226
QuizApp.Database/Migrations/QuizContextModelSnapshot.cs
This file was deleted.
Oops, something went wrong.
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,6 +1,9 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace QuizApp.Database.Models; | ||
|
||
public class BaseModel | ||
{ | ||
[Key] | ||
public int Id { get; set; } | ||
} |
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
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); | ||
} |
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,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; | ||
} | ||
} |
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,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 | ||
} |
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,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> |
Oops, something went wrong.