From e53f40866e1f7719ec8607b9e9a509a31c3bf9f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Sat, 19 Nov 2022 12:59:25 +0100 Subject: [PATCH] DeleteClient() --- server/api/handler/api.go | 64 +++++++++++++++++++++++++++------ server/db/methods.go | 14 ++++++++ server/swagger/swagger.json | 71 ++++++++++++++++++++++++++++++++++++- server/swagger/swagger.yaml | 48 ++++++++++++++++++++++++- 4 files changed, 185 insertions(+), 12 deletions(-) diff --git a/server/api/handler/api.go b/server/api/handler/api.go index c4a3e13..e6b1b40 100644 --- a/server/api/handler/api.go +++ b/server/api/handler/api.go @@ -257,7 +257,7 @@ func (h APIHandler) ListClients(g *gin.Context) ginresp.HTTPResponse { // GetClient swaggerdoc // -// @Summary get a single clients +// @Summary Get a single clients // @ID api-clients-get // // @Param uid path int true "UserID" @@ -300,18 +300,18 @@ func (h APIHandler) GetClient(g *gin.Context) ginresp.HTTPResponse { // AddClient swaggerdoc // -// @Summary get a single clients -// @ID api-clients-get +// @Summary Add a new clients +// @ID api-clients-create // -// @Param uid path int true "UserID" +// @Param uid path int true "UserID" // // @Param post_body body handler.AddClient.body false " " // -// @Success 200 {object} models.ClientJSON -// @Failure 400 {object} ginresp.apiError -// @Failure 401 {object} ginresp.apiError -// @Failure 404 {object} ginresp.apiError -// @Failure 500 {object} ginresp.apiError +// @Success 200 {object} models.ClientJSON +// @Failure 400 {object} ginresp.apiError +// @Failure 401 {object} ginresp.apiError +// @Failure 404 {object} ginresp.apiError +// @Failure 500 {object} ginresp.apiError // // @Router /api-v2/user/{uid}/clients [POST] func (h APIHandler) AddClient(g *gin.Context) ginresp.HTTPResponse { @@ -354,8 +354,52 @@ func (h APIHandler) AddClient(g *gin.Context) ginresp.HTTPResponse { return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, client.JSON())) } +// DeleteClient swaggerdoc +// +// @Summary Delete a client +// @ID api-clients-delete +// +// @Param uid path int true "UserID" +// @Param cid path int true "ClientID" +// +// @Success 200 {object} models.ClientJSON +// @Failure 400 {object} ginresp.apiError +// @Failure 401 {object} ginresp.apiError +// @Failure 404 {object} ginresp.apiError +// @Failure 500 {object} ginresp.apiError +// +// @Router /api-v2/user/{uid}/clients [POST] func (h APIHandler) DeleteClient(g *gin.Context) ginresp.HTTPResponse { - return ginresp.NotImplemented() + type uri struct { + UserID int64 `uri:"uid"` + ClientID int64 `uri:"cid"` + } + + var u uri + ctx, errResp := h.app.StartRequest(g, &u, nil, nil) + if errResp != nil { + return *errResp + } + defer ctx.Cancel() + + if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil { + return *permResp + } + + client, err := h.app.Database.GetClient(ctx, u.UserID, u.ClientID) + if err == sql.ErrNoRows { + return ginresp.InternAPIError(404, apierr.CLIENT_NOT_FOUND, "Client not found", err) + } + if err != nil { + return ginresp.InternAPIError(500, apierr.DATABASE_ERROR, "Failed to query client", err) + } + + err = h.app.Database.DeleteClient(ctx, u.ClientID) + if err != nil { + return ginresp.InternAPIError(500, apierr.DATABASE_ERROR, "Failed to delete client", err) + } + + return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, client.JSON())) } func (h APIHandler) ListChannels(g *gin.Context) ginresp.HTTPResponse { diff --git a/server/db/methods.go b/server/db/methods.go index ffb8a21..39e5b5c 100644 --- a/server/db/methods.go +++ b/server/db/methods.go @@ -240,3 +240,17 @@ func (db *Database) GetClient(ctx TxContext, userid int64, clientid int64) (mode return client, nil } + +func (db *Database) DeleteClient(ctx TxContext, clientid int64) error { + tx, err := ctx.GetOrCreateTransaction(db) + if err != nil { + return err + } + + _, err = tx.ExecContext(ctx, "DELETE FROM clients WHERE client_id = ?", clientid) + if err != nil { + return err + } + + return nil +} diff --git a/server/swagger/swagger.json b/server/swagger/swagger.json index 6ab7c7b..c18eb6a 100644 --- a/server/swagger/swagger.json +++ b/server/swagger/swagger.json @@ -184,11 +184,63 @@ } } } + }, + "post": { + "summary": "Delete a client", + "operationId": "api-clients-delete", + "parameters": [ + { + "type": "integer", + "description": "UserID", + "name": "uid", + "in": "path", + "required": true + }, + { + "type": "integer", + "description": "ClientID", + "name": "cid", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/models.ClientJSON" + } + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/ginresp.apiError" + } + }, + "401": { + "description": "Unauthorized", + "schema": { + "$ref": "#/definitions/ginresp.apiError" + } + }, + "404": { + "description": "Not Found", + "schema": { + "$ref": "#/definitions/ginresp.apiError" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/ginresp.apiError" + } + } + } } }, "/api-v2/user/{uid}/clients/{cid}": { "get": { - "summary": "get a single clients", + "summary": "Get a single clients", "operationId": "api-clients-get", "parameters": [ { @@ -674,6 +726,23 @@ } } }, + "handler.AddClient.body": { + "type": "object", + "properties": { + "agent_model": { + "type": "string" + }, + "agent_version": { + "type": "string" + }, + "client_type": { + "type": "string" + }, + "fcm_token": { + "type": "string" + } + } + }, "handler.CreateUser.body": { "type": "object", "properties": { diff --git a/server/swagger/swagger.yaml b/server/swagger/swagger.yaml index bb8c416..10c8f22 100644 --- a/server/swagger/swagger.yaml +++ b/server/swagger/swagger.yaml @@ -23,6 +23,17 @@ definitions: success: type: string type: object + handler.AddClient.body: + properties: + agent_model: + type: string + agent_version: + type: string + client_type: + type: string + fcm_token: + type: string + type: object handler.CreateUser.body: properties: agent_model: @@ -377,6 +388,41 @@ paths: schema: $ref: '#/definitions/ginresp.apiError' summary: List all clients + post: + operationId: api-clients-delete + parameters: + - description: UserID + in: path + name: uid + required: true + type: integer + - description: ClientID + in: path + name: cid + required: true + type: integer + responses: + "200": + description: OK + schema: + $ref: '#/definitions/models.ClientJSON' + "400": + description: Bad Request + schema: + $ref: '#/definitions/ginresp.apiError' + "401": + description: Unauthorized + schema: + $ref: '#/definitions/ginresp.apiError' + "404": + description: Not Found + schema: + $ref: '#/definitions/ginresp.apiError' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/ginresp.apiError' + summary: Delete a client /api-v2/user/{uid}/clients/{cid}: get: operationId: api-clients-get @@ -412,7 +458,7 @@ paths: description: Internal Server Error schema: $ref: '#/definitions/ginresp.apiError' - summary: get a single clients + summary: Get a single clients /api/ack.php: get: operationId: compat-ack