123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- package controllers
- import (
- "sparrow/pkg/models"
- "sparrow/pkg/rpcs"
- "sparrow/pkg/server"
- "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))
- // if upload a image file
- if product.ProductImage != "" {
- //move image
- args := &rpcs.ArgsMoveFile{
- Source: product.ProductImage,
- Target: "product",
- }
- reply := &rpcs.ReplyMoveFile{}
- err := server.RPCCallByName("fileaccess", "FileAccess.MoveFile", args, reply)
- if err != nil {
- server.Log.Error(err)
- product.ProductImage = ""
- } else {
- product.ProductImage = reply.FilePath
- }
- }
- 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("name")
- 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,
- })
- }
- // GetBy 获取产品信息
- // GET /product/{key}
- func (a *ProductController) GetBy(key string) {
- pro, err := a.Service.QueryOne(a.Token.getVendorID(a.Ctx), key)
- if err != nil {
- responseError(a.Ctx, ErrDatabase, err.Error())
- return
- }
- done(a.Ctx, pro)
- }
|