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.

54 lines
1.3 KiB

  1. package Messages
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "github.com/gofrs/uuid"
  6. "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database"
  7. "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models"
  8. )
  9. type RawCreateConversationData struct {
  10. ID string `json:"id"`
  11. Name string `json:"name"`
  12. Users string `json:"users"`
  13. UserConversations []Models.UserConversation `json:"user_conversations"`
  14. }
  15. func CreateConversation(w http.ResponseWriter, r *http.Request) {
  16. var (
  17. rawConversationData RawCreateConversationData
  18. messageThread Models.ConversationDetail
  19. err error
  20. )
  21. err = json.NewDecoder(r.Body).Decode(&rawConversationData)
  22. if err != nil {
  23. http.Error(w, "Error", http.StatusInternalServerError)
  24. return
  25. }
  26. messageThread = Models.ConversationDetail{
  27. Base: Models.Base{
  28. ID: uuid.FromStringOrNil(rawConversationData.ID),
  29. },
  30. Name: rawConversationData.Name,
  31. Users: rawConversationData.Users,
  32. }
  33. err = Database.CreateConversationDetail(&messageThread)
  34. if err != nil {
  35. http.Error(w, "Error", http.StatusInternalServerError)
  36. return
  37. }
  38. err = Database.CreateUserConversations(&rawConversationData.UserConversations)
  39. if err != nil {
  40. http.Error(w, "Error", http.StatusInternalServerError)
  41. return
  42. }
  43. w.WriteHeader(http.StatusOK)
  44. }