refactor(charge):基本完成用户续费记录接口到fiber框架的切换。

This commit is contained in:
徐涛 2022-09-28 16:02:24 +08:00
parent 4b08952916
commit e13193de6d

View File

@ -9,35 +9,33 @@ import (
"strconv"
"time"
"github.com/gin-gonic/gin"
"github.com/gofiber/fiber/v2"
"github.com/shopspring/decimal"
)
func InitializeChargesController(router *gin.Engine) {
router.GET("/charges", security.OPSAuthorize, listAllCharges)
router.POST("/charge", security.OPSAuthorize, recordNewCharge)
router.PUT("/charge/:uid/:seq", security.OPSAuthorize, modifyChargeState)
func InitializeChargesController(app *fiber.App) {
app.Get("/charges", security.OPSAuthorize, listAllCharges)
app.Post("/charge", security.OPSAuthorize, recordNewCharge)
app.Put("/charge/:uid/:seq", security.OPSAuthorize, modifyChargeState)
}
func listAllCharges(c *gin.Context) {
func listAllCharges(c *fiber.Ctx) error {
result := response.NewResult(c)
requestPage, err := strconv.Atoi(c.DefaultQuery("page", "1"))
requestPage, err := strconv.Atoi(c.Query("page", "1"))
if err != nil {
result.NotAccept("查询参数[page]格式不正确。")
return
return result.NotAccept("查询参数[page]格式不正确。")
}
requestKeyword := c.DefaultQuery("keyword", "")
requestBeginDate := c.DefaultQuery("begin", "")
requestEndDate := c.DefaultQuery("end", "")
requestKeyword := c.Query("keyword", "")
requestBeginDate := c.Query("begin", "")
requestEndDate := c.Query("end", "")
charges, total, err := service.ChargeService.ListPagedChargeRecord(requestKeyword, requestBeginDate, requestEndDate, requestPage)
if err != nil {
result.NotFound(err.Error())
return
return result.NotFound(err.Error())
}
result.Json(
return result.Json(
http.StatusOK, "已获取到符合条件的计费记录。",
response.NewPagedResponse(requestPage, total).ToMap(),
gin.H{"records": charges},
fiber.Map{"records": charges},
)
}
@ -49,10 +47,12 @@ type _NewChargeFormData struct {
ChargeTo model.Date `json:"chargeTo" form:"chargeTo"`
}
func recordNewCharge(c *gin.Context) {
func recordNewCharge(c *fiber.Ctx) error {
result := response.NewResult(c)
formData := new(_NewChargeFormData)
c.BindJSON(formData)
if err := c.BodyParser(formData); err != nil {
return result.UnableToParse("无法解析提交的数据。")
}
currentTime := time.Now()
newRecord := &model.UserCharge{
UserId: formData.UserId,
@ -65,30 +65,29 @@ func recordNewCharge(c *gin.Context) {
}
err := service.ChargeService.CreateChargeRecord(newRecord, true)
if err != nil {
result.Error(http.StatusInternalServerError, err.Error())
return
return result.Error(http.StatusInternalServerError, err.Error())
}
result.Created("指定用户的服务已延期。")
return result.Created("指定用户的服务已延期。")
}
type _StateChangeFormData struct {
Cancelled bool `json:"cancelled"`
}
func modifyChargeState(c *gin.Context) {
func modifyChargeState(c *fiber.Ctx) error {
result := response.NewResult(c)
formData := new(_StateChangeFormData)
c.BindJSON(formData)
requestUserID := c.Param("uid")
requestChargeSeq, err := strconv.Atoi(c.Param("seq"))
if err != nil {
result.Error(http.StatusNotAcceptable, "参数[记录流水号]解析错误。")
return
if err := c.BodyParser(formData); err != nil {
return result.UnableToParse("无法解析提交的数据。")
}
requestUserID := c.Params("uid", "")
requestChargeSeq, err := strconv.Atoi(c.Params("seq", "-1"))
if err != nil || requestChargeSeq == -1 {
return result.Error(http.StatusNotAcceptable, "参数[记录流水号]解析错误。")
}
err = service.ChargeService.CancelCharge(int64(requestChargeSeq), requestUserID)
if err != nil {
result.Error(http.StatusInternalServerError, err.Error())
return
return result.Error(http.StatusInternalServerError, err.Error())
}
result.Updated("指定用户服务延期记录状态已经更新。")
return result.Updated("指定用户服务延期记录状态已经更新。")
}