Skip to content

Commit

Permalink
refactor(server): update room stream
Browse files Browse the repository at this point in the history
  • Loading branch information
a-wing committed Dec 28, 2023
1 parent 74292a4 commit f907523
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
2 changes: 1 addition & 1 deletion server/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func NewApi(rdb *redis.Client, live777Url string, live777Token string) http.Hand
r.Get("/room/{roomId}", handle.ShowRoom)
//r.Patch("/room/{roomId}", handle.UpdateRoom)
r.Post("/room/{roomId}/stream", handle.CreateRoomStream)
//r.Patch("/room/{roomId}/stream/{streamId}", handle.UpdateRoomStream)
r.Patch("/room/{roomId}/stream/{streamId}", handle.UpdateRoomStream)

//r.Post("/room/{roomId}/message", handle.CreateMessage)
//r.Get("/room/{roomId}/message", handle.ShowMessage)
Expand Down
19 changes: 3 additions & 16 deletions server/api/v1/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,13 @@ func (h *Handler) helperCreateStreamId() (string, error) {
return id.String(), err
}

func (h *Handler) helperCreateRoomStream(r *http.Request, roomId, streamId string) (*model.Stream, error) {
stream := &model.Stream{
// TODO:
Name: "",
// TODO:
Token: "",
Audio: false,
Video: false,
Screen: false,
}

func (h *Handler) helperSetRoomStream(r *http.Request, roomId, streamId string, stream *model.Stream) error {
gobStream, err := helper.GobEncode(stream)
if err != nil {
return stream, err
return err
}

if err := h.rdb.HSet(context.TODO(), roomId, streamId, gobStream).Err(); err != nil {
return stream, err
}
return stream, err
return h.rdb.HSet(context.TODO(), roomId, streamId, gobStream).Err()
}

func (h *Handler) helperShowRoom(r *http.Request) (*model.Room, error) {
Expand Down
29 changes: 28 additions & 1 deletion server/api/v1/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package v1

import (
"net/http"
"woom/server/model"

"github.com/go-chi/chi/v5"
"github.com/go-chi/render"
)

Expand All @@ -15,7 +17,32 @@ func (h *Handler) CreateRoomStream(w http.ResponseWriter, r *http.Request) {
}

streamId, err := h.helperCreateStreamId()
if _, err := h.helperCreateRoomStream(r, room.RoomId, streamId); err != nil {
if err := h.helperSetRoomStream(r, room.RoomId, streamId, &model.Stream{}); err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
room.StreamId = streamId
render.JSON(w, r, room)
}

func (h *Handler) UpdateRoomStream(w http.ResponseWriter, r *http.Request) {
streamId := chi.URLParam(r, "streamId")
room, err := h.helperShowRoom(r)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}

stream := &model.Stream{}
if err := render.DecodeJSON(r.Body, stream); err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}

if err := h.helperSetRoomStream(r, room.RoomId, streamId, stream); err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
Expand Down

0 comments on commit f907523

Please sign in to comment.