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

Minor fixes, update on ArticleController #91

Merged
merged 2 commits into from
Dec 10, 2023
Merged
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;
}
}
6 changes: 3 additions & 3 deletions GdscBackend/Database/IRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ public interface IRepository<T> where T : class, IModel
{
DbSet<T> DbSet { get; }
Task<T> AddAsync([NotNull] T entity);
Task<T> GetAsync([NotNull] string id);
Task<T?> GetAsync([NotNull] string id);
Task<IEnumerable<T>> GetAsync();
Task<T> AddOrUpdateAsync([NotNull] T entity);
Task<T> UpdateAsync([NotNull] string id ,[NotNull] object entity);
Task<T>? DeleteAsync([NotNull] string id);
Task<T?> UpdateAsync([NotNull] string id ,[NotNull] object entity);
Task<T?> DeleteAsync([NotNull] string id);
Task<IEnumerable<T>> DeleteAsync([NotNull] IEnumerable<string> ids);
}
10 changes: 5 additions & 5 deletions GdscBackend/Database/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,21 @@ public async Task<IEnumerable<T>> GetAsync()
return await DbSet.ToListAsync();
}

public async Task<T> GetAsync([NotNull] string id)
public async Task<T?> GetAsync([NotNull] string id)
{
return await DbSet.FirstOrDefaultAsync(e => e.Id == id);
}

public async Task<T> AddOrUpdateAsync([NotNull] T entity)
{
if (entity is null) return null;
// if (entity is null) return null;

var existing = await DbSet.FirstOrDefaultAsync(item => item.Id == entity.Id);

return existing is null ? await AddAsync(entity) : await UpdateAsync(entity.Id, entity);
}

public async Task<T> UpdateAsync([NotNull] string id, [NotNull] object newEntity)
public async Task<T?> UpdateAsync([NotNull] string id, [NotNull] object newEntity)
{
var entity = await GetAsync(id);
if (entity is null) return null;
Expand All @@ -60,7 +60,7 @@ public async Task<T> UpdateAsync([NotNull] string id, [NotNull] object newEntity
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
93 changes: 48 additions & 45 deletions GdscBackend/Features/Articles/ArticleController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using GdscBackend.Database;
using System.Security.Claims;
using System.Xml.Linq;
using GdscBackend.Common.Extensions;
using GdscBackend.Database;
using GdscBackend.Utils;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
Expand All @@ -7,64 +11,60 @@ namespace GdscBackend.Features.Articles;

[ApiController]
[ApiVersion("1")]
[Authorize("CoreTeam")]
[Authorize(AuthorizeConstants.CoreTeam)]
[Route("v1/Articles")]
public class ArticleController : ControllerBase
{
private readonly AppDbContext _dbContext;
private readonly IRepository<ArticleModel> _repo;

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

/*

[HttpPost]
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
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!");
}

var article = new ArticleModel
{
Id = Guid.NewGuid().ToString(),
Created = DateTime.UtcNow,
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);
}
*/


[HttpGet]
[AllowAnonymous]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<ActionResult<IEnumerable<ArticleResponse>>> Get()
{
var result = _dbContext.Articles.Select(
article => new ArticleResponse
{
Id = article.Id,
Created = article.Created,
Title = article.Title,
Content = article.Content,
AuthorId = article.AuthorId
}).ToList();
var result = await _repo.DbSet.Select(article => new ArticleResponse
{
Id = article.Id,
Created = article.Created,
Title = article.Title,
Content = article.Content,
AuthorId = article.AuthorId
}).ToListAsync();

return Ok(result);
}
Expand All @@ -76,12 +76,13 @@ public async Task<ActionResult<IEnumerable<ArticleResponse>>> Get()
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public async Task<ActionResult<ArticleModel>> Delete([FromRoute] string id)
{
var articol = await _dbContext.Articles.FirstOrDefaultAsync(entity => entity.Id == id);

var articol = await _repo.GetAsync(id);

if (articol is null) return NotFound("Article not found!");
var result = await _repo.DeleteAsync(articol.Id);

var result = _dbContext.Articles.Remove(articol);
await _dbContext.SaveChangesAsync();
return Ok(result.Entity);
return Ok(result);
}


Expand All @@ -90,29 +91,26 @@ public async Task<ActionResult<ArticleModel>> Delete([FromRoute] string id)
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public async Task<ActionResult<ArticleResponse>> ChangeAuthor([FromRoute] string id, [FromBody] string authorid)
public async Task<ActionResult<ArticleResponse>> ChangeAuthor([FromRoute] string id)
{
var article = await _dbContext.Articles.FirstOrDefaultAsync(entity => entity.Id == id);
var article = await _repo.GetAsync(id);
if (article is null) return NotFound("Article not found!");

/* check from keycloak
if (author is null)
{
return NotFound("Author not found!");
}
*/
var authorid = User.GetUserId();
if (authorid is null) return NotFound("Author not found!");

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

await _dbContext.SaveChangesAsync();
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 @@ -123,12 +121,14 @@ public async Task<ActionResult<ArticleResponse>> ChangeAuthor([FromRoute] string
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public async Task<ActionResult<ArticleResponse>> ChangeContent([FromRoute] string id, [FromBody] string content)
{
var article = await _dbContext.Articles.FirstOrDefaultAsync(entity => entity.Id == id);
var article = await _repo.GetAsync(id);
if (article is null) return NotFound("Article not found!");

article.Content = content;
article.Updated = DateTime.UtcNow;
await _dbContext.SaveChangesAsync();

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

return Ok(new ArticleResponse
{
Id = article.Id,
Expand All @@ -146,12 +146,15 @@ public async Task<ActionResult<ArticleResponse>> ChangeContent([FromRoute] strin
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public async Task<ActionResult<ArticleResponse>> ChangeTitle([FromRoute] string id, [FromBody] string title)
{
var article = await _dbContext.Articles.FirstOrDefaultAsync(entity => entity.Id == id);
var article = await _repo.GetAsync(id);
if (article is null) return NotFound("Article not found!");

article.Title = title;
article.Updated = DateTime.UtcNow;
await _dbContext.SaveChangesAsync();

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


return Ok(new ArticleResponse
{
Id = article.Id,
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; }
}
2 changes: 1 addition & 1 deletion GdscBackend/Features/Events/EventsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public async Task<ActionResult<EventModel>> Post(EventRequest entity)
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<ActionResult<EventModel>> Delete([FromRoute] string id)
public async Task<ActionResult<EventModel?>> Delete([FromRoute] string id)
{
var entity = await _repository.DeleteAsync(id);
return entity is null ? NotFound() : Ok(entity);
Expand Down
14 changes: 13 additions & 1 deletion GdscBackend/GdscBackend.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,19 @@
</ItemGroup>

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

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

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

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

</Project>
11 changes: 0 additions & 11 deletions GdscBackend/template.appsettings.Development.json

This file was deleted.

Loading