I have an NET Core application and I need to get the current user token for map an object with Automapper.
This is my NET Core controller:
public async Task<IActionResult> Add([FromBody] EnrollSkill request)
{
var model = _autoMapper.Map<Domain.Entities.UserSkill>(request);
var response = await _userService.AddSkillAsync(model);
return Ok();
}
Note that I'm trying to map EnrollSkill viewmodel to UserSkill domain model.
This is my EnrollSkill class:
public class EnrollSkill
{
public string Id { get; set; } // Skill Id (not user Id)
public int KnowledgeLevel { get; set; }
public int Order { get; set; }
}
And this is my UserSkill class:
public class UserSkill : Base
{
public int KnowledgeLevel { get; set; }
public int Order { get; set; }
public DateTime CreatedDate { get; set; }
public string UserId { get; set; }
public User User { get; set; }
public string SkillId { get; set; }
public Skill Skill { get; set; }
}
In my repository service, I need populate UserId to invoke SaveChangesAsync()
This UserId exists in the Controller because I can read the user claims with:
User.Claims
Now, I have this profile in Automapper:
CreateMap<EnrollSkill, UserSkill>().
BeforeMap((from, to) =>
{
to.UserId = "12345"
});
But, how can I read correctly this value in Automapper? What's the best way?
I'm trying to populate this UserId in controller with a method named SetUserId, but I think it's a wrong solution because I'm messing my domain entity:
var model = _autoMapper.Map<Domain.Entities.UserSkill>(request).SetUserId(CurrentUserId);
Thanks