Added api endpoints and tests
This commit is contained in:
36
api/delete.go
Normal file
36
api/delete.go
Normal file
@@ -0,0 +1,36 @@
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user