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
+6
View File
@@ -214,6 +214,12 @@ const docTemplate = `{
"$ref": "#/definitions/handler.AddMessageResponse" "$ref": "#/definitions/handler.AddMessageResponse"
} }
}, },
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/handler.AddMessageResponse"
}
},
"500": { "500": {
"description": "Internal Server Error", "description": "Internal Server Error",
"schema": { "schema": {
+6
View File
@@ -208,6 +208,12 @@
"$ref": "#/definitions/handler.AddMessageResponse" "$ref": "#/definitions/handler.AddMessageResponse"
} }
}, },
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/handler.AddMessageResponse"
}
},
"500": { "500": {
"description": "Internal Server Error", "description": "Internal Server Error",
"schema": { "schema": {
+4
View File
@@ -322,6 +322,10 @@ paths:
description: Created description: Created
schema: schema:
$ref: '#/definitions/handler.AddMessageResponse' $ref: '#/definitions/handler.AddMessageResponse'
"400":
description: Bad Request
schema:
$ref: '#/definitions/handler.AddMessageResponse'
"500": "500":
description: Internal Server Error description: Internal Server Error
schema: schema:
+29 -13
View File
@@ -20,11 +20,6 @@ type AddMessageResponse struct {
Data []message.Message `json:"data"` Data []message.Message `json:"data"`
} }
type GetMessageResponse struct {
Message string `json:"message"`
Data []message.Message `json:"data"`
}
type MessageHandler struct { type MessageHandler struct {
MessageStore store.MessageStore MessageStore store.MessageStore
} }
@@ -33,6 +28,8 @@ func NewMessageHandler(str store.MessageStore) *MessageHandler {
return &MessageHandler{MessageStore: str} return &MessageHandler{MessageStore: str}
} }
const Message_Limit = 200
// AddMessage godoc // AddMessage godoc
// @Summary Add message // @Summary Add message
// @Description Add a message record to send a text to (requires JWT) // @Description Add a message record to send a text to (requires JWT)
@@ -42,6 +39,7 @@ func NewMessageHandler(str store.MessageStore) *MessageHandler {
// @Security BearerAuth // @Security BearerAuth
// @Param request body RequestAddMessage true "Data to add message" // @Param request body RequestAddMessage true "Data to add message"
// @Success 201 {object} AddMessageResponse // @Success 201 {object} AddMessageResponse
// @Failure 400 {object} AddMessageResponse
// @Failure 500 {object} AddMessageResponse // @Failure 500 {object} AddMessageResponse
// @Router /message/new [post] // @Router /message/new [post]
func (m *MessageHandler) AddMessage(w http.ResponseWriter, r *http.Request) { 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 var resp AddMessageResponse
newMessage := message.Message{Content: req.Content, UserId: req.UserId} 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 { if err := m.MessageStore.CreateMessage(ctx, &newMessage); err != nil {
fmt.Println("Error:", err) fmt.Println("Error:", err)
statusCode = http.StatusInternalServerError statusCode = http.StatusInternalServerError
resp.Message = err.Error() resp.Message = err.Error()
} else {
statusCode = http.StatusCreated
resp.Message = "Message created"
resp.Data = append(resp.Data, newMessage)
}
} else { } else {
statusCode = http.StatusCreated statusCode = http.StatusBadRequest
resp.Message = "Message created" resp.Message = fmt.Sprintf("Message length exceeds maximum limit. Current size: %d Limit: %d", len(newMessage.Content), Message_Limit)
resp.Data = append(resp.Data, newMessage)
} }
RespondWithJSON(w, statusCode, &resp) 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 // GetMessage godoc
// @Summary Get message // @Summary Get message
// @Description Get a message record to have a recipient to send a text to (requires JWT) // @Description Get a message record to have a recipient to send a text to (requires JWT)