70 lines
1.5 KiB
Go
70 lines
1.5 KiB
Go
package server
|
|
|
|
import (
|
|
"log"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"pkg.bing89.com/travebing/controller/way"
|
|
"pkg.bing89.com/travebing/http"
|
|
"pkg.bing89.com/travebing/notifier"
|
|
"pkg.bing89.com/travebing/tberror"
|
|
)
|
|
|
|
func (s *Server)HandleWayList(c *gin.Context){
|
|
plan, err := strconv.Atoi(c.Param("plan"))
|
|
if err != nil {
|
|
http.Error(c, tberror.CodeBadRequest, err)
|
|
return
|
|
}
|
|
ret, code, err := s.ctrl.Ways(plan).List()
|
|
if err != nil {
|
|
http.Error(c, code, err)
|
|
return
|
|
}
|
|
http.Success(c, ret)
|
|
}
|
|
|
|
func (s *Server)HandleWayCreate(c *gin.Context){
|
|
plan := c.Param("plan")
|
|
planID, err := strconv.Atoi(plan)
|
|
if err != nil {
|
|
http.Error(c, tberror.CodeBadRequest, err)
|
|
return
|
|
}
|
|
opt := way.CreateOption{}
|
|
if err := c.ShouldBind(&opt); err != nil {
|
|
http.Error(c, tberror.CodeBadRequest, err)
|
|
return
|
|
}
|
|
file, err := c.FormFile("file")
|
|
if err == nil {
|
|
opt.File = file
|
|
}else{
|
|
opt.File = nil
|
|
}
|
|
|
|
ret, code, err := s.ctrl.Ways(planID).Create(opt)
|
|
if err != nil {
|
|
http.Error(c, code, err)
|
|
return
|
|
}
|
|
s.notifier.Send(ret, plan)
|
|
http.Success(c, ret)
|
|
}
|
|
|
|
func (s *Server)HandleWaySync(c *gin.Context){
|
|
c.Header("Content-Type", "text/event-stream")
|
|
c.Header("Cache-Control", "no-cache")
|
|
c.Header("Connection", "keep-alive")
|
|
c.Header("Access-Control-Allow-Origin", "*")
|
|
session, err := notifier.NewSession(c)
|
|
if err != nil {
|
|
log.Println("new session failed:", err)
|
|
http.Error(c, tberror.CodeSystem, err)
|
|
return
|
|
}
|
|
s.notifier.AddSession(session)
|
|
log.Println(session.ID(), "returned")
|
|
defer s.notifier.DeleteSession(session.User(), session.ID())
|
|
} |