From 52e6704e431ab3becb08f6837dcfff9dd2c30af3 Mon Sep 17 00:00:00 2001 From: ddlifter Date: Thu, 23 May 2024 01:35:19 +0300 Subject: [PATCH] feat(client): Implement createroom frontend-client --- client/src/handle_front/handle_front.go | 27 +++++++++++++++++++++++++ client/src/main.go | 5 +++-- client/src/send_message/send_message.go | 19 ++++++++++------- frontend/scripts/create_room.js | 8 ++------ 4 files changed, 44 insertions(+), 15 deletions(-) diff --git a/client/src/handle_front/handle_front.go b/client/src/handle_front/handle_front.go index 1113ecd..9d78ed9 100644 --- a/client/src/handle_front/handle_front.go +++ b/client/src/handle_front/handle_front.go @@ -24,11 +24,16 @@ func CORSHandler(next http.Handler) http.Handler { type Message struct { Message string `json:"message"` } + type UserCreds struct { Login string `json:"login"` Password string `json:"password"` } +type Usernames struct { + Usernames []string `json:"usernames"` +} + func sendMessageHandler(w http.ResponseWriter, r *http.Request) { var text Message err := json.NewDecoder(r.Body).Decode(&text) @@ -74,11 +79,33 @@ func loginHandler(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(map[string]string{"status": "success"}) } +func createroomHandler(w http.ResponseWriter, r *http.Request) { + var usernames Usernames + err := json.NewDecoder(r.Body).Decode(&usernames) + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + fmt.Println(usernames.Usernames) + + var usernames2 []string + + for _, value := range usernames.Usernames { + usernames2 = append(usernames2, sendmessage.LookUpUser(value)) + } + fmt.Println(usernames2) + sendmessage.CreateRoom(usernames2) + + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(map[string]string{"status": "success"}) +} + func main() { mux := http.NewServeMux() mux.HandleFunc("/send", sendMessageHandler) mux.HandleFunc("/register", registerHandler) mux.HandleFunc("/login", loginHandler) + mux.HandleFunc("/createroom", createroomHandler) // Добавляем CORSHandler handler := CORSHandler(mux) diff --git a/client/src/main.go b/client/src/main.go index d1e0763..a0dfe5c 100644 --- a/client/src/main.go +++ b/client/src/main.go @@ -10,7 +10,8 @@ func main() { //sendmessage.SubscribeToUser() //sendmessage.ReceiveMessage("1e3f536e-9948-4980-b09a-7c54032ae6c2") //sendmessage.SendMessage("Дима ЛОХ!!!", "bcf4ca3f-54ee-4137-916b-4b973ad4f8c6") - //sendmessage.CreateRoom("f27925bc-d039-43e4-abb1-661c6934dfa8") + value := sendmessage.LookUpUser("klausr") + sendmessage.CreateRoom([]string{value}) //sendmessage.ListMessages("1e3f536e-9948-4980-b09a-7c54032ae6c2") - sendmessage.LookUpUser("klausr") + //sendmessage.LookUpUser("klausr") } diff --git a/client/src/send_message/send_message.go b/client/src/send_message/send_message.go index 370cab5..a93cc02 100644 --- a/client/src/send_message/send_message.go +++ b/client/src/send_message/send_message.go @@ -177,7 +177,7 @@ func SubscribeToUser() { } } -func CreateRoom(uuid string) { +func CreateRoom(uuids []string) { UserUUID, AuthToken := getUserAuthData() conn, err := grpc.Dial("luna:9001", grpc.WithInsecure()) @@ -194,11 +194,15 @@ func CreateRoom(uuid string) { )) roomReq := &proto.ClientsideRoom{ - Name: "Room111", - Members: []*proto.UUID{ - &proto.UUID{Uuid: UserUUID}, - &proto.UUID{Uuid: uuid}, - }, + Name: "Room111", + Members: []*proto.UUID{}, + } + + currentUserUUID := &proto.UUID{Uuid: UserUUID} + roomReq.Members = append(roomReq.Members, currentUserUUID) + + for _, uuid := range uuids { + roomReq.Members = append(roomReq.Members, &proto.UUID{Uuid: uuid}) } response, err := client.CreateRoom(ctx, roomReq) @@ -272,7 +276,7 @@ func ListMessages(room string) { } } -func LookUpUser(user string) { +func LookUpUser(user string) string { UserUUID, AuthToken := getUserAuthData() conn, err := grpc.Dial("luna:9001", grpc.WithInsecure()) @@ -304,4 +308,5 @@ func LookUpUser(user string) { fmt.Printf("Username: %s\n", lookupUserResponse.Username) fmt.Printf("User ID: %s\n", lookupUserResponse.Uuid) + return lookupUserResponse.Uuid.Uuid } diff --git a/frontend/scripts/create_room.js b/frontend/scripts/create_room.js index fa55b16..fe06252 100644 --- a/frontend/scripts/create_room.js +++ b/frontend/scripts/create_room.js @@ -26,22 +26,18 @@ function createChat() { users.push(input.value.trim()); } }); - fetch("http://localhost:8080/createroom", { method: "POST", headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ - usernames: users - }) + body: JSON.stringify({usernames: users}) }) - .then(response => response.json()) .then(data => { alert("Чат создан с пользователями: " + users.join(', ')); console.log("Чат создан:", data); window.location.href = "index.html"; - }) + }) .catch(error => { console.error("Ошибка при создании чата:", error); });