コード例 #1
0
        // POST api/<controller>
        //http://localhost:20487/api/Testimonials
        //Content-Type: text/json
        //{"Name":"test","Comment":"test comment","Date":"4014","Password":"******"}
        public string Post([FromBody] Testimonial value)
        {
            string response = "Failed to add testimonial.";


            if (value != null && value.Name != null)
            {
                int newID = -1;
                try
                {
                    newID = AddTestimonialToDB(value);

                    if (newID >= 0)
                    {
                        value.ID = newID;

                        response = string.Format("Successfully added testimonial {0}, {1}. Thankyou {2}. ", value.ID, value.Comment, value.Name);
                    }
                    else
                    {
                        ErrorHandler.Write("Error adding the Testimonial.");
                    }
                }
                catch (Exception ex)
                {
                    ErrorHandler.Write("Error adding the Testimonial.", ex);
                }
            }
            else
            {
                ErrorHandler.Write("Error adding the Testimonial. No Name for testimonial.");
            }

            return(response);
        }
        // GET: api/UserDetails/?cypher=oi3O2UZtGo3VjfKW9w7NHB1i35o5M6PmmwJn9NkOamxdCBkNHwC1687mBUPf46bn
        //original string must be encoded first to remove whitepace or else the Decrypt breaks.
        public Testimonial Get(string cypher)
        {
            try
            {
                var formattedCypher = cypher.Replace(' ', '+');

                var decrypted = EncryptDecrypt.Decrypt(formattedCypher);
                var userDetails = decrypted.Split('|');

                if (userDetails == null)
                    ErrorHandler.Write("Could not decrpyt. Cypher is empty.");

                if (userDetails.Length > 0)
                {
                    string name = string.Empty, email = string.Empty, date = string.Empty;
                    if (userDetails.Length > 1)
                        name = userDetails[0];
                    if (userDetails.Length > 2)
                        email = userDetails[1];
                    if (userDetails.Length > 3)
                        date = userDetails[2];

                    var result = new Testimonial() { Name = name, Date = date };
                    return result;
                }
            }
            catch (Exception e)
            {
                ErrorHandler.Write("Error Decrypting the Testimonial user details.", e);
            }
            return null;
        }
コード例 #3
0
        private int AddTestimonialToDB(Testimonial testimonial)
        {
            var result = -1;

            if (testimonial.Date == null)
            {
                testimonial.Date = DateTime.Today.Year.ToString();
            }

            var connectionString = ConnectionString();

            using (SqlConnection sqlConnection1 = new SqlConnection(connectionString))
            {
                SqlCommand cmd = new SqlCommand();

                cmd.CommandText = "AddTestimonial";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection  = sqlConnection1;

                sqlConnection1.Open();

                cmd.Parameters.Add("@Name", SqlDbType.VarChar, 50).Value     = testimonial.Name;
                cmd.Parameters.Add("@Comment", SqlDbType.VarChar, 400).Value = testimonial.Comment;
                cmd.Parameters.Add("@Date", SqlDbType.VarChar, 50).Value     = testimonial.Date;


                result = (int)cmd.ExecuteScalar();
                //result = (int)cmd.ExecuteNonQuery();
            }

            return(result);
        }
        // GET api/<controller>
        public IEnumerable<Testimonial> Get()
        {
            var result = new List<Testimonial>();

            try
            {

                var connectionString = ConnectionString();

                using (SqlConnection sqlConnection1 = new SqlConnection(connectionString))
                {

                    SqlCommand cmd = new SqlCommand();
                    SqlDataReader reader;

                    cmd.CommandText = "GetAllTestimonials";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Connection = sqlConnection1;

                    sqlConnection1.Open();



                    using (reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            int index = reader.GetInt32(0);
                            string name = reader.GetString(1);
                            string comment = reader.GetString(2);
                            string date = reader.GetString(3);

                            var testimonial = new Testimonial();
                            testimonial.ID = index;
                            testimonial.Name = name;
                            testimonial.Comment = comment;
                            testimonial.Date = date;

                            result.Add(testimonial);

                        }
                    }
                }

            }
            catch (Exception ex)
            {
                ErrorHandler.Write("something went wrong", ex);
                return null;
            }

            return result;



        }
コード例 #5
0
 // POST: api/Email
 public void Post([FromBody] Testimonial testimonial)
 {
     try
     {
         //Generate email.
         EmailSender.Send(testimonial, Request.RequestUri.Host);
     }
     catch (Exception e)
     {
         ErrorHandler.Write("Error creating email.", e);
     }
 }
コード例 #6
0
        // GET api/<controller>/5
        public Testimonial Get(int id)
        {
            Testimonial result = null;

            var testimonials = Get();

            if (testimonials.Where(t => t.ID == id).Any())
            {
                result = testimonials.Where(t => t.ID == id).Single();
            }


            return(result);
        }
コード例 #7
0
        // GET api/<controller>
        public IEnumerable <Testimonial> Get()
        {
            var result = new List <Testimonial>();

            try
            {
                var connectionString = ConnectionString();

                using (SqlConnection sqlConnection1 = new SqlConnection(connectionString))
                {
                    SqlCommand    cmd = new SqlCommand();
                    SqlDataReader reader;

                    cmd.CommandText = "GetAllTestimonials";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Connection  = sqlConnection1;

                    sqlConnection1.Open();



                    using (reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            int    index   = reader.GetInt32(0);
                            string name    = reader.GetString(1);
                            string comment = reader.GetString(2);
                            string date    = reader.GetString(3);

                            var testimonial = new Testimonial();
                            testimonial.ID      = index;
                            testimonial.Name    = name;
                            testimonial.Comment = comment;
                            testimonial.Date    = date;

                            result.Add(testimonial);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorHandler.Write("something went wrong", ex);
                return(null);
            }

            return(result);
        }
コード例 #8
0
        public static void Send(Testimonial userDetails, string rootUrl)
        {
            string name  = userDetails.Name;
            string date  = userDetails.Date;
            string email = userDetails.Email;

            string value     = userDetails.ToString();
            var    encrypted = EncryptDecrypt.Encrypt(value);

            string url = rootUrl + "/?" + encrypted;

            MailAddress from = new MailAddress("*****@*****.**",
                                               "Ballyglass Thatched Cottage",
                                               System.Text.Encoding.UTF8);

            SmtpClient client = new SmtpClient()
            {
                Host                  = "smtp.gmail.com",
                Port                  = 587,
                EnableSsl             = true,
                DeliveryMethod        = System.Net.Mail.SmtpDeliveryMethod.Network,
                UseDefaultCredentials = false,
                Credentials           = new NetworkCredential(from.Address, "ballyglass1A")
            };



            MailAddress to = new MailAddress(email);

            MailMessage message = new MailMessage(from, to);

            message.Body    = string.Format("Dear {0}, Thank you for Visiting us at Ballyglass Thatched Cottage. Please add a comment about your visit by visiting our page: {1}. Thank you, Niall Fallon.", name, url);
            message.Subject = "Ballyglass Thatched Cottage - Thank You!";
            //message.SubjectEncoding = System.Text.Encoding.UTF8;

            client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);


            client.SendAsync(message, email);

            //client.SendAsyncCancel();

            //message.Dispose();
        }
コード例 #9
0
        public static void Send(Testimonial userDetails, string rootUrl)
        {
            string name = userDetails.Name;
            string date = userDetails.Date;
            string email = userDetails.Email;

            string value = userDetails.ToString();
            var encrypted = EncryptDecrypt.Encrypt(value);

            string url = rootUrl + "/?" + encrypted;
                
            MailAddress from = new MailAddress("*****@*****.**",
               "Ballyglass Thatched Cottage",
            System.Text.Encoding.UTF8);

            SmtpClient client = new SmtpClient()
            {
                Host = "smtp.gmail.com",
                Port = 587,
                EnableSsl = true,
                DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network,
                UseDefaultCredentials = false,
                Credentials = new NetworkCredential(from.Address, "ballyglass1A")
            };



            MailAddress to = new MailAddress(email);

            MailMessage message = new MailMessage(from, to);
            message.Body = string.Format("Dear {0}, Thank you for Visiting us at Ballyglass Thatched Cottage. Please add a comment about your visit by visiting our page: {1}. Thank you, Niall Fallon.", name, url);
            message.Subject = "Ballyglass Thatched Cottage - Thank You!";
            //message.SubjectEncoding = System.Text.Encoding.UTF8;

            client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);

            
            client.SendAsync(message, email);

            //client.SendAsyncCancel();
         
            //message.Dispose();
            
        }
コード例 #10
0
        // GET: api/UserDetails/?cypher=oi3O2UZtGo3VjfKW9w7NHB1i35o5M6PmmwJn9NkOamxdCBkNHwC1687mBUPf46bn
        //original string must be encoded first to remove whitepace or else the Decrypt breaks.
        public Testimonial Get(string cypher)
        {
            try
            {
                var formattedCypher = cypher.Replace(' ', '+');

                var decrypted   = EncryptDecrypt.Decrypt(formattedCypher);
                var userDetails = decrypted.Split('|');

                if (userDetails == null)
                {
                    ErrorHandler.Write("Could not decrpyt. Cypher is empty.");
                }

                if (userDetails.Length > 0)
                {
                    string name = string.Empty, email = string.Empty, date = string.Empty;
                    if (userDetails.Length > 1)
                    {
                        name = userDetails[0];
                    }
                    if (userDetails.Length > 2)
                    {
                        email = userDetails[1];
                    }
                    if (userDetails.Length > 3)
                    {
                        date = userDetails[2];
                    }

                    var result = new Testimonial()
                    {
                        Name = name, Date = date
                    };
                    return(result);
                }
            }
            catch (Exception e)
            {
                ErrorHandler.Write("Error Decrypting the Testimonial user details.", e);
            }
            return(null);
        }
        private int AddTestimonialToDB(Testimonial testimonial)
        {
            var result = -1;

            if (testimonial.Date == null)
                testimonial.Date = DateTime.Today.Year.ToString();

            var connectionString = ConnectionString();

            using (SqlConnection sqlConnection1 = new SqlConnection(connectionString))
            {
                 
                SqlCommand cmd = new SqlCommand();

                cmd.CommandText = "AddTestimonial";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = sqlConnection1;

                sqlConnection1.Open();

                cmd.Parameters.Add("@Name", SqlDbType.VarChar, 50).Value = testimonial.Name;
                cmd.Parameters.Add("@Comment", SqlDbType.VarChar, 400).Value = testimonial.Comment;
                cmd.Parameters.Add("@Date", SqlDbType.VarChar, 50).Value = testimonial.Date;


                result = (int)cmd.ExecuteScalar(); 
                //result = (int)cmd.ExecuteNonQuery();


            }

            return result;
        }