diff --git a/server/api/api.go b/server/api/api.go index 8a71eab..6b4e682 100644 --- a/server/api/api.go +++ b/server/api/api.go @@ -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) diff --git a/server/api/v1/helper.go b/server/api/v1/helper.go index b073d21..56cf9d0 100644 --- a/server/api/v1/helper.go +++ b/server/api/v1/helper.go @@ -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) { diff --git a/server/api/v1/stream.go b/server/api/v1/stream.go index ae20bdb..421202f 100644 --- a/server/api/v1/stream.go +++ b/server/api/v1/stream.go @@ -2,7 +2,9 @@ package v1 import ( "net/http" + "woom/server/model" + "github.com/go-chi/chi/v5" "github.com/go-chi/render" ) @@ -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