82 lines
2.0 KiB
Go
82 lines
2.0 KiB
Go
package handlers
|
|
|
|
import (
|
|
"context"
|
|
"golias/models"
|
|
"html/template"
|
|
"net/http"
|
|
)
|
|
|
|
func HandleCreateRoom(w http.ResponseWriter, r *http.Request) {
|
|
// parse payload
|
|
payload := &models.RoomReq{
|
|
RoomPass: r.PostFormValue("room_pass"),
|
|
RoomName: r.PostFormValue("room_name"),
|
|
}
|
|
// create a room
|
|
room, err := createRoom(r.Context(), payload)
|
|
if err != nil {
|
|
msg := "failed to create a room"
|
|
log.Error(msg, "error", err)
|
|
abortWithError(w, msg)
|
|
return
|
|
}
|
|
ctx := context.WithValue(r.Context(), "current_room", room.ID)
|
|
ctx, err = updateRoomInSession(ctx, room.ID)
|
|
if err != nil {
|
|
msg := "failed to set current room to session"
|
|
log.Error(msg, "error", err)
|
|
abortWithError(w, msg)
|
|
return
|
|
}
|
|
// send msg of created room
|
|
// h.Broker.Notifier <- broker.NotificationEvent{
|
|
// EventName: models.MsgRoomListUpdate,
|
|
// Payload: fmt.Sprintf("%s created a room named %s", r.CreatorName, r.RoomName),
|
|
// }
|
|
// return html
|
|
tmpl, err := template.ParseGlob("components/*.html")
|
|
if err != nil {
|
|
abortWithError(w, err.Error())
|
|
return
|
|
}
|
|
tmpl.ExecuteTemplate(w, "base", nil)
|
|
}
|
|
|
|
func HandleRoomEnter(w http.ResponseWriter, r *http.Request) {
|
|
// parse payload
|
|
roomID := r.URL.Query().Get("id")
|
|
if roomID == "" {
|
|
// error
|
|
return
|
|
}
|
|
// create a room
|
|
room, err := getRoomByID(roomID)
|
|
if err != nil {
|
|
msg := "failed to find the room"
|
|
log.Error(msg, "error", err, "room_id", roomID)
|
|
abortWithError(w, msg)
|
|
return
|
|
}
|
|
ctx := context.WithValue(r.Context(), "current_room", room.ID)
|
|
ctx, err = updateRoomInSession(ctx, room.ID)
|
|
if err != nil {
|
|
msg := "failed to set current room to session"
|
|
log.Error(msg, "error", err)
|
|
abortWithError(w, msg)
|
|
return
|
|
}
|
|
// send msg of created room
|
|
// h.Broker.Notifier <- broker.NotificationEvent{
|
|
// EventName: models.MsgRoomListUpdate,
|
|
// Payload: fmt.Sprintf("%s created a room named %s", r.CreatorName, r.RoomName),
|
|
// }
|
|
// return html
|
|
tmpl, err := template.ParseGlob("components/*.html")
|
|
if err != nil {
|
|
abortWithError(w, err.Error())
|
|
return
|
|
}
|
|
tmpl.ExecuteTemplate(w, "base", room)
|
|
}
|