Exemple #1
0
        public async Task PersonInsertTestAsync()
        {
            // arrange
            PersonModel       retrieved;
            PersonInsertModel inserted;

            using (var contextScope = ContextFactory.Create())
            {
                var activity = await GetTestActivityAsync();

                inserted = new PersonInsertModel()
                {
                    PersonActivityId = activity.ActivityId,
                    PersonFirstName  = "TestFirstName",
                    PersonLastName   = "TestLastName",
                    PersonEmail      = "*****@*****.**"
                };

                // act
                var newId = await _repository.InsertPersonAsync(inserted);

                retrieved = await DbContext.QuerySingleOrDefaultAsync <PersonModel>($"Select * from Person where PersonId = {newId}", commandType : CommandType.Text);
            }

            // assert
            Assert.IsTrue(inserted.PersonActivityId == retrieved.PersonActivityId, "Person ActivityId is not equal");
            Assert.IsTrue(inserted.PersonFirstName == retrieved.PersonFirstName, "Person First Name is not equal");
            Assert.IsTrue(inserted.PersonLastName == retrieved.PersonLastName, "Person Last Name is not equal");
            Assert.IsTrue(inserted.PersonEmail == retrieved.PersonEmail, "Person Email is not equal");
        }
        /// <summary>
        /// this method inserts a new person record into the Person db Table
        /// </summary>
        /// <param name="model">the model containing the details of the new person</param>
        /// <returns>the Id value of the new person record</returns>
        public async Task <ServiceResult <int> > InsertPersonAsync(PersonInsertModel model)
        {
            try
            {
                // validate
                var validationResult = _validators.ValidateInsertModel(model);

                using (var dbScope = ContextFactory.Create())
                {
                    if (validationResult.IsValid)
                    {
                        if (!(await _repository.CheckPersonExistsInActivityAsync(model.PersonActivityId, model.PersonEmail)))
                        {
                            var personId = await _repository.InsertPersonAsync(model);

                            dbScope.Transaction.Commit();
                            return(new ServiceResult <int>(personId));
                        }
                    }

                    return(new ServiceResult <int>(validationResult.ValidationErrors));
                }
            }
            catch (Exception x)
            {
                return(new ServiceResult <int>(new ServiceError()
                {
                    Location = "InsertPersonAsync", Exception = x.Message
                }));
            }
        }
        /// <summary>
        /// this method inserts a new record into the person table
        /// </summary>
        /// <param name="model">this is the data model containing the field values</param>
        /// <returns>the Id value of the new record</returns>
        public async Task <int> InsertPersonAsync(PersonInsertModel model)
        {
            var parameters = new DynamicParameters(model);

            parameters.Add("NewId", dbType: DbType.Int32, direction: ParameterDirection.Output);

            await DbContext.ExecuteAsync(StoredProcedures.PersonInsert, parameters, commandType : CommandType.StoredProcedure);

            return(parameters.Get <int>("NewId"));
        }
        /// <summary>
        /// this method validates the person model on insert
        /// </summary>
        /// <param name="model">the model containing the details of the person</param>
        /// <returns>a ValidationResults model</returns>
        public ValidationResults ValidateInsertModel(PersonInsertModel model)
        {
            var returnValue = new ValidationResults();

            if (model.PersonFirstName.Length > 50)
            {
                returnValue.ValidationErrors.Add(new ValidationError()
                {
                    FieldName = "PersonFirstName", ErrorDetail = "The first name is too long."
                });
            }
            if (model.PersonLastName.Length > 50)
            {
                returnValue.ValidationErrors.Add(new ValidationError()
                {
                    FieldName = "PersonFirstName", ErrorDetail = "The last name is too long."
                });
            }
            if (model.PersonEmail.Length > 120)
            {
                returnValue.ValidationErrors.Add(new ValidationError()
                {
                    FieldName = "PersonEmail", ErrorDetail = "The email is too long."
                });
            }
            try
            {
                var address = new MailAddress(model.PersonEmail);
            }
            catch
            {
                returnValue.ValidationErrors.Add(new ValidationError()
                {
                    FieldName = "PersonEmail", ErrorDetail = "The email is not a valid email address."
                });
            }

            return(returnValue);
        }
Exemple #5
0
        public async Task <ActionResult> InsertPersonAsync(PersonInsertModel person)
        {
            try
            {
                _logger.LogInformation("InsertPersonAsync executing...");
                var srvResult = await _service.InsertPersonAsync(person);

                if (srvResult.IsSuccessful)
                {
                    return(new OkObjectResult(srvResult.Payload));
                }
                if (srvResult.IsException)
                {
                    return(Problem(detail: srvResult.Exception.Exception, title: srvResult.Exception.Location));
                }
                return(new BadRequestObjectResult(srvResult.Errors));
            }
            catch (Exception x)
            {
                _logger.LogError(x, "InsertPersonAsync");
                return(Problem(detail: x.StackTrace, title: x.Message));
            }
        }