70 lines
1.7 KiB
Go
70 lines
1.7 KiB
Go
package handler
|
|
|
|
import (
|
|
"database/sql"
|
|
"net/http"
|
|
|
|
"healthflow/internal/config"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type VersionHandler struct {
|
|
db *sql.DB
|
|
cfg *config.Config
|
|
}
|
|
|
|
func NewVersionHandler(db *sql.DB, cfg *config.Config) *VersionHandler {
|
|
return &VersionHandler{db: db, cfg: cfg}
|
|
}
|
|
|
|
type VersionInfo struct {
|
|
VersionCode int `json:"version_code"`
|
|
VersionName string `json:"version_name"`
|
|
DownloadURL string `json:"download_url"`
|
|
UpdateLog string `json:"update_log"`
|
|
ForceUpdate bool `json:"force_update"`
|
|
}
|
|
|
|
func (h *VersionHandler) GetLatestVersion(c *gin.Context) {
|
|
var info VersionInfo
|
|
err := h.db.QueryRow(`
|
|
SELECT version_code, version_name, download_url, update_log, force_update
|
|
FROM app_version ORDER BY version_code DESC LIMIT 1
|
|
`).Scan(&info.VersionCode, &info.VersionName, &info.DownloadURL, &info.UpdateLog, &info.ForceUpdate)
|
|
|
|
if err == sql.ErrNoRows {
|
|
c.JSON(http.StatusOK, VersionInfo{
|
|
VersionCode: 1,
|
|
VersionName: "1.0.0",
|
|
})
|
|
return
|
|
}
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "database error"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, info)
|
|
}
|
|
|
|
func (h *VersionHandler) SetVersion(c *gin.Context) {
|
|
var req VersionInfo
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
_, err := h.db.Exec(`
|
|
INSERT INTO app_version (version_code, version_name, download_url, update_log, force_update)
|
|
VALUES (?, ?, ?, ?, ?)
|
|
`, req.VersionCode, req.VersionName, req.DownloadURL, req.UpdateLog, req.ForceUpdate)
|
|
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to set version"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "version updated"})
|
|
}
|