tsk-43: Added message limit code (#47)

Closes #43

Reviewed-on: phoenix/textsender-api#47
Co-authored-by: phoenix <kundeng00@pm.me>
Co-committed-by: phoenix <kundeng00@pm.me>
This commit is contained in:
phoenix
2025-11-29 20:48:14 +00:00
committed by phoenix
parent a8dbd693ba
commit 34262cda68
4 changed files with 45 additions and 13 deletions
+29 -13
View File
@@ -20,11 +20,6 @@ type AddMessageResponse struct {
Data []message.Message `json:"data"`
}
type GetMessageResponse struct {
Message string `json:"message"`
Data []message.Message `json:"data"`
}
type MessageHandler struct {
MessageStore store.MessageStore
}
@@ -33,6 +28,8 @@ func NewMessageHandler(str store.MessageStore) *MessageHandler {
return &MessageHandler{MessageStore: str}
}
const Message_Limit = 200
// AddMessage godoc
// @Summary Add message
// @Description Add a message record to send a text to (requires JWT)
@@ -42,6 +39,7 @@ func NewMessageHandler(str store.MessageStore) *MessageHandler {
// @Security BearerAuth
// @Param request body RequestAddMessage true "Data to add message"
// @Success 201 {object} AddMessageResponse
// @Failure 400 {object} AddMessageResponse
// @Failure 500 {object} AddMessageResponse
// @Router /message/new [post]
func (m *MessageHandler) AddMessage(w http.ResponseWriter, r *http.Request) {
@@ -60,21 +58,39 @@ func (m *MessageHandler) AddMessage(w http.ResponseWriter, r *http.Request) {
var resp AddMessageResponse
newMessage := message.Message{Content: req.Content, UserId: req.UserId}
ctx := r.Context()
if isMessageValid(&newMessage) {
ctx := r.Context()
if err := m.MessageStore.CreateMessage(ctx, &newMessage); err != nil {
fmt.Println("Error:", err)
statusCode = http.StatusInternalServerError
resp.Message = err.Error()
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)
}
} else {
statusCode = http.StatusCreated
resp.Message = "Message created"
resp.Data = append(resp.Data, newMessage)
statusCode = http.StatusBadRequest
resp.Message = fmt.Sprintf("Message length exceeds maximum limit. Current size: %d Limit: %d", len(newMessage.Content), Message_Limit)
}
RespondWithJSON(w, statusCode, &resp)
}
func isMessageValid(msg *message.Message) bool {
if len(msg.Content) <= Message_Limit {
return true
} else {
return false
}
}
type GetMessageResponse struct {
Message string `json:"message"`
Data []message.Message `json:"data"`
}
// GetMessage godoc
// @Summary Get message
// @Description Get a message record to have a recipient to send a text to (requires JWT)