implement POST message creation
This commit is contained in:
@@ -8,6 +8,8 @@ import (
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/render"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -57,6 +59,35 @@ func ListMessages(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
func newMessageID() string {
|
||||
return "msg_" + uuid.New().String()
|
||||
}
|
||||
|
||||
func NewMessage(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
var msg Message
|
||||
err := json.NewDecoder(r.Body).Decode(&msg)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid JSON", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
msg.ID = newMessageID()
|
||||
|
||||
msg.Timestamp = time.Now()
|
||||
|
||||
err = dbAddMessage(&msg)
|
||||
if err != nil {
|
||||
render.Render(w, r, ErrRender(err))
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
type messageKey struct{}
|
||||
|
||||
type Message struct {
|
||||
|
Reference in New Issue
Block a user