Пример #1
0
        public async Task <Result> Update(string userId, long attributeId, UpdateUserAttributeModel updateUserAttribute)
        {
            ValidationResult validationResult = _updateUserAttribute.Validate(updateUserAttribute);

            if (!validationResult.IsValid)
            {
                _logger.LogWarning($"Invalid {typeof(UpdateUserAttributeModel).CustomAttributes} model");
                return(Result.Fail(validationResult.Errors));
            }

            BaseSpecification <UserAttributeEntity> baseSpecification = new BaseSpecification <UserAttributeEntity>();

            baseSpecification.AddFilter(x => x.Id == attributeId);
            baseSpecification.AddFilter(x => x.UserId == userId);

            UserAttributeEntity userAttribute = await _userAttributeRepository.SingleOrDefault(baseSpecification);

            if (userAttribute == null)
            {
                _logger.LogError($"No UserAttribute. UserId {userId}, AttributeId {attributeId}");
                return(Result.Fail("no_user_attribute", "No UserAttribute"));
            }

            userAttribute.Value = updateUserAttribute.Value;

            bool updateResult = await _userAttributeRepository.Update(userAttribute);

            if (!updateResult)
            {
                _logger.LogError($"Failed to update UserAttribute. UserId {userId}, AttributeId {attributeId}");
                return(Result.Fail("failed_to_update_user_attribute", "Failed to update user attribute"));
            }

            return(Result.Ok());
        }
        public async Task <IActionResult> Update([FromRoute] string userId, [FromRoute] long attributeId, [FromBody] UpdateUserAttributeModel updateUserAttribute)
        {
            Result result = await _userAttributeService.Update(userId, attributeId, updateUserAttribute);

            if (result.Failure)
            {
                ModelState.AddErrors(result);
                return(BadRequest(ModelState));
            }

            return(Ok(new EmptyResult()));
        }