< Summary

Information
Class: LGDXRobotCloud.API.Services.Identity.CurrentUserService
Assembly: LGDXRobotCloud.API
File(s): /builds/yukaitung/lgdxrobot2-cloud/LGDXRobotCloud.API/Services/Identity/CurrentUserService.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 28
Coverable lines: 28
Total lines: 52
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 14
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)0%620%
GetUserAsync()0%7280%
UpdateUserAsync()0%2040%

File(s)

/builds/yukaitung/lgdxrobot2-cloud/LGDXRobotCloud.API/Services/Identity/CurrentUserService.cs

#LineLine coverage
 1using LGDXRobotCloud.API.Exceptions;
 2using LGDXRobotCloud.Data.Entities;
 3using LGDXRobotCloud.Data.Models.Business.Administration;
 4using LGDXRobotCloud.Data.Models.Business.Identity;
 5using Microsoft.AspNetCore.Identity;
 6
 7namespace LGDXRobotCloud.API.Services.Identity;
 8
 9public interface ICurrentUserService
 10{
 11  Task<LgdxUserBusinessModel> GetUserAsync(string userId);
 12  Task<bool> UpdateUserAsync(string userId, LgdxUserUpdateBusinessModel lgdxUserBusinessModel);
 13}
 14
 015public class CurrentUserService(
 016    UserManager<LgdxUser> userManager
 017  ) : ICurrentUserService
 18{
 019  private readonly UserManager<LgdxUser> _userManager = userManager ?? throw new ArgumentNullException(nameof(userManage
 20
 21  public async Task<LgdxUserBusinessModel> GetUserAsync(string userId)
 022  {
 023    var user = await _userManager.FindByIdAsync(userId)
 024      ?? throw new LgdxNotFound404Exception();
 25
 026    return new LgdxUserBusinessModel {
 027      Id = Guid.Parse(user.Id!),
 028      Name = user.Name ?? string.Empty,
 029      UserName = user.UserName ?? string.Empty,
 030      Email = user.Email ?? string.Empty,
 031      Roles = await _userManager.GetRolesAsync(user),
 032      TwoFactorEnabled = user.TwoFactorEnabled,
 033      AccessFailedCount = user.AccessFailedCount
 034    };
 035  }
 36
 37  public async Task<bool> UpdateUserAsync(string userId, LgdxUserUpdateBusinessModel lgdxUserBusinessModel)
 038  {
 039    var user = await _userManager.FindByIdAsync(userId)
 040      ?? throw new LgdxNotFound404Exception();
 41
 042    user.Name = lgdxUserBusinessModel.Name;
 043    user.Email = lgdxUserBusinessModel.Email;
 044    var result = await _userManager.UpdateAsync(user);
 045    if (!result.Succeeded)
 046    {
 047      throw new LgdxIdentity400Expection(result.Errors);
 48    }
 49
 050    return true;
 051  }
 52}