82 lines
1.9 KiB
Go
82 lines
1.9 KiB
Go
package handlers
|
|
|
|
import (
|
|
"database/sql"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"query-database/api/internal/auth"
|
|
)
|
|
|
|
func (h *Handlers) Me(c *gin.Context) {
|
|
userID := auth.UserID(c)
|
|
var email, name, moduleKey, exp string
|
|
var onboarding int
|
|
if err := h.sqlite.QueryRow(
|
|
`SELECT email, name, module_key, experience_level, onboarding_completed FROM users WHERE id = ?`,
|
|
userID,
|
|
).Scan(&email, &name, &moduleKey, &exp, &onboarding); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"message": "服务异常"})
|
|
return
|
|
}
|
|
|
|
active, err := h.getActiveDatabase(userID)
|
|
if err != nil {
|
|
if onboarding == 1 {
|
|
now := time.Now().UTC().Format(time.RFC3339)
|
|
_ = h.activateMockForUser(userID, moduleKey, now)
|
|
active, _ = h.getActiveDatabase(userID)
|
|
}
|
|
}
|
|
|
|
var activeDBID any = nil
|
|
var activeDB any = nil
|
|
if active != nil {
|
|
activeDBID = active.ID
|
|
activeDB = gin.H{
|
|
"id": active.ID,
|
|
"name": active.Name,
|
|
"source": active.Source,
|
|
"schemaName": active.SchemaName,
|
|
}
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"id": userID,
|
|
"email": email,
|
|
"name": name,
|
|
"moduleKey": moduleKey,
|
|
"experienceLevel": exp,
|
|
"onboardingCompleted": onboarding == 1,
|
|
"activeDatabaseId": activeDBID,
|
|
"activeDatabase": activeDB,
|
|
})
|
|
}
|
|
|
|
type activeDatabase struct {
|
|
ID string
|
|
Name string
|
|
Source string
|
|
SchemaName string
|
|
}
|
|
|
|
func (h *Handlers) getActiveDatabase(userID string) (*activeDatabase, error) {
|
|
row := h.sqlite.QueryRow(
|
|
`SELECT id, name, source, schema_name
|
|
FROM user_databases
|
|
WHERE user_id = ? AND is_active = 1
|
|
LIMIT 1`,
|
|
userID,
|
|
)
|
|
var id, name, source, schema string
|
|
if err := row.Scan(&id, &name, &source, &schema); err != nil {
|
|
if err == sql.ErrNoRows {
|
|
return nil, err
|
|
}
|
|
return nil, err
|
|
}
|
|
return &activeDatabase{ID: id, Name: name, Source: source, SchemaName: schema}, nil
|
|
}
|