package web import ( "net/http" "strconv" "goji.io/pat" libapi "dev.sum7.eu/sum7/warehost/lib/api" ) // PageList give all pages of a website func pageList(w http.ResponseWriter, r *http.Request) { ctx := r.Context() logger := log.GetLog(r, "pagelist") var pages []*Page dbconnection.Where("website = ?", ctx.Value("websiteid").(int64)).Preload("Menu").Find(&pages) logger.Info("done") libapi.JSONWrite(w, r, pages, nil) } // PageAdd to add a new page func pageAdd(w http.ResponseWriter, r *http.Request) { ctx := r.Context() logger := log.GetLog(r, "pageadd") var page Page returnerr := libapi.JSONDecoder(w, r, logger, &page) if returnerr != nil { libapi.JSONWrite(w, r, false, returnerr) 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") libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error"}) return } logger.Info("done") libapi.JSONWrite(w, r, true, nil) } // PageEdit to edit page func pageEdit(w http.ResponseWriter, r *http.Request) { ctx := r.Context() logger := log.GetLog(r, "pageedit") var page Page pageid, err := strconv.ParseInt(pat.Param(r, "pageid"), 10, 64) if err != nil { logger.Warn("invalid pageid, no integer") libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Fields: []string{"pageid"}, Message: "Not a valid pageid"}) return } logger = logger.WithField("id", pageid) returnerr := libapi.JSONDecoder(w, r, logger, &page) if returnerr != nil { libapi.JSONWrite(w, r, false, returnerr) 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") libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error"}) return } logger.Info("done") libapi.JSONWrite(w, r, true, nil) } // PageDelete to delete page func pageDelete(w http.ResponseWriter, r *http.Request) { ctx := r.Context() logger := log.GetLog(r, "pagedelete") pageid, err := strconv.ParseInt(pat.Param(r, "pageid"), 10, 64) if err != nil { logger.Warn("invalid pageid, no integer") libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Fields: []string{"pageid"}, Message: "Not a valid pageid"}) 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") libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error"}) return } logger.Info("done") libapi.JSONWrite(w, r, true, nil) }