package web import ( "encoding/json" "net/http" "strconv" "goji.io/pat" "golang.org/x/net/context" libapi "dev.sum7.eu/sum7/warehost/lib/api" ) // PageList give all pages of a website func pageList(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { returndata = false logger := log.GetLog(r, "pagelist") var pages []*Page dbconnection.Where("website = ?", ctx.Value("websiteid").(int64)).Preload("Menu").Find(&pages) returndata = pages logger.Info("done") return } // PageAdd to add a new page func pageAdd(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { returndata = false logger := log.GetLog(r, "pageadd") var page Page err := json.NewDecoder(r.Body).Decode(&page) if err != nil { logger.Error("fetch request") http.Error(w, err.Error(), http.StatusInternalServerError) returnerr = &libapi.ErrorResult{ Message: "Internal Request Error", } return } page.WebsiteID = ctx.Value("websiteid").(int64) if menu := page.Menu; menu != nil { page.MenuID = page.Menu.ID page.Menu = nil } if err := dbconnection.Create(&page).Error; err != nil { logger.Error("database: during create page") returnerr = &libapi.ErrorResult{Message: "Internal Database Error"} return } returndata = true logger.Info("done") return } // PageEdit to edit page func pageEdit(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { returndata = false logger := log.GetLog(r, "pageedit") var page Page pageid, err := strconv.ParseInt(pat.Param(ctx, "pageid"), 10, 64) if err != nil { returnerr = &libapi.ErrorResult{Fields: []string{"pageid"}, Message: "Not a valid pageid"} logger.Warn("invalid pageid, no integer") return } logger = logger.WithField("id", pageid) err = json.NewDecoder(r.Body).Decode(&page) if err != nil { logger.Error("fetch request") http.Error(w, err.Error(), http.StatusInternalServerError) returnerr = &libapi.ErrorResult{ Message: "Internal Request Error", } return } page.WebsiteID = ctx.Value("websiteid").(int64) page.ID = pageid if menu := page.Menu; menu != nil { page.MenuID = page.Menu.ID page.Menu = nil } if err := dbconnection.Save(page).Error; err != nil { logger.Error("database: during delete website page") returnerr = &libapi.ErrorResult{Message: "Internal Database Error"} return } returndata = true logger.Info("done") return } // PageDelete to delete page func pageDelete(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { returndata = false logger := log.GetLog(r, "pagedelete") pageid, err := strconv.ParseInt(pat.Param(ctx, "pageid"), 10, 64) if err != nil { returnerr = &libapi.ErrorResult{Fields: []string{"pageid"}, Message: "Not a valid pageid"} logger.Warn("invalid pageid, no integer") return } logger = logger.WithField("id", pageid) page := &Page{ WebsiteID: ctx.Value("websiteid").(int64), ID: pageid, } if err := dbconnection.Unscoped().Delete(page).Error; err != nil { logger.Error("database: during delete website page") returnerr = &libapi.ErrorResult{Message: "Internal Database Error"} return } returndata = true logger.Info("done") return }