diff --git a/OpenApi.yml b/OpenApi.yml index 9dc7cd3..b8128ad 100644 --- a/OpenApi.yml +++ b/OpenApi.yml @@ -56,6 +56,14 @@ paths: - rooms summary: Create new room operationId: roomCreate + parameters: + - in: query + name: start + required: false + schema: + type: boolean + default: true + description: Start room after creation requestBody: content: application/json: @@ -257,6 +265,14 @@ paths: required: true schema: type: string + - in: query + name: start + required: false + schema: + type: boolean + description: | + Whether the room should be started or stopped after + recreation, default is the current state of the room. requestBody: content: application/json: diff --git a/client/src/api/api.ts b/client/src/api/api.ts index 892c6b3..390dd14 100644 --- a/client/src/api/api.ts +++ b/client/src/api/api.ts @@ -1056,11 +1056,12 @@ export const RoomsApiAxiosParamCreator = function (configuration?: Configuration /** * * @summary Create new room + * @param {boolean} [start] * @param {RoomSettings} [roomSettings] * @param {*} [options] Override http request option. * @throws {RequiredError} */ - roomCreate: async (roomSettings?: RoomSettings, options: AxiosRequestConfig = {}): Promise => { + roomCreate: async (start?: boolean, roomSettings?: RoomSettings, options: AxiosRequestConfig = {}): Promise => { const localVarPath = `/api/rooms`; // use dummy base URL string because the URL constructor only accepts absolute URLs. const localVarUrlObj = new URL(localVarPath, DUMMY_BASE_URL); @@ -1073,6 +1074,10 @@ export const RoomsApiAxiosParamCreator = function (configuration?: Configuration const localVarHeaderParameter = {} as any; const localVarQueryParameter = {} as any; + if (start !== undefined) { + localVarQueryParameter['start'] = start; + } + localVarHeaderParameter['Content-Type'] = 'application/json'; @@ -1159,11 +1164,12 @@ export const RoomsApiAxiosParamCreator = function (configuration?: Configuration * * @summary Recreate room * @param {string} roomId + * @param {boolean} [start] * @param {RoomSettings} [roomSettings] * @param {*} [options] Override http request option. * @throws {RequiredError} */ - roomRecreate: async (roomId: string, roomSettings?: RoomSettings, options: AxiosRequestConfig = {}): Promise => { + roomRecreate: async (roomId: string, start?: boolean, roomSettings?: RoomSettings, options: AxiosRequestConfig = {}): Promise => { // verify required parameter 'roomId' is not null or undefined assertParamExists('roomRecreate', 'roomId', roomId) const localVarPath = `/api/rooms/{roomId}/recreate` @@ -1179,6 +1185,10 @@ export const RoomsApiAxiosParamCreator = function (configuration?: Configuration const localVarHeaderParameter = {} as any; const localVarQueryParameter = {} as any; + if (start !== undefined) { + localVarQueryParameter['start'] = start; + } + localVarHeaderParameter['Content-Type'] = 'application/json'; @@ -1457,12 +1467,13 @@ export const RoomsApiFp = function(configuration?: Configuration) { /** * * @summary Create new room + * @param {boolean} [start] * @param {RoomSettings} [roomSettings] * @param {*} [options] Override http request option. * @throws {RequiredError} */ - async roomCreate(roomSettings?: RoomSettings, options?: AxiosRequestConfig): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise> { - const localVarAxiosArgs = await localVarAxiosParamCreator.roomCreate(roomSettings, options); + async roomCreate(start?: boolean, roomSettings?: RoomSettings, options?: AxiosRequestConfig): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise> { + const localVarAxiosArgs = await localVarAxiosParamCreator.roomCreate(start, roomSettings, options); return createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration); }, /** @@ -1491,12 +1502,13 @@ export const RoomsApiFp = function(configuration?: Configuration) { * * @summary Recreate room * @param {string} roomId + * @param {boolean} [start] * @param {RoomSettings} [roomSettings] * @param {*} [options] Override http request option. * @throws {RequiredError} */ - async roomRecreate(roomId: string, roomSettings?: RoomSettings, options?: AxiosRequestConfig): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise> { - const localVarAxiosArgs = await localVarAxiosParamCreator.roomRecreate(roomId, roomSettings, options); + async roomRecreate(roomId: string, start?: boolean, roomSettings?: RoomSettings, options?: AxiosRequestConfig): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise> { + const localVarAxiosArgs = await localVarAxiosParamCreator.roomRecreate(roomId, start, roomSettings, options); return createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration); }, /** @@ -1598,12 +1610,13 @@ export const RoomsApiFactory = function (configuration?: Configuration, basePath /** * * @summary Create new room + * @param {boolean} [start] * @param {RoomSettings} [roomSettings] * @param {*} [options] Override http request option. * @throws {RequiredError} */ - roomCreate(roomSettings?: RoomSettings, options?: any): AxiosPromise { - return localVarFp.roomCreate(roomSettings, options).then((request) => request(axios, basePath)); + roomCreate(start?: boolean, roomSettings?: RoomSettings, options?: any): AxiosPromise { + return localVarFp.roomCreate(start, roomSettings, options).then((request) => request(axios, basePath)); }, /** * @@ -1629,12 +1642,13 @@ export const RoomsApiFactory = function (configuration?: Configuration, basePath * * @summary Recreate room * @param {string} roomId + * @param {boolean} [start] * @param {RoomSettings} [roomSettings] * @param {*} [options] Override http request option. * @throws {RequiredError} */ - roomRecreate(roomId: string, roomSettings?: RoomSettings, options?: any): AxiosPromise { - return localVarFp.roomRecreate(roomId, roomSettings, options).then((request) => request(axios, basePath)); + roomRecreate(roomId: string, start?: boolean, roomSettings?: RoomSettings, options?: any): AxiosPromise { + return localVarFp.roomRecreate(roomId, start, roomSettings, options).then((request) => request(axios, basePath)); }, /** * @@ -1730,13 +1744,14 @@ export class RoomsApi extends BaseAPI { /** * * @summary Create new room + * @param {boolean} [start] * @param {RoomSettings} [roomSettings] * @param {*} [options] Override http request option. * @throws {RequiredError} * @memberof RoomsApi */ - public roomCreate(roomSettings?: RoomSettings, options?: AxiosRequestConfig) { - return RoomsApiFp(this.configuration).roomCreate(roomSettings, options).then((request) => request(this.axios, this.basePath)); + public roomCreate(start?: boolean, roomSettings?: RoomSettings, options?: AxiosRequestConfig) { + return RoomsApiFp(this.configuration).roomCreate(start, roomSettings, options).then((request) => request(this.axios, this.basePath)); } /** @@ -1767,13 +1782,14 @@ export class RoomsApi extends BaseAPI { * * @summary Recreate room * @param {string} roomId + * @param {boolean} [start] * @param {RoomSettings} [roomSettings] * @param {*} [options] Override http request option. * @throws {RequiredError} * @memberof RoomsApi */ - public roomRecreate(roomId: string, roomSettings?: RoomSettings, options?: AxiosRequestConfig) { - return RoomsApiFp(this.configuration).roomRecreate(roomId, roomSettings, options).then((request) => request(this.axios, this.basePath)); + public roomRecreate(roomId: string, start?: boolean, roomSettings?: RoomSettings, options?: AxiosRequestConfig) { + return RoomsApiFp(this.configuration).roomRecreate(roomId, start, roomSettings, options).then((request) => request(this.axios, this.basePath)); } /** diff --git a/client/src/store/index.ts b/client/src/store/index.ts index 2019baf..7b59b0f 100644 --- a/client/src/store/index.ts +++ b/client/src/store/index.ts @@ -85,7 +85,7 @@ export default new Vuex.Store({ commit('ROOMS_SET', res.data); }, async ROOMS_CREATE({ commit }: ActionContext, roomSettings: RoomSettings): Promise { - const res = await roomsApi.roomCreate(roomSettings) + const res = await roomsApi.roomCreate(true, roomSettings) commit('ROOMS_ADD', res.data); return res.data }, diff --git a/internal/api/rooms.go b/internal/api/rooms.go index f1e347c..612e101 100644 --- a/internal/api/rooms.go +++ b/internal/api/rooms.go @@ -6,6 +6,7 @@ import ( "errors" "io" "net/http" + "strconv" "strings" "github.com/go-chi/chi/v5" @@ -38,6 +39,16 @@ func (manager *ApiManagerCtx) roomsList(w http.ResponseWriter, r *http.Request) } func (manager *ApiManagerCtx) roomCreate(w http.ResponseWriter, r *http.Request) { + var start = true // default value + if s := r.URL.Query().Get("start"); s != "" { + var err error + start, err = strconv.ParseBool(s) + if err != nil { + http.Error(w, err.Error(), 400) + return + } + } + // Default values request := types.RoomSettings{ MaxConnections: 10, @@ -58,10 +69,12 @@ func (manager *ApiManagerCtx) roomCreate(w http.ResponseWriter, r *http.Request) return } - if err := manager.rooms.Start(r.Context(), ID); err != nil { - manager.logger.Error().Err(err).Msg("create: failed to start room") - http.Error(w, err.Error(), 500) - return + if start { + if err := manager.rooms.Start(r.Context(), ID); err != nil { + manager.logger.Error().Err(err).Msg("create: failed to start room") + http.Error(w, err.Error(), 500) + return + } } response, err := manager.rooms.GetEntry(r.Context(), ID) @@ -78,21 +91,37 @@ func (manager *ApiManagerCtx) roomCreate(w http.ResponseWriter, r *http.Request) func (manager *ApiManagerCtx) roomRecreate(w http.ResponseWriter, r *http.Request) { roomId := chi.URLParam(r, "roomId") - entry, err := manager.rooms.GetEntry(r.Context(), roomId) - if err != nil { - if errors.Is(err, types.ErrRoomNotFound) { - http.Error(w, err.Error(), 404) - } else { - manager.logger.Error().Err(err).Msg("recreate: failed to get room entry") - http.Error(w, err.Error(), 500) + var start bool + if s := r.URL.Query().Get("start"); s != "" { + var err error + start, err = strconv.ParseBool(s) + if err != nil { + http.Error(w, err.Error(), 400) + return } - return + } else { + entry, err := manager.rooms.GetEntry(r.Context(), roomId) + if err != nil { + if errors.Is(err, types.ErrRoomNotFound) { + http.Error(w, err.Error(), 404) + } else { + manager.logger.Error().Err(err).Msg("recreate: failed to get room entry") + http.Error(w, err.Error(), 500) + } + return + } + + start = entry.Running } settings, err := manager.rooms.GetSettings(r.Context(), roomId) if err != nil { - manager.logger.Error().Err(err).Msg("recreate: failed to get room settings") - http.Error(w, err.Error(), 500) + if errors.Is(err, types.ErrRoomNotFound) { + http.Error(w, err.Error(), 404) + } else { + manager.logger.Error().Err(err).Msg("recreate: failed to get room settings") + http.Error(w, err.Error(), 500) + } return } @@ -115,7 +144,7 @@ func (manager *ApiManagerCtx) roomRecreate(w http.ResponseWriter, r *http.Reques return } - if entry.Running { + if start { if err := manager.rooms.Start(r.Context(), ID); err != nil { manager.logger.Error().Err(err).Msg("recreate: failed to start room") http.Error(w, err.Error(), 500)