0

Good day, I have one problem. I have three Domain objects

 public class User
{
    public Guid Id { get; set; }
    public string Username { get; set; }
    public string EmailAddress { get; set; }
    public string Password { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    [NotMapped]
    public List<string> Roles { get; set; }

    //nav prop
    public List<User_Role> UserRoles { get; set; }
}

 public class User_Role
{
    public Guid Id { get; set; }

    public Guid UserId { get; set; }
    //nav prop
    public User User { get; set; }

    public Guid RoleId { get; set; }
    //nav prop
    public Role Role { get; set; }
}
 public class Role
{
    public Guid Id { get; set; }
    public string Name { get; set; }

    public List<User_Role> UserRoles { get; set; }
}

I want to create DTO object from them

 public class ReturnUserDto
{
    public Guid Id { get; set; }
    public string Username { get; set; }
    public string EmailAddress { get; set; }
    public string Password { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public List<string> Roles { get; set; }
}

I have created a controller for it

[Route("api/[controller]")]
[ApiController]
public class UserController : ControllerBase
{
    public readonly IUserRepository _userRepository;
    public readonly IRoleRepository _roleRepository;
    public readonly IUserRoleRepository _userRoleRepository

    public UserController(IUserRepository userRepository, IRoleRepository IRoleRepository,
        IUserRoleRepository userRoleRepository)
    {
        _userRepository = userRepository;
        _roleRepository = IRoleRepository;
        _userRoleRepository = userRoleRepository;   
    }

    [HttpGet]
    public async Task<IActionResult> GetAllUsersAsync()
    {
        var users = _userRepository.GetAllAsync();

        var usersDto = users.ConvertToDto(unknow arguments)


    }
}

And i am trying achieve it by using static DtoConverstion function It looks like this

public static IEnumerable<ReturnUserDto> ConvertToDto(this IEnumerable<User> users,
        IEnumerable<Role>  Role)
    {
        var  returnUserDto = (from user in users
                              select new ReturnUserDto
                              {
                                  Id = user.Id,
                                  Username = user.Username,
                                  EmailAddress = user.EmailAddress,
                                  Password = user.Password,
                                  FirstName = user.FirstName,
                                  LastName = user.LastName,
                                  Roles = ?(Something like Role.name)
                              })
    }

How do I achieve it? I understand that I need to use user_roles to get roles from it for a specific user, then add it to DtoConvertion function. I am just not sure how to do it.

1 Answers1

0

Can do it multiple ways.One way is to use the Include() and ThenInclude() option

Instead of _userRepository.GetAllAsync(), add a new method which will get the list of Users along with User Roles and its Roles with it. It will be something like

context.Users
    .Include(u => u.UserRoles)
    .ThenInclude(u => u.Role).ToList()

Then, in the convert method, something like

    public static IEnumerable<ReturnUserDto> ConvertToDto(this IEnumerable<User> users)
    {
        var  returnUserDto = (from user in users
                              select new ReturnUserDto
                              {
                                  Id = user.Id,
                                  Username = user.Username,
                                  EmailAddress = user.EmailAddress,
                                  Password = user.Password,
                                  FirstName = user.FirstName,
                                  LastName = user.LastName,
                                  Roles = user.UserRoles.Select(s => s.Role.Name).ToList()
                              })
    }

Refer https://learn.microsoft.com/en-us/ef/ef6/querying/related-data if you are not familiar with the Include() method.

Hope this helps.

WisdomSeeker
  • 854
  • 6
  • 8
  • This approach does not work for me because I need Role String which is in not in user_role but in the role class. When I get user_roles object I need it to use it in search of Role class and then add Role.name to Roles. – Modestas Vacerskas Oct 12 '22 at 13:51
  • The example i showed is getting the value from the `Role` table itself. `ThenInclude` is used to get the `Roles` inside `UserRole` – WisdomSeeker Oct 12 '22 at 13:54