コード例 #1
0
        public Results.GenericResult <TodoDTO> Post([FromBody] TodoDTO model)
        {
            var result = new Results.GenericResult <TodoDTO>();

            var validatorResult = validator.Validate(model);

            if (validatorResult.IsValid)
            {
                try
                {
                    result.Result       = appService.Create(model);
                    result.Success      = true;
                    Response.StatusCode = (int)HttpStatusCode.Created;
                }
                catch (Exception ex)
                {
                    Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                    result.Errors       = new string[] { ex.Message };
                }
            }
            else
            {
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                result.Errors       = validatorResult.GetErrors();
            }

            return(result);
        }
コード例 #2
0
        public Results.GenericResult <TodoDto> Post([FromBody] TodoDto model)
        {
            var result = new Results.GenericResult <TodoDto>();

            var validatorResult = validator.Validate(model);

            if (validatorResult.IsValid)
            {
                try
                {
                    result.Result  = appService.Create(model);
                    result.Success = true;
                }
                catch (Exception ex)
                {
                    result.Errors = new string[] { ex.Message };
                }
            }
            else
            {
                result.Errors = validatorResult.GetErrors();
            }


            return(result);
        }
コード例 #3
0
        public void Can_Validate_A_Valid_Todo(string task)
        {
            var subject = new TodoValidator();
            var todo = new TodoEntity(new TodoId("123"), task);
            var result = subject.Validate(todo).Result;

            Assert.True(result.IsRight);
        }
コード例 #4
0
        public void Can_Validate_An_Invalid_Todo(string task)
        {
            var subject = new TodoValidator();
            var todo = new TodoEntity(new TodoId("123"), task);
            var result = subject.Validate(todo).Result;

            Assert.True(result.IsLeft);
            Assert.Equal(DomainErrorCode.FailedValidation, result.LeftAsEnumerable().Head.ErrorCode);
        }