From 44749cc59ada2a52b477b4902e0588072b7ed92b Mon Sep 17 00:00:00 2001 From: Razvan Bitea Date: Mon, 22 Jan 2024 22:20:18 +0200 Subject: [PATCH] added postb articles --- .../Common/Extensions/ClaimsPrincipalExtensions.cs | 11 +++++++++++ GdscBackend/Features/Articles/ArticleController.cs | 13 +++++++------ GdscBackend/Features/Articles/ArticleRequest.cs | 2 +- GdscBackend/GdscBackend.csproj | 4 ---- 4 files changed, 19 insertions(+), 11 deletions(-) create mode 100644 GdscBackend/Common/Extensions/ClaimsPrincipalExtensions.cs diff --git a/GdscBackend/Common/Extensions/ClaimsPrincipalExtensions.cs b/GdscBackend/Common/Extensions/ClaimsPrincipalExtensions.cs new file mode 100644 index 0000000..af8875a --- /dev/null +++ b/GdscBackend/Common/Extensions/ClaimsPrincipalExtensions.cs @@ -0,0 +1,11 @@ +using System.Security.Claims; + +namespace GdscBackend.Common.Extensions; + +public static class ClaimsPrincipalExtensions +{ + public static string? GetUserId(this ClaimsPrincipal user) + { + return user.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value; + } +} \ No newline at end of file diff --git a/GdscBackend/Features/Articles/ArticleController.cs b/GdscBackend/Features/Articles/ArticleController.cs index 2929629..9f0af75 100644 --- a/GdscBackend/Features/Articles/ArticleController.cs +++ b/GdscBackend/Features/Articles/ArticleController.cs @@ -1,4 +1,5 @@ -using GdscBackend.Database; +using GdscBackend.Common.Extensions; +using GdscBackend.Database; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; @@ -18,7 +19,7 @@ public ArticleController(AppDbContext appDbContext) _dbContext = appDbContext; } - /* + [HttpPost] [ProducesResponseType(StatusCodes.Status201Created)] [ProducesResponseType(StatusCodes.Status404NotFound)] @@ -27,8 +28,8 @@ public async Task> Post(ArticleRequest request) { - //var author = await _dbContext.Users.FirstOrDefaultAsync(entity => entity.Id == request.AuthorId); - if (author is null) + var authorid = User.GetUserId(); + if (authorid is null) { return NotFound("User not found!"); } @@ -40,7 +41,7 @@ public async Task> Post(ArticleRequest request) Updated = DateTime.UtcNow, Title = request.Title, Content = request.Content, - //Author = author --> author from keycloak + AuthorId = authorid }; var result = await _dbContext.Articles.AddAsync(article); @@ -48,7 +49,7 @@ public async Task> Post(ArticleRequest request) return Created("v1/Articles", result.Entity); } - */ + [HttpGet] [AllowAnonymous] diff --git a/GdscBackend/Features/Articles/ArticleRequest.cs b/GdscBackend/Features/Articles/ArticleRequest.cs index bb14bfb..c4d680a 100644 --- a/GdscBackend/Features/Articles/ArticleRequest.cs +++ b/GdscBackend/Features/Articles/ArticleRequest.cs @@ -8,5 +8,5 @@ public class ArticleRequest public string Content { get; set; } - [Required]public string AuthorId { get; set; } + //[Required]public string AuthorId { get; set; } } \ No newline at end of file diff --git a/GdscBackend/GdscBackend.csproj b/GdscBackend/GdscBackend.csproj index cd0ce6e..c615cd1 100644 --- a/GdscBackend/GdscBackend.csproj +++ b/GdscBackend/GdscBackend.csproj @@ -27,8 +27,4 @@ - - - -