package web import ( "net/http" "strconv" "goji.io/pat" libapi "dev.sum7.eu/sum7/warehost/lib/api" ) func getBlog(w http.ResponseWriter, r *http.Request) (blog Blog, returnerr *libapi.ErrorResult) { ctx := r.Context() id, err := strconv.ParseInt(pat.Param(r, "blogid"), 10, 64) if err != nil { returnerr = &libapi.ErrorResult{Message: "Internal Request Error"} w.WriteHeader(http.StatusBadRequest) return } blog = Blog{} dbconnection.Where(map[string]interface{}{"id": id, "website": ctx.Value("websiteid").(int64)}).Preload("URL").Preload("Posts").Find(&blog) if blog.ID <= 0 { returnerr = &libapi.ErrorResult{Fields: []string{"blog"}, Message: "not found"} w.WriteHeader(http.StatusNotFound) } return } // blogList give all blogs of a website func blogList(w http.ResponseWriter, r *http.Request) { ctx := r.Context() logger := log.GetLog(r, "bloglist") var blogges []*Blog dbconnection.Where("website = ?", ctx.Value("websiteid").(int64)).Preload("URL").Find(&blogges) logger.Info("done") libapi.JSONWrite(w, r, blogges, nil) } func blogShow(w http.ResponseWriter, r *http.Request) { logger := log.GetLog(r, "blogshow") blog, returnerr := getBlog(w, r) if returnerr != nil { logger.Info("not found") libapi.JSONWrite(w, r, false, returnerr) return } logger = logger.WithField("bID", blog.ID) logger.Info("done") libapi.JSONWrite(w, r, blog, nil) } // BlogAdd to add a new blog func blogAdd(w http.ResponseWriter, r *http.Request) { ctx := r.Context() logger := log.GetLog(r, "blogadd") var blog Blog returnerr := libapi.JSONDecoder(w, r, logger, &blog) if returnerr != nil { libapi.JSONWrite(w, r, false, returnerr) return } blog.WebsiteID = ctx.Value("websiteid").(int64) blog.URL = &URL{ WebsiteID: ctx.Value("websiteid").(int64), Path: FixPath(blog.URL.Path), } if err := dbconnection.Create(&blog).Error; err != nil { logger.Error("database: during create blog: ", err) libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error"}) return } logger.Info("done") libapi.JSONWrite(w, r, true, nil) } // BlogEdit to edit blog func blogEdit(w http.ResponseWriter, r *http.Request) { ctx := r.Context() logger := log.GetLog(r, "blogedit") var blog Blog blogid, err := strconv.ParseInt(pat.Param(r, "blogid"), 10, 64) if err != nil { logger.Warn("invalid blogid, no integer") libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Fields: []string{"blogid"}, Message: "Not a valid blogid"}) return } logger = logger.WithField("id", blogid) returnerr := libapi.JSONDecoder(w, r, logger, &blog) if returnerr != nil { libapi.JSONWrite(w, r, false, returnerr) return } blog.ID = blogid blog.WebsiteID = ctx.Value("websiteid").(int64) blog.URL.WebsiteID = blog.WebsiteID blog.URL.Path = FixPath(blog.URL.Path) tx := dbconnection.Begin() if err := tx.Save(blog.URL).Error; err != nil { logger.Error("database: during save website url: ", err) libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error"}) return } blog.URLID = blog.URL.ID blog.URL = nil if err := tx.Save(blog).Error; err != nil { logger.Error("database: during save website blog: ", err) libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error"}) return } tx.Commit() logger.Info("done") libapi.JSONWrite(w, r, true, nil) } // BlogDelete to delete blog func blogDelete(w http.ResponseWriter, r *http.Request) { logger := log.GetLog(r, "blogdelete") blogid, err := strconv.ParseInt(pat.Param(r, "blogid"), 10, 64) if err != nil { logger.Warn("invalid blogid, no integer") libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Fields: []string{"blogid"}, Message: "Not a valid blogid"}) return } logger = logger.WithField("id", blogid) blog := &Blog{} dbconnection.Where("id = ?", blogid).Preload("URL").First(blog) if err := dbconnection.Unscoped().Delete(blog.URL).Error; err != nil { logger.Error("database: during delete website blog: ", err) libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error"}) return } logger.Info("done") libapi.JSONWrite(w, r, true, nil) }