diff --git a/controllers/api.go b/controllers/api.go index c917af68..6bf3fcb0 100644 --- a/controllers/api.go +++ b/controllers/api.go @@ -188,8 +188,8 @@ func API_Templates(w http.ResponseWriter, r *http.Request) { switch { case r.Method == "GET": ts, err := models.GetTemplates(ctx.Get(r, "user_id").(int64)) - if checkError(err, w, "Templates not found", http.StatusNotFound) { - return + if err != nil { + fmt.Println(err) } JSONResponse(w, ts, http.StatusOK) //POST: Create a new template and return it as JSON @@ -261,8 +261,8 @@ func API_Pages(w http.ResponseWriter, r *http.Request) { switch { case r.Method == "GET": ps, err := models.GetPages(ctx.Get(r, "user_id").(int64)) - if checkError(err, w, "Pages not found", http.StatusNotFound) { - return + if err != nil { + fmt.Println(err) } JSONResponse(w, ps, http.StatusOK) //POST: Create a new page and return it as JSON diff --git a/controllers/route.go b/controllers/route.go index 73211ca9..99021a5b 100644 --- a/controllers/route.go +++ b/controllers/route.go @@ -16,6 +16,8 @@ import ( ) var templateDelims = []string{"{{%", "%}}"} + +// Logger is used to send logging messages to stdout. var Logger = log.New(os.Stdout, " ", log.Ldate|log.Ltime|log.Lshortfile) // CreateAdminRouter creates the routes for handling requests to the web interface. @@ -23,11 +25,12 @@ var Logger = log.New(os.Stdout, " ", log.Ldate|log.Ltime|log.Lshortfile) func CreateAdminRouter() http.Handler { router := mux.NewRouter() // Base Front-end routes + router.HandleFunc("/", Use(Base, mid.RequireLogin)) router.HandleFunc("/login", Login) router.HandleFunc("/logout", Use(Logout, mid.RequireLogin)) router.HandleFunc("/register", Register) - router.HandleFunc("/", Use(Base, mid.RequireLogin)) router.HandleFunc("/settings", Use(Settings, mid.RequireLogin)) + router.HandleFunc("/preview", Use(Preview, mid.RequireLogin)) // Create the API routes api := router.PathPrefix("/api").Subrouter() api = api.StrictSlash(true) @@ -144,16 +147,6 @@ func Register(w http.ResponseWriter, r *http.Request) { } } -// Logout destroys the current user session -func Logout(w http.ResponseWriter, r *http.Request) { - // If it is a post request, attempt to register the account - // Now that we are all registered, we can log the user in - session := ctx.Get(r, "session").(*sessions.Session) - delete(session.Values, "id") - Flash(w, r, "success", "You have successfully logged out") - http.Redirect(w, r, "login", 302) -} - // Base handles the default path and template execution func Base(w http.ResponseWriter, r *http.Request) { // Example of using session - will be removed. @@ -221,6 +214,24 @@ func Login(w http.ResponseWriter, r *http.Request) { } } +// Logout destroys the current user session +func Logout(w http.ResponseWriter, r *http.Request) { + // If it is a post request, attempt to register the account + // Now that we are all registered, we can log the user in + session := ctx.Get(r, "session").(*sessions.Session) + delete(session.Values, "id") + Flash(w, r, "success", "You have successfully logged out") + http.Redirect(w, r, "/login", 302) +} + +// Preview allows for the viewing of page html in a separate browser window +func Preview(w http.ResponseWriter, r *http.Request) { + if r.Method != "POST" { + http.Error(w, "Method not allowed", http.StatusBadRequest) + } + getTemplate(w, "dashboard").ExecuteTemplate(w, "base", struct{}{}) +} + func getTemplate(w http.ResponseWriter, tmpl string) *template.Template { templates := template.New("template") templates.Delims(templateDelims[0], templateDelims[1]) @@ -241,6 +252,7 @@ func checkError(e error, w http.ResponseWriter, m string, c int) bool { return false } +// Flash handles the rendering flash messages func Flash(w http.ResponseWriter, r *http.Request, t string, m string) { session := ctx.Get(r, "session").(*sessions.Session) session.AddFlash(models.Flash{ diff --git a/models/campaign.go b/models/campaign.go index 2bb3f8e1..56570b35 100644 --- a/models/campaign.go +++ b/models/campaign.go @@ -15,7 +15,9 @@ type Campaign struct { CreatedDate time.Time `json:"created_date"` CompletedDate time.Time `json:"completed_date"` TemplateId int64 `json:"-"` - Template Template `json:"template"` //This may change + Template Template `json:"template"` + PageId int64 `json:"-"` + Page Page `json:"page"` Status string `json:"status"` EmailsSent string `json:"emails_sent"` Results []Result `json:"results,omitempty"` @@ -24,6 +26,7 @@ type Campaign struct { SMTP SMTP `json:"smtp"` } +// Validate checks to make sure there are no invalid fields in a submitted campaign func (c *Campaign) Validate() (string, bool) { switch { case c.Name == "": @@ -36,11 +39,13 @@ func (c *Campaign) Validate() (string, bool) { return "", true } +// UpdateStatus changes the campaign status appropriately func (c *Campaign) UpdateStatus(s string) error { // This could be made simpler, but I think there's a bug in gorm return db.Table("campaigns").Where("id=?", c.Id).Update("status", s).Error } +// AddEvent creates a new campaign event in the database func (c *Campaign) AddEvent(e Event) error { e.CampaignId = c.Id e.Time = time.Now() diff --git a/models/page.go b/models/page.go index a2383d19..466535f6 100644 --- a/models/page.go +++ b/models/page.go @@ -58,8 +58,12 @@ func GetPageByName(n string, uid int64) (Page, error) { // PostPage creates a new page in the database. func PostPage(p *Page) error { + err := p.Validate() + if err != nil { + return err + } // Insert into the DB - err := db.Save(p).Error + err = db.Save(p).Error if err != nil { Logger.Println(err) } diff --git a/static/js/app/partials/campaigns.html b/static/js/app/partials/campaigns.html index 231b832e..fd9b7cb3 100644 --- a/static/js/app/partials/campaigns.html +++ b/static/js/app/partials/campaigns.html @@ -36,7 +36,7 @@