produdct.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package controllers
  2. import (
  3. "sparrow/pkg/models"
  4. "sparrow/pkg/rpcs"
  5. "sparrow/pkg/server"
  6. "sparrow/services/knowoapi/services"
  7. "strings"
  8. "github.com/kataras/iris"
  9. )
  10. // ProductController 产品API
  11. type ProductController struct {
  12. Ctx iris.Context
  13. Service services.ProductService
  14. Token Token
  15. }
  16. // Post /product 添加产品
  17. func (a *ProductController) Post() {
  18. product := new(models.Product)
  19. if err := parseBody(a.Ctx, product); err != nil {
  20. badRequest(a.Ctx, err)
  21. return
  22. }
  23. product.VendorID = int32(a.Token.getVendorID(a.Ctx))
  24. // if upload a image file
  25. if product.ProductImage != "" {
  26. //move image
  27. args := &rpcs.ArgsMoveFile{
  28. Source: product.ProductImage,
  29. Target: "product",
  30. }
  31. reply := &rpcs.ReplyMoveFile{}
  32. err := server.RPCCallByName("fileaccess", "FileAccess.MoveFile", args, reply)
  33. if err != nil {
  34. server.Log.Error(err)
  35. product.ProductImage = ""
  36. } else {
  37. product.ProductImage = reply.FilePath
  38. }
  39. }
  40. err := a.Service.Create(product)
  41. if err != nil {
  42. responseError(a.Ctx, ErrDatabase, err.Error())
  43. return
  44. }
  45. done(a.Ctx, product)
  46. }
  47. // Delete /product 删除
  48. func (a *ProductController) Delete() {
  49. product := new(models.Product)
  50. if err := parseBody(a.Ctx, product); err != nil {
  51. badRequest(a.Ctx, err)
  52. return
  53. }
  54. if product.VendorID != int32(a.Token.getVendorID(a.Ctx)) {
  55. responseError(a.Ctx, ErrNormal, "非法操作")
  56. return
  57. }
  58. err := a.Service.Delete(product)
  59. if err != nil {
  60. responseError(a.Ctx, ErrDatabase, err.Error())
  61. return
  62. }
  63. done(a.Ctx, "删除成功")
  64. }
  65. // Put /produdct 更新产品信息
  66. func (a *ProductController) Put() {
  67. product := new(models.Product)
  68. if err := parseBody(a.Ctx, product); err != nil {
  69. badRequest(a.Ctx, err)
  70. return
  71. }
  72. if product.VendorID != int32(a.Token.getVendorID(a.Ctx)) {
  73. responseError(a.Ctx, ErrNormal, "非法操作")
  74. return
  75. }
  76. // check update image
  77. if strings.Contains(product.ProductImage, "tmp") {
  78. //move image
  79. args := &rpcs.ArgsMoveFile{
  80. Source: product.ProductImage,
  81. Target: "product",
  82. }
  83. reply := &rpcs.ReplyMoveFile{}
  84. err := server.RPCCallByName("fileaccess", "FileAccess.MoveFile", args, reply)
  85. if err != nil {
  86. server.Log.Error(err)
  87. product.ProductImage = ""
  88. } else {
  89. product.ProductImage = reply.FilePath
  90. }
  91. }
  92. pro, err := a.Service.Update(product)
  93. if err != nil {
  94. responseError(a.Ctx, ErrDatabase, err.Error())
  95. return
  96. }
  97. done(a.Ctx, pro)
  98. }
  99. // Get /product 查询我的产品
  100. func (a *ProductController) Get() {
  101. pi, err := a.Ctx.URLParamInt("pi")
  102. if err != nil {
  103. badRequest(a.Ctx, err)
  104. return
  105. }
  106. ps, err := a.Ctx.URLParamInt("ps")
  107. if err != nil {
  108. badRequest(a.Ctx, err)
  109. return
  110. }
  111. name := a.Ctx.URLParam("name")
  112. ds, total, err := a.Service.GetVendorProducts(a.Token.getVendorID(a.Ctx),
  113. pi,
  114. ps,
  115. name)
  116. if err != nil {
  117. responseError(a.Ctx, ErrDatabase, err.Error())
  118. return
  119. }
  120. done(a.Ctx, map[string]interface{}{
  121. "list": ds,
  122. "total": total,
  123. })
  124. }
  125. // GetBy 获取产品信息
  126. // GET /product/{key}
  127. func (a *ProductController) GetBy(key string) {
  128. pro, err := a.Service.QueryOne(a.Token.getVendorID(a.Ctx), key)
  129. if err != nil {
  130. responseError(a.Ctx, ErrDatabase, err.Error())
  131. return
  132. }
  133. done(a.Ctx, pro)
  134. }