From dc1938dbb98dc9edf16558e505b2961246c83e6e Mon Sep 17 00:00:00 2001 From: Luca Matei Pintilie Date: Wed, 30 Oct 2024 21:59:53 +0100 Subject: Add API and Test --- Api/Api.csproj | 15 ++++++++++++++ Api/Contract/NewContent.cs | 7 +++++++ Api/Contract/WikiPage.cs | 7 +++++++ Api/Contract/WikiProperty.cs | 8 ++++++++ Api/Controllers/ContentApi.cs | 37 +++++++++++++++++++++++++++++++++ Api/Models/Content.cs | 7 +++++++ Api/Models/DbWikiWakaContext.cs | 11 ++++++++++ Api/Models/Language.cs | 7 +++++++ Api/Models/Property.cs | 11 ++++++++++ Api/Models/Version.cs | 6 ++++++ Api/Program.cs | 38 ++++++++++++++++++++++++++++++++++ Api/Properties/launchSettings.json | 41 +++++++++++++++++++++++++++++++++++++ Api/Utils/DbInit.cs | 25 ++++++++++++++++++++++ Api/appsettings.Development.json | 8 ++++++++ Api/appsettings.json | 12 +++++++++++ Api/databse.dat | Bin 0 -> 4096 bytes Api/databse.dat-shm | Bin 0 -> 32768 bytes Api/databse.dat-wal | Bin 0 -> 45352 bytes 18 files changed, 240 insertions(+) create mode 100644 Api/Api.csproj create mode 100644 Api/Contract/NewContent.cs create mode 100644 Api/Contract/WikiPage.cs create mode 100644 Api/Contract/WikiProperty.cs create mode 100644 Api/Controllers/ContentApi.cs create mode 100644 Api/Models/Content.cs create mode 100644 Api/Models/DbWikiWakaContext.cs create mode 100644 Api/Models/Language.cs create mode 100644 Api/Models/Property.cs create mode 100644 Api/Models/Version.cs create mode 100644 Api/Program.cs create mode 100644 Api/Properties/launchSettings.json create mode 100644 Api/Utils/DbInit.cs create mode 100644 Api/appsettings.Development.json create mode 100644 Api/appsettings.json create mode 100644 Api/databse.dat create mode 100644 Api/databse.dat-shm create mode 100644 Api/databse.dat-wal (limited to 'Api') diff --git a/Api/Api.csproj b/Api/Api.csproj new file mode 100644 index 0000000..ab1cb00 --- /dev/null +++ b/Api/Api.csproj @@ -0,0 +1,15 @@ + + + + net8.0 + enable + enable + + + + + + + + + diff --git a/Api/Contract/NewContent.cs b/Api/Contract/NewContent.cs new file mode 100644 index 0000000..67a0019 --- /dev/null +++ b/Api/Contract/NewContent.cs @@ -0,0 +1,7 @@ +namespace WikiWaka.Api.Contract; + +public class NewContent +{ + public required string Text { get; set; } + public required string LanguageCode { get; set; } +} diff --git a/Api/Contract/WikiPage.cs b/Api/Contract/WikiPage.cs new file mode 100644 index 0000000..0ea9d0a --- /dev/null +++ b/Api/Contract/WikiPage.cs @@ -0,0 +1,7 @@ +namespace WikiWaka.Api.Contract; + +public class WikiPage +{ + public required int Id { get; init; } + public required WikiProperty Property { get; init; } +} diff --git a/Api/Contract/WikiProperty.cs b/Api/Contract/WikiProperty.cs new file mode 100644 index 0000000..7f00812 --- /dev/null +++ b/Api/Contract/WikiProperty.cs @@ -0,0 +1,8 @@ +namespace WikiWaka.Api.Contract; + +public class WikiProperty +{ + public required int Id { get; init; } + public required string Text { get; set; } + public required string LanguageCode { get; set; } +} diff --git a/Api/Controllers/ContentApi.cs b/Api/Controllers/ContentApi.cs new file mode 100644 index 0000000..757fd78 --- /dev/null +++ b/Api/Controllers/ContentApi.cs @@ -0,0 +1,37 @@ +using Microsoft.AspNetCore.Mvc; +using WikiWaka.Api.Contract; +using WikiWaka.Api.Models; + +namespace WikiWaka.Api.Controllers; + +[ApiController] +[Route("[controller]")] +public class ContentApiController(DbWikiWakaContext dbContext) : Controller +{ + [HttpGet(nameof(Content))] + public IAsyncEnumerable GetContents() + { + return dbContext.Content.AsAsyncEnumerable(); + } + + [HttpPost(nameof(Content))] + public async Task PostContents(NewContent newContent) + { + Language language = dbContext.Language.Single(l => l.LanguageCode == newContent.LanguageCode); + Content content = new(); + Models.Version version = new(); + Property property = new() + { + Content = content, + Language = language, + Version = version, + Text = newContent.Text, + }; + await dbContext.Content.AddAsync(content); + await dbContext.Version.AddAsync(version); + await dbContext.Property.AddAsync(property); + await dbContext.SaveChangesAsync(); + + return content; + } +} diff --git a/Api/Models/Content.cs b/Api/Models/Content.cs new file mode 100644 index 0000000..d59c8e1 --- /dev/null +++ b/Api/Models/Content.cs @@ -0,0 +1,7 @@ +namespace WikiWaka.Api.Models; + +public class Content +{ + public int Id { get; set; } + public ICollection Properties { get; set; } +} diff --git a/Api/Models/DbWikiWakaContext.cs b/Api/Models/DbWikiWakaContext.cs new file mode 100644 index 0000000..cf5632a --- /dev/null +++ b/Api/Models/DbWikiWakaContext.cs @@ -0,0 +1,11 @@ +using Microsoft.EntityFrameworkCore; + +namespace WikiWaka.Api.Models; + +public class DbWikiWakaContext(DbContextOptions options) : DbContext(options) +{ + public DbSet Content { get; set; } + public DbSet Version { get; set; } + public DbSet Property { get; set; } + public DbSet Language { get; set; } +} diff --git a/Api/Models/Language.cs b/Api/Models/Language.cs new file mode 100644 index 0000000..a8fd30e --- /dev/null +++ b/Api/Models/Language.cs @@ -0,0 +1,7 @@ +namespace WikiWaka.Api.Models; + +public class Language +{ + public int Id { get; set; } + public required string LanguageCode { get; set; } +} diff --git a/Api/Models/Property.cs b/Api/Models/Property.cs new file mode 100644 index 0000000..b20adac --- /dev/null +++ b/Api/Models/Property.cs @@ -0,0 +1,11 @@ +namespace WikiWaka.Api.Models; + +public class Property +{ + public int Id { get; set; } + public required string Text { get; set; } + + public required Content Content { get; set; } + public required Language Language { get; set; } + public required Version Version { get; set; } +} diff --git a/Api/Models/Version.cs b/Api/Models/Version.cs new file mode 100644 index 0000000..e4971e9 --- /dev/null +++ b/Api/Models/Version.cs @@ -0,0 +1,6 @@ +namespace WikiWaka.Api.Models; + +public class Version +{ + public int Id { get; set; } +} diff --git a/Api/Program.cs b/Api/Program.cs new file mode 100644 index 0000000..0fefe46 --- /dev/null +++ b/Api/Program.cs @@ -0,0 +1,38 @@ +using Microsoft.EntityFrameworkCore; +using WikiWaka.Api.Models; + +var builder = WebApplication.CreateBuilder(args); + +builder.Services.AddControllers(); +builder.Services.AddHealthChecks(); +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(); +builder.Services.AddDbContext(options => + options.UseSqlite(builder.Configuration.GetConnectionString("DbWikiWaka"))); + +var app = builder.Build(); + +if (app.Environment.IsDevelopment()) +{ + app.UseSwagger(); + app.UseSwaggerUI(); +} + +app.MapControllers(); +app.MapHealthChecks("/healthz"); + +using (var scope = app.Services.CreateScope()) +{ + await using DbWikiWakaContext db = scope.ServiceProvider.GetRequiredService(); + ILogger logger = scope.ServiceProvider.GetRequiredService>(); + bool isNewDatabase = await db.Database.EnsureCreatedAsync(); + if (isNewDatabase) + { + logger.LogInformation("Creating new database..."); + await DbInit.InitializeLanguages(db); + } + else + logger.LogInformation("Database existed before, not changing"); +} + +app.Run(); diff --git a/Api/Properties/launchSettings.json b/Api/Properties/launchSettings.json new file mode 100644 index 0000000..63b924d --- /dev/null +++ b/Api/Properties/launchSettings.json @@ -0,0 +1,41 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:16311", + "sslPort": 44364 + } + }, + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "http://localhost:5161", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "https://localhost:7140;http://localhost:5161", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/Api/Utils/DbInit.cs b/Api/Utils/DbInit.cs new file mode 100644 index 0000000..0fc2d86 --- /dev/null +++ b/Api/Utils/DbInit.cs @@ -0,0 +1,25 @@ +using WikiWaka.Api.Models; + +public static class DbInit +{ + private static readonly Language[] languages = [ + new() { + Id = 1, + LanguageCode = "nb-NO", + }, + new() { + Id = 2, + LanguageCode = "nn-NO", + }, + new() { + Id = 3, + LanguageCode = "en-US", + }, + ]; + + public static async Task InitializeLanguages(DbWikiWakaContext db) + { + await db.Language.AddRangeAsync(languages); + _ = await db.SaveChangesAsync(); + } +} diff --git a/Api/appsettings.Development.json b/Api/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/Api/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/Api/appsettings.json b/Api/appsettings.json new file mode 100644 index 0000000..2a784da --- /dev/null +++ b/Api/appsettings.json @@ -0,0 +1,12 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*", + "ConnectionStrings": { + "DbWikiWaka": "Data Source=databse.dat" + } +} diff --git a/Api/databse.dat b/Api/databse.dat new file mode 100644 index 0000000..9a47220 Binary files /dev/null and b/Api/databse.dat differ diff --git a/Api/databse.dat-shm b/Api/databse.dat-shm new file mode 100644 index 0000000..d7be745 Binary files /dev/null and b/Api/databse.dat-shm differ diff --git a/Api/databse.dat-wal b/Api/databse.dat-wal new file mode 100644 index 0000000..6bbfb28 Binary files /dev/null and b/Api/databse.dat-wal differ -- cgit v1.2.3