예제 #1
0
        // Do NOT do this in practices, have a separate create for phone numbers.
        // The only reason I did this is we scaffolded the phone number table and I didn't want to cause conflicts.
        public int CreatePerson(string firstName, string lastName, string phone)
        {
            firstName = firstName != null?firstName.Trim() : null;

            lastName = lastName != null?lastName.Trim() : null;

            phone = phone != null?phone.Trim() : null;

            int createdID;

            PersonValidationException exception = new PersonValidationException();

            // Be a little more specific than "== null" because that doesn't account for whitespace.
            if (string.IsNullOrWhiteSpace(firstName))
            {
                exception.SubExceptions.Add(new ArgumentNullException(nameof(firstName), "First name was not provided."));
            }
            else
            {
                if (firstName.Any(x => char.IsDigit(x)))
                {
                    exception.SubExceptions.Add(new ArgumentException(nameof(firstName), "First name cannot contain numbers."));
                }
                if (firstName.Length > 50)
                {
                    exception.SubExceptions.Add(new ArgumentOutOfRangeException(nameof(firstName), "First name cannot be more than 50 characters long."));
                }
            }

            if (string.IsNullOrWhiteSpace(lastName))
            {
                exception.SubExceptions.Add(new ArgumentNullException(nameof(lastName), "Last name was not provided."));
            }
            else
            {
                if (lastName.Any(x => char.IsDigit(x)))
                {
                    exception.SubExceptions.Add(new ArgumentException(nameof(lastName), "Last name cannot contain numbers."));
                }
                if (lastName.Length > 50)
                {
                    exception.SubExceptions.Add(new ArgumentOutOfRangeException(nameof(lastName), "Last name cannot be more than 50 characters long."));
                }
            }

            if (string.IsNullOrWhiteSpace(phone))
            {
                exception.SubExceptions.Add(new ArgumentNullException(nameof(phone), "Phone number was not provided."));
            }
            else
            {
                // Check for phone number formatting (feel free to use RegEx or any other method).
                // Has to be in the else branch to avoid null reference exceptions.
                int      temp;
                string[] phoneParts = phone.Split('-');
                if (!(
                        phoneParts[0].Length == 3 &&
                        int.TryParse(phoneParts[0], out temp) &&
                        phoneParts[1].Length == 3 &&
                        int.TryParse(phoneParts[1], out temp) &&
                        phoneParts[2].Length == 4 &&
                        int.TryParse(phoneParts[2], out temp)
                        ))
                {
                    exception.SubExceptions.Add(new ArgumentException(nameof(phone), "Phone number was not in a valid format."));
                }
            }

            // If any exceptions have been generated by any validation, throw them as one bundled exception.
            if (exception.SubExceptions.Count > 0)
            {
                throw exception;
            }

            // If we're at this point, we have no exceptions, as nothing got thrown.
            // At this point, ID is 0.
            Person newPerson = new Person()
            {
                FirstName = firstName,
                LastName  = lastName
            };
            PhoneNumber newPhoneNumber = new PhoneNumber()
            {
                Number = phone,
                Person = newPerson
            };

            // Add the new model instances to the database.
            using (PersonContext context = new PersonContext())
            {
                // By adding our object to the context, we're queueing it to receive an AUTO_INCREMENT ID once saved.
                context.People.Add(newPerson);
                context.PhoneNumbers.Add(newPhoneNumber);
                // When we save it, the object and all references thereto are updated with the new ID.
                context.SaveChanges();
                // Which makes it very simple to then get the new ID to return.
                createdID = newPerson.ID;
            }
            return(createdID);
        }
예제 #2
0
        // A PUT request, semantically, overwrites an entire entity and does not update a specific field.
        public void UpdatePerson(string id, string firstName, string lastName)
        {
            id = id != null?id.Trim() : null;

            firstName = firstName != null?firstName.Trim() : null;

            lastName = lastName != null?lastName.Trim() : null;

            int idParsed = 0;

            using (PersonContext context = new PersonContext())
            {
                PersonValidationException exception = new PersonValidationException();

                if (string.IsNullOrWhiteSpace(id))
                {
                    exception.SubExceptions.Add(new ArgumentNullException(nameof(id), "ID was not provided."));
                }
                else
                {
                    if (!int.TryParse(id, out idParsed))
                    {
                        exception.SubExceptions.Add(new ArgumentException(nameof(id), "ID was not valid."));
                    }
                    else
                    {
                        if (context.People.Where(x => x.ID == idParsed).Count() != 1)
                        {
                            exception.SubExceptions.Add(new NullReferenceException("Person with that ID does not exist."));
                        }
                    }
                }
                if (string.IsNullOrWhiteSpace(firstName))
                {
                    exception.SubExceptions.Add(new ArgumentNullException(nameof(firstName), "First name was not provided."));
                }
                else
                {
                    if (firstName.Any(x => char.IsDigit(x)))
                    {
                        exception.SubExceptions.Add(new ArgumentException(nameof(firstName), "First name cannot contain numbers."));
                    }
                    if (firstName.Length > 50)
                    {
                        exception.SubExceptions.Add(new ArgumentOutOfRangeException(nameof(firstName), "First name cannot be more than 50 characters long."));
                    }
                }

                if (string.IsNullOrWhiteSpace(lastName))
                {
                    exception.SubExceptions.Add(new ArgumentNullException(nameof(lastName), "Last name was not provided."));
                }
                else
                {
                    if (lastName.Any(x => char.IsDigit(x)))
                    {
                        exception.SubExceptions.Add(new ArgumentException(nameof(lastName), "Last name cannot contain numbers."));
                    }
                    if (lastName.Length > 50)
                    {
                        exception.SubExceptions.Add(new ArgumentOutOfRangeException(nameof(lastName), "Last name cannot be more than 50 characters long."));
                    }
                }

                // If any exceptions have been generated by any validation, throw them as one bundled exception.
                if (exception.SubExceptions.Count > 0)
                {
                    throw exception;
                }

                // If we're at this point, we have no exceptions, as nothing got thrown.
                Person target = context.People.Where(x => x.ID == idParsed).Single();
                target.FirstName = firstName;
                target.LastName  = lastName;
                context.SaveChanges();
            }
        }