My login method is below and you’ll see I have to call my user again after I fetch it with _userManager.FindByEmailAsync()
because there are a couple of fields Points
and Tokens
that are calculated as a Sum()
Question – Is there any way I can extend _userManager.FindByEmailAsync()
so that it will run the Sum
as I do in the .Select()
call, so I don’t have a redundant call to the DB?
Here is my login method.
[AllowAnonymous]
[HttpPost("login")]
public async Task<IActionResult> Login(LoginDto loginDto) {
var userFromRepo = await _userManager.FindByEmailAsync(loginDto.Email);
if (userFromRepo == null) return Unauthorized(new ApiResponse(401));
if (userFromRepo.IsActive == false) return NotFound(new ApiResponse(404, "This account is no longer active."));
var result = await _signInManager.CheckPasswordSignInAsync(userFromRepo, loginDto.Password, false);
if (!result.Succeeded) return Unauthorized(new ApiResponse(401));
var user = await _dbContext.Users
.Select(p => new {
Id = p.Id,
Email = p.Email,
UserName = p.UserName,
Hosted = p.Hosted,
Instructed = p.Instructed,
Attended = p.Attended,
IsBoarded = p.IsBoarded,
IsActive = p.IsActive,
Likers = p.LikersCount,
Rating = p.Rating,
YearStarted = p.YearStarted,
YearsInstructing = p.YearsInstructing,
YearsPracticing = System.DateTime.Now.Year - p.YearStarted,
Certification = p.Certification.ToString(),
CreatedDate = p.CreatedDate,
PhotoUrl = p.IsBoarded ? p.UserPhoto : "assets/images/user.png",
Age = p.DateOfBirth.CalculateAge(),
DateOfBirth = p.DateOfBirth,
ExperienceLevel = p.ExperienceLevel.GetEnumName(),
Points = p.UserPoints.Sum(p => p.Points),
Tokens = p.Tokens.Sum(p => p.Tokens), // p.Tokens.OrderBy(p => p.Id).Last().Total,
Token = _tokenService.CreateToken(userFromRepo, (from userRole in p.UserRoles join role in _dbContext.Roles on userRole.RoleId equals role.Id select role.Name).ToList()),
IsInstructor = p.IsInstructor
})
.FirstOrDefaultAsync(p => p.Id == userFromRepo.Id);
if (user == null) return NotFound(new ApiResponse(404));
return Ok(user);
}