From d43a888b26e3b7302b38bfa219fe934f4b241350 Mon Sep 17 00:00:00 2001 From: Jordan Wright Date: Tue, 8 Mar 2016 22:37:55 -0600 Subject: [PATCH] Updating gorm constructs to support gorm v1.0 released yesterday. --- controllers/api.go | 12 ++++++------ models/campaign.go | 8 ++++---- models/group.go | 2 +- models/models.go | 2 +- models/result.go | 2 +- models/template.go | 10 +++++----- models/user.go | 2 +- 7 files changed, 19 insertions(+), 19 deletions(-) diff --git a/controllers/api.go b/controllers/api.go index 50f1a560..9ca0aa26 100644 --- a/controllers/api.go +++ b/controllers/api.go @@ -130,7 +130,7 @@ func API_Groups(w http.ResponseWriter, r *http.Request) { return } _, 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) return } @@ -204,7 +204,7 @@ func API_Templates(w http.ResponseWriter, r *http.Request) { return } _, 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) return } @@ -288,7 +288,7 @@ func API_Pages(w http.ResponseWriter, r *http.Request) { } // Check to make sure the name is unique _, 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) Logger.Println(err) return @@ -365,7 +365,7 @@ func API_SMTP(w http.ResponseWriter, r *http.Request) { } // Check to make sure the name is unique _, 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) Logger.Println(err) return @@ -540,7 +540,7 @@ func API_Send_Test_Email(w http.ResponseWriter, r *http.Request) { } else { // Get the Template requested by name 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) JSONResponse(w, models.Response{Success: false, Message: models.ErrTemplateNotFound.Error()}, http.StatusBadRequest) } 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 { // Otherwise get the SMTP requested by name 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) JSONResponse(w, models.Response{Success: false, Message: models.ErrSMTPNotFound.Error()}, http.StatusBadRequest) } else if err != nil { diff --git a/models/campaign.go b/models/campaign.go index 6adf33d0..acefad97 100644 --- a/models/campaign.go +++ b/models/campaign.go @@ -201,7 +201,7 @@ func PostCampaign(c *Campaign, uid int64) error { // Check to make sure all the groups already exist for i, g := range c.Groups { 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) return ErrGroupNotFound } else if err != nil { @@ -211,7 +211,7 @@ func PostCampaign(c *Campaign, uid int64) error { } // Check to make sure the template exists 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) return ErrTemplateNotFound } else if err != nil { @@ -222,7 +222,7 @@ func PostCampaign(c *Campaign, uid int64) error { c.TemplateId = t.Id // Check to make sure the page exists 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) return ErrPageNotFound } else if err != nil { @@ -233,7 +233,7 @@ func PostCampaign(c *Campaign, uid int64) error { c.PageId = p.Id // Check to make sure the sending profile exists 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) return ErrPageNotFound } else if err != nil { diff --git a/models/group.go b/models/group.go index 73cd7a2f..f7669a47 100644 --- a/models/group.go +++ b/models/group.go @@ -203,7 +203,7 @@ func insertTargetIntoGroup(t Target, gid int64) error { return err } 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 if err != nil { Logger.Println(err) diff --git a/models/models.go b/models/models.go index 6d4e50ce..1b403cca 100644 --- a/models/models.go +++ b/models/models.go @@ -15,7 +15,7 @@ import ( _ "github.com/mattn/go-sqlite3" // Blank import needed to import sqlite3 ) -var db gorm.DB +var db *gorm.DB var err error // ErrUsernameTaken is thrown when a user attempts to register a username that is taken. diff --git a/models/result.go b/models/result.go index ef804adb..f5d43816 100644 --- a/models/result.go +++ b/models/result.go @@ -75,7 +75,7 @@ func (r *Result) GenerateId() { io.ReadFull(rand.Reader, k) r.RId = fmt.Sprintf("%x", k) err := db.Table("results").Where("id=?", r.RId).First(&Result{}).Error - if err == gorm.RecordNotFound { + if err == gorm.ErrRecordNotFound { break } } diff --git a/models/template.go b/models/template.go index c6027cc6..aa28867b 100644 --- a/models/template.go +++ b/models/template.go @@ -49,7 +49,7 @@ func GetTemplates(uid int64) ([]Template, error) { if err == nil && len(ts[i].Attachments) == 0 { ts[i].Attachments = make([]Attachment, 0) } - if err != nil && err != gorm.RecordNotFound { + if err != nil && err != gorm.ErrRecordNotFound { Logger.Println(err) return ts, err } @@ -66,7 +66,7 @@ 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 && err != gorm.RecordNotFound { + if err != nil && err != gorm.ErrRecordNotFound { Logger.Println(err) return t, err } @@ -85,7 +85,7 @@ func GetTemplateByName(n string, uid int64) (Template, error) { return t, err } 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) return t, err } @@ -126,11 +126,11 @@ 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 != gorm.RecordNotFound { + if err != nil && err != gorm.ErrRecordNotFound { Logger.Println(err) return err } - if err == gorm.RecordNotFound { + if err == gorm.ErrRecordNotFound { err = nil } for i, _ := range t.Attachments { diff --git a/models/user.go b/models/user.go index c131786f..bb72cb12 100644 --- a/models/user.go +++ b/models/user.go @@ -38,7 +38,7 @@ func GetUserByUsername(username string) (User, error) { u := User{} err := db.Where("username = ?", username).First(&u).Error // No issue if we don't find a record - if err == gorm.RecordNotFound { + if err == gorm.ErrRecordNotFound { return u, nil } else if err == nil { return u, ErrUsernameTaken