public int InsertParcels(ParcelDetails Parcel, long parcelEnquiryId) { int result = 0; try { string query = @"INSERT INTO Parcels (`Length`, `Width`, `Height`, `Weight`, `ParcelEnquiriesId` ) VALUES(@Length, @Width, @Height, @Weight, @ParcelEnquiriesId);"; using (MySqlConnection mysqlconnection = new MySqlConnection(connectionString)) { mysqlconnection.Open(); MySqlCommand command = new MySqlCommand(query, mysqlconnection); command.Parameters.AddWithValue("@Length", Parcel.length); command.Parameters.AddWithValue("@Width", Parcel.width); command.Parameters.AddWithValue("@Height", Parcel.height); command.Parameters.AddWithValue("@Weight", Parcel.weight); command.Parameters.AddWithValue("@ParcelEnquiriesId", parcelEnquiryId); result = command.ExecuteNonQuery(); return(result); } } catch (Exception e) { // Set-up logging errors to the Database } return(result); }
[HttpPost("/calcCost")] // using http post to display the shipping cost to the user // ^^this line is the path in the url public ActionResult Item(int length, int width, int height) // ^^is "Item" becasuse it must match the name of the "backend" file. The parameters are the auto-implemented properties { ParcelDetails shipEstimate = new ParcelDetails(length, width, height); // ^^ it gets these params because it is the user input given on the form.cshtml page shipEstimate.CalcVolume(); // ^is "shipEstimate" because this is the new instance of parcelDetails, therefore CalcVolume acts on this object shipEstimate.CalcShippingCost(); return(View("Index", shipEstimate)); // ^^ In the View shipEstimate must be oassedin as an argumanet because the .cshtml wont know which Model were referencing without it. shipEstimate is the specific instance of the ParcelDetails class that we will interact with in our .cshtml, by calling @Model.ShippingCost. }
protected string ParcelEnquiry([FromBody] ParcelEnquiry parcelEnquiry) { long parcelEnquiryId = dataManager.SaveEnquiry(parcelEnquiry); for (int i = 0; i < parcelEnquiry.AllParcels.Count(); i++) { dataManager.InsertParcels(parcelEnquiry.AllParcels[i], parcelEnquiryId); } // Send Email StringBuilder emailBody = new StringBuilder(); emailBody.Append(parcelEnquiry.fromCountry); emailBody.Append("<br />"); emailBody.Append(parcelEnquiry.fromAddress); emailBody.Append("<br />"); emailBody.Append(parcelEnquiry.toCountry); emailBody.Append("<br />"); emailBody.Append(parcelEnquiry.toAddress); emailBody.Append("<br />"); emailBody.Append(parcelEnquiry.contactNumber); emailBody.Append("<br />"); emailBody.Append(parcelEnquiry.parcelDescription); emailBody.Append("<br />"); emailBody.Append(parcelEnquiry.parcelQuantity); for (int q = 0; q < parcelEnquiry.AllParcels.Count(); q++) { ParcelDetails parcela = parcelEnquiry.AllParcels[q]; emailBody.AppendFormat("Height:{0}", parcela.height); emailBody.AppendFormat("Length:{0}", parcela.length); emailBody.AppendFormat("Width:{0}", parcela.width); emailBody.AppendFormat("Weight:{0}", parcela.weight); emailBody.Append("<br />"); emailBody.Append("<br />"); emailBody.Append("<br />"); } MailMessage mailMessage = new MailMessage("*****@*****.**", "*****@*****.**"); mailMessage.Subject = "Booking"; mailMessage.Body = emailBody.ToString(); mailMessage.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(emailBody.ToString(), new System.Net.Mime.ContentType("text/html"))); var client = new SmtpClient("smtp.gmail.com", 587); client.Credentials = new System.Net.NetworkCredential() { //Credentials = new NetworkCredential("*****@*****.**", "Mad2behere"), //EnableSsl = true UserName = "******", Password = "******" }; try { client.EnableSsl = true; client.Send(mailMessage); } catch (Exception e) { string error = e.Message; } return(JsonConvert.SerializeObject(parcelEnquiry.contactNumber)); } // End Method