Skip to content
This repository has been archived by the owner on Jul 17, 2020. It is now read-only.

Commit

Permalink
API implementation to create a member for auth (#86)
Browse files Browse the repository at this point in the history
* Create Member Via API implementation

* Add TODO to MemberController

Co-Authored-By: Paul Scharnofske <[email protected]>

* Improve generic usage in MemberControllerTests

Co-Authored-By: Paul Scharnofske <[email protected]>

* Remove Generic from Entity Result and Add DI Extensions to WebApp.Testing

* Add migrations

* Remake inital migration

Co-authored-by: Paul Scharnofske <[email protected]>
  • Loading branch information
misha130 and asynts authored Apr 9, 2020
1 parent dda224b commit d4c8a39
Show file tree
Hide file tree
Showing 12 changed files with 9,421 additions and 5 deletions.
10 changes: 9 additions & 1 deletion src/Application/Members/IMembersRepository.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Codidact.Core.Domain.Entities;
using Codidact.Core.Domain.Common;
using Codidact.Core.Domain.Entities;
using System.Threading.Tasks;

namespace Codidact.Core.Application.Members
Expand All @@ -14,5 +15,12 @@ public interface IMembersRepository
/// <param name="userId">The user id to search for</param>
/// <returns>Member</returns>
Task<Member> GetSingleByUserIdAsync(long userId);

/// <summary>
/// Creates a new member
/// </summary>
/// <param name="member"></param>
/// <returns></returns>
Task<EntityResult> Create(Member member);
}
}
11 changes: 11 additions & 0 deletions src/Application/Members/MembersRepository.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Codidact.Core.Application.Common.Interfaces;
using Codidact.Core.Domain.Common;
using Codidact.Core.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using System.Threading;
using System.Threading.Tasks;

namespace Codidact.Core.Application.Members
Expand All @@ -17,5 +19,14 @@ public async Task<Member> GetSingleByUserIdAsync(long userId)
{
return await _context.Members.SingleAsync(member => member.UserId == userId).ConfigureAwait(false);
}

public async Task<EntityResult> Create(Member member)
{
_context.Members.Add(member);

await _context.SaveChangesAsync(CancellationToken.None);

return new EntityResult(member.Id);
}
}
}
35 changes: 35 additions & 0 deletions src/Domain/Common/EntityResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Collections.Generic;
using System.Linq;

namespace Codidact.Core.Domain.Common
{
public class EntityResult
{
public List<string> Errors { get; set; } = new List<string>();

public bool Success { get; set; }

public long? Id { get; set; }

public EntityResult()
{

}

public EntityResult(bool success)
{
Success = success;
}

public EntityResult(long id)
{
Id = id;
Success = true;
}

public EntityResult(params string[] error)
{
Errors = error.ToList();
}
}
}
7 changes: 5 additions & 2 deletions src/Infrastructure/Identity/CurrentUserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,18 @@ public string GetUserId()
/// </summary>
public long GetMemberId()
{
var context = _httpContextAccessor.HttpContext;
var claimsIdentity = _httpContextAccessor.HttpContext.User.Claims as ClaimsIdentity;
Claim memberClaim = _httpContextAccessor.HttpContext?.User.Claims.FirstOrDefault(claim => claim.Type == "codidact_member_id");
if (!string.IsNullOrEmpty(memberClaim.Value))
if (!string.IsNullOrEmpty(memberClaim?.Value))
{
return long.Parse(memberClaim.Value);
}
else
{
throw new Exception("Claim for memberId is missing in token");
// TODO: remove this hack. Currently 0, maybe don't set memberId if its not existant.;
return 0;
// throw new Exception("Claim for memberId is missing in token");
}
}
}
Expand Down
Loading

0 comments on commit d4c8a39

Please sign in to comment.