github.com/99MyCql/duffett@v0.1.0/app/strategy/api.go (about)

     1  package strategy
     2  
     3  import (
     4  	"net/http"
     5  	"os"
     6  
     7  	"github.com/gin-gonic/gin"
     8  	log "github.com/sirupsen/logrus"
     9  
    10  	"github.com/99MyCql/duffett/app/strategy/model"
    11  	strategyModel "github.com/99MyCql/duffett/app/strategy/model"
    12  	userModel "github.com/99MyCql/duffett/app/user/model"
    13  	"github.com/99MyCql/duffett/pkg"
    14  )
    15  
    16  // @Summary GetStrategies
    17  // @Tags Strategy
    18  // @Accept json
    19  // @Param Authorization header string false "Bearer <token>"
    20  // @Success 200 {string} json "{"code":0,"data":{},"msg":""}"
    21  // @Router /api/v1/strategy/getStrategies [get]
    22  func GetStrategies(c *gin.Context) {
    23  	username, _ := c.Get("username")
    24  	strategies := model.ListStrategies(username.(string))
    25  	c.JSON(http.StatusOK, pkg.SucWithData("", strategies))
    26  	return
    27  }
    28  
    29  type createReq struct {
    30  	Name    string `json:"name" binding:"required,excludes= "`
    31  	Desc    string `json:"desc" binding:""`
    32  	Content string `json:"content" binding:"required"`
    33  }
    34  
    35  // @Summary Create
    36  // @Tags Strategy
    37  // @Accept json
    38  // @Param Authorization header string false "Bearer <token>"
    39  // @Param strategy body createReq true "createReq"
    40  // @Success 200 {string} json "{"code":0,"data":{},"msg":""}"
    41  // @Router /api/v1/strategy/create [post]
    42  func Create(c *gin.Context) {
    43  	var req createReq
    44  	if err := c.ShouldBind(&req); err != nil {
    45  		log.Error(err)
    46  		c.JSON(http.StatusOK, pkg.ClientErr(err.Error()))
    47  		return
    48  	}
    49  	log.Debug(req)
    50  
    51  	username, _ := c.Get("username")
    52  	u := userModel.FindByName(username.(string))
    53  	if u == nil {
    54  		log.Error("查询不到该用户")
    55  		c.JSON(http.StatusOK, pkg.ServerErr("查询不到该用户"))
    56  		return
    57  	}
    58  
    59  	// 检查该策略名是否已存在
    60  	if s := strategyModel.FindByName(u.Username + "_" + req.Name); s != nil {
    61  		log.Error("该策略名已存在")
    62  		c.JSON(http.StatusOK, pkg.ClientErr("该策略名已存在"))
    63  		return
    64  	}
    65  
    66  	s := strategyModel.Strategy{
    67  		Name:    u.Username + "_" + req.Name,
    68  		Desc:    req.Desc,
    69  		Content: req.Content,
    70  		UserID:  u.ID,
    71  	}
    72  	rsp := strategyModel.Create(&s)
    73  	if rsp.Code == pkg.SucCode {
    74  		rsp.Msg = "创建成功!"
    75  		rsp.Data = s
    76  		c.JSON(http.StatusOK, rsp)
    77  	} else {
    78  		rsp.Msg = "创建失败:" + rsp.Msg
    79  		c.JSON(http.StatusOK, rsp)
    80  	}
    81  
    82  }
    83  
    84  type updateReq struct {
    85  	Id      uint   `json:"id" binding:"required"`
    86  	Desc    string `json:"desc" binding:""`
    87  	Content string `json:"content" binding:"required"`
    88  }
    89  
    90  // @Summary Update
    91  // @Tags Strategy
    92  // @Accept json
    93  // @Param Authorization header string false "Bearer <token>"
    94  // @Param strategy body updateReq true "updateReq"
    95  // @Success 200 {string} json "{"code":0,"data":{},"msg":""}"
    96  // @Router /api/v1/strategy/update [post]
    97  func Update(c *gin.Context) {
    98  	var req updateReq
    99  	if err := c.ShouldBind(&req); err != nil {
   100  		log.Error(err)
   101  		c.JSON(http.StatusOK, pkg.ClientErr(err.Error()))
   102  		return
   103  	}
   104  	log.Debug(req)
   105  
   106  	s := strategyModel.FindById(req.Id)
   107  	if s == nil {
   108  		c.JSON(http.StatusOK, pkg.ClientErr("策略ID不存在"))
   109  		return
   110  	}
   111  
   112  	s.Desc = req.Desc
   113  	s.Content = req.Content
   114  	rsp := strategyModel.Update(s)
   115  	if rsp.Code == pkg.SucCode {
   116  		rsp.Msg = "更新成功!"
   117  		c.JSON(http.StatusOK, rsp)
   118  	} else {
   119  		rsp.Msg = "更新失败:" + rsp.Msg
   120  		c.JSON(http.StatusOK, rsp)
   121  	}
   122  }
   123  
   124  type deleteReq struct {
   125  	StrategyId uint `json:"strategyId" binding:"required"`
   126  }
   127  
   128  // @Summary Delete
   129  // @Tags Strategy
   130  // @Accept json
   131  // @Param Authorization header string false "Bearer <token>"
   132  // @Param strategyId body deleteReq true "deleteReq"
   133  // @Success 200 {string} json "{"code":0,"data":{},"msg":""}"
   134  // @Router /api/v1/strategy/delete [post]
   135  func Delete(c *gin.Context) {
   136  	var req deleteReq
   137  	if err := c.ShouldBind(&req); err != nil {
   138  		log.Error(err)
   139  		c.JSON(http.StatusOK, pkg.ClientErr(err.Error()))
   140  		return
   141  	}
   142  	log.Debug(req)
   143  	rsp := model.UnscopedDeleteById(req.StrategyId)
   144  	if rsp.Code == pkg.SucCode {
   145  		rsp.Msg = "删除成功!"
   146  		c.JSON(http.StatusOK, rsp)
   147  	} else {
   148  		rsp.Msg = "删除失败:" + rsp.Msg
   149  		c.JSON(http.StatusOK, rsp)
   150  	}
   151  }
   152  
   153  type testExecReq struct {
   154  	Name string `json:"name" binding:"required"`
   155  }
   156  
   157  // @Summary TestExec
   158  // @Tags Strategy
   159  // @Accept json
   160  // @Param Authorization header string false "Bearer <token>"
   161  // @Param testExecReq body testExecReq true "testExecReq"
   162  // @Success 200 {string} json "{"code":0,"data":{},"msg":""}"
   163  // @Router /api/v1/strategy/testExec [post]
   164  func TestExec(c *gin.Context) {
   165  	var req testExecReq
   166  	if err := c.ShouldBind(&req); err != nil {
   167  		log.Error(err)
   168  		c.JSON(http.StatusOK, pkg.ClientErr(err.Error()))
   169  		return
   170  	}
   171  	log.Debug(req)
   172  
   173  	filepath := ""
   174  	tsCode := "000001.SZ"
   175  	c.JSON(http.StatusOK, ExecStrategy(&filepath, req.Name, tsCode))
   176  	os.Remove(filepath)
   177  }