77 lines
2.1 KiB
Go
77 lines
2.1 KiB
Go
// upstream_handler.go Day 41(清债③):上家(通道方)管理端点——补齐 FE-1 UpstreamList.vue 的既有调用。
|
|
package v1
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"go-sms/internal/model/entity"
|
|
"go-sms/internal/pkg/errors"
|
|
"go-sms/internal/pkg/response"
|
|
"go-sms/internal/service/channel"
|
|
)
|
|
|
|
// upstreamHandler 上家接口。
|
|
type upstreamHandler struct{ svc *channel.UpstreamService }
|
|
|
|
// list GET /channels/upstreams?page&page_size&name&status。
|
|
func (h *upstreamHandler) list(c *gin.Context) {
|
|
q := &channel.UpstreamQuery{
|
|
Page: intQuery(c, "page", 1), PageSize: intQuery(c, "page_size", 20),
|
|
Name: c.Query("name"),
|
|
}
|
|
if v := c.Query("status"); v != "" {
|
|
st, _ := strconv.Atoi(v)
|
|
q.Status = int8(st)
|
|
} else {
|
|
q.Status = -1
|
|
}
|
|
list, total, err := h.svc.List(c.Request.Context(), q)
|
|
if err != nil {
|
|
bizResp(c, err)
|
|
return
|
|
}
|
|
response.Success(c, gin.H{"total": total, "list": list})
|
|
}
|
|
|
|
// create POST /channels/upstreams {name,contact,phone,category,status,remark}。
|
|
func (h *upstreamHandler) create(c *gin.Context) {
|
|
var req entity.ChannelUpstream
|
|
if err := c.ShouldBindJSON(&req); err != nil || req.Name == "" {
|
|
response.ErrorWithMsg(c, errors.InvalidParam, "name 必填")
|
|
return
|
|
}
|
|
u, err := h.svc.Create(c.Request.Context(), &req)
|
|
if err != nil {
|
|
bizResp(c, err)
|
|
return
|
|
}
|
|
response.Success(c, u)
|
|
}
|
|
|
|
// update PUT /channels/upstreams/:id(白名单字段)。
|
|
func (h *upstreamHandler) update(c *gin.Context) {
|
|
id, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
var body map[string]any
|
|
if err := c.ShouldBindJSON(&body); err != nil || len(body) == 0 {
|
|
response.ErrorWithMsg(c, errors.InvalidParam, "请求体需为字段对象")
|
|
return
|
|
}
|
|
if err := h.svc.Update(c.Request.Context(), id, body); err != nil {
|
|
bizResp(c, err)
|
|
return
|
|
}
|
|
response.Success(c, gin.H{"id": id})
|
|
}
|
|
|
|
// remove DELETE /channels/upstreams/:id(被通道引用则拒绝)。
|
|
func (h *upstreamHandler) remove(c *gin.Context) {
|
|
id, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
if err := h.svc.Delete(c.Request.Context(), id); err != nil {
|
|
bizResp(c, err)
|
|
return
|
|
}
|
|
response.Success(c, gin.H{"id": id})
|
|
}
|