package Api import ( "encoding/json" "io/ioutil" "log" "net/http" "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 getUsers(w http.ResponseWriter, r *http.Request) { var ( users []Models.User returnJson []byte values url.Values page, pageSize int search string err error ) _, err = Auth.CheckCookie(r) if err != nil { w.WriteHeader(http.StatusUnauthorized) return } 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") users, err = Database.GetUsers(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(users) == 0 { Util.JsonReturn(w, 404, "No more data") return } returnJson, err = json.MarshalIndent(users, "", " ") if err != nil { Util.JsonReturn(w, 500, "An error occured") return } // Return updated json w.WriteHeader(http.StatusOK) w.Write(returnJson) } func getUser(w http.ResponseWriter, r *http.Request) { var ( userData Models.User returnJson []byte err error ) _, err = Auth.CheckCookie(r) if err != nil { w.WriteHeader(http.StatusUnauthorized) return } userData, err = Util.GetUserById(w, r) if err != nil { return } returnJson, err = json.MarshalIndent(userData, "", " ") if err != nil { Util.JsonReturn(w, 500, "An error occured") return } // Return updated json w.WriteHeader(http.StatusOK) w.Write(returnJson) } func createUser(w http.ResponseWriter, r *http.Request) { var ( userData Models.User requestBody []byte returnJson []byte err error ) 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") return } userData, err = JsonSerialization.DeserializeUser(requestBody, []string{ "id", "last_login", }, false) if err != nil { log.Printf("Invalid data provided to user API: %s\n", err.Error()) Util.JsonReturn(w, 405, "Invalid data") return } if userData.FirstName == "" || userData.LastName == "" || userData.Email == "" || userData.Password == "" || userData.ConfirmPassword == "" { Util.JsonReturn(w, http.StatusUnprocessableEntity, "Invalid data") return } err = Database.CheckUniqueEmail(userData.Email) if err != nil || !Util.IsEmailValid(userData.Email) { Util.JsonReturn(w, 405, "invalid_email") return } if userData.Password != userData.ConfirmPassword { Util.JsonReturn(w, 405, "invalid_password") return } userData.Password, err = Auth.HashPassword(userData.Password) if err != nil { Util.JsonReturn(w, 500, "An error occured") return } err = Database.CreateUser(&userData) if err != nil { Util.JsonReturn(w, 500, "An error occured") return } returnJson, err = json.MarshalIndent(userData, "", " ") if err != nil { Util.JsonReturn(w, 500, "An error occured") return } // Return updated json w.WriteHeader(http.StatusOK) w.Write(returnJson) } func updateUser(w http.ResponseWriter, r *http.Request) { var ( currentUserData Models.User userData Models.User requestBody []byte returnJson []byte err error ) currentUserData, err = Auth.CheckCookieCurrentUser(w, 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()) Util.JsonReturn(w, 500, "An error occured") return } userData, err = JsonSerialization.DeserializeUser(requestBody, []string{}, true) if err != nil { log.Printf("Invalid data provided to users API: %s\n", err.Error()) Util.JsonReturn(w, 405, "Invalid data") return } err = Database.UpdateUser(currentUserData.ID.String(), &userData) if err != nil { log.Printf("An error occured: %s\n", err.Error()) Util.JsonReturn(w, 500, "An error occured") return } returnJson, err = json.MarshalIndent(userData, "", " ") 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) w.Write(returnJson) } func deleteUser(w http.ResponseWriter, r *http.Request) { var ( userData Models.User err error ) _, err = Auth.CheckCookie(r) if err != nil { w.WriteHeader(http.StatusUnauthorized) return } userData, err = Util.GetUserById(w, r) if err != nil { w.WriteHeader(http.StatusNotFound) return } err = Database.DeleteUser(&userData) 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) }