Exemple #1
0
func BuyRequestHandler(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	decoder := json.NewDecoder(r.Body)
	var request MainRequest
	err := decoder.Decode(&request)
	if err != nil {
		c.Errorf("Error decoding main request json")
	}
	listing, err := craigslist.NewListing(c, request.Cl_url)
	if err != nil {
		c.Errorf("%s", err)
	}
	request.Cl_title = listing.Title
	request.Cl_price = listing.Price

	key, err := datastore.Put(c, datastore.NewIncompleteKey(c, "request", nil), &request)
	if err != nil {
		c.Errorf("Error putting purchase request into database: %s", err)
	}
	emailStruct := ReqEmail{
		BuyerName:    request.Pm_dropoff_name,
		ListingTitle: listing.Title,
		Price:        request.Offer,
		AcceptUrl:    createConfirmationURL(c, key),
	}
	c.Infof("Email url: %s", emailStruct.AcceptUrl)
	emailTemplate, err := template.ParseFiles("new_offer_email.html")
	if err != nil {
		c.Errorf("Error parsing template %s", err)
	}
	b := new(bytes.Buffer)
	emailTemplate.ExecuteTemplate(b, "email", emailStruct)
	msg := &mail.Message{
		Sender:   "craig-o-mation <*****@*****.**>",
		To:       []string{request.Cl_email},
		Subject:  "Purchase Request for \"" + listing.Title + "\"",
		HTMLBody: string(b.Bytes()),
	}
	c.Debugf("Email body: %s", msg.Body)
	if err := mail.Send(c, msg); err != nil {
		c.Errorf("Couldn't send email: %v", err)
	}
}
Exemple #2
0
func GetCL(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	b, err := ioutil.ReadAll(r.Body)
	if err != nil {
		c.Errorf("%s", err)
	}
	bstr := string(b)
	if bstr == "" {
		return
	}
	listing, err := craigslist.NewListing(c, bstr)
	if err != nil {
		c.Errorf("%s", err)
	}

	err = json.NewEncoder(w).Encode(&listing)
	if err != nil {
		c.Errorf("%s", err)
	}
}