|
- package controller
-
- import (
- "net/http"
- "strconv"
-
- "github.com/QuantumNous/new-api/common"
- "github.com/QuantumNous/new-api/model"
-
- "github.com/gin-gonic/gin"
- )
-
- func GetUserChannelRatios(c *gin.Context) {
- userIdStr := c.Param("user_id")
- userId, err := strconv.Atoi(userIdStr)
- if err != nil {
- common.ApiError(c, err)
- return
- }
-
- ratios, err := model.GetUserChannelRatiosByUserId(userId)
- if err != nil {
- common.ApiError(c, err)
- return
- }
- c.JSON(http.StatusOK, gin.H{
- "success": true,
- "data": ratios,
- })
- }
-
- type createUserChannelRatioRequest struct {
- UserId int `json:"user_id"`
- ModelName string `json:"model_name"`
- ChannelId int `json:"channel_id"`
- Ratio float64 `json:"ratio"`
- }
-
- func CreateUserChannelRatio(c *gin.Context) {
- var req createUserChannelRatioRequest
- if err := c.ShouldBindJSON(&req); err != nil {
- common.ApiError(c, err)
- return
- }
-
- if req.Ratio <= 0 {
- common.ApiErrorMsg(c, "ratio must be positive")
- return
- }
-
- ucr := &model.UserChannelRatio{
- UserId: req.UserId,
- ModelName: req.ModelName,
- ChannelId: req.ChannelId,
- Ratio: req.Ratio,
- }
- if err := ucr.Insert(); err != nil {
- common.ApiError(c, err)
- return
- }
- c.JSON(http.StatusOK, gin.H{
- "success": true,
- "data": ucr,
- })
- }
-
- func UpdateUserChannelRatio(c *gin.Context) {
- idStr := c.Param("id")
- id, err := strconv.Atoi(idStr)
- if err != nil {
- common.ApiError(c, err)
- return
- }
-
- var req struct {
- Ratio float64 `json:"ratio"`
- }
- if err := c.ShouldBindJSON(&req); err != nil {
- common.ApiError(c, err)
- return
- }
- if req.Ratio <= 0 {
- common.ApiErrorMsg(c, "ratio must be positive")
- return
- }
-
- ucr := &model.UserChannelRatio{
- Id: id,
- Ratio: req.Ratio,
- }
- if err := ucr.Update(); err != nil {
- common.ApiError(c, err)
- return
- }
- c.JSON(http.StatusOK, gin.H{
- "success": true,
- "data": ucr,
- })
- }
-
- func DeleteUserChannelRatio(c *gin.Context) {
- idStr := c.Param("id")
- id, err := strconv.Atoi(idStr)
- if err != nil {
- common.ApiError(c, err)
- return
- }
-
- if err := model.DeleteUserChannelRatioById(id); err != nil {
- common.ApiError(c, err)
- return
- }
- c.JSON(http.StatusOK, gin.H{
- "success": true,
- })
- }
|