123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- package controllers
- import (
- "sparrow/pkg/models"
- "sparrow/services/knowoapi/services"
- "github.com/kataras/iris"
- )
- // ProductController 产品API
- type ProductController struct {
- Ctx iris.Context
- Service services.ProductService
- Token Token
- }
- // Post /product 添加产品
- func (a *ProductController) Post() {
- product := new(models.Product)
- if err := parseBody(a.Ctx, product); err != nil {
- badRequest(a.Ctx, err)
- return
- }
- product.VendorID = int32(a.Token.getVendorID(a.Ctx))
- err := a.Service.Create(product)
- if err != nil {
- responseError(a.Ctx, ErrDatabase, err.Error())
- return
- }
- done(a.Ctx, product)
- }
- // Delete /product 删除
- func (a *ProductController) Delete() {
- product := new(models.Product)
- if err := parseBody(a.Ctx, product); err != nil {
- badRequest(a.Ctx, err)
- return
- }
- if product.VendorID != int32(a.Token.getVendorID(a.Ctx)) {
- responseError(a.Ctx, ErrNormal, "非法操作")
- return
- }
- err := a.Service.Delete(product)
- if err != nil {
- responseError(a.Ctx, ErrDatabase, err.Error())
- return
- }
- done(a.Ctx, "删除成功")
- }
- // Put /produdct 更新产品信息
- func (a *ProductController) Put() {
- product := new(models.Product)
- if err := parseBody(a.Ctx, product); err != nil {
- badRequest(a.Ctx, err)
- return
- }
- if product.VendorID != int32(a.Token.getVendorID(a.Ctx)) {
- responseError(a.Ctx, ErrNormal, "非法操作")
- return
- }
- pro, err := a.Service.Update(product)
- if err != nil {
- responseError(a.Ctx, ErrDatabase, err.Error())
- return
- }
- done(a.Ctx, pro)
- }
- // Get /product 查询我的产品
- func (a *ProductController) Get() {
- pi, err := a.Ctx.URLParamInt("pi")
- if err != nil {
- badRequest(a.Ctx, err)
- return
- }
- ps, err := a.Ctx.URLParamInt("ps")
- if err != nil {
- badRequest(a.Ctx, err)
- return
- }
- name := a.Ctx.URLParam("companyname")
- ds, total, err := a.Service.GetVendorProducts(a.Token.getVendorID(a.Ctx),
- pi,
- ps,
- name)
- if err != nil {
- responseError(a.Ctx, ErrDatabase, err.Error())
- return
- }
- done(a.Ctx, map[string]interface{}{
- "list": ds,
- "total": total,
- })
- }
|