package api import ( "TheAdversary/database" "encoding/json" "net/http" ) type deletePayload struct { Id int `json:"id"` } func Delete(w http.ResponseWriter, r *http.Request) { authorId, ok := authorizedSession(r) if !ok { w.WriteHeader(http.StatusUnauthorized) return } var payload deletePayload if err := json.NewDecoder(r.Body).Decode(&payload); err != nil { InvalidJson.Send(w) return } if !database.Exists(database.GetDB().Table("article"), "id = ?", payload.Id) { w.WriteHeader(http.StatusNotFound) return } else if !database.Exists(database.GetDB().Table("article_author"), "article_id = ? AND author_id = ?", payload.Id, authorId) { w.WriteHeader(http.StatusUnauthorized) return } database.GetDB().Table("article").Delete(&payload) w.WriteHeader(http.StatusOK) }