Exemplo n.º 1
0
        public HttpResponseMessage Post(Person person)
        {
            var personRepository = PersonRepositoryFactory.PersonRepository();

            personBL = new PersonBL(personRepository);

            HttpResponseMessage response;

            // Initiate Validation.
            var validator            = new PersonValidator();
            ValidationResult results = validator.Validate(person, ruleSet: "Names");


            // Add error messages if any.
            ModelStateDictionary modelDictionary = new ModelStateDictionary();

            foreach (ValidationFailure result in results.Errors)
            {
                modelDictionary.AddModelError(result.PropertyName, result.ErrorMessage);
            }


            if (results.IsValid)
            {
                // Calls Business Logic to create.
                try
                {
                    personBL.CreatePerson(person);
                }
                catch (Exception ex)
                {
                    modelDictionary.AddModelError("Error", "Service Error");
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, modelDictionary));
                }


                // Creates response
                response = Request.CreateResponse(HttpStatusCode.Created, person);
            }
            else
            {
                // Creates response
                response = Request.CreateErrorResponse(HttpStatusCode.BadRequest, modelDictionary);
            }

            return(response);
        }