Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added postb articles #92

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions GdscBackend/Common/Extensions/ClaimsPrincipalExtensions.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
13 changes: 7 additions & 6 deletions GdscBackend/Features/Articles/ArticleController.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -18,7 +19,7 @@ public ArticleController(AppDbContext appDbContext)
_dbContext = appDbContext;
}

/*

[HttpPost]
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
Expand All @@ -27,8 +28,8 @@ public async Task<ActionResult<ArticleModel>> 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!");
}
Expand All @@ -40,15 +41,15 @@ public async Task<ActionResult<ArticleModel>> 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);
await _dbContext.SaveChangesAsync();

return Created("v1/Articles", result.Entity);
}
*/


[HttpGet]
[AllowAnonymous]
Expand Down
2 changes: 1 addition & 1 deletion GdscBackend/Features/Articles/ArticleRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ public class ArticleRequest

public string Content { get; set; }

[Required]public string AuthorId { get; set; }
//[Required]public string AuthorId { get; set; }
}
4 changes: 0 additions & 4 deletions GdscBackend/GdscBackend.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,4 @@
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>

<ItemGroup>
<Folder Include="Common"/>
</ItemGroup>

</Project>
Loading