package Messages
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"git.tovijaeschke.xyz/tovi/Envelope/Backend/Database"
|
|
"git.tovijaeschke.xyz/tovi/Envelope/Backend/Models"
|
|
|
|
"github.com/gorilla/mux"
|
|
)
|
|
|
|
// Messages gets messages by the associationKey
|
|
func Messages(w http.ResponseWriter, r *http.Request) {
|
|
var (
|
|
messages []Models.Message
|
|
urlVars map[string]string
|
|
associationKey string
|
|
returnJSON []byte
|
|
ok bool
|
|
err error
|
|
)
|
|
|
|
urlVars = mux.Vars(r)
|
|
associationKey, ok = urlVars["associationKey"]
|
|
if !ok {
|
|
http.Error(w, "Not Found", http.StatusNotFound)
|
|
return
|
|
}
|
|
|
|
messages, err = Database.GetMessagesByAssociationKey(associationKey)
|
|
if !ok {
|
|
http.Error(w, "Not Found", http.StatusNotFound)
|
|
return
|
|
}
|
|
|
|
returnJSON, err = json.MarshalIndent(messages, "", " ")
|
|
if err != nil {
|
|
http.Error(w, "Error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
w.Write(returnJSON)
|
|
}
|