summaryrefslogtreecommitdiff
path: root/Api
diff options
context:
space:
mode:
authorLuca Matei Pintilie <luca@lucamatei.com>2024-10-30 20:59:53 +0000
committerLuca Matei Pintilie <luca@lucamatei.com>2024-10-30 20:59:53 +0000
commitdc1938dbb98dc9edf16558e505b2961246c83e6e (patch)
tree7953186976085ceca6c03a208d1ffd9596f745be /Api
parentfc3c7e3b3c955cc274d4668768466fecb8fb727b (diff)
downloadwikiwaka-dc1938dbb98dc9edf16558e505b2961246c83e6e.tar
wikiwaka-dc1938dbb98dc9edf16558e505b2961246c83e6e.tar.gz
wikiwaka-dc1938dbb98dc9edf16558e505b2961246c83e6e.tar.bz2
wikiwaka-dc1938dbb98dc9edf16558e505b2961246c83e6e.tar.lz
wikiwaka-dc1938dbb98dc9edf16558e505b2961246c83e6e.tar.xz
wikiwaka-dc1938dbb98dc9edf16558e505b2961246c83e6e.tar.zst
wikiwaka-dc1938dbb98dc9edf16558e505b2961246c83e6e.zip
Add API and TestHEADmaster
Diffstat (limited to 'Api')
-rw-r--r--Api/Api.csproj15
-rw-r--r--Api/Contract/NewContent.cs7
-rw-r--r--Api/Contract/WikiPage.cs7
-rw-r--r--Api/Contract/WikiProperty.cs8
-rw-r--r--Api/Controllers/ContentApi.cs37
-rw-r--r--Api/Models/Content.cs7
-rw-r--r--Api/Models/DbWikiWakaContext.cs11
-rw-r--r--Api/Models/Language.cs7
-rw-r--r--Api/Models/Property.cs11
-rw-r--r--Api/Models/Version.cs6
-rw-r--r--Api/Program.cs38
-rw-r--r--Api/Properties/launchSettings.json41
-rw-r--r--Api/Utils/DbInit.cs25
-rw-r--r--Api/appsettings.Development.json8
-rw-r--r--Api/appsettings.json12
-rw-r--r--Api/databse.datbin0 -> 4096 bytes
-rw-r--r--Api/databse.dat-shmbin0 -> 32768 bytes
-rw-r--r--Api/databse.dat-walbin0 -> 45352 bytes
18 files changed, 240 insertions, 0 deletions
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 @@
+<Project Sdk="Microsoft.NET.Sdk.Web">
+
+ <PropertyGroup>
+ <TargetFramework>net8.0</TargetFramework>
+ <Nullable>enable</Nullable>
+ <ImplicitUsings>enable</ImplicitUsings>
+ </PropertyGroup>
+
+ <ItemGroup>
+ <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.8" />
+ <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.10" />
+ <PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
+ </ItemGroup>
+
+</Project>
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<Content> GetContents()
+ {
+ return dbContext.Content.AsAsyncEnumerable();
+ }
+
+ [HttpPost(nameof(Content))]
+ public async Task<Content> 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<Property> 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> Content { get; set; }
+ public DbSet<Version> Version { get; set; }
+ public DbSet<Property> Property { get; set; }
+ public DbSet<Language> 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<DbWikiWakaContext>(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<DbWikiWakaContext>();
+ ILogger<Program> logger = scope.ServiceProvider.GetRequiredService<ILogger<Program>>();
+ 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
--- /dev/null
+++ b/Api/databse.dat
Binary files differ
diff --git a/Api/databse.dat-shm b/Api/databse.dat-shm
new file mode 100644
index 0000000..d7be745
--- /dev/null
+++ b/Api/databse.dat-shm
Binary files differ
diff --git a/Api/databse.dat-wal b/Api/databse.dat-wal
new file mode 100644
index 0000000..6bbfb28
--- /dev/null
+++ b/Api/databse.dat-wal
Binary files differ