Encrypted messaging app
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
931 B

  1. package Util
  2. import (
  3. "errors"
  4. "log"
  5. "net/http"
  6. "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database"
  7. "github.com/gorilla/mux"
  8. )
  9. func GetUserId(r *http.Request) (string, error) {
  10. var (
  11. urlVars map[string]string
  12. id string
  13. ok bool
  14. )
  15. urlVars = mux.Vars(r)
  16. id, ok = urlVars["userID"]
  17. if !ok {
  18. return id, errors.New("Could not get id")
  19. }
  20. return id, nil
  21. }
  22. func GetUserById(w http.ResponseWriter, r *http.Request) (Database.User, error) {
  23. var (
  24. postData Database.User
  25. id string
  26. err error
  27. )
  28. id, err = GetUserId(r)
  29. if err != nil {
  30. log.Printf("Error encountered getting id\n")
  31. http.Error(w, "Error", http.StatusInternalServerError)
  32. return postData, err
  33. }
  34. postData, err = Database.GetUserById(id)
  35. if err != nil {
  36. log.Printf("Could not find user with id %s\n", id)
  37. http.Error(w, "Error", http.StatusInternalServerError)
  38. return postData, err
  39. }
  40. return postData, nil
  41. }