예제 #1
0
 public static void Validate(this CopyWorkflowDto dto)
 {
     if (dto.Name == null || dto.Name.Length < WorkflowConstants.MinNameLength || dto.Name.Length > WorkflowConstants.MaxNameLength)
     {
         throw new BadRequestException(ErrorMessages.WorkflowNameError, ErrorCodes.BadRequest);
     }
 }
예제 #2
0
        public async Task <int> CopyWorkflowAsync(int workflowId, int userId, CopyWorkflowDto copyWorkfloDto)
        {
            if (workflowId < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(workflowId));
            }

            var parameters = new DynamicParameters();

            parameters.Add("@WorkflowId", workflowId);
            parameters.Add("@UserId", userId);
            parameters.Add("@Name", copyWorkfloDto.Name);
            parameters.Add("@ErrorCode", dbType: DbType.Int32, direction: ParameterDirection.Output);

            var result = await _connectionWrapper.ExecuteScalarAsync <int>("CopyWorkflow", parameters, commandType : CommandType.StoredProcedure);

            var errorCode = parameters.Get <int?>("ErrorCode");

            if (errorCode.HasValue)
            {
                switch (errorCode.Value)
                {
                case (int)SqlErrorCodes.WorkflowWithCurrentIdNotExist:
                    throw new ResourceNotFoundException(ErrorMessages.WorkflowNotExist, ErrorCodes.ResourceNotFound);

                case (int)SqlErrorCodes.WorkflowWithSuchANameAlreadyExists:
                    throw new ConflictException(ErrorMessages.WorkflowAlreadyExists, ErrorCodes.WorkflowAlreadyExists);

                case (int)SqlErrorCodes.GeneralSqlError:
                    throw new Exception(ErrorMessages.GeneralErrorOfCopyingWorkflow);
                }
            }

            return(result);
        }
예제 #3
0
        public async Task <IHttpActionResult> CopyWorkflowAsync(int workflowId, [FromBody] CopyWorkflowDto copyWorkfloDto)
        {
            await _privilegesManager.Demand(Session.UserId, InstanceAdminPrivileges.AccessAllProjectData);

            if (copyWorkfloDto == null)
            {
                throw new BadRequestException(ErrorMessages.WorkflowModelIsEmpty, ErrorCodes.BadRequest);
            }

            copyWorkfloDto.Name = copyWorkfloDto.Name?.Trim(); // remove whitespaces

            copyWorkfloDto.Validate();

            var result = await _workflowRepository.CopyWorkflowAsync(workflowId, Session.UserId, copyWorkfloDto);

            return(Ok(result));
        }
        public void Validate_NameIsValid_NoException()
        {
            // arrange
            Exception exception = null;
            var       model     = new CopyWorkflowDto {
                Name = "L"
            };

            // act
            try
            {
                model.Validate();
            }
            catch (Exception ex)
            {
                exception = ex;
            }

            // assert
            Assert.IsNull(exception);
        }
        public void Validate_NameToLong_ReturnBadRequestException()
        {
            // arrange
            Exception exception = null;
            var       model     = new CopyWorkflowDto {
                Name = "Lorem ipsum dolor sit ame"
            };                                                                      // 25 symbols - only max 24 is Ok

            // act
            try
            {
                model.Validate();
            }
            catch (Exception ex)
            {
                exception = ex;
            }

            // assert
            Assert.IsNotNull(exception);
            Assert.IsInstanceOfType(exception, typeof(BadRequestException));
            Assert.AreEqual(ErrorMessages.WorkflowNameError, exception.Message);
        }
        public void Validate_NameIsEmptyString_BadRequest()
        {
            // arrange
            Exception exception = null;
            var       model     = new CopyWorkflowDto {
                Name = string.Empty
            };

            // act
            try
            {
                model.Validate();
            }
            catch (Exception ex)
            {
                exception = ex;
            }

            // assert
            Assert.IsNotNull(exception);
            Assert.IsInstanceOfType(exception, typeof(BadRequestException));
            Assert.AreEqual(ErrorMessages.WorkflowNameError, exception.Message);
        }