Can now add/delete pages

Fixed test for importing a site - Now actually performs the right test.
pull/64/head
Jordan Wright 2015-08-25 21:03:12 -05:00
parent fad36607e4
commit ba11f6428c
6 changed files with 34 additions and 31 deletions

View File

@ -283,6 +283,7 @@ func API_Pages(w http.ResponseWriter, r *http.Request) {
JSONResponse(w, models.Response{Success: false, Message: "Invalid request"}, http.StatusBadRequest)
return
}
// Check to make sure the name is unique
_, err = models.GetPageByName(p.Name, ctx.Get(r, "user_id").(int64))
if err != gorm.RecordNotFound {
JSONResponse(w, models.Response{Success: false, Message: "Page name already in use"}, http.StatusConflict)
@ -293,7 +294,7 @@ func API_Pages(w http.ResponseWriter, r *http.Request) {
p.UserId = ctx.Get(r, "user_id").(int64)
err = models.PostPage(&p)
if err != nil {
JSONResponse(w, models.Response{Success: false, Message: "Error inserting page"}, http.StatusInternalServerError)
JSONResponse(w, models.Response{Success: false, Message: err.Error()}, http.StatusInternalServerError)
return
}
JSONResponse(w, p, http.StatusCreated)

View File

@ -2,8 +2,8 @@ package controllers
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
@ -45,6 +45,7 @@ func (s *ControllersSuite) TestSiteImportBaseHref() {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, h)
}))
hr := fmt.Sprintf("<html><head><base href=\"%s\"/></head><body><img src=\"/test.png\"/>\n</body></html>", ts.URL)
defer ts.Close()
resp, err := http.Post(fmt.Sprintf("%s/api/import/site?api_key=%s", as.URL, s.ApiKey), "application/json",
bytes.NewBuffer([]byte(fmt.Sprintf(`
@ -55,9 +56,10 @@ func (s *ControllersSuite) TestSiteImportBaseHref() {
`, ts.URL))))
s.Nil(err)
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
cs := cloneResponse{}
err = json.NewDecoder(resp.Body).Decode(&cs)
s.Nil(err)
fmt.Printf("%s", body)
s.Equal(cs.HTML, hr)
}
func (s *ControllersSuite) TearDownSuite() {

View File

@ -19,7 +19,7 @@ function save(idx){
})
} else {
// Submit the page
api.landing_pages.post(page)
api.pages.post(page)
.success(function(data){
successFlash("Page added successfully!")
load()
@ -37,9 +37,9 @@ function dismiss(){
$("#html_editor").val("")
}
function deleteTemplate(idx){
function deletePage(idx){
if (confirm("Delete " + pages[idx].name + "?")){
api.landing_pageId.delete(pages[idx].id)
api.pageId.delete(pages[idx].id)
.success(function(data){
successFlash(data.message)
load()
@ -79,22 +79,25 @@ function edit(idx){
}
function load(){
/*
load() - Loads the current pages using the API
*/
$("#pagesTable").hide()
$("#emptyMessage").hide()
$("#loading").show()
api.landing_pages.get()
api.pages.get()
.success(function(ps){
pages = ps
$("#loading").hide()
if (pages.length > 0){
$("#pagesTable").show()
pagesTable = $("#templateTable").DataTable();
pagesTable = $("#pagesTable").DataTable();
pagesTable.clear()
$.each(pages, function(i, page){
pagesTable.row.add([
page.name,
moment(page.modified_date).format('MMMM Do YYYY, h:mm:ss a'),
"<div class='pull-right'><button class='btn btn-primary' data-toggle='modal' data-target='#modal' onclick='edit(" + i + ")'>\
"<div class='pull-right'><button class='btn btn-primary' data-toggle='modal' data-target='#newLandingPageModal' onclick='edit(" + i + ")'>\
<i class='fa fa-pencil'></i>\
</button>\
<button class='btn btn-danger' onclick='deletePage(" + i + ")'>\
@ -110,6 +113,9 @@ function load(){
$("#loading").hide()
errorFlash("Error fetching pages")
})
}
$(document).ready(function(){
// Setup multiple modals
// Code based on http://miles-by-motorcycle.com/static/bootstrap-modal/index.html
$('.modal').on('hidden.bs.modal', function( event ) {
@ -135,8 +141,5 @@ function load(){
$( '.modal-backdrop' ).not( '.fv-modal-stack' ).css( 'z-index', 1039 + (10 * $('body').data( 'fv_open_modals' )));
$( '.modal-backdrop' ).not( 'fv-modal-stack' ).addClass( 'fv-modal-stack' );
});
}
$(document).ready(function(){
load()
load()
})

View File

@ -114,10 +114,10 @@ function load(){
$("#loading").show()
api.groups.get()
.success(function(gs){
$("#loading").hide()
if (gs.length > 0){
groups = gs
$("#emptyMessage").hide()
$("#loading").hide()
$("#groupTable").show()
groupTable = $("#groupTable").DataTable();
groupTable.clear();
@ -143,7 +143,6 @@ function load(){
]).draw()
})
} else {
$("#loading").hide()
$("#emptyMessage").show()
}
})

View File

@ -106,30 +106,30 @@ var api = {
return query("/templates/" + id, "DELETE", {})
}
},
// landing_pages contains the endpoints for /landing_pages
landing_pages : {
// get() - Queries the API for GET /landing_pages
// pages contains the endpoints for /pages
pages : {
// get() - Queries the API for GET /pages
get: function(){
return query("/landing_pages/", "GET", {})
return query("/pages/", "GET", {})
},
// post() - Posts a campaign to POST /landing_pages
// post() - Posts a campaign to POST /pages
post: function(page){
return query("/landing_pages/", "POST", page)
return query("/pages/", "POST", page)
}
},
// templateId contains the endpoints for /templates/:id
landing_pageId : {
pageId : {
// get() - Queries the API for GET /templates/:id
get: function(id){
return query("/landing_pages/" + id, "GET", {})
return query("/pages/" + id, "GET", {})
},
// put() - Puts a campaign to PUT /templates/:id
put: function (page){
return query("/landing_pages/" + page.id, "PUT", page)
return query("/pages/" + page.id, "PUT", page)
},
// delete() - Deletes a campaign at DELETE /templates/:id
delete: function(id){
return query("/landing_pages/" + id, "DELETE", {})
return query("/pages/" + id, "DELETE", {})
}
},
clone_site : function(req){

View File

@ -33,11 +33,9 @@
<div id="loading">
<i class="fa fa-spinner fa-spin fa-4x"></i>
</div>
<div style="display:none;">
<div class="row">
<div class="alert alert-info">
No pages created yet. Let's create one!
</div>
<div id="emptyMessage" class="row" style="display:none;">
<div class="alert alert-info">
No pages created yet. Let's create one!
</div>
</div>
<div class="row">