Skip to content

Commit

Permalink
ArticleController interface implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
sxergiu committed Dec 10, 2023
1 parent 0401703 commit dc077e9
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 37 deletions.
11 changes: 2 additions & 9 deletions GdscBackend/Common/Extensions/ClaimsPrincipalExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@ public static class ClaimsPrincipalExtensions
{
public static string? GetUserId(this ClaimsPrincipal user)
{
var userIdClaim = user.Claims.FirstOrDefault(c => c.Type == "sub");

if (userIdClaim is not null && !string.IsNullOrEmpty(userIdClaim.Value))
{
return userIdClaim.Value;
}

return null;
return user.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value;
}
}
}
2 changes: 1 addition & 1 deletion GdscBackend/Database/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public async Task<T> AddOrUpdateAsync([NotNull] T entity)
return _dbSet.First(e => e.Id == id);
}

public async Task<T>? DeleteAsync([NotNull] string id)
public async Task<T?> DeleteAsync([NotNull] string id)
{
var entity = await DbSet.FirstOrDefaultAsync(item => item.Id == id);

Expand Down
45 changes: 20 additions & 25 deletions GdscBackend/Features/Articles/ArticleController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Xml.Linq;
using System.Security.Claims;
using System.Xml.Linq;
using GdscBackend.Common.Extensions;
using GdscBackend.Database;
using GdscBackend.Utils;
Expand All @@ -14,15 +15,12 @@ namespace GdscBackend.Features.Articles;
[Route("v1/Articles")]
public class ArticleController : ControllerBase
{
private readonly AppDbContext _dbContext;
private readonly IRepository<ArticleModel> _repo;

public ArticleController(AppDbContext appDbContext,IRepository<ArticleModel> repo)
public ArticleController( IRepository<ArticleModel> repo )
{
_dbContext = appDbContext;
_repo = repo;
}


[HttpPost]
[ProducesResponseType(StatusCodes.Status201Created)]
Expand All @@ -31,16 +29,11 @@ public ArticleController(AppDbContext appDbContext,IRepository<ArticleModel> rep
public async Task<ActionResult<ArticleModel>> Post(ArticleRequest request)
{

var author = User.GetUserId();

return Ok(User.Claims.ToString());

if (author is null)
{
var authorid = User.GetUserId();
if (authorid is null)
{
return NotFound("User not found!");
}

return Ok(author);

var article = new ArticleModel
{
Expand All @@ -49,13 +42,12 @@ 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 _repo.AddAsync(article);

var result = await _dbContext.Articles.AddAsync(article);
await _dbContext.SaveChangesAsync();

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


Expand Down Expand Up @@ -101,26 +93,24 @@ public async Task<ActionResult<ArticleModel>> Delete([FromRoute] string id)
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public async Task<ActionResult<ArticleResponse>> ChangeAuthor([FromRoute] string id)
{
var article = await _repo.GetAsync(id);
var article = await _repo.GetAsync(id);
if (article is null) return NotFound("Article not found!");

var authorid = User.GetUserId();

if (authorid is null)
{
return NotFound("Author not found!");
}
if (authorid is null) return NotFound("Author not found!");

article.AuthorId = authorid;
article.Updated = DateTime.UtcNow;

await _repo.UpdateAsync(article.Id,article);

return Ok(new ArticleResponse
{
Id = article.Id,
Created = article.Created,
Title = article.Title,
Content = article.Content,
AuthorId = article.AuthorId
AuthorId = article.AuthorId // from keycloak
});
}

Expand All @@ -136,6 +126,8 @@ public async Task<ActionResult<ArticleResponse>> ChangeContent([FromRoute] strin

article.Content = content;
article.Updated = DateTime.UtcNow;

await _repo.UpdateAsync(article.Id, article);

return Ok(new ArticleResponse
{
Expand All @@ -159,6 +151,9 @@ public async Task<ActionResult<ArticleResponse>> ChangeTitle([FromRoute] string

article.Title = title;
article.Updated = DateTime.UtcNow;

await _repo.UpdateAsync(article.Id, article);


return Ok(new ArticleResponse
{
Expand Down
2 changes: 0 additions & 2 deletions GdscBackend/Features/Articles/ArticleRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@ public class ArticleRequest
[Required]public string Title { get; set; }

public string Content { get; set; }

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

<ItemGroup>
<Compile Remove="Features\Users\**" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Remove="Features\Users\**" />
</ItemGroup>

<ItemGroup>
<Content Remove="Features\Users\**" />
</ItemGroup>

<ItemGroup>
<None Remove="Features\Users\**" />
</ItemGroup>

</Project>

0 comments on commit dc077e9

Please sign in to comment.