Still working on pages integration. Added skeleton for page HTML previewing in a new browser.

Additional cleanup, documentation
Changed return values for /api/templates and /api/pages to return empty array [] if no results (like /api/campaigns was already doing)
This commit is contained in:
unknown 2015-02-07 10:41:53 -06:00
parent c318424ac0
commit c8be0ddb74
9 changed files with 64 additions and 22 deletions

View file

@ -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

View file

@ -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{

View file

@ -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()

View file

@ -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)
}

View file

@ -36,7 +36,7 @@
<div ng-show="!campaigns.length">
<div class="row">
<div class="alert alert-info">
No campaigns yet.
No campaigns created yet. Let's create one!
</div>
</div>
</div>

View file

@ -33,7 +33,14 @@
<button type="button" class="btn btn-primary" ng-click="editPage('new')" data-toggle="modal" data-target="#newLandingPageModal"><i class="fa fa-plus"></i> New Page</button>
</div>
&nbsp;
<div class="row">
<div ng-show="!pages.length">
<div class="row">
<div class="alert alert-info">
No pages created yet. Let's create one!
</div>
</div>
</div>
<div ng-show="pages.length" class="row">
<table ng-table="mainTableParams" class="table table-hover table-striped table-bordered">
<tbody>
<tr ng-repeat="page in $data" class="editable-row">

View file

@ -32,5 +32,5 @@
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" ng-click="cancel()">Cancel</button>
<button type="button" class="btn btn-primary" ng-click="ok(template)" data-dismiss="modal">Save Page</button>
<button type="button" class="btn btn-primary" ng-click="ok(page)" data-dismiss="modal">Save Page</button>
</div>

View file

@ -28,7 +28,14 @@
<button type="button" class="btn btn-primary" ng-click="editTemplate('new')"><i class="fa fa-plus"></i> New Template</button>
</div>
&nbsp;
<div class="row">
<div ng-show="!templates.length">
<div class="row">
<div class="alert alert-info">
No templates created yet. Let's create one!
</div>
</div>
</div>
<div ng-show="templates.length" class="row">
<table ng-table="mainTableParams" class="table table-hover table-striped table-bordered">
<tbody>
<tr ng-repeat="template in $data" class="editable-row">

View file

@ -33,7 +33,14 @@
<button type="button" class="btn btn-primary" ng-click="editGroup('new')" data-toggle="modal" data-target="#newGroupModal"><i class="fa fa-plus"></i> New Group</button>
</div>
&nbsp;
<div class="row">
<div ng-show="!groups.length">
<div class="row">
<div class="alert alert-info">
No groups created yet. Let's create one!
</div>
</div>
</div>
<div ng-show="groups.length" class="row">
<table ng-table="mainTableParams" class="table table-hover table-striped table-bordered">
<tbody>
<tr ng-repeat="group in $data" class="editable-row">