api: added devices list api endpoint
This commit is contained in:
parent
6edf2c534d
commit
7ad677209b
77
api/devices_list.go
Normal file
77
api/devices_list.go
Normal file
|
@ -0,0 +1,77 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/tigorlazuardi/redmage/db/queries"
|
||||
"github.com/tigorlazuardi/redmage/pkg/errs"
|
||||
)
|
||||
|
||||
type DevicesListParams struct {
|
||||
All bool
|
||||
Query string
|
||||
Limit int64
|
||||
Offset int64
|
||||
}
|
||||
|
||||
type DevicesListResult struct {
|
||||
Devices []queries.Device `json:"devices"`
|
||||
Total int64 `json:"total"`
|
||||
}
|
||||
|
||||
func (api *API) DevicesList(ctx context.Context, params DevicesListParams) (result DevicesListResult, err error) {
|
||||
ctx, span := tracer.Start(ctx, "*API.DevicesList")
|
||||
defer span.End()
|
||||
|
||||
q := params.Query
|
||||
|
||||
if params.All {
|
||||
result.Devices, err = api.queries.DeviceGetAll(ctx)
|
||||
if err != nil {
|
||||
return result, errs.Wrapw(err, "failed to get all devices", "params", params)
|
||||
}
|
||||
result.Total, err = api.queries.DeviceCount(ctx)
|
||||
if err != nil {
|
||||
return result, errs.Wrapw(err, "failed to count all devices", "params", params)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
if q != "" {
|
||||
like := "%" + q + "%"
|
||||
result.Devices, err = api.queries.DeviceSearch(ctx, queries.DeviceSearchParams{
|
||||
Name: like,
|
||||
Slug: like,
|
||||
Limit: params.Limit,
|
||||
Offset: params.Offset,
|
||||
})
|
||||
if err != nil {
|
||||
return result, errs.Wrapw(err, "failed to search device", "params", params)
|
||||
}
|
||||
result.Total, err = api.queries.DeviceSearchCount(ctx, queries.DeviceSearchCountParams{
|
||||
Name: like,
|
||||
Slug: like,
|
||||
Limit: params.Limit,
|
||||
Offset: params.Offset,
|
||||
})
|
||||
if err != nil {
|
||||
return result, errs.Wrapw(err, "failed to count device search", "params", params)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
result.Devices, err = api.queries.DeviceList(ctx, queries.DeviceListParams{
|
||||
Limit: params.Limit,
|
||||
Offset: params.Offset,
|
||||
})
|
||||
if err != nil {
|
||||
return result, errs.Wrapw(err, "failed to list device", "params", params)
|
||||
}
|
||||
|
||||
result.Total, err = api.queries.DeviceCount(ctx)
|
||||
if err != nil {
|
||||
return result, errs.Wrapw(err, "failed to count all devices", "params", params)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
|
@ -188,6 +188,7 @@ func (api *API) createDeviceImageWriters(post reddit.Post, devices []queries.Dev
|
|||
}
|
||||
return nil, nil, errs.Wrapw(err, "failed to open temp image file",
|
||||
"device_name", device.Name,
|
||||
"device_slug", device.Slug,
|
||||
"filename", filename,
|
||||
)
|
||||
}
|
||||
|
|
|
@ -240,12 +240,12 @@ func (post *Post) GetThumbnailRelativePath() string {
|
|||
}
|
||||
|
||||
func (post *Post) GetImageRelativePath(device queries.Device) string {
|
||||
return path.Join(device.Name, post.GetSubreddit(), post.GetImageFilename())
|
||||
return path.Join(device.Slug, post.GetSubreddit(), post.GetImageFilename())
|
||||
}
|
||||
|
||||
func (post *Post) GetWindowsWallpaperImageRelativePath(device queries.Device) string {
|
||||
filename := fmt.Sprintf("%s_%s", post.GetSubreddit(), post.GetImageFilename())
|
||||
return path.Join(device.Name, filename)
|
||||
return path.Join(device.Slug, filename)
|
||||
}
|
||||
|
||||
func (post *Post) GetImageFilename() string {
|
||||
|
|
|
@ -34,7 +34,7 @@ var DefaultConfig = map[string]any{
|
|||
"telemetry.openobserve.log.endpoint": "http://localhost:5080/api/default/default/_json",
|
||||
"telemetry.openobserve.log.concurrency": 4,
|
||||
"telemetry.openobserve.log.buffer.size": 2 * 1024, // 2kb
|
||||
"telemetry.openobserve.log.buffer.timeout": "500ms",
|
||||
"telemetry.openobserve.log.buffer.timeout": "2s",
|
||||
"telemetry.openobserve.log.username": "root@example.com",
|
||||
"telemetry.openobserve.log.password": "Complexpass#123",
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
-- +goose StatementBegin
|
||||
CREATE TABLE devices(
|
||||
id INTEGER PRIMARY KEY,
|
||||
slug VARCHAR(255) NOT NULL,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
resolution_x DOUBLE NOT NULL,
|
||||
resolution_y DOUBLE NOT NULL,
|
||||
|
@ -16,7 +17,7 @@ CREATE TABLE devices(
|
|||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
|
||||
);
|
||||
|
||||
CREATE INDEX idx_devices_name ON devices(name);
|
||||
CREATE UNIQUE INDEX idx_devices_name ON devices(slug);
|
||||
|
||||
CREATE TRIGGER update_devices_timestamp AFTER UPDATE ON devices FOR EACH ROW
|
||||
BEGIN
|
||||
|
|
|
@ -1,7 +1,28 @@
|
|||
-- name: GetDevices :many
|
||||
SELECT * FROM devices;
|
||||
-- name: DeviceGetAll :many
|
||||
SELECT * FROM devices
|
||||
ORDER BY name;
|
||||
|
||||
-- name: CreateDevice :one
|
||||
-- name: DeviceCount :one
|
||||
SELECT COUNT(*) FROM devices;
|
||||
|
||||
-- name: DeviceList :many
|
||||
SELECT * FROM devices
|
||||
ORDER BY name
|
||||
LIMIT ? OFFSET ?;
|
||||
|
||||
-- name: DeviceSearch :many
|
||||
SELECT * FROM devices
|
||||
WHERE (name LIKE ? OR slug LIKE ?)
|
||||
ORDER BY name
|
||||
LIMIT ? OFFSET ?;
|
||||
|
||||
-- name: DeviceSearchCount :one
|
||||
SELECT COUNT(*) FROM devices
|
||||
WHERE (name LIKE ? OR slug LIKE ?)
|
||||
ORDER BY name
|
||||
LIMIT ? OFFSET ?;
|
||||
|
||||
-- name: DeviceCreate :one
|
||||
INSERT INTO devices (name, resolution_x, resolution_y, aspect_ratio_tolerance, min_x, min_y, max_x, max_y, nsfw, windows_wallpaper_mode)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
RETURNING *;
|
||||
|
|
41
server/routes/api_device_list.go
Normal file
41
server/routes/api_device_list.go
Normal file
|
@ -0,0 +1,41 @@
|
|||
package routes
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/tigorlazuardi/redmage/api"
|
||||
"github.com/tigorlazuardi/redmage/pkg/log"
|
||||
)
|
||||
|
||||
func (routes *Routes) APIDeviceList(rw http.ResponseWriter, r *http.Request) {
|
||||
ctx, span := tracer.Start(r.Context(), "*Routes.APIDeviceList")
|
||||
defer span.End()
|
||||
|
||||
query := parseApiDeviceListQueries(r)
|
||||
|
||||
result, err := routes.API.DevicesList(ctx, query)
|
||||
if err != nil {
|
||||
rw.WriteHeader(http.StatusInternalServerError)
|
||||
_ = json.NewEncoder(rw).Encode(map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if err := json.NewEncoder(rw).Encode(result); err != nil {
|
||||
log.New(ctx).Err(err).Error("failed to marshal json api devices")
|
||||
}
|
||||
}
|
||||
|
||||
func parseApiDeviceListQueries(req *http.Request) (params api.DevicesListParams) {
|
||||
params.All, _ = strconv.ParseBool(req.FormValue("all"))
|
||||
params.Offset, _ = strconv.ParseInt(req.FormValue("offset"), 10, 64)
|
||||
params.Limit, _ = strconv.ParseInt(req.FormValue("limit"), 10, 64)
|
||||
params.Query = req.FormValue("q")
|
||||
|
||||
if params.Limit < 1 {
|
||||
params.Limit = 10
|
||||
}
|
||||
|
||||
return params
|
||||
}
|
|
@ -35,6 +35,7 @@ func (routes *Routes) registerV1APIRoutes(router chi.Router) {
|
|||
router.Use(chimiddleware.SetHeader("Content-Type", "application/json"))
|
||||
|
||||
router.Get("/subreddits", routes.SubredditsListAPI)
|
||||
router.Get("/devices", routes.APIDeviceList)
|
||||
}
|
||||
|
||||
func (routes *Routes) registerWWWRoutes(router chi.Router) {
|
||||
|
|
Loading…
Reference in a new issue