From 7ce728e04bc00f926a0a22fd632df617bfbdf89f Mon Sep 17 00:00:00 2001 From: igh9410 Date: Sun, 5 May 2024 22:08:53 +0900 Subject: [PATCH] Fixed GET /api/chatroom/:id endpoint --- backend/.dockerignore | 3 ++- backend/.gitignore | 2 +- backend/Dockerfile.multistage | 5 +---- backend/Makefile | 11 +++++----- backend/internal/chat/handler.go | 2 +- backend/internal/chat/repository.go | 31 ++++++++++++++--------------- backend/internal/chat/service.go | 5 +++-- docker-compose.yml | 2 +- nginx.conf | 4 ++-- 9 files changed, 32 insertions(+), 33 deletions(-) diff --git a/backend/.dockerignore b/backend/.dockerignore index e5c878ad..d0780c2a 100644 --- a/backend/.dockerignore +++ b/backend/.dockerignore @@ -8,4 +8,5 @@ .docker_commands.txt ./index.html ./index2.html -./kubernetes/* \ No newline at end of file +./kubernetes/* +postgres-data/ \ No newline at end of file diff --git a/backend/.gitignore b/backend/.gitignore index fd6756bf..468e1482 100644 --- a/backend/.gitignore +++ b/backend/.gitignore @@ -40,7 +40,7 @@ dockerbuild.sh *.sh # Python test scripts -scripts/ +#scripts/ # Database migrations #db/migrations/* diff --git a/backend/Dockerfile.multistage b/backend/Dockerfile.multistage index 430f043d..834b1c30 100644 --- a/backend/Dockerfile.multistage +++ b/backend/Dockerfile.multistage @@ -1,5 +1,5 @@ # Build the application from source -FROM golang:1.21 AS build-stage +FROM golang:1.22 AS build-stage WORKDIR /app @@ -12,9 +12,6 @@ RUN go mod download # Copy the entire application COPY . . -# Run the tests -RUN CGO_ENABLED=1 GOOS=linux go test -v ./... - # Build the application RUN CGO_ENABLED=1 GOOS=linux go build -o blabber-hive ./cmd/blabber-hive/main.go diff --git a/backend/Makefile b/backend/Makefile index 08970686..c6354c41 100644 --- a/backend/Makefile +++ b/backend/Makefile @@ -9,11 +9,12 @@ run: @go run cmd/blabber-hive/main.go # Build the application using Docker -docker-build: - @echo "Generating Swagger documentation before building the application..." - @swag init -g cmd/blabber-hive/main.go - @echo "Building the application using Docker..." - @./scripts/docker-build.sh +docker-push: +#@echo "Generating Swagger documentation before building the application..." +# @swag init -g cmd/blabber-hive/main.go +# @echo "Building the application using Docker..." + @./scripts/docker-push.sh + # Run the application using Docker Compose docker-run: diff --git a/backend/internal/chat/handler.go b/backend/internal/chat/handler.go index b4a13182..9fc70629 100644 --- a/backend/internal/chat/handler.go +++ b/backend/internal/chat/handler.go @@ -50,7 +50,7 @@ func (h *Handler) GetChatRoom(c *gin.Context) { if err != nil { log.Printf("Error occured with chat room ID %v: %v", chatRoomID, err.Error()) } - res, err := h.Service.GetChatRoomInfoByID(c.Request.Context(), chatRoomID) + res, err := h.Service.GetChatRoomByID(c.Request.Context(), chatRoomID) if err != nil { c.JSON(http.StatusNotFound, gin.H{"error": fmt.Sprintf("Chat ID %s not found", chatRoomID)}) return diff --git a/backend/internal/chat/repository.go b/backend/internal/chat/repository.go index 7737e4a2..989ac94a 100644 --- a/backend/internal/chat/repository.go +++ b/backend/internal/chat/repository.go @@ -89,40 +89,39 @@ func (r *repository) FindChatRoomByID(ctx context.Context, id uuid.UUID) (*ChatR // FindChatRoomInfoByID implements Repository. func (r *repository) FindChatRoomInfoByID(ctx context.Context, chatRoomID uuid.UUID) (*ChatRoomInfo, error) { chatRoomInfo := &ChatRoomInfo{} - - query := "SELECT uicr.user_id, uicr.chat_room_id FROM users_in_chat_rooms AS uicr INNER JOIN chat_rooms AS cr ON uicr.chat_room_id = cr.id WHERE cr.id = $1" - + query := ` + SELECT + uicr.id, + uicr.user_id, + uicr.chat_room_id, + cr.created_at + FROM users_in_chat_rooms AS uicr + INNER JOIN chat_rooms AS cr ON uicr.chat_room_id = cr.id + WHERE cr.id = $1 + GROUP BY uicr.id, uicr.user_id, uicr.chat_room_id, cr.created_at + ` rows, err := r.db.QueryContext(ctx, query, chatRoomID) if err != nil { log.Printf("Failed to fetch chat room info, err: %v", err) return nil, err } - defer rows.Close() - // Create a slice to store user IDs associated with the chat room. usersInChatRoom := make([]UserInChatRoom, 0) - + var createdAt time.Time for rows.Next() { var userInChatRoom UserInChatRoom - err := rows.Scan(&userInChatRoom.UserID, &userInChatRoom.ChatRoomID) + err := rows.Scan(&userInChatRoom.ID, &userInChatRoom.UserID, &userInChatRoom.ChatRoomID, &createdAt) if err != nil { log.Printf("Failed to scan chat room info, err: %v", err) return nil, err } - log.Printf("User ID: %v", userInChatRoom.UserID) usersInChatRoom = append(usersInChatRoom, userInChatRoom) } - // Set the chat room ID in the chat room info. chatRoomInfo.ID = chatRoomID - - // Set the user IDs in the chat room info. - chatRoomInfo.UserList = make([]UserInChatRoom, 0, len(usersInChatRoom)) - for _, userInChatRoom := range usersInChatRoom { - chatRoomInfo.UserList = append(chatRoomInfo.UserList, UserInChatRoom{ID: userInChatRoom.ID, UserID: userInChatRoom.UserID, ChatRoomID: userInChatRoom.ChatRoomID}) - } - + chatRoomInfo.UserList = usersInChatRoom + chatRoomInfo.CreatedAt = createdAt return chatRoomInfo, nil } diff --git a/backend/internal/chat/service.go b/backend/internal/chat/service.go index 6cb6e7d0..edd24640 100644 --- a/backend/internal/chat/service.go +++ b/backend/internal/chat/service.go @@ -15,7 +15,7 @@ import ( type Service interface { CreateChatRoom(ctx context.Context, req *CreateChatRoomReq) (*CreateChatRoomRes, error) GetChatRoomByID(ctx context.Context, chatRoomID uuid.UUID) (*ChatRoom, error) - GetChatRoomInfoByID(ctx context.Context, chatRoomID uuid.UUID) (*ChatRoomInfo, error) + //GetChatRoomInfoByID(ctx context.Context, chatRoomID uuid.UUID) (*ChatRoomInfo, error) JoinChatRoomByID(ctx context.Context, chatRoomID uuid.UUID, userID uuid.UUID) (*ChatRoom, error) GetChatRoomList(ctx context.Context) ([]*ChatRoom, error) RegisterClient(ctx context.Context, hub *Hub, conn *websocket.Conn, chatroomID uuid.UUID, userID uuid.UUID, kafkaProducer *confluentKafka.Producer) (*Client, error) @@ -84,6 +84,7 @@ func (s *service) JoinChatRoomByID(ctx context.Context, chatroomId uuid.UUID, us } +/* // GetChatRoomInfoByID implements Service. func (s *service) GetChatRoomInfoByID(ctx context.Context, chatRoomID uuid.UUID) (*ChatRoomInfo, error) { chatRoomInfo, err := s.Repository.FindChatRoomInfoByID(ctx, chatRoomID) @@ -92,7 +93,7 @@ func (s *service) GetChatRoomInfoByID(ctx context.Context, chatRoomID uuid.UUID) return nil, err } return chatRoomInfo, nil -} +} */ // GetChatRoomList implements Service. func (s *service) GetChatRoomList(ctx context.Context) ([]*ChatRoom, error) { diff --git a/docker-compose.yml b/docker-compose.yml index e9952d02..1c48b72d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,6 +1,6 @@ services: blabber-hive: - image: igh9410/blabber-hive:v1 + image: blabber-hive:v1 container_name: blabber-hive ports: - "8080:8080" diff --git a/nginx.conf b/nginx.conf index 4e4129f5..5408d409 100644 --- a/nginx.conf +++ b/nginx.conf @@ -35,8 +35,8 @@ http { proxy_intercept_errors off; } - location /ml/ { - proxy_pass http://fastapi; + location /ml/api/ { + proxy_pass http://fastapi/api/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;