Files
schedtxt_api/internal/handler/message.go
T
phoenixandphoenix a8dbd693ba tsk-40: Get Contact bug (#46)
Closes #40

Reviewed-on: phoenix/textsender-api#46
Co-authored-by: phoenix <kundeng00@pm.me>
Co-committed-by: phoenix <kundeng00@pm.me>
2025-11-29 20:22:31 +00:00

166 lines
4.4 KiB
Go

package handler
import (
"fmt"
"net/http"
"git.kundeng.us/phoenix/textsender-models/tx0/message"
"github.com/google/uuid"
"git.kundeng.us/phoenix/textsender-api/internal/store"
)
type RequestAddMessage struct {
Content string `json:"content"`
UserId uuid.UUID `json:"user_id"`
}
type AddMessageResponse struct {
Message string `json:"message"`
Data []message.Message `json:"data"`
}
type GetMessageResponse struct {
Message string `json:"message"`
Data []message.Message `json:"data"`
}
type MessageHandler struct {
MessageStore store.MessageStore
}
func NewMessageHandler(str store.MessageStore) *MessageHandler {
return &MessageHandler{MessageStore: str}
}
// AddMessage godoc
// @Summary Add message
// @Description Add a message record to send a text to (requires JWT)
// @Tags messages
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param request body RequestAddMessage true "Data to add message"
// @Success 201 {object} AddMessageResponse
// @Failure 500 {object} AddMessageResponse
// @Router /message/new [post]
func (m *MessageHandler) AddMessage(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
var req RequestAddMessage
if err := ExtractFromRequest(r, &req); err != nil {
http.Error(w, "Invalid JSON: "+err.Error(), http.StatusBadRequest)
}
defer r.Body.Close()
var statusCode int
var resp AddMessageResponse
newMessage := message.Message{Content: req.Content, UserId: req.UserId}
ctx := r.Context()
if err := m.MessageStore.CreateMessage(ctx, &newMessage); err != nil {
fmt.Println("Error:", err)
statusCode = http.StatusInternalServerError
resp.Message = err.Error()
} else {
statusCode = http.StatusCreated
resp.Message = "Message created"
resp.Data = append(resp.Data, newMessage)
}
RespondWithJSON(w, statusCode, &resp)
}
// GetMessage godoc
// @Summary Get message
// @Description Get a message record to have a recipient to send a text to (requires JWT)
// @Tags messages
// @Accept json
// @Produce json
// @Param id path string true "Message Id"
// @Param user_id path string true "User Id"
// @Security BearerAuth
// @Success 200 {object} GetMessageResponse
// @Failure 400 {object} GetMessageResponse
// @Failure 500 {object} GetMessageResponse
// @Router /message [get]
func (c *MessageHandler) GetMessage(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
var id, userId uuid.UUID
queryParams := r.URL.Query()
// Check if parameter exists
if _, exists := queryParams["id"]; !exists {
if _, exists := queryParams["user_id"]; !exists {
fmt.Fprintf(w, "Name parameter not provided")
http.Error(w, "Query params", http.StatusBadRequest)
return
} else {
var err error
userId, err = uuid.Parse(queryParams.Get("user_id"))
if err != nil {
fmt.Fprintf(w, "Name parameter exists: %s", userId)
}
}
} else {
var err error
idTmp := queryParams.Get("id")
id, err = uuid.Parse(idTmp)
if err != nil {
fmt.Println("Error:", err)
http.Error(w, "Error parsing Id", http.StatusBadRequest)
return
}
}
var statusCode int
var resp GetMessageResponse
ctx := r.Context()
if id != uuid.Nil {
fmt.Println("Checking with Id")
if con, err := c.MessageStore.GetMessageByID(ctx, id); err == nil {
statusCode = http.StatusOK
resp.Message = "Successful"
resp.Data = append(resp.Data, *con)
} else {
statusCode = http.StatusInternalServerError
resp.Message = err.Error()
}
} else if userId != uuid.Nil {
fmt.Println("Checking with User Id")
if messages, err := c.MessageStore.GetAllMessages(ctx); err == nil {
for _, msg := range messages {
if msg.UserId == userId {
resp.Data = append(resp.Data, *msg)
}
}
if len(resp.Data) > 0 {
statusCode = http.StatusOK
resp.Message = "Successful"
} else {
statusCode = http.StatusNotFound
resp.Message = "Message not found"
}
} else {
statusCode = http.StatusInternalServerError
resp.Message = err.Error()
}
} else {
statusCode = http.StatusBadRequest
resp.Message = "Invalid query parameter"
}
RespondWithJSON(w, statusCode, &resp)
}