|
|
@ -8,9 +8,11 @@ import ( |
|
|
|
"net/url" |
|
|
|
"strconv" |
|
|
|
|
|
|
|
"git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Api/Auth" |
|
|
|
"git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Api/JsonSerialization" |
|
|
|
"git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Database" |
|
|
|
"git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Models" |
|
|
|
"git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Util" |
|
|
|
) |
|
|
|
|
|
|
|
func getPosts(w http.ResponseWriter, r *http.Request) { |
|
|
@ -27,27 +29,27 @@ func getPosts(w http.ResponseWriter, r *http.Request) { |
|
|
|
page, err = strconv.Atoi(values.Get("page")) |
|
|
|
if err != nil { |
|
|
|
log.Println("Could not parse page url argument") |
|
|
|
JsonReturn(w, 500, "An error occured") |
|
|
|
Util.JsonReturn(w, 500, "An error occured") |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
page, err = strconv.Atoi(values.Get("pageSize")) |
|
|
|
if err != nil { |
|
|
|
log.Println("Could not parse pageSize url argument") |
|
|
|
JsonReturn(w, 500, "An error occured") |
|
|
|
Util.JsonReturn(w, 500, "An error occured") |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
posts, err = Database.GetPosts(page, pageSize) |
|
|
|
if err != nil { |
|
|
|
log.Printf("An error occured: %s\n", err.Error()) |
|
|
|
JsonReturn(w, 500, "An error occured") |
|
|
|
Util.JsonReturn(w, 500, "An error occured") |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
returnJson, err = json.MarshalIndent(posts, "", " ") |
|
|
|
if err != nil { |
|
|
|
JsonReturn(w, 500, "An error occured") |
|
|
|
Util.JsonReturn(w, 500, "An error occured") |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
@ -66,13 +68,13 @@ func getFrontPagePosts(w http.ResponseWriter, r *http.Request) { |
|
|
|
posts, err = Database.GetFrontPagePosts() |
|
|
|
if err != nil { |
|
|
|
log.Printf("An error occured: %s\n", err.Error()) |
|
|
|
JsonReturn(w, 500, "An error occured") |
|
|
|
Util.JsonReturn(w, 500, "An error occured") |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
returnJson, err = json.MarshalIndent(posts, "", " ") |
|
|
|
if err != nil { |
|
|
|
JsonReturn(w, 500, "An error occured") |
|
|
|
Util.JsonReturn(w, 500, "An error occured") |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
@ -88,14 +90,14 @@ func getPost(w http.ResponseWriter, r *http.Request) { |
|
|
|
err error |
|
|
|
) |
|
|
|
|
|
|
|
postData, err = getPostById(w, r) |
|
|
|
postData, err = Util.GetPostById(w, r) |
|
|
|
if err != nil { |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
returnJson, err = json.MarshalIndent(postData, "", " ") |
|
|
|
if err != nil { |
|
|
|
JsonReturn(w, 500, "An error occured") |
|
|
|
Util.JsonReturn(w, 500, "An error occured") |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
@ -112,12 +114,16 @@ func createPost(w http.ResponseWriter, r *http.Request) { |
|
|
|
err error |
|
|
|
) |
|
|
|
|
|
|
|
// TODO: Add auth
|
|
|
|
_, err = Auth.CheckCookie(r) |
|
|
|
if err != nil { |
|
|
|
w.WriteHeader(http.StatusUnauthorized) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
requestBody, err = ioutil.ReadAll(r.Body) |
|
|
|
if err != nil { |
|
|
|
log.Printf("Error encountered reading POST body: %s\n", err.Error()) |
|
|
|
JsonReturn(w, 500, "An error occured") |
|
|
|
Util.JsonReturn(w, 500, "An error occured") |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
@ -129,21 +135,20 @@ func createPost(w http.ResponseWriter, r *http.Request) { |
|
|
|
"audios", |
|
|
|
}, false) |
|
|
|
if err != nil { |
|
|
|
panic(err) |
|
|
|
log.Printf("Invalid data provided to posts API: %s\n", err.Error()) |
|
|
|
JsonReturn(w, 405, "Invalid data") |
|
|
|
Util.JsonReturn(w, 405, "Invalid data") |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
err = Database.CreatePost(&postData) |
|
|
|
if err != nil { |
|
|
|
JsonReturn(w, 405, "Invalid data") |
|
|
|
Util.JsonReturn(w, 405, "Invalid data") |
|
|
|
} |
|
|
|
|
|
|
|
returnJson, err = json.MarshalIndent(postData, "", " ") |
|
|
|
if err != nil { |
|
|
|
log.Printf("An error occured: %s\n", err.Error()) |
|
|
|
JsonReturn(w, 500, "An error occured") |
|
|
|
Util.JsonReturn(w, 500, "An error occured") |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
@ -161,38 +166,44 @@ func updatePost(w http.ResponseWriter, r *http.Request) { |
|
|
|
err error |
|
|
|
) |
|
|
|
|
|
|
|
id, err = getPostId(r) |
|
|
|
_, err = Auth.CheckCookie(r) |
|
|
|
if err != nil { |
|
|
|
w.WriteHeader(http.StatusUnauthorized) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
id, err = Util.GetPostId(r) |
|
|
|
if err != nil { |
|
|
|
log.Printf("Error encountered getting id\n") |
|
|
|
JsonReturn(w, 500, "An error occured") |
|
|
|
Util.JsonReturn(w, 500, "An error occured") |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
requestBody, err = ioutil.ReadAll(r.Body) |
|
|
|
if err != nil { |
|
|
|
log.Printf("Error encountered reading POST body: %s\n", err.Error()) |
|
|
|
JsonReturn(w, 500, "An error occured") |
|
|
|
Util.JsonReturn(w, 500, "An error occured") |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
postData, err = JsonSerialization.DeserializePost(requestBody, []string{}, true) |
|
|
|
if err != nil { |
|
|
|
log.Printf("Invalid data provided to posts API: %s\n", err.Error()) |
|
|
|
JsonReturn(w, 405, "Invalid data") |
|
|
|
Util.JsonReturn(w, 405, "Invalid data") |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
postData, err = Database.UpdatePost(id, &postData) |
|
|
|
if err != nil { |
|
|
|
log.Printf("An error occured: %s\n", err.Error()) |
|
|
|
JsonReturn(w, 500, "An error occured") |
|
|
|
Util.JsonReturn(w, 500, "An error occured") |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
returnJson, err = json.MarshalIndent(postData, "", " ") |
|
|
|
if err != nil { |
|
|
|
log.Printf("An error occured: %s\n", err.Error()) |
|
|
|
JsonReturn(w, 500, "An error occured") |
|
|
|
Util.JsonReturn(w, 500, "An error occured") |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
@ -207,7 +218,13 @@ func deletePost(w http.ResponseWriter, r *http.Request) { |
|
|
|
err error |
|
|
|
) |
|
|
|
|
|
|
|
postData, err = getPostById(w, r) |
|
|
|
_, err = Auth.CheckCookie(r) |
|
|
|
if err != nil { |
|
|
|
w.WriteHeader(http.StatusUnauthorized) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
postData, err = Util.GetPostById(w, r) |
|
|
|
if err != nil { |
|
|
|
return |
|
|
|
} |
|
|
@ -215,7 +232,7 @@ func deletePost(w http.ResponseWriter, r *http.Request) { |
|
|
|
err = Database.DeletePost(&postData) |
|
|
|
if err != nil { |
|
|
|
log.Printf("An error occured: %s\n", err.Error()) |
|
|
|
JsonReturn(w, 500, "An error occured") |
|
|
|
Util.JsonReturn(w, 500, "An error occured") |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|