produdct.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package controllers
  2. import (
  3. "sparrow/pkg/models"
  4. "sparrow/services/knowoapi/services"
  5. "github.com/kataras/iris"
  6. )
  7. // ProductController 产品API
  8. type ProductController struct {
  9. Ctx iris.Context
  10. Service services.ProductService
  11. Token Token
  12. }
  13. // Post /product 添加产品
  14. func (a *ProductController) Post() {
  15. product := new(models.Product)
  16. if err := parseBody(a.Ctx, product); err != nil {
  17. badRequest(a.Ctx, err)
  18. return
  19. }
  20. product.VendorID = int32(a.Token.getVendorID(a.Ctx))
  21. err := a.Service.Create(product)
  22. if err != nil {
  23. responseError(a.Ctx, ErrDatabase, err.Error())
  24. return
  25. }
  26. done(a.Ctx, product)
  27. }
  28. // Delete /product 删除
  29. func (a *ProductController) Delete() {
  30. product := new(models.Product)
  31. if err := parseBody(a.Ctx, product); err != nil {
  32. badRequest(a.Ctx, err)
  33. return
  34. }
  35. if product.VendorID != int32(a.Token.getVendorID(a.Ctx)) {
  36. responseError(a.Ctx, ErrNormal, "非法操作")
  37. return
  38. }
  39. err := a.Service.Delete(product)
  40. if err != nil {
  41. responseError(a.Ctx, ErrDatabase, err.Error())
  42. return
  43. }
  44. done(a.Ctx, "删除成功")
  45. }
  46. // Put /produdct 更新产品信息
  47. func (a *ProductController) Put() {
  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. pro, err := a.Service.Update(product)
  58. if err != nil {
  59. responseError(a.Ctx, ErrDatabase, err.Error())
  60. return
  61. }
  62. done(a.Ctx, pro)
  63. }
  64. // Get /product 查询我的产品
  65. func (a *ProductController) Get() {
  66. pi, err := a.Ctx.URLParamInt("pi")
  67. if err != nil {
  68. badRequest(a.Ctx, err)
  69. return
  70. }
  71. ps, err := a.Ctx.URLParamInt("ps")
  72. if err != nil {
  73. badRequest(a.Ctx, err)
  74. return
  75. }
  76. name := a.Ctx.URLParam("companyname")
  77. ds, total, err := a.Service.GetVendorProducts(a.Token.getVendorID(a.Ctx),
  78. pi,
  79. ps,
  80. name)
  81. if err != nil {
  82. responseError(a.Ctx, ErrDatabase, err.Error())
  83. return
  84. }
  85. done(a.Ctx, map[string]interface{}{
  86. "list": ds,
  87. "total": total,
  88. })
  89. }