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.

51 lines
980 B

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