forked from mtusi2026/mtusi2026lab2
165 lines
4.4 KiB
Go
165 lines
4.4 KiB
Go
package http
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
inmemory2 "gitea.privcloud.tech/mtusi2026/internal/adapter/repository/cache/inmemory"
|
|
"gitea.privcloud.tech/mtusi2026/internal/dto"
|
|
"gitea.privcloud.tech/mtusi2026/internal/gen/contracts"
|
|
)
|
|
|
|
type ChatHandler struct {
|
|
*BaseHandler
|
|
users inmemory2.UserCache
|
|
chats inmemory2.ChatCache
|
|
messages inmemory2.MessageCache
|
|
}
|
|
|
|
func NewChatHandler(base *BaseHandler, users inmemory2.UserCache, chats inmemory2.ChatCache, messages inmemory2.MessageCache) Handler {
|
|
return &ChatHandler{BaseHandler: base, users: users, chats: chats, messages: messages}
|
|
}
|
|
|
|
func (h *ChatHandler) RegisterRoutes() {
|
|
h.router.Post("/api/v1/chats", h.Create)
|
|
h.router.Get("/api/v1/chats", h.Get)
|
|
h.router.Get("/api/v1/chats/{chat_id}", h.GetByID)
|
|
h.router.Post("/api/v1/chats/{chat_id}/messages", h.SendMessage)
|
|
h.router.Get("/api/v1/chats/{chat_id}/messages", h.GetMessages)
|
|
}
|
|
|
|
func (h *ChatHandler) Create(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
user, ok := h.authenticatedUser(w, r, h.users)
|
|
if !ok {
|
|
return
|
|
}
|
|
var req contracts.CreateChat
|
|
if !h.Decode(w, r, &req) || !h.Validate(w, r, &req) {
|
|
return
|
|
}
|
|
chat, err := h.chats.Create(ctx, dto.CreateChat{
|
|
CreatorID: user.ID,
|
|
Title: req.Title,
|
|
MemberIDs: req.MemberIds,
|
|
})
|
|
if err != nil {
|
|
h.JSONError(ctx, w, err)
|
|
return
|
|
}
|
|
h.JSONResponse(w, http.StatusCreated, contractChat(chat))
|
|
}
|
|
|
|
func (h *ChatHandler) Get(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
user, ok := h.authenticatedUser(w, r, h.users)
|
|
if !ok {
|
|
return
|
|
}
|
|
limit, afterID, ok := h.pageParams(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
page, err := h.chats.ListForMember(ctx, user.ID, limit, afterID)
|
|
if err != nil {
|
|
h.JSONError(ctx, w, err)
|
|
return
|
|
}
|
|
items := make([]contracts.Chat, 0, len(page.Items))
|
|
for _, chat := range page.Items {
|
|
items = append(items, contractChat(chat))
|
|
}
|
|
h.JSONResponse(w, http.StatusOK, contracts.ChatPage{Items: items, NextCursor: cursorString(page.NextCursorID)})
|
|
}
|
|
|
|
func (h *ChatHandler) GetByID(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
user, ok := h.authenticatedUser(w, r, h.users)
|
|
if !ok {
|
|
return
|
|
}
|
|
chatID, err := h.ParseIDFromPathParam(ctx, w, r, "chat_id")
|
|
if err != nil {
|
|
return
|
|
}
|
|
chat, err := h.chats.GetForMember(ctx, chatID, user.ID)
|
|
if err != nil {
|
|
h.JSONError(ctx, w, err)
|
|
return
|
|
}
|
|
h.JSONResponse(w, http.StatusOK, contractChat(chat))
|
|
}
|
|
|
|
func (h *ChatHandler) SendMessage(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
user, ok := h.authenticatedUser(w, r, h.users)
|
|
if !ok {
|
|
return
|
|
}
|
|
chatID, err := h.ParseIDFromPathParam(ctx, w, r, "chat_id")
|
|
if err != nil {
|
|
return
|
|
}
|
|
if _, err := h.chats.GetForMember(ctx, chatID, user.ID); err != nil {
|
|
h.JSONError(ctx, w, err)
|
|
return
|
|
}
|
|
var req contracts.CreateMessage
|
|
if !h.Decode(w, r, &req) || !h.Validate(w, r, &req) {
|
|
return
|
|
}
|
|
message, err := h.messages.Create(ctx, dto.CreateMessage{ChatID: chatID, SenderID: user.ID, Text: req.Text})
|
|
if err != nil {
|
|
h.JSONError(ctx, w, err)
|
|
return
|
|
}
|
|
h.JSONResponse(w, http.StatusCreated, contractMessage(message))
|
|
}
|
|
|
|
func (h *ChatHandler) GetMessages(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
user, ok := h.authenticatedUser(w, r, h.users)
|
|
if !ok {
|
|
return
|
|
}
|
|
chatID, err := h.ParseIDFromPathParam(ctx, w, r, "chat_id")
|
|
if err != nil {
|
|
return
|
|
}
|
|
if _, err := h.chats.GetForMember(ctx, chatID, user.ID); err != nil {
|
|
h.JSONError(ctx, w, err)
|
|
return
|
|
}
|
|
limit, afterID, ok := h.pageParams(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
page, err := h.messages.ListForChat(ctx, chatID, user.ID, limit, afterID)
|
|
if err != nil {
|
|
h.JSONError(ctx, w, err)
|
|
return
|
|
}
|
|
items := make([]contracts.Message, 0, len(page.Items))
|
|
for _, message := range page.Items {
|
|
items = append(items, contractMessage(message))
|
|
}
|
|
h.JSONResponse(w, http.StatusOK, contracts.MessagePage{Items: items, NextCursor: cursorString(page.NextCursorID)})
|
|
}
|
|
|
|
func contractChat(chat dto.Chat) contracts.Chat {
|
|
return contracts.Chat{Id: chat.ID, Title: chat.Title, MemberIds: chat.MemberIDs, CreatedAt: chat.CreatedAt}
|
|
}
|
|
|
|
func contractMessage(message dto.Message) contracts.Message {
|
|
return contracts.Message{Id: message.ID, ChatId: message.ChatID, SenderId: message.SenderID, Text: message.Text, CreatedAt: message.CreatedAt}
|
|
}
|
|
|
|
func cursorString(id *uuid.UUID) *string {
|
|
if id == nil {
|
|
return nil
|
|
}
|
|
value := id.String()
|
|
return &value
|
|
}
|