feat: create room endpoint

not really sets the cookie, but returns the rendered index on success
This commit is contained in:
efim 2023-10-30 05:19:43 +00:00
parent 201b3760da
commit 850b6c693b
2 changed files with 46 additions and 6 deletions

View File

@ -68,6 +68,6 @@ func (redisRM RedisRM) Get(roomName string) (Room, bool, error) {
}
func (redisRM RedisRM) Save(room Room) error {
err := redisRM.Rdb.Set(ctx, roomNameToRedisId(room.Name), room, 0).Err() // maybe even set expiration?
err := redisRM.Rdb.Set(ctx, roomNameToRedisId(room.Name), &room, 0).Err() // maybe even set expiration?
return err
}

View File

@ -14,7 +14,7 @@ import (
func registerLoginRoutes(
templateFs *embed.FS,
sessionSM sessions.SessionManagement,
rooms rooms.RoomManager,
roomsM rooms.RoomManager,
) {
// login page
http.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
@ -38,7 +38,7 @@ func registerLoginRoutes(
pn := r.PostFormValue("personalName")
pp := r.PostFormValue("personalPassword")
room, _, err := rooms.Get(rn)
room, _, err := roomsM.Get(rn)
if err != nil {
log.Printf("/login/submit error getting room %s", rn)
// return i guess
@ -60,16 +60,56 @@ func registerLoginRoutes(
// b) get room data
// c) check if such person exists,
// either create one, or check password
// d) how should i monitor sessions?
// d) how should i monitor sessions? - space in redis
// so save session to redis and add cookie with sessionId
})
// when room does not exist
http.HandleFunc("/login/create", func(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
// TODO return error notice somehow
w.WriteHeader(http.StatusBadRequest)
}
fmt.Fprintf(w, "got submitted form %+v", r.PostForm)
roomName := r.PostFormValue("roomName")
_, exists, _ := roomsM.Get(roomName)
if exists {
// TODO return anouther error notice
log.Printf("error, room name occupied", roomName)
return
}
newRoom := rooms.Room{
Name: roomName,
PasswordHash: r.PostFormValue("roomPassword"), // TODO hash the password, not to store
AdminIds: []int64{1},
Paricipants: []rooms.Person{
{
PersonId: 1,
Name: r.PostFormValue("personalName"),
PasswordHash: r.PostFormValue("personalPassword"),
},
},
}
err = roomsM.Save(newRoom)
http.SetCookie(w, &http.Cookie{
Name: "auth",
Value: "session-id",
Secure: true,
HttpOnly: true,
Path: "/",
})
if err != nil {
log.Printf("what am i to do? error saving room %s", err)
// todo return error notice somehow
}
var templFile = "templates/index.gohtml"
tmpl := template.Must(template.ParseFS(templateFs, templFile))
w.Header().Add("HX-Retarget", "body")
w.Header().Add("HX-Push-Url", "/")
err = tmpl.Execute(w, nil)
if err != nil {
log.Printf("my error in executing template, huh\n %s", err)
}
})
// checking whether the room name already exists
@ -80,7 +120,7 @@ func registerLoginRoutes(
w.WriteHeader(http.StatusBadRequest)
}
roomName := r.PostFormValue("roomName")
_, isFound, err := rooms.Get(roomName)
_, isFound, err := roomsM.Get(roomName)
if err != nil {
log.Printf("/login/room-name-check error finding room %s\n", err)
}