diff --git a/docs/docs.go b/docs/docs.go index 3f0dddf..ed790b8 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -147,6 +147,61 @@ const docTemplate = `{ } } }, + "/chat/direct/{chatId}": { + "get": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "Devuelve los detalles de un chat directo específico", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Chat" + ], + "summary": "Obtiene un chat directo por ID", + "parameters": [ + { + "type": "string", + "description": "ID del chat directo", + "name": "chatId", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "Detalles del chat directo", + "schema": { + "$ref": "#/definitions/models.DirectChat" + } + }, + "401": { + "description": "No autorizado", + "schema": { + "type": "string" + } + }, + "404": { + "description": "Chat no encontrado", + "schema": { + "type": "string" + } + }, + "500": { + "description": "Error interno del servidor", + "schema": { + "type": "string" + } + } + } + } + }, "/chat/direct/{chatId}/messages": { "get": { "security": [ diff --git a/docs/swagger.json b/docs/swagger.json index e9767cf..7e57785 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -140,6 +140,61 @@ } } }, + "/chat/direct/{chatId}": { + "get": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "Devuelve los detalles de un chat directo específico", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Chat" + ], + "summary": "Obtiene un chat directo por ID", + "parameters": [ + { + "type": "string", + "description": "ID del chat directo", + "name": "chatId", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "Detalles del chat directo", + "schema": { + "$ref": "#/definitions/models.DirectChat" + } + }, + "401": { + "description": "No autorizado", + "schema": { + "type": "string" + } + }, + "404": { + "description": "Chat no encontrado", + "schema": { + "type": "string" + } + }, + "500": { + "description": "Error interno del servidor", + "schema": { + "type": "string" + } + } + } + } + }, "/chat/direct/{chatId}/messages": { "get": { "security": [ diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 15ae2f0..ecf85a5 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -206,6 +206,41 @@ paths: summary: Registra un nuevo usuario tags: - Auth + /chat/direct/{chatId}: + get: + consumes: + - application/json + description: Devuelve los detalles de un chat directo específico + parameters: + - description: ID del chat directo + in: path + name: chatId + required: true + type: string + produces: + - application/json + responses: + "200": + description: Detalles del chat directo + schema: + $ref: '#/definitions/models.DirectChat' + "401": + description: No autorizado + schema: + type: string + "404": + description: Chat no encontrado + schema: + type: string + "500": + description: Error interno del servidor + schema: + type: string + security: + - BearerAuth: [] + summary: Obtiene un chat directo por ID + tags: + - Chat /chat/direct/{chatId}/messages: get: consumes: diff --git a/internal/handlers/chat_handler.go b/internal/handlers/chat_handler.go index 6a04aff..ce66707 100644 --- a/internal/handlers/chat_handler.go +++ b/internal/handlers/chat_handler.go @@ -376,3 +376,50 @@ func (h *ChatHandler) GetRoomMessagesSimple(w http.ResponseWriter, r *http.Reque json.NewEncoder(w).Encode(messages) } + +// GetChat obtiene un chat directo por ID +// +// @Summary Obtiene un chat directo por ID +// @Description Devuelve los detalles de un chat directo específico +// @Tags Chat +// @Accept json +// @Produce json +// @Security BearerAuth +// @Param chatId path string true "ID del chat directo" +// @Success 200 {object} models.DirectChat "Detalles del chat directo" +// @Failure 401 {string} string "No autorizado" +// @Failure 404 {string} string "Chat no encontrado" +// @Failure 500 {string} string "Error interno del servidor" +// @Router /chat/direct/{chatId} [get] +func (h *ChatHandler) GetChat(w http.ResponseWriter, r *http.Request) { + chatID := chi.URLParam(r, "chatId") + + // Obtener el ID del usuario del contexto + userID, ok := r.Context().Value("userID").(string) + if !ok { + http.Error(w, "User ID not found in context", http.StatusInternalServerError) + return + } + + chat, err := h.DirectChatService.GetDirectChat(chatID) + if err != nil { + http.Error(w, "Error getting chat: "+err.Error(), http.StatusInternalServerError) + return + } + + // Verificar si el usuario tiene acceso a este chat + hasAccess := false + for _, id := range chat.UserIDs { + if id == userID { + hasAccess = true + break + } + } + + if !hasAccess { + http.Error(w, "Unauthorized access to this chat", http.StatusForbidden) + return + } + + json.NewEncoder(w).Encode(chat) +} diff --git a/internal/routes/router.go b/internal/routes/router.go index 147b58d..90021b0 100644 --- a/internal/routes/router.go +++ b/internal/routes/router.go @@ -77,6 +77,7 @@ func NewRouter( r.Route("/direct", func(r chi.Router) { r.Post("/{otherUserId}", chatHandler.CreateDirectChat) r.Get("/me", chatHandler.GetUserDirectChats) + r.Get("/{chatId}", chatHandler.GetChat) r.Get("/{chatId}/messages", chatHandler.GetDirectChatMessages) }) })