diff options
author | Luca Matei Pintilie <luca@lucamatei.com> | 2024-10-30 20:59:53 +0000 |
---|---|---|
committer | Luca Matei Pintilie <luca@lucamatei.com> | 2024-10-30 20:59:53 +0000 |
commit | dc1938dbb98dc9edf16558e505b2961246c83e6e (patch) | |
tree | 7953186976085ceca6c03a208d1ffd9596f745be /Api | |
parent | fc3c7e3b3c955cc274d4668768466fecb8fb727b (diff) | |
download | wikiwaka-master.tar wikiwaka-master.tar.gz wikiwaka-master.tar.bz2 wikiwaka-master.tar.lz wikiwaka-master.tar.xz wikiwaka-master.tar.zst wikiwaka-master.zip |
Diffstat (limited to '')
-rw-r--r-- | Api/Api.csproj | 15 | ||||
-rw-r--r-- | Api/Contract/NewContent.cs | 7 | ||||
-rw-r--r-- | Api/Contract/WikiPage.cs | 7 | ||||
-rw-r--r-- | Api/Contract/WikiProperty.cs | 8 | ||||
-rw-r--r-- | Api/Controllers/ContentApi.cs | 37 | ||||
-rw-r--r-- | Api/Models/Content.cs | 7 | ||||
-rw-r--r-- | Api/Models/DbWikiWakaContext.cs | 11 | ||||
-rw-r--r-- | Api/Models/Language.cs | 7 | ||||
-rw-r--r-- | Api/Models/Property.cs | 11 | ||||
-rw-r--r-- | Api/Models/Version.cs | 6 | ||||
-rw-r--r-- | Api/Program.cs | 38 | ||||
-rw-r--r-- | Api/Properties/launchSettings.json | 41 | ||||
-rw-r--r-- | Api/Utils/DbInit.cs | 25 | ||||
-rw-r--r-- | Api/appsettings.Development.json | 8 | ||||
-rw-r--r-- | Api/appsettings.json | 12 | ||||
-rw-r--r-- | Api/databse.dat | bin | 0 -> 4096 bytes | |||
-rw-r--r-- | Api/databse.dat-shm | bin | 0 -> 32768 bytes | |||
-rw-r--r-- | Api/databse.dat-wal | bin | 0 -> 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 Binary files differnew file mode 100644 index 0000000..9a47220 --- /dev/null +++ b/Api/databse.dat diff --git a/Api/databse.dat-shm b/Api/databse.dat-shm Binary files differnew file mode 100644 index 0000000..d7be745 --- /dev/null +++ b/Api/databse.dat-shm diff --git a/Api/databse.dat-wal b/Api/databse.dat-wal Binary files differnew file mode 100644 index 0000000..6bbfb28 --- /dev/null +++ b/Api/databse.dat-wal |