Bug fixes on PUT /api/template

Hotfix for buffer issue in worker.go.. going to find out the root cause and fix it properly
This commit is contained in:
Jordan 2014-08-07 05:48:52 -05:00
parent 86dca67a5a
commit 33b9ec2196
5 changed files with 28 additions and 13 deletions

View file

@ -220,6 +220,7 @@ func API_Templates_Id(w http.ResponseWriter, r *http.Request) {
id, _ := strconv.ParseInt(vars["id"], 0, 64)
t, err := models.GetTemplate(id, ctx.Get(r, "user_id").(int64))
if checkError(err, w, "Template not found", http.StatusNotFound) {
Logger.Println(err)
return
}
switch {

View file

@ -141,7 +141,7 @@ func PostCampaign(c *Campaign, uid int64) error {
for _, g := range c.Groups {
// Insert a result for each target in the group
for _, t := range g.Targets {
r := Result{Email: t.Email, Status: STATUS_UNKNOWN, CampaignId: c.Id, UserId: c.UserId}
r := Result{Email: t.Email, Status: STATUS_UNKNOWN, CampaignId: c.Id, UserId: c.UserId, FirstName: t.FirstName, LastName: t.LastName}
r.GenerateId()
err = db.Save(&r).Error
if err != nil {

View file

@ -14,6 +14,8 @@ type Result struct {
UserId int64 `json:"-"`
RId string `json:"id"`
Email string `json:"email"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Status string `json:"status" sql:"not null"`
}

View file

@ -3,6 +3,8 @@ package models
import (
"errors"
"time"
"github.com/jinzhu/gorm"
)
type Template struct {
@ -39,10 +41,13 @@ func GetTemplates(uid int64) ([]Template, error) {
}
for i, _ := range ts {
err = db.Where("template_id=?", ts[i].Id).Find(&ts[i].Attachments).Error
if err != nil {
if err != nil && err != gorm.RecordNotFound {
Logger.Println(err)
return ts, err
}
if err == gorm.RecordNotFound {
err = nil
}
}
return ts, err
}
@ -56,10 +61,13 @@ func GetTemplate(id int64, uid int64) (Template, error) {
return t, err
}
err = db.Where("template_id=?", t.Id).Find(&t.Attachments).Error
if err != nil {
if err != nil && err != gorm.RecordNotFound {
Logger.Println(err)
return t, err
}
if err == gorm.RecordNotFound {
err = nil
}
return t, err
}
@ -98,11 +106,14 @@ func PostTemplate(t *Template) error {
// Per the PUT Method RFC, it presumes all data for a template is provided.
func PutTemplate(t *Template) error {
// Delete all attachments, and replace with new ones
err := db.Where("template_id=?", t.Id).Delete(&Attachment{}).Error
if err != nil {
err := db.Debug().Where("template_id=?", t.Id).Delete(&Attachment{}).Error
if err != nil && err != gorm.RecordNotFound {
Logger.Println(err)
return err
}
if err == gorm.RecordNotFound {
err = nil
}
for i, _ := range t.Attachments {
t.Attachments[i].TemplateId = t.Id
err := db.Save(&t.Attachments[i]).Error
@ -111,7 +122,7 @@ func PutTemplate(t *Template) error {
return err
}
}
err = db.Where("id=?", t.Id).Save(t).Error
err = db.Debug().Where("id=?", t.Id).Save(t).Error
if err != nil {
Logger.Println(err)
return err

View file

@ -49,27 +49,28 @@ func processCampaign(c *models.Campaign) {
}
for _, t := range c.Results {
// Parse the templates
var buff bytes.Buffer
var html_buff bytes.Buffer
var text_buff bytes.Buffer
tmpl, err := template.New("html_template").Parse(c.Template.HTML)
if err != nil {
Logger.Println(err)
}
err = tmpl.Execute(&buff, t)
err = tmpl.Execute(&html_buff, t)
if err != nil {
Logger.Println(err)
}
e.HTML = buff.Bytes()
buff.Reset()
e.HTML = html_buff.Bytes()
//buff.Reset()
tmpl, err = template.New("text_template").Parse(c.Template.Text)
if err != nil {
Logger.Println(err)
}
err = tmpl.Execute(&buff, t)
err = tmpl.Execute(&text_buff, t)
if err != nil {
Logger.Println(err)
}
e.Text = buff.Bytes()
buff.Reset()
e.Text = text_buff.Bytes()
//buff.Reset()
Logger.Println("Creating email using template")
e.To = []string{t.Email}
err = e.Send(c.SMTP.Host, auth)