produdct.go 2.7 KB

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