Beispiel #1
0
        public IHttpActionResult SavePhone([FromBody] PhoneForCreationDto newPhone)
        {
            var response = new PhoneResponse();

            if (string.IsNullOrEmpty(newPhone?.PhoneNumber))
            {
                response.Meta.HttpStatusCode = (int)HttpStatusCode.BadRequest;
                response.Meta.ErrorMessage   = "Phone Number is null!";
                response.Meta.ErrorType      = "DataValidationError";

                return(ResponseMessage(
                           Request.CreateResponse(
                               HttpStatusCode.BadRequest,
                               response)
                           ));
            }

            if (!ModelState.IsValid)
            {
                response.Meta.HttpStatusCode = (int)HttpStatusCode.BadRequest;
                response.Meta.ErrorMessage   = "Phone object contains Invalid data";
                response.Meta.ErrorType      = "DataValidationError";

                return(ResponseMessage(
                           Request.CreateResponse(
                               HttpStatusCode.BadRequest,
                               response)
                           ));
            }

            // parse the incoming string and see if it's a valid phone number
            var parsedPhone = PhoneHelper.Parse(newPhone.PhoneNumber);

            if (parsedPhone == null)
            {
                response.Meta.HttpStatusCode = (int)HttpStatusCode.BadRequest;
                response.Meta.ErrorMessage   = $"Phone Number = {newPhone.PhoneNumber} is Invalid!";
                response.Meta.ErrorType      = "InvalidDataError";

                return(ResponseMessage(
                           Request.CreateResponse(
                               HttpStatusCode.BadRequest,
                               response)));
            }

            try
            {
                if (BondsBuddyDataStore.Current.Phones.Exists(p => p.RawPhoneNumber == parsedPhone.RawPhoneNumber))
                {
                    response.Meta.HttpStatusCode = (int)HttpStatusCode.Conflict;
                    response.Meta.ErrorMessage   = $"Phone object with Phone Number = {newPhone.PhoneNumber} already exists in database!";
                    response.Meta.ErrorType      = "DataValidationError";

                    return(ResponseMessage(
                               Request.CreateResponse(
                                   HttpStatusCode.Conflict,
                                   response)));
                }

                int maxPhoneId = BondsBuddyDataStore.Current.Phones.Any() ? BondsBuddyDataStore.Current.Phones.Max(a => a.Id) : 0;

                var phoneToCreate = Mapper.Map <CanonicalPhoneNumber, Phone>(parsedPhone);

                phoneToCreate.Id        = ++maxPhoneId;
                phoneToCreate.PhoneName = newPhone.PhoneName;

                BondsBuddyDataStore.Current.Phones.Add(phoneToCreate);

                var newlyCreatedPhone = BondsBuddyDataStore.Current.Phones.First(a => a.Id == phoneToCreate.Id);

                response.Meta.HttpStatusCode = (int)HttpStatusCode.OK;

                var newlyCreatedPhoneDto = Mapper.Map <Phone, PhoneDto>(newlyCreatedPhone);
                response.Phone = newlyCreatedPhoneDto;

                return(CreatedAtRoute("GetPhone", new { id = phoneToCreate.Id }, response));
            }
            catch (Exception exception)
            {
                // TODO: Log Exception details
                Console.WriteLine(exception.Message);

                response.Meta.HttpStatusCode = (int)HttpStatusCode.InternalServerError;
                response.Meta.ErrorMessage   =
                    "Oops! An unexpected error occurred. Our DevOps is investigating. Please try again later.";
                response.Meta.ErrorType = "ApiServerError";

                return(ResponseMessage(
                           Request.CreateResponse(
                               HttpStatusCode.InternalServerError,
                               response)));
            }
        }