2023-04-22 12:39:27 +00:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gin-gonic/gin"
|
2024-01-28 11:38:58 +00:00
|
|
|
"github.com/songquanpeng/one-api/common/config"
|
|
|
|
"github.com/songquanpeng/one-api/common/helper"
|
|
|
|
"github.com/songquanpeng/one-api/model"
|
2023-04-22 12:39:27 +00:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2023-05-13 09:08:13 +00:00
|
|
|
"strings"
|
2023-04-22 12:39:27 +00:00
|
|
|
)
|
|
|
|
|
2023-04-22 13:41:16 +00:00
|
|
|
func GetAllChannels(c *gin.Context) {
|
2023-04-22 12:39:27 +00:00
|
|
|
p, _ := strconv.Atoi(c.Query("p"))
|
|
|
|
if p < 0 {
|
|
|
|
p = 0
|
|
|
|
}
|
2024-03-10 10:34:57 +00:00
|
|
|
channels, err := model.GetAllChannels(p*config.ItemsPerPage, config.ItemsPerPage, "limited")
|
2023-04-22 12:39:27 +00:00
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"success": false,
|
|
|
|
"message": err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"success": true,
|
|
|
|
"message": "",
|
2023-04-22 14:02:59 +00:00
|
|
|
"data": channels,
|
2023-04-22 12:39:27 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-22 13:41:16 +00:00
|
|
|
func SearchChannels(c *gin.Context) {
|
2023-04-22 12:39:27 +00:00
|
|
|
keyword := c.Query("keyword")
|
2023-04-22 14:02:59 +00:00
|
|
|
channels, err := model.SearchChannels(keyword)
|
2023-04-22 12:39:27 +00:00
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"success": false,
|
|
|
|
"message": err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"success": true,
|
|
|
|
"message": "",
|
2023-04-22 14:02:59 +00:00
|
|
|
"data": channels,
|
2023-04-22 12:39:27 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-22 14:02:59 +00:00
|
|
|
func GetChannel(c *gin.Context) {
|
|
|
|
id, err := strconv.Atoi(c.Param("id"))
|
2023-04-22 12:39:27 +00:00
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"success": false,
|
|
|
|
"message": err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2023-04-23 10:24:11 +00:00
|
|
|
channel, err := model.GetChannelById(id, false)
|
2023-04-22 14:02:59 +00:00
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"success": false,
|
|
|
|
"message": err.Error(),
|
|
|
|
})
|
|
|
|
return
|
2023-04-22 12:39:27 +00:00
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"success": true,
|
|
|
|
"message": "",
|
2023-04-22 14:02:59 +00:00
|
|
|
"data": channel,
|
2023-04-22 12:39:27 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-22 14:02:59 +00:00
|
|
|
func AddChannel(c *gin.Context) {
|
|
|
|
channel := model.Channel{}
|
|
|
|
err := c.ShouldBindJSON(&channel)
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
2023-04-22 12:39:27 +00:00
|
|
|
"success": false,
|
2023-04-22 14:02:59 +00:00
|
|
|
"message": err.Error(),
|
2023-04-22 12:39:27 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2024-01-21 15:21:42 +00:00
|
|
|
channel.CreatedTime = helper.GetTimestamp()
|
2023-05-13 09:08:13 +00:00
|
|
|
keys := strings.Split(channel.Key, "\n")
|
2023-08-30 13:15:56 +00:00
|
|
|
channels := make([]model.Channel, 0, len(keys))
|
2023-05-13 09:08:13 +00:00
|
|
|
for _, key := range keys {
|
|
|
|
if key == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
localChannel := channel
|
|
|
|
localChannel.Key = key
|
|
|
|
channels = append(channels, localChannel)
|
|
|
|
}
|
|
|
|
err = model.BatchInsertChannels(channels)
|
2023-04-22 14:02:59 +00:00
|
|
|
if err != nil {
|
2023-04-22 12:39:27 +00:00
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"success": false,
|
2023-04-22 14:02:59 +00:00
|
|
|
"message": err.Error(),
|
2023-04-22 12:39:27 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2023-04-22 14:02:59 +00:00
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"success": true,
|
|
|
|
"message": "",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteChannel(c *gin.Context) {
|
|
|
|
id, _ := strconv.Atoi(c.Param("id"))
|
|
|
|
channel := model.Channel{Id: id}
|
|
|
|
err := channel.Delete()
|
2023-04-22 12:39:27 +00:00
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
2023-04-22 14:02:59 +00:00
|
|
|
"success": false,
|
2023-04-22 12:39:27 +00:00
|
|
|
"message": err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2023-04-22 14:02:59 +00:00
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"success": true,
|
|
|
|
"message": "",
|
|
|
|
})
|
|
|
|
return
|
2023-04-22 12:39:27 +00:00
|
|
|
}
|
|
|
|
|
2023-10-14 09:25:48 +00:00
|
|
|
func DeleteDisabledChannel(c *gin.Context) {
|
|
|
|
rows, err := model.DeleteDisabledChannel()
|
2023-10-02 05:06:27 +00:00
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"success": false,
|
|
|
|
"message": err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"success": true,
|
|
|
|
"message": "",
|
|
|
|
"data": rows,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-22 14:02:59 +00:00
|
|
|
func UpdateChannel(c *gin.Context) {
|
|
|
|
channel := model.Channel{}
|
|
|
|
err := c.ShouldBindJSON(&channel)
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"success": false,
|
|
|
|
"message": err.Error(),
|
|
|
|
})
|
2023-04-22 12:39:27 +00:00
|
|
|
return
|
|
|
|
}
|
2023-04-22 14:02:59 +00:00
|
|
|
err = channel.Update()
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"success": false,
|
|
|
|
"message": err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"success": true,
|
|
|
|
"message": "",
|
2023-04-23 07:42:23 +00:00
|
|
|
"data": channel,
|
2023-04-22 14:02:59 +00:00
|
|
|
})
|
|
|
|
return
|
2023-04-22 12:39:27 +00:00
|
|
|
}
|