sum7/warehost
sum7
/
warehost
Archived
1
0
Fork 0
This repository has been archived on 2020-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
warehost/modul/web/apiblogpost.go

115 lines
3.2 KiB
Go
Raw Normal View History

package web
import (
"net/http"
"strconv"
"goji.io/pat"
libapi "dev.sum7.eu/sum7/warehost/lib/api"
)
// to add a new blog post
func blogpostAdd(w http.ResponseWriter, r *http.Request) {
logger := log.GetLog(r, "blogpostadd")
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)
var blogpost BlogPost
returnerr = libapi.JSONDecoder(w, r, logger, &blogpost)
if returnerr != nil {
libapi.JSONWrite(w, r, false, returnerr)
return
}
blogpost.BlogID = blog.ID
if blogpost.Preview != nil {
blogpost.PreviewID = &blogpost.Preview.ID
blogpost.Preview = nil
}
if err := dbconnection.Create(&blogpost).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)
}
// to edit a blog post
func blogpostEdit(w http.ResponseWriter, r *http.Request) {
logger := log.GetLog(r, "blogpostedit")
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)
blogpostid, err := strconv.ParseInt(pat.Param(r, "blogpostid"), 10, 64)
if err != nil {
logger.Warn("invalid blogpostid, no integer")
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Fields: []string{"blogid"}, Message: "Not a valid blogpostid"})
return
}
logger = logger.WithField("pID", blogpostid)
var blogpost BlogPost
returnerr = libapi.JSONDecoder(w, r, logger, &blogpost)
if returnerr != nil {
libapi.JSONWrite(w, r, false, returnerr)
return
}
blogpost.BlogID = blog.ID
blogpost.ID = blogpostid
if blogpost.Preview != nil {
blogpost.PreviewID = &blogpost.Preview.ID
blogpost.Preview = nil
}
if err := dbconnection.Save(&blogpost).Error; err != nil {
logger.Error("database: during save website blogpost: ", err)
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error"})
return
}
logger.Info("done")
libapi.JSONWrite(w, r, true, nil)
}
// to delete a blog post
func blogpostDelete(w http.ResponseWriter, r *http.Request) {
logger := log.GetLog(r, "blogpostdelete")
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)
blogpostid, err := strconv.ParseInt(pat.Param(r, "blogpostid"), 10, 64)
if err != nil {
logger.Warn("invalid blogpostid, no integer")
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Fields: []string{"blogid"}, Message: "Not a valid blogpostid"})
return
}
logger = logger.WithField("pID", blogpostid)
blogpost := &BlogPost{}
dbconnection.Where("blog = ? and id = ?", blog.ID, blogpostid).First(blogpost)
if err := dbconnection.Unscoped().Delete(blogpost).Error; err != nil {
logger.Error("database: during delete website blogpost: ", err)
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error"})
return
}
logger.Info("done")
libapi.JSONWrite(w, r, true, nil)
}