public Result Add(string groupId, AddGroupAttributeRequest addGroupAttribute)
        {
            ValidationResult validationResult = _addGroupAttributeValidator.Validate(addGroupAttribute);

            if (!validationResult.IsValid)
            {
                _logger.LogWarning($"Invalid {nameof(addGroupAttribute)} modal");
                return(Result.Fail(validationResult.Errors));
            }

            BaseSpecification <GroupEntity> groupExistSpecification = new BaseSpecification <GroupEntity>();

            groupExistSpecification.AddFilter(x => x.Id == groupId);

            bool groupExist = _groupRepository.Exist(groupExistSpecification);

            if (!groupExist)
            {
                _logger.LogError($"No Group. GroupId {groupId}");
                return(Result.Fail("no_group", "No Group"));
            }

            BaseSpecification <GroupAttributeEntity> keyExistSpecification = new BaseSpecification <GroupAttributeEntity>();

            keyExistSpecification.AddFilter(x => x.GroupId == groupId);
            keyExistSpecification.AddFilter(x => x.Key.ToUpper() == addGroupAttribute.Key.ToUpper());

            bool keyExist = _groupAttributeRepository.Exist(keyExistSpecification);

            if (keyExist)
            {
                _logger.LogError($"GroupAttribute key already exist. GroupId {groupId}, key {addGroupAttribute.Key}");
                return(Result.Fail("group_attribute_key_already_exist", "GroupAttribute key already exist"));
            }

            GroupAttributeEntity groupAttributeEntity = new GroupAttributeEntity(
                key: addGroupAttribute.Key,
                value: addGroupAttribute.Value,
                groupId: groupId);

            bool addResult = _groupAttributeRepository.Add(groupAttributeEntity);

            if (!addResult)
            {
                _logger.LogError($"Failed to add GroupAttribute. GroupId {groupId}, Key {addGroupAttribute.Key}");
                return(Result.Fail("failed_to_add_group_attribute", "Failed to add group attribute"));
            }

            return(Result.Ok());
        }
Esempio n. 2
0
        public IActionResult Add([FromRoute] string groupId, [FromBody] AddGroupAttributeRequest addGroupAttribute)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            Result result = _groupAttributeService.Add(groupId, addGroupAttribute);

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

            return(Ok(new EmptyResult()));
        }
        public Task <IActionResult> Add([FromRoute] string groupId, [FromBody] AddGroupAttributeRequest request)
        {
            Core.Models.Result.Result result = _groupAttributeService.Add(groupId, request);

            return(Task.FromResult(result.ToNewResult().ToApiResult()));
        }