diff --git a/server/api/apierr/enums.go b/server/api/apierr/enums.go index f82bf44..e0d2728 100644 --- a/server/api/apierr/enums.go +++ b/server/api/apierr/enums.go @@ -23,7 +23,8 @@ const ( TIMESTAMP_OUT_OF_RANGE APIError = 1205 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 diff --git a/server/api/handler/api.go b/server/api/handler/api.go index ef1fba9..7adb920 100644 --- a/server/api/handler/api.go +++ b/server/api/handler/api.go @@ -247,7 +247,7 @@ func (h APIHandler) ListClients(g *gin.Context) ginresp.HTTPResponse { clients, err := h.app.Database.ListClients(ctx, u.UserID) 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() }) @@ -255,8 +255,47 @@ func (h APIHandler) ListClients(g *gin.Context) ginresp.HTTPResponse { 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 { - 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 { diff --git a/server/db/methods.go b/server/db/methods.go index d4f22ed..ffb8a21 100644 --- a/server/db/methods.go +++ b/server/db/methods.go @@ -221,3 +221,22 @@ func (db *Database) ListClients(ctx TxContext, userid int64) ([]models.Client, e 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 +} diff --git a/server/swagger/swagger.json b/server/swagger/swagger.json index e52c461..6ab7c7b 100644 --- a/server/swagger/swagger.json +++ b/server/swagger/swagger.json @@ -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": { "get": { "summary": "Acknowledge that a message was received", diff --git a/server/swagger/swagger.yaml b/server/swagger/swagger.yaml index 015c2f6..bb8c416 100644 --- a/server/swagger/swagger.yaml +++ b/server/swagger/swagger.yaml @@ -377,6 +377,42 @@ paths: schema: $ref: '#/definitions/ginresp.apiError' 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: get: operationId: compat-ack