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.

45 lines
957 B

  1. package Messages
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database"
  6. "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models"
  7. "github.com/gorilla/mux"
  8. )
  9. // Messages gets messages by the associationKey
  10. func Messages(w http.ResponseWriter, r *http.Request) {
  11. var (
  12. messages []Models.Message
  13. urlVars map[string]string
  14. associationKey string
  15. returnJSON []byte
  16. ok bool
  17. err error
  18. )
  19. urlVars = mux.Vars(r)
  20. associationKey, ok = urlVars["associationKey"]
  21. if !ok {
  22. http.Error(w, "Not Found", http.StatusNotFound)
  23. return
  24. }
  25. messages, err = Database.GetMessagesByAssociationKey(associationKey)
  26. if !ok {
  27. http.Error(w, "Not Found", http.StatusNotFound)
  28. return
  29. }
  30. returnJSON, err = json.MarshalIndent(messages, "", " ")
  31. if err != nil {
  32. http.Error(w, "Error", http.StatusInternalServerError)
  33. return
  34. }
  35. w.WriteHeader(http.StatusOK)
  36. w.Write(returnJSON)
  37. }