Example #1
0
        public HttpResponseMessage Post([FromBody] ContactModel model)
        {
            HttpResponseMessage responseMessage = new HttpResponseMessage();

            responseMessage.StatusCode = HttpStatusCode.OK;

            #region Field Validation

            ErrorsManager errorsManager = new ErrorsManager();
            if (string.IsNullOrEmpty(model.FirstName))
            {
                errorsManager.AddError(new ValidationException("First name is empty.", "FirstName"));
            }
            if (string.IsNullOrEmpty(model.LastName))
            {
                errorsManager.AddError(new ValidationException("Last name is empty.", "LastName"));
            }
            if (string.IsNullOrEmpty(model.Company))
            {
                errorsManager.AddError(new ValidationException("Company is empty.", "Company"));
            }

            if (errorsManager.HasErrors)
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError)
                {
                    Content = new StringContent(errorsManager.ToValidationString())
                });
            }
            #endregion
            try
            {
                ContactUsOpsMgmt.InsertItem(model.FirstName, model.LastName, model.Address, model.Address2, model.Company,
                                            model.City, model.State, model.Zip, model.Company, model.Phone, model.Message, model.ReasonsForContact, model.HasOptedForSubscription);

                EmailEngine emailEngine = new EmailEngine(string.Empty, "ContactTemplate", "A new contact request has been submitted.");
                emailEngine.AddAdminReceivers();
                emailEngine.AddMergingField("FirstName", model.FirstName);
                emailEngine.AddMergingField("LastName", model.LastName);
                emailEngine.AddMergingField("Address", model.Address);
                emailEngine.AddMergingField("Address2", model.Address);
                emailEngine.AddMergingField("Company", model.Company);
                emailEngine.AddMergingField("City", model.City);
                emailEngine.AddMergingField("State", model.State);
                emailEngine.AddMergingField("Zip", model.Zip);
                emailEngine.AddMergingField("Phone", model.Phone);
                emailEngine.AddMergingField("Country", model.Country);
                emailEngine.AddMergingField("Message", model.Message);
                emailEngine.AddMergingField("ReasonsForContact", model.ReasonsForContact);
                emailEngine.AddMergingField("HasOptedForSubscription", model.HasOptedForSubscription);


                emailEngine.SendEmail();

                return(responseMessage);
            }
            catch (Exception ex)
            {
                Log.Write(ex, System.Diagnostics.TraceEventType.Error);
                errorsManager.AddError(new ValidationException("Unknown error has occured.", "General"));
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError)
                {
                    Content = new StringContent(errorsManager.ToValidationString())
                });
            }
        }
        public HttpResponseMessage Post([FromBody] ImageSubmissionModel model)
        {
            HttpResponseMessage responseMessage = new HttpResponseMessage();

            responseMessage.StatusCode = HttpStatusCode.OK;

            #region Field Validation

            ErrorsManager errorsManager = new ErrorsManager();
            if (string.IsNullOrEmpty(model.FullName))
            {
                errorsManager.AddError(new ValidationException("Name is empty.", "FullName"));
            }
            if (string.IsNullOrEmpty(model.Company))
            {
                errorsManager.AddError(new ValidationException("Company is empty.", "Company"));
            }
            if (string.IsNullOrEmpty(model.Email))
            {
                errorsManager.AddError(new ValidationException("Email is empty.", "Email"));
            }
            else
            {
                if (!(Regex.IsMatch(model.Email, @"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z", RegexOptions.IgnoreCase)))
                {
                    errorsManager.AddError(new ValidationException("Email is not valid.", "Email"));
                }
            }
            if (errorsManager.HasErrors)
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError)
                {
                    Content = new StringContent(errorsManager.ToValidationString())
                });
            }
            #endregion
            try
            {
                ImageSubmissionOpsMgmt.InsertItem(model.FullName, model.Company, model.PhoneNumber, model.Email, model.Comments);

                EmailEngine emailEngine = new EmailEngine(string.Empty, "ImageSubmissionTemplate", "A new image upload request has been submitted.");
                emailEngine.AddAdminReceivers();
                emailEngine.AddMergingField("FullName", model.FullName);
                emailEngine.AddMergingField("Company", model.Company);
                emailEngine.AddMergingField("PhoneNumber", model.PhoneNumber);
                emailEngine.AddMergingField("Email", model.Email);
                emailEngine.AddMergingField("Content", model.Comments);


                emailEngine.SendEmail();

                return(responseMessage);
            }
            catch (Exception ex)
            {
                Log.Write(ex, System.Diagnostics.TraceEventType.Error);
                errorsManager.AddError(new ValidationException("Unknown error has occured.", "General"));
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError)
                {
                    Content = new StringContent(errorsManager.ToValidationString())
                });
            }
        }