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/Controllers/ContentApi.cs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Api/Controllers/ContentApi.cs (limited to 'Api/Controllers') 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; + } +} -- cgit v1.2.3