Ejemplo n.º 1
0
        public async Task UpdateProfileAsync(long id, GoblinIdentityUpdateProfileModel model,
                                             CancellationToken cancellationToken = default)
        {
            var userEntity = await _userRepo.Get(x => x.Id == id)
                             .FirstOrDefaultAsync(cancellationToken)
                             .ConfigureAwait(true);

            if (userEntity == null)
            {
                throw new GoblinException(nameof(GoblinIdentityErrorCode.UserNotFound),
                                          GoblinIdentityErrorCode.UserNotFound);
            }

            using var transaction =
                      await GoblinUnitOfWork.BeginTransactionAsync(cancellationToken).ConfigureAwait(true);

            if (model.IsUpdateRoles)
            {
                _userRoleRepo.DeleteWhere(x => x.UserId == userEntity.Id);

                await GoblinUnitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(true);

                // User Roles

                if (model.Roles?.Any() == true)
                {
                    model.Roles = model.Roles.Select(x => x.Trim()).ToList();

                    var roleEntities = await _roleRepo.Get(x => model.Roles.Contains(x.Name))
                                       .ToListAsync(cancellationToken).ConfigureAwait(true);

                    foreach (var roleEntity in roleEntities)
                    {
                        _userRoleRepo.Add(new UserRoleEntity
                        {
                            UserId = userEntity.Id,
                            RoleId = roleEntity.Id
                        });
                    }
                }
            }

            model.MapTo(userEntity);

            _userRepo.Update(userEntity,
                             x => x.AvatarUrl,
                             x => x.FullName,
                             x => x.Bio,
                             x => x.GithubId,
                             x => x.SkypeId,
                             x => x.FacebookId,
                             x => x.WebsiteUrl,
                             x => x.CompanyName,
                             x => x.CompanyUrl
                             );

            await GoblinUnitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(true);

            transaction.Commit();
        }
Ejemplo n.º 2
0
        public static async Task UpdateProfileAsync(long id, GoblinIdentityUpdateProfileModel model, CancellationToken cancellationToken = default)
        {
            ValidationHelper.Validate <GoblinIdentityUpdateProfileModelValidator, GoblinIdentityUpdateProfileModel>(model);

            try
            {
                var endpoint = GetRequest(model.LoggedInUserId).AppendPathSegment(GoblinIdentityEndpoints.UpdateProfile.Replace("{id}", id.ToString()));

                await endpoint
                .PutJsonAsync(model, cancellationToken : cancellationToken)
                .ConfigureAwait(true);
            }
            catch (FlurlHttpException ex)
            {
                await FlurlHttpExceptionHelper.HandleErrorAsync(ex).ConfigureAwait(true);
            }
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> UpdateProfile([FromRoute] long id, [FromBody] GoblinIdentityUpdateProfileModel model, CancellationToken cancellationToken = default)
        {
            await _userService.UpdateProfileAsync(id, model, cancellationToken);

            return(NoContent());
        }