DeleteClient()

This commit is contained in:
Mike Schwörer 2022-11-19 12:59:25 +01:00
parent 650ba20e5d
commit e53f40866e
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
4 changed files with 185 additions and 12 deletions

View File

@ -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 {

View File

@ -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
}

View File

@ -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": {

View File

@ -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