Exemplo n.º 1
0
        public IActionResult PutUserOwnerInfo([FromBody] UserInfoUpdateModel model)
        {
            try
            {
                // Map model to entity and set id
                var user = _mapper.Map <User>(model);
                user.Id = Auth.GetUserIdFromClaims(this);

                // Update
                _userService.Update(user, model.Password);
                return(Ok());
            }
            catch (Exception ex)
            {
                // Return error message if there was an exception
                return(BadRequest(new { message = ex.Message }));
            }
        }
Exemplo n.º 2
0
        public async Task <IActionResult> UpdateUserInfo([FromBody] JsonPatchDocument <UserInfoUpdateModel>
                                                         userInfo)
        {
            var subjectId = User.GetSubjectId();

            var storedUser = await _databaseContext.UserData.Include(u => u.ProfileImage)
                             .FirstOrDefaultAsync(UserIdPredicate(subjectId));

            if (storedUser == null)
            {
                throw new HttpResponseException();
            }

            var userInfoNew = new UserInfoUpdateModel
            {
                DisplayName = storedUser.DisplayName,
                ImageId     = storedUser.ProfileImageId,
            };

            userInfo.ApplyTo(userInfoNew, ModelState);
            CustomValidation(userInfoNew);

            if (userInfoNew.ImageId != null)
            {
                var image = await _databaseContext.Image.FirstOrDefaultAsync(u =>
                                                                             u.CreatorId == storedUser.Id && u.Id == userInfoNew.ImageId);

                if (image == null)
                {
                    throw new HttpResponseException(new UserInfoError(), HttpStatusCode.Forbidden);
                }
            }

            storedUser.DisplayName    = userInfoNew.DisplayName;
            storedUser.ProfileImageId = userInfoNew.ImageId;

            await _databaseContext.SaveChangesAsync();

            await _userActivityService.CreateActivityAsync(UserActivityActionName.UserUpdate, storedUser.Id);

            return(Ok(storedUser));
        }