init
This commit is contained in:
106
xiangj-adapter/internal/controller/data_controller.go
Normal file
106
xiangj-adapter/internal/controller/data_controller.go
Normal file
@@ -0,0 +1,106 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
|
||||
"xiangj-adapter/internal/apperror"
|
||||
"xiangj-adapter/internal/model"
|
||||
"xiangj-adapter/internal/service"
|
||||
)
|
||||
|
||||
type DataController struct {
|
||||
service *service.DataService
|
||||
log *slog.Logger
|
||||
}
|
||||
|
||||
func NewDataController(service *service.DataService, log *slog.Logger) *DataController {
|
||||
return &DataController{service: service, log: log}
|
||||
}
|
||||
|
||||
func (c *DataController) GetData(ctx *fiber.Ctx) error {
|
||||
op := strings.ToLower(ctx.Query("op"))
|
||||
if op == "" {
|
||||
op = "select"
|
||||
}
|
||||
switch op {
|
||||
case "select":
|
||||
return c.handleSelect(ctx)
|
||||
default:
|
||||
return apperror.BadRequest("op not supported")
|
||||
}
|
||||
}
|
||||
|
||||
func (c *DataController) handleSelect(ctx *fiber.Ctx) error {
|
||||
doid := ctx.Query("doid")
|
||||
table := strings.ToLower(lastSegment(doid))
|
||||
if table == "" {
|
||||
return apperror.BadRequest("doid is required")
|
||||
}
|
||||
|
||||
offset, err := parseNonNegativeIntDefault(ctx.Query("offset"), "offset", 0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
count, err := parsePositiveIntDefault(ctx.Query("count"), "count", 100)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
data, err := c.service.Select(ctx.Context(), table, offset, count)
|
||||
if err != nil {
|
||||
c.log.Error("select failed", "table", table, "err", err)
|
||||
return apperror.Internal("query failed")
|
||||
}
|
||||
|
||||
return ctx.JSON(model.APIResponse{Data: data, Error: ""})
|
||||
}
|
||||
|
||||
func lastSegment(input string) string {
|
||||
trimmed := strings.Trim(input, "/")
|
||||
if trimmed == "" {
|
||||
return ""
|
||||
}
|
||||
parts := strings.Split(trimmed, "/")
|
||||
return parts[len(parts)-1]
|
||||
}
|
||||
|
||||
func parseNonNegativeInt(value, field string) (int, error) {
|
||||
if value == "" {
|
||||
return 0, apperror.BadRequest(field + " is required")
|
||||
}
|
||||
parsed, err := strconv.Atoi(value)
|
||||
if err != nil || parsed < 0 {
|
||||
return 0, apperror.BadRequest(field + " must be a non-negative integer")
|
||||
}
|
||||
return parsed, nil
|
||||
}
|
||||
|
||||
func parsePositiveInt(value, field string) (int, error) {
|
||||
if value == "" {
|
||||
return 0, apperror.BadRequest(field + " is required")
|
||||
}
|
||||
parsed, err := strconv.Atoi(value)
|
||||
if err != nil || parsed <= 0 {
|
||||
return 0, apperror.BadRequest(field + " must be a positive integer")
|
||||
}
|
||||
return parsed, nil
|
||||
}
|
||||
|
||||
func parseNonNegativeIntDefault(value, field string, defaultValue int) (int, error) {
|
||||
if value == "" {
|
||||
return defaultValue, nil
|
||||
}
|
||||
return parseNonNegativeInt(value, field)
|
||||
}
|
||||
|
||||
func parsePositiveIntDefault(value, field string, defaultValue int) (int, error) {
|
||||
if value == "" {
|
||||
return defaultValue, nil
|
||||
}
|
||||
return parsePositiveInt(value, field)
|
||||
}
|
||||
Reference in New Issue
Block a user