Feat: Add JWT config, auth middleware, and token parsing using username.
Signed-off-by: Goldbro233 <bowensun_06@outlook.com>
This commit is contained in:
43
controllers/ex_controller.go
Normal file
43
controllers/ex_controller.go
Normal 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})
|
||||
}
|
||||
Reference in New Issue
Block a user