public async Task <IActionResult> Register(
            UserAddDto dto
            )
        {
            try
            {
                var newUser = await _userService.AddUserAsync(dto);

                return(Ok(newUser));
            }
            catch (Exception ex)
            {
                return(BadRequest(new { ex.Message }));
            }
        }
Exemplo n.º 2
0
        public async Task <UserReturnDto> AddUserAsync(UserAddDto dto)
        {
            var userInDb =
                await(await _userRepository.GetAllAsync(d => d.UserName == dto.Username))
                .FirstOrDefaultAsync();

            if (userInDb != null)
            {
                throw new ObjectAlreadyExistException("User with such name already added.");
            }

            var userForDb = new User
            {
                UserName = dto.Username,
            };

            var user = (await _userRepository.InsertAsync(userForDb)).ToUserReturnDto();


            if (dto.RolesIds.Count != 0)
            {
                var roles = await _usersRolesService.AddRolesToUserAsync(user.Id, dto.RolesIds);

                user.Roles = roles.Select(o => o.Role).ToList();
            }

            if (dto.PermissionsIds.Count != 0)
            {
                var permissions =
                    await _usersPermissionsService.AddPermissionsToUserAsync(user.Id, dto.PermissionsIds);

                user.Permissions = permissions.Select(o => o.Permission).ToList();
            }

            return(user);
        }
Exemplo n.º 3
0
 public Task <UserReturnDto> GetUserAsync(UserAddDto dto)
 {
     return(GetUserAsync(dto.Username));
 }