Updating gorm constructs to support gorm v1.0 released yesterday.

pull/207/head
Jordan Wright 2016-03-08 22:37:55 -06:00
parent 095da00a05
commit d43a888b26
7 changed files with 19 additions and 19 deletions

View File

@ -130,7 +130,7 @@ func API_Groups(w http.ResponseWriter, r *http.Request) {
return return
} }
_, err = models.GetGroupByName(g.Name, ctx.Get(r, "user_id").(int64)) _, err = models.GetGroupByName(g.Name, ctx.Get(r, "user_id").(int64))
if err != gorm.RecordNotFound { if err != gorm.ErrRecordNotFound {
JSONResponse(w, models.Response{Success: false, Message: "Group name already in use"}, http.StatusConflict) JSONResponse(w, models.Response{Success: false, Message: "Group name already in use"}, http.StatusConflict)
return return
} }
@ -204,7 +204,7 @@ func API_Templates(w http.ResponseWriter, r *http.Request) {
return return
} }
_, err = models.GetTemplateByName(t.Name, ctx.Get(r, "user_id").(int64)) _, err = models.GetTemplateByName(t.Name, ctx.Get(r, "user_id").(int64))
if err != gorm.RecordNotFound { if err != gorm.ErrRecordNotFound {
JSONResponse(w, models.Response{Success: false, Message: "Template name already in use"}, http.StatusConflict) JSONResponse(w, models.Response{Success: false, Message: "Template name already in use"}, http.StatusConflict)
return return
} }
@ -288,7 +288,7 @@ func API_Pages(w http.ResponseWriter, r *http.Request) {
} }
// Check to make sure the name is unique // Check to make sure the name is unique
_, err = models.GetPageByName(p.Name, ctx.Get(r, "user_id").(int64)) _, err = models.GetPageByName(p.Name, ctx.Get(r, "user_id").(int64))
if err != gorm.RecordNotFound { if err != gorm.ErrRecordNotFound {
JSONResponse(w, models.Response{Success: false, Message: "Page name already in use"}, http.StatusConflict) JSONResponse(w, models.Response{Success: false, Message: "Page name already in use"}, http.StatusConflict)
Logger.Println(err) Logger.Println(err)
return return
@ -365,7 +365,7 @@ func API_SMTP(w http.ResponseWriter, r *http.Request) {
} }
// Check to make sure the name is unique // Check to make sure the name is unique
_, err = models.GetSMTPByName(s.Name, ctx.Get(r, "user_id").(int64)) _, err = models.GetSMTPByName(s.Name, ctx.Get(r, "user_id").(int64))
if err != gorm.RecordNotFound { if err != gorm.ErrRecordNotFound {
JSONResponse(w, models.Response{Success: false, Message: "SMTP name already in use"}, http.StatusConflict) JSONResponse(w, models.Response{Success: false, Message: "SMTP name already in use"}, http.StatusConflict)
Logger.Println(err) Logger.Println(err)
return return
@ -540,7 +540,7 @@ func API_Send_Test_Email(w http.ResponseWriter, r *http.Request) {
} else { } else {
// Get the Template requested by name // Get the Template requested by name
s.Template, err = models.GetTemplateByName(s.Template.Name, ctx.Get(r, "user_id").(int64)) s.Template, err = models.GetTemplateByName(s.Template.Name, ctx.Get(r, "user_id").(int64))
if err == gorm.RecordNotFound { if err == gorm.ErrRecordNotFound {
Logger.Printf("Error - Template %s does not exist", s.Template.Name) Logger.Printf("Error - Template %s does not exist", s.Template.Name)
JSONResponse(w, models.Response{Success: false, Message: models.ErrTemplateNotFound.Error()}, http.StatusBadRequest) JSONResponse(w, models.Response{Success: false, Message: models.ErrTemplateNotFound.Error()}, http.StatusBadRequest)
} else if err != nil { } else if err != nil {
@ -554,7 +554,7 @@ func API_Send_Test_Email(w http.ResponseWriter, r *http.Request) {
if err := s.SMTP.Validate(); err != nil { if err := s.SMTP.Validate(); err != nil {
// Otherwise get the SMTP requested by name // Otherwise get the SMTP requested by name
s.SMTP, err = models.GetSMTPByName(s.SMTP.Name, ctx.Get(r, "user_id").(int64)) s.SMTP, err = models.GetSMTPByName(s.SMTP.Name, ctx.Get(r, "user_id").(int64))
if err == gorm.RecordNotFound { if err == gorm.ErrRecordNotFound {
Logger.Printf("Error - Sending profile %s does not exist", s.SMTP.Name) Logger.Printf("Error - Sending profile %s does not exist", s.SMTP.Name)
JSONResponse(w, models.Response{Success: false, Message: models.ErrSMTPNotFound.Error()}, http.StatusBadRequest) JSONResponse(w, models.Response{Success: false, Message: models.ErrSMTPNotFound.Error()}, http.StatusBadRequest)
} else if err != nil { } else if err != nil {

View File

@ -201,7 +201,7 @@ func PostCampaign(c *Campaign, uid int64) error {
// Check to make sure all the groups already exist // Check to make sure all the groups already exist
for i, g := range c.Groups { for i, g := range c.Groups {
c.Groups[i], err = GetGroupByName(g.Name, uid) c.Groups[i], err = GetGroupByName(g.Name, uid)
if err == gorm.RecordNotFound { if err == gorm.ErrRecordNotFound {
Logger.Printf("Error - Group %s does not exist", g.Name) Logger.Printf("Error - Group %s does not exist", g.Name)
return ErrGroupNotFound return ErrGroupNotFound
} else if err != nil { } else if err != nil {
@ -211,7 +211,7 @@ func PostCampaign(c *Campaign, uid int64) error {
} }
// Check to make sure the template exists // Check to make sure the template exists
t, err := GetTemplateByName(c.Template.Name, uid) t, err := GetTemplateByName(c.Template.Name, uid)
if err == gorm.RecordNotFound { if err == gorm.ErrRecordNotFound {
Logger.Printf("Error - Template %s does not exist", t.Name) Logger.Printf("Error - Template %s does not exist", t.Name)
return ErrTemplateNotFound return ErrTemplateNotFound
} else if err != nil { } else if err != nil {
@ -222,7 +222,7 @@ func PostCampaign(c *Campaign, uid int64) error {
c.TemplateId = t.Id c.TemplateId = t.Id
// Check to make sure the page exists // Check to make sure the page exists
p, err := GetPageByName(c.Page.Name, uid) p, err := GetPageByName(c.Page.Name, uid)
if err == gorm.RecordNotFound { if err == gorm.ErrRecordNotFound {
Logger.Printf("Error - Page %s does not exist", p.Name) Logger.Printf("Error - Page %s does not exist", p.Name)
return ErrPageNotFound return ErrPageNotFound
} else if err != nil { } else if err != nil {
@ -233,7 +233,7 @@ func PostCampaign(c *Campaign, uid int64) error {
c.PageId = p.Id c.PageId = p.Id
// Check to make sure the sending profile exists // Check to make sure the sending profile exists
s, err := GetSMTPByName(c.SMTP.Name, uid) s, err := GetSMTPByName(c.SMTP.Name, uid)
if err == gorm.RecordNotFound { if err == gorm.ErrRecordNotFound {
Logger.Printf("Error - Sending profile %s does not exist", s.Name) Logger.Printf("Error - Sending profile %s does not exist", s.Name)
return ErrPageNotFound return ErrPageNotFound
} else if err != nil { } else if err != nil {

View File

@ -203,7 +203,7 @@ func insertTargetIntoGroup(t Target, gid int64) error {
return err return err
} }
err = trans.Where("group_id=? and target_id=?", gid, t.Id).Find(&GroupTarget{}).Error err = trans.Where("group_id=? and target_id=?", gid, t.Id).Find(&GroupTarget{}).Error
if err == gorm.RecordNotFound { if err == gorm.ErrRecordNotFound {
err = trans.Save(&GroupTarget{GroupId: gid, TargetId: t.Id}).Error err = trans.Save(&GroupTarget{GroupId: gid, TargetId: t.Id}).Error
if err != nil { if err != nil {
Logger.Println(err) Logger.Println(err)

View File

@ -15,7 +15,7 @@ import (
_ "github.com/mattn/go-sqlite3" // Blank import needed to import sqlite3 _ "github.com/mattn/go-sqlite3" // Blank import needed to import sqlite3
) )
var db gorm.DB var db *gorm.DB
var err error var err error
// ErrUsernameTaken is thrown when a user attempts to register a username that is taken. // ErrUsernameTaken is thrown when a user attempts to register a username that is taken.

View File

@ -75,7 +75,7 @@ func (r *Result) GenerateId() {
io.ReadFull(rand.Reader, k) io.ReadFull(rand.Reader, k)
r.RId = fmt.Sprintf("%x", k) r.RId = fmt.Sprintf("%x", k)
err := db.Table("results").Where("id=?", r.RId).First(&Result{}).Error err := db.Table("results").Where("id=?", r.RId).First(&Result{}).Error
if err == gorm.RecordNotFound { if err == gorm.ErrRecordNotFound {
break break
} }
} }

View File

@ -49,7 +49,7 @@ func GetTemplates(uid int64) ([]Template, error) {
if err == nil && len(ts[i].Attachments) == 0 { if err == nil && len(ts[i].Attachments) == 0 {
ts[i].Attachments = make([]Attachment, 0) ts[i].Attachments = make([]Attachment, 0)
} }
if err != nil && err != gorm.RecordNotFound { if err != nil && err != gorm.ErrRecordNotFound {
Logger.Println(err) Logger.Println(err)
return ts, err return ts, err
} }
@ -66,7 +66,7 @@ func GetTemplate(id int64, uid int64) (Template, error) {
return t, err return t, err
} }
err = db.Where("template_id=?", t.Id).Find(&t.Attachments).Error err = db.Where("template_id=?", t.Id).Find(&t.Attachments).Error
if err != nil && err != gorm.RecordNotFound { if err != nil && err != gorm.ErrRecordNotFound {
Logger.Println(err) Logger.Println(err)
return t, err return t, err
} }
@ -85,7 +85,7 @@ func GetTemplateByName(n string, uid int64) (Template, error) {
return t, err return t, err
} }
err = db.Where("template_id=?", t.Id).Find(&t.Attachments).Error err = db.Where("template_id=?", t.Id).Find(&t.Attachments).Error
if err != nil && err != gorm.RecordNotFound { if err != nil && err != gorm.ErrRecordNotFound {
Logger.Println(err) Logger.Println(err)
return t, err return t, err
} }
@ -126,11 +126,11 @@ func PutTemplate(t *Template) error {
} }
// Delete all attachments, and replace with new ones // Delete all attachments, and replace with new ones
err = db.Where("template_id=?", t.Id).Delete(&Attachment{}).Error err = db.Where("template_id=?", t.Id).Delete(&Attachment{}).Error
if err != nil && err != gorm.RecordNotFound { if err != nil && err != gorm.ErrRecordNotFound {
Logger.Println(err) Logger.Println(err)
return err return err
} }
if err == gorm.RecordNotFound { if err == gorm.ErrRecordNotFound {
err = nil err = nil
} }
for i, _ := range t.Attachments { for i, _ := range t.Attachments {

View File

@ -38,7 +38,7 @@ func GetUserByUsername(username string) (User, error) {
u := User{} u := User{}
err := db.Where("username = ?", username).First(&u).Error err := db.Where("username = ?", username).First(&u).Error
// No issue if we don't find a record // No issue if we don't find a record
if err == gorm.RecordNotFound { if err == gorm.ErrRecordNotFound {
return u, nil return u, nil
} else if err == nil { } else if err == nil {
return u, ErrUsernameTaken return u, ErrUsernameTaken