Feat: Add JWT config, auth middleware, and token parsing using username.

Signed-off-by: Goldbro233 <bowensun_06@outlook.com>
This commit is contained in:
2025-07-20 21:03:42 +08:00
parent 490c1d281f
commit 3698c4b339
8 changed files with 128 additions and 4 deletions

View File

@@ -0,0 +1,43 @@
package controllers
import (
"GinTutorial/global"
"GinTutorial/models"
"net/http"
"time"
"github.com/gin-gonic/gin"
)
func CreateExchangeRate(ctx *gin.Context) {
var exchangeRate models.ExchangeRate
if err := ctx.ShouldBindJSON(&exchangeRate); err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
exchangeRate.Date = time.Now()
if err := global.Db.AutoMigrate(&exchangeRate); err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
if err := global.Db.Create(&exchangeRate).Error; err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
ctx.JSON(http.StatusCreated, gin.H{"data": exchangeRate})
}
func GetExchangeRates(ctx *gin.Context) {
var exchangeRates []models.ExchangeRate
if err := global.Db.Find(&exchangeRates).Error; err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
ctx.JSON(http.StatusOK, gin.H{"data": exchangeRates})
}