package Api import ( "encoding/json" "log" "net/http" "net/url" "strconv" "git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Database" "git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Models" "git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Util" ) func getFrontPagePosts(w http.ResponseWriter, r *http.Request) { var ( posts []Models.Post returnJson []byte err error ) posts, err = Database.GetFrontPagePosts() if err != nil { log.Printf("An error occured: %s\n", err.Error()) Util.JsonReturn(w, 500, "An error occured") 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 getPostsPublic(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.GetPostsPublic(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 getPostPublic(w http.ResponseWriter, r *http.Request) { var ( postData Models.Post returnJson []byte id string err error ) id, err = Util.GetPostId(r) if err != nil { Util.JsonReturn(w, 500, "An error occured") return } postData, err = Database.GetPostByIdPublic(id) if err != nil { Util.JsonReturn(w, 404, "Not found") return } returnJson, err = json.MarshalIndent(postData, "", " ") if err != nil { Util.JsonReturn(w, 500, "An error occured") return } // Return updated json w.WriteHeader(http.StatusOK) w.Write(returnJson) }