|
@ -2,70 +2,16 @@ package Api |
|
|
|
|
|
|
|
|
import ( |
|
|
import ( |
|
|
"encoding/json" |
|
|
"encoding/json" |
|
|
"io/ioutil" |
|
|
|
|
|
"log" |
|
|
"log" |
|
|
"net/http" |
|
|
"net/http" |
|
|
"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/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" |
|
|
"git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Util" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
func getPosts(w http.ResponseWriter, r *http.Request) { |
|
|
|
|
|
var ( |
|
|
|
|
|
posts []Models.Post |
|
|
|
|
|
returnJson []byte |
|
|
|
|
|
values url.Values |
|
|
|
|
|
page, pageSize int |
|
|
|
|
|
search string |
|
|
|
|
|
err error |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
values = r.URL.Query() |
|
|
|
|
|
|
|
|
|
|
|
page, err = strconv.Atoi(values.Get("page")) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
log.Println("Could not parse page url argument") |
|
|
|
|
|
Util.JsonReturn(w, 500, "An error occured") |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pageSize, err = strconv.Atoi(values.Get("pageSize")) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
log.Println("Could not parse pageSize url argument") |
|
|
|
|
|
Util.JsonReturn(w, 500, "An error occured") |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
search = values.Get("search") |
|
|
|
|
|
|
|
|
|
|
|
posts, err = Database.GetPosts(page, pageSize, search) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
log.Printf("An error occured: %s\n", err.Error()) |
|
|
|
|
|
Util.JsonReturn(w, 500, "An error occured") |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if len(posts) == 0 { |
|
|
|
|
|
Util.JsonReturn(w, 404, "No more data") |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
returnJson, err = json.MarshalIndent(posts, "", " ") |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
Util.JsonReturn(w, 500, "An error occured") |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Return updated json
|
|
|
|
|
|
w.WriteHeader(http.StatusOK) |
|
|
|
|
|
w.Write(returnJson) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func getFrontPagePosts(w http.ResponseWriter, r *http.Request) { |
|
|
func getFrontPagePosts(w http.ResponseWriter, r *http.Request) { |
|
|
var ( |
|
|
var ( |
|
|
posts []Models.Post |
|
|
posts []Models.Post |
|
@ -91,71 +37,48 @@ func getFrontPagePosts(w http.ResponseWriter, r *http.Request) { |
|
|
w.Write(returnJson) |
|
|
w.Write(returnJson) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func getPost(w http.ResponseWriter, r *http.Request) { |
|
|
|
|
|
|
|
|
func getPostsPublic(w http.ResponseWriter, r *http.Request) { |
|
|
var ( |
|
|
var ( |
|
|
postData Models.Post |
|
|
|
|
|
returnJson []byte |
|
|
|
|
|
err error |
|
|
|
|
|
|
|
|
posts []Models.Post |
|
|
|
|
|
returnJson []byte |
|
|
|
|
|
values url.Values |
|
|
|
|
|
page, pageSize int |
|
|
|
|
|
search string |
|
|
|
|
|
err error |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
postData, err = Util.GetPostById(w, r) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
values = r.URL.Query() |
|
|
|
|
|
|
|
|
returnJson, err = json.MarshalIndent(postData, "", " ") |
|
|
|
|
|
|
|
|
page, err = strconv.Atoi(values.Get("page")) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
|
|
|
log.Println("Could not parse page url argument") |
|
|
Util.JsonReturn(w, 500, "An error occured") |
|
|
Util.JsonReturn(w, 500, "An error occured") |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Return updated json
|
|
|
|
|
|
w.WriteHeader(http.StatusOK) |
|
|
|
|
|
w.Write(returnJson) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func createPost(w http.ResponseWriter, r *http.Request) { |
|
|
|
|
|
var ( |
|
|
|
|
|
requestBody []byte |
|
|
|
|
|
postData Models.Post |
|
|
|
|
|
returnJson []byte |
|
|
|
|
|
err error |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
_, err = Auth.CheckCookie(r) |
|
|
|
|
|
|
|
|
pageSize, err = strconv.Atoi(values.Get("pageSize")) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
w.WriteHeader(http.StatusUnauthorized) |
|
|
|
|
|
|
|
|
log.Println("Could not parse pageSize url argument") |
|
|
|
|
|
Util.JsonReturn(w, 500, "An error occured") |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
requestBody, err = ioutil.ReadAll(r.Body) |
|
|
|
|
|
|
|
|
search = values.Get("search") |
|
|
|
|
|
|
|
|
|
|
|
posts, err = Database.GetPostsPublic(page, pageSize, search) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
log.Printf("Error encountered reading POST body: %s\n", err.Error()) |
|
|
|
|
|
|
|
|
log.Printf("An error occured: %s\n", err.Error()) |
|
|
Util.JsonReturn(w, 500, "An error occured") |
|
|
Util.JsonReturn(w, 500, "An error occured") |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
postData, err = JsonSerialization.DeserializePost(requestBody, []string{ |
|
|
|
|
|
"id", |
|
|
|
|
|
"links", |
|
|
|
|
|
"images", |
|
|
|
|
|
"videos", |
|
|
|
|
|
"audios", |
|
|
|
|
|
}, false) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
log.Printf("Invalid data provided to posts API: %s\n", err.Error()) |
|
|
|
|
|
Util.JsonReturn(w, 405, "Invalid data") |
|
|
|
|
|
|
|
|
if len(posts) == 0 { |
|
|
|
|
|
Util.JsonReturn(w, 404, "No more data") |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
err = Database.CreatePost(&postData) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
Util.JsonReturn(w, 405, "Invalid data") |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
returnJson, err = json.MarshalIndent(postData, "", " ") |
|
|
|
|
|
|
|
|
returnJson, err = json.MarshalIndent(posts, "", " ") |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
log.Printf("An error occured: %s\n", err.Error()) |
|
|
|
|
|
Util.JsonReturn(w, 500, "An error occured") |
|
|
Util.JsonReturn(w, 500, "An error occured") |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
@ -165,52 +88,28 @@ func createPost(w http.ResponseWriter, r *http.Request) { |
|
|
w.Write(returnJson) |
|
|
w.Write(returnJson) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func updatePost(w http.ResponseWriter, r *http.Request) { |
|
|
|
|
|
|
|
|
func getPostPublic(w http.ResponseWriter, r *http.Request) { |
|
|
var ( |
|
|
var ( |
|
|
postData Models.Post |
|
|
|
|
|
requestBody []byte |
|
|
|
|
|
returnJson []byte |
|
|
|
|
|
id string |
|
|
|
|
|
err error |
|
|
|
|
|
|
|
|
postData Models.Post |
|
|
|
|
|
returnJson []byte |
|
|
|
|
|
id string |
|
|
|
|
|
err error |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
_, err = Auth.CheckCookie(r) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
w.WriteHeader(http.StatusUnauthorized) |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
id, err = Util.GetPostId(r) |
|
|
id, err = Util.GetPostId(r) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
log.Printf("Error encountered getting id\n") |
|
|
|
|
|
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()) |
|
|
|
|
|
Util.JsonReturn(w, 500, "An error occured") |
|
|
Util.JsonReturn(w, 500, "An error occured") |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
postData, err = JsonSerialization.DeserializePost(requestBody, []string{}, true) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
log.Printf("Invalid data provided to posts API: %s\n", err.Error()) |
|
|
|
|
|
Util.JsonReturn(w, 405, "Invalid data") |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
postData, err = Database.UpdatePost(id, &postData) |
|
|
|
|
|
|
|
|
postData, err = Database.GetPostByIdPublic(id) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
log.Printf("An error occured: %s\n", err.Error()) |
|
|
|
|
|
Util.JsonReturn(w, 500, "An error occured") |
|
|
|
|
|
|
|
|
Util.JsonReturn(w, 404, "Not found") |
|
|
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()) |
|
|
|
|
|
Util.JsonReturn(w, 500, "An error occured") |
|
|
Util.JsonReturn(w, 500, "An error occured") |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
@ -219,31 +118,3 @@ func updatePost(w http.ResponseWriter, r *http.Request) { |
|
|
w.WriteHeader(http.StatusOK) |
|
|
w.WriteHeader(http.StatusOK) |
|
|
w.Write(returnJson) |
|
|
w.Write(returnJson) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func deletePost(w http.ResponseWriter, r *http.Request) { |
|
|
|
|
|
var ( |
|
|
|
|
|
postData Models.Post |
|
|
|
|
|
err error |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
_, err = Auth.CheckCookie(r) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
w.WriteHeader(http.StatusUnauthorized) |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
postData, err = Util.GetPostById(w, r) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
err = Database.DeletePost(&postData) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
log.Printf("An error occured: %s\n", err.Error()) |
|
|
|
|
|
Util.JsonReturn(w, 500, "An error occured") |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Return updated json
|
|
|
|
|
|
w.WriteHeader(http.StatusOK) |
|
|
|
|
|
} |
|
|
|