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