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
945 B

  1. package Api
  2. import (
  3. "errors"
  4. "log"
  5. "net/http"
  6. "git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Database"
  7. "git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Models"
  8. "github.com/gorilla/mux"
  9. )
  10. func getPostId(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["postID"]
  18. if !ok {
  19. return id, errors.New("Could not get id")
  20. }
  21. return id, nil
  22. }
  23. func getPostById(w http.ResponseWriter, r *http.Request) (Models.Post, error) {
  24. var (
  25. postData Models.Post
  26. id string
  27. err error
  28. )
  29. id, err = getPostId(r)
  30. if err != nil {
  31. log.Printf("Error encountered getting id\n")
  32. JsonReturn(w, 500, "An error occured")
  33. return postData, err
  34. }
  35. postData, err = Database.GetPostById(id)
  36. if err != nil {
  37. log.Printf("Could not find pet with id %s\n", id)
  38. JsonReturn(w, 404, "Not found")
  39. return postData, err
  40. }
  41. return postData, nil
  42. }