GetClient()

This commit is contained in:
Mike Schwörer 2022-11-19 12:50:41 +01:00
parent f555f0f1cf
commit 6e01c41c22
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
5 changed files with 152 additions and 3 deletions

View File

@ -23,7 +23,8 @@ const (
TIMESTAMP_OUT_OF_RANGE APIError = 1205 TIMESTAMP_OUT_OF_RANGE APIError = 1205
USER_NOT_FOUND APIError = 1301 USER_NOT_FOUND APIError = 1301
USER_AUTH_FAILED APIError = 1302 CLIENT_NOT_FOUND APIError = 1302
USER_AUTH_FAILED APIError = 1311
NO_DEVICE_LINKED APIError = 1401 NO_DEVICE_LINKED APIError = 1401

View File

@ -247,7 +247,7 @@ func (h APIHandler) ListClients(g *gin.Context) ginresp.HTTPResponse {
clients, err := h.app.Database.ListClients(ctx, u.UserID) clients, err := h.app.Database.ListClients(ctx, u.UserID)
if err != nil { if err != nil {
return ginresp.InternAPIError(500, apierr.DATABASE_ERROR, "Failed to query user", err) return ginresp.InternAPIError(500, apierr.DATABASE_ERROR, "Failed to query clients", err)
} }
res := langext.ArrMap(clients, func(v models.Client) models.ClientJSON { return v.JSON() }) res := langext.ArrMap(clients, func(v models.Client) models.ClientJSON { return v.JSON() })
@ -255,8 +255,47 @@ func (h APIHandler) ListClients(g *gin.Context) ginresp.HTTPResponse {
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, result{Clients: res})) return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, result{Clients: res}))
} }
// GetClient swaggerdoc
//
// @Summary get a single clients
// @ID api-clients-get
//
// @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/{cid} [GET]
func (h APIHandler) GetClient(g *gin.Context) ginresp.HTTPResponse { func (h APIHandler) GetClient(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.CheckPermissionUserRead(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)
}
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, client.JSON()))
} }
func (h APIHandler) AddClient(g *gin.Context) ginresp.HTTPResponse { func (h APIHandler) AddClient(g *gin.Context) ginresp.HTTPResponse {

View File

@ -221,3 +221,22 @@ func (db *Database) ListClients(ctx TxContext, userid int64) ([]models.Client, e
return data, nil return data, nil
} }
func (db *Database) GetClient(ctx TxContext, userid int64, clientid int64) (models.Client, error) {
tx, err := ctx.GetOrCreateTransaction(db)
if err != nil {
return models.Client{}, err
}
rows, err := tx.QueryContext(ctx, "SELECT * FROM clients WHERE user_id = ? AND client_id = ? LIMIT 1", userid, clientid)
if err != nil {
return models.Client{}, err
}
client, err := models.DecodeClient(rows)
if err != nil {
return models.Client{}, err
}
return client, nil
}

View File

@ -186,6 +186,60 @@
} }
} }
}, },
"/api-v2/user/{uid}/clients/{cid}": {
"get": {
"summary": "get a single clients",
"operationId": "api-clients-get",
"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/ack.php": { "/api/ack.php": {
"get": { "get": {
"summary": "Acknowledge that a message was received", "summary": "Acknowledge that a message was received",

View File

@ -377,6 +377,42 @@ paths:
schema: schema:
$ref: '#/definitions/ginresp.apiError' $ref: '#/definitions/ginresp.apiError'
summary: List all clients summary: List all clients
/api-v2/user/{uid}/clients/{cid}:
get:
operationId: api-clients-get
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: get a single clients
/api/ack.php: /api/ack.php:
get: get:
operationId: compat-ack operationId: compat-ack