123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- package controller
- import (
- "message-pusher/channel"
- "message-pusher/common"
- "message-pusher/model"
- "net/http"
- "strconv"
- "github.com/gin-gonic/gin"
- )
- func GetAllChannels(c *gin.Context) {
- if c.Query("brief") != "" {
- GetBriefChannels(c)
- return
- }
- userId := c.GetInt("id")
- p, _ := strconv.Atoi(c.Query("p"))
- if p < 0 {
- p = 0
- }
- channels, err := model.GetChannelsByUserId(userId, p*common.ItemsPerPage, common.ItemsPerPage)
- 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": channels,
- })
- return
- }
- func GetBriefChannels(c *gin.Context) {
- userId := c.GetInt("id")
- channels, err := model.GetBriefChannelsByUserId(userId)
- 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": channels,
- })
- return
- }
- func SearchChannels(c *gin.Context) {
- userId := c.GetInt("id")
- keyword := c.Query("keyword")
- channels, err := model.SearchChannels(userId, keyword)
- 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": channels,
- })
- return
- }
- func GetChannel(c *gin.Context) {
- id, err := strconv.Atoi(c.Param("id"))
- userId := c.GetInt("id")
- if err != nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": err.Error(),
- })
- return
- }
- channel_, err := model.GetChannelById(id, userId, false)
- 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": channel_,
- })
- return
- }
- func AddChannel(c *gin.Context) {
- channel_ := model.Channel{}
- err := c.ShouldBindJSON(&channel_)
- if err != nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": err.Error(),
- })
- return
- }
- if len(channel_.Name) == 0 || len(channel_.Name) > 20 {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": "通道名称长度必须在1-20之间",
- })
- return
- }
- if channel_.Name == "email" {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": "不能使用系统保留名称",
- })
- return
- }
- cleanChannel := model.Channel{
- Type: channel_.Type,
- UserId: c.GetInt("id"),
- Name: channel_.Name,
- Description: channel_.Description,
- Status: common.ChannelStatusEnabled,
- Secret: channel_.Secret,
- AppId: channel_.AppId,
- AccountId: channel_.AccountId,
- URL: channel_.URL,
- Other: channel_.Other,
- CreatedTime: common.GetTimestamp(),
- Token: channel_.Token,
- }
- err = cleanChannel.Insert()
- if err != nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": err.Error(),
- })
- return
- }
- channel.TokenStoreAddChannel(&cleanChannel)
- c.JSON(http.StatusOK, gin.H{
- "success": true,
- "message": "",
- })
- return
- }
- func DeleteChannel(c *gin.Context) {
- id, _ := strconv.Atoi(c.Param("id"))
- userId := c.GetInt("id")
- channel_, err := model.DeleteChannelById(id, userId)
- if err != nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": err.Error(),
- })
- return
- }
- channel.TokenStoreRemoveChannel(channel_)
- c.JSON(http.StatusOK, gin.H{
- "success": true,
- "message": "",
- })
- return
- }
- func UpdateChannel(c *gin.Context) {
- userId := c.GetInt("id")
- statusOnly := c.Query("status_only")
- channel_ := model.Channel{}
- err := c.ShouldBindJSON(&channel_)
- if err != nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": err.Error(),
- })
- return
- }
- oldChannel, err := model.GetChannelById(channel_.Id, userId, true)
- if err != nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": err.Error(),
- })
- return
- }
- cleanChannel := *oldChannel
- if statusOnly != "" {
- cleanChannel.Status = channel_.Status
- } else {
- // If you add more fields, please also update channel_.Update()
- cleanChannel.Type = channel_.Type
- cleanChannel.Name = channel_.Name
- cleanChannel.Description = channel_.Description
- if channel_.Secret != "" {
- cleanChannel.Secret = channel_.Secret
- }
- cleanChannel.AppId = channel_.AppId
- cleanChannel.AccountId = channel_.AccountId
- cleanChannel.URL = channel_.URL
- cleanChannel.Other = channel_.Other
- cleanChannel.Token = channel_.Token
- }
- err = cleanChannel.Update()
- if err != nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": err.Error(),
- })
- return
- }
- channel.TokenStoreUpdateChannel(&cleanChannel, oldChannel)
- c.JSON(http.StatusOK, gin.H{
- "success": true,
- "message": "",
- "data": cleanChannel,
- })
- return
- }
|