Example #1
0
func CreateTechSupport(rw http.ResponseWriter, req *http.Request, enc encoding.Encoder, params martini.Params, dtx *apicontext.DataContext) string {
	contType := req.Header.Get("Content-Type")

	var t techSupport.TechSupport
	var err error
	contactTypeID, err := strconv.Atoi(params["contactReceiverTypeID"]) //to whom the emails go
	sendEmail, err := strconv.ParseBool(params["sendEmail"])

	// if contType == "application/json" {
	if strings.Contains(contType, "application/json") {
		//json
		requestBody, err := ioutil.ReadAll(req.Body)
		if err != nil {
			apierror.GenerateError("Error parsing JSON.", err, rw, req)
		}

		err = json.Unmarshal(requestBody, &t)
		if err != nil {
			apierror.GenerateError("Error unmarshalling request body.", err, rw, req)
		}
	} else {
		//else, form
		t.VehicleMake = req.FormValue("vehicle_make")
		t.VehicleModel = req.FormValue("vehicle_model")
		t.VehicleYear, err = strconv.Atoi(req.FormValue("vehicle_year"))
		d, err := time.Parse(timeFormat, req.FormValue("purchase_date"))
		t.PurchaseDate = d
		t.PurchasedFrom = req.FormValue("purchased_from")
		t.DealerName = req.FormValue("dealer_name")
		t.ProductCode = req.FormValue("product_code")
		t.DateCode = req.FormValue("date_code")
		t.Issue = req.FormValue("issue")

		t.Contact.FirstName = req.FormValue("first_name")
		t.Contact.LastName = req.FormValue("last_name")
		t.Contact.Email = req.FormValue("email")
		t.Contact.Phone = req.FormValue("phone")
		t.Contact.Subject = req.FormValue("subject")
		t.Contact.Message = req.FormValue("message")
		t.Contact.Type = req.FormValue("type")
		t.Contact.Address1 = req.FormValue("address1")
		t.Contact.Address2 = req.FormValue("address2")
		t.Contact.City = req.FormValue("city")
		t.Contact.State = req.FormValue("state")
		t.Contact.PostalCode = req.FormValue("postal_code")
		t.Contact.Country = req.FormValue("country")
		if err != nil {
			apierror.GenerateError("Error parsing purchase date.", err, rw, req)
		}
	}
	t.BrandID = dtx.BrandID
	err = t.Create()
	if err != nil {
		apierror.GenerateError("Error creating Tech Support.", err, rw, req)
	}

	if sendEmail == true {
		//Send Email
		body :=
			"Name: " + t.Contact.FirstName + " " + t.Contact.LastName + "\n" +
				"Email: " + t.Contact.Email + "\n" +
				"Phone: " + t.Contact.Phone + "\n" +
				"Make: " + t.VehicleMake + "\n" +
				"Model: " + t.VehicleModel + "\n" +
				"Year: " + strconv.Itoa(t.VehicleYear) + "\n" +
				"Purchase Date: " + t.PurchaseDate.String() + "\n" +
				"Purchased From: " + t.PurchasedFrom + "\n" +
				"Dealer Name: " + t.DealerName + "\n" +
				"Product Code: " + t.ProductCode + "\n" +
				"Date Code: " + t.DateCode + "\n\n" +
				"Issue: " + t.Issue + "\n"

		var ct contact.ContactType
		ct.ID = contactTypeID
		subject := "Email from Tech Support Request Form"
		err = contact.SendEmail(ct, subject, body) //contact type id, subject, techSupport
		if err != nil {
			apierror.GenerateError("Error sending email to Tech Support.", err, rw, req)
		}
	}
	//Return JSON
	return encoding.Must(enc.Encode(t))
}
Example #2
0
func CreateWarranty(rw http.ResponseWriter, req *http.Request, enc encoding.Encoder, params martini.Params) string {
	contType := req.Header.Get("Content-Type")
	var w warranty.Warranty
	var err error

	contactTypeID, err := strconv.Atoi(params["contactReceiverTypeID"]) //to whom the emails go
	if err != nil {
		apierror.GenerateError("Trouble parsing contact type ID.", err, rw, req)
		return ""
	}
	sendEmail, err := strconv.ParseBool(params["sendEmail"])
	if err != nil {
		apierror.GenerateError("Trouble parsing send email.", err, rw, req)
		return ""
	}
	if strings.Contains(contType, "application/json") {
		//json
		requestBody, err := ioutil.ReadAll(req.Body)
		if err != nil {
			apierror.GenerateError("Trouble reading request body for creating warranty", err, rw, req)
			return ""
		}

		err = json.Unmarshal(requestBody, &w)
		if err != nil {
			apierror.GenerateError("Trouble unmarshalling request body for creating warranty", err, rw, req)
			return ""
		}

	} else {
		//else, form
		w.PartNumber, err = strconv.Atoi(req.FormValue("part_number"))
		w.OldPartNumber = req.FormValue("old_part_number")
		date, err := time.Parse(timeFormat, req.FormValue("date"))
		if err != nil {
			apierror.GenerateError("Trouble creating warranty", err, rw, req)
			return ""
		}
		w.Date = &date
		w.SerialNumber = req.FormValue("serial_number")

		w.Contact.FirstName = req.FormValue("first_name")
		w.Contact.LastName = req.FormValue("last_name")
		w.Contact.Email = req.FormValue("email")
		w.Contact.Phone = req.FormValue("phone")
		w.Contact.Type = req.FormValue("type")
		w.Contact.Address1 = req.FormValue("address1")
		w.Contact.Address2 = req.FormValue("address2")
		w.Contact.City = req.FormValue("city")
		w.Contact.State = req.FormValue("state")
		w.Contact.PostalCode = req.FormValue("postal_code")
		w.Contact.Country = req.FormValue("country")
	}
	err = w.Create()
	if err != nil {
		apierror.GenerateError("Trouble creating warranty", err, rw, req)
		return ""
	}
	if sendEmail == true {
		//Send Email
		body :=
			"Name: " + w.Contact.FirstName + " " + w.Contact.LastName + "\n" +
				"Email: " + w.Contact.Email + "\n" +
				"Phone: " + w.Contact.Phone + "\n" +
				"Serial Number: " + w.SerialNumber + "\n" +
				"Date: " + w.Date.String() + "\n" +
				"Part Number: " + strconv.Itoa(w.PartNumber) + "\n"

		var ct contact.ContactType
		ct.ID = contactTypeID
		subject := "Email from Warranty Applications Form"
		err = contact.SendEmail(ct, subject, body) //contact type id, subject, techSupport
		if err != nil {
			apierror.GenerateError("Trouble sending email to receivers while creating warranty", err, rw, req)
			return ""
		}
	}
	//Return JSON
	return encoding.Must(enc.Encode(w))
}