Skip to content

Commit

Permalink
feat(client): Implement createroom frontend-client
Browse files Browse the repository at this point in the history
  • Loading branch information
ddlifter committed May 22, 2024
1 parent 69c4b4d commit 52e6704
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 15 deletions.
27 changes: 27 additions & 0 deletions client/src/handle_front/handle_front.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
5 changes: 3 additions & 2 deletions client/src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
19 changes: 12 additions & 7 deletions client/src/send_message/send_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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)
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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
}
8 changes: 2 additions & 6 deletions frontend/scripts/create_room.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down

0 comments on commit 52e6704

Please sign in to comment.