124 lines
3.4 KiB
Go
124 lines
3.4 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"inbox/internal/base/httpx"
|
|
"inbox/internal/domain/message"
|
|
"inbox/internal/domain/topic"
|
|
)
|
|
|
|
func (h *Handler) listTopics(w http.ResponseWriter, r *http.Request) {
|
|
items, err := h.Topics.List(r.Context(), r.URL.Query().Get("workspace_id"))
|
|
if err != nil {
|
|
writeStoreError(w, err)
|
|
return
|
|
}
|
|
httpx.WriteJSON(w, http.StatusOK, map[string]any{"topics": items})
|
|
}
|
|
|
|
func (h *Handler) createTopic(w http.ResponseWriter, r *http.Request) {
|
|
type request struct {
|
|
WorkspaceID string `json:"workspace_id"`
|
|
Slug string `json:"slug"`
|
|
Title string `json:"title"`
|
|
Space string `json:"space"`
|
|
Status string `json:"status"`
|
|
Summary string `json:"summary"`
|
|
}
|
|
var req request
|
|
if err := httpx.DecodeJSON(r, &req); err != nil {
|
|
httpx.WriteError(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
item, err := h.Topics.Create(r.Context(), topic.Record{
|
|
WorkspaceID: req.WorkspaceID,
|
|
Slug: req.Slug,
|
|
Title: req.Title,
|
|
Space: topic.Space(req.Space),
|
|
Status: req.Status,
|
|
Summary: req.Summary,
|
|
})
|
|
if err != nil {
|
|
writeStoreError(w, err)
|
|
return
|
|
}
|
|
httpx.WriteJSON(w, http.StatusCreated, item)
|
|
}
|
|
|
|
func (h *Handler) getTopic(w http.ResponseWriter, r *http.Request) {
|
|
item, err := h.Topics.Get(r.Context(), r.PathValue("topicID"))
|
|
if err != nil {
|
|
writeStoreError(w, err)
|
|
return
|
|
}
|
|
httpx.WriteJSON(w, http.StatusOK, item)
|
|
}
|
|
|
|
func (h *Handler) stopTopic(w http.ResponseWriter, r *http.Request) {
|
|
item, err := h.Topics.Stop(r.Context(), r.PathValue("topicID"))
|
|
if err != nil {
|
|
writeStoreError(w, err)
|
|
return
|
|
}
|
|
httpx.WriteJSON(w, http.StatusAccepted, item)
|
|
}
|
|
|
|
func (h *Handler) confirmTopicPlan(w http.ResponseWriter, r *http.Request) {
|
|
item, err := h.Topics.ConfirmPlan(r.Context(), r.PathValue("topicID"))
|
|
if err != nil {
|
|
writeStoreError(w, err)
|
|
return
|
|
}
|
|
httpx.WriteJSON(w, http.StatusAccepted, item)
|
|
}
|
|
|
|
func (h *Handler) deleteTopic(w http.ResponseWriter, r *http.Request) {
|
|
if err := h.Topics.Delete(r.Context(), r.PathValue("topicID")); err != nil {
|
|
writeStoreError(w, err)
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
|
|
func (h *Handler) listTopicMessages(w http.ResponseWriter, r *http.Request) {
|
|
items, err := h.Topics.ListMessages(r.Context(), r.PathValue("topicID"))
|
|
if err != nil {
|
|
writeStoreError(w, err)
|
|
return
|
|
}
|
|
httpx.WriteJSON(w, http.StatusOK, map[string]any{"messages": items})
|
|
}
|
|
|
|
func (h *Handler) createTopicMessage(w http.ResponseWriter, r *http.Request) {
|
|
type request struct {
|
|
WorkspaceID string `json:"workspace_id"`
|
|
FromRoleName string `json:"from_role_name"`
|
|
ToExpr string `json:"to_expr"`
|
|
Type string `json:"type"`
|
|
Stage string `json:"stage"`
|
|
ReplyToMessageID string `json:"reply_to_message_id"`
|
|
BodyMarkdown string `json:"body_markdown"`
|
|
}
|
|
var req request
|
|
if err := httpx.DecodeJSON(r, &req); err != nil {
|
|
httpx.WriteError(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
item, err := h.Topics.CreateMessage(r.Context(), message.Record{
|
|
WorkspaceID: req.WorkspaceID,
|
|
TopicID: r.PathValue("topicID"),
|
|
FromRoleName: req.FromRoleName,
|
|
ToExpr: req.ToExpr,
|
|
Type: message.Type(req.Type),
|
|
Stage: req.Stage,
|
|
ReplyToMessageID: req.ReplyToMessageID,
|
|
BodyMarkdown: req.BodyMarkdown,
|
|
})
|
|
if err != nil {
|
|
writeStoreError(w, err)
|
|
return
|
|
}
|
|
httpx.WriteJSON(w, http.StatusCreated, item)
|
|
}
|