增加产品列表接口
This commit is contained in:
4
product/etc/product.yaml
Normal file
4
product/etc/product.yaml
Normal file
@@ -0,0 +1,4 @@
|
||||
Name: product
|
||||
Host: 0.0.0.0
|
||||
Port: 8888
|
||||
DataSource: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest
|
||||
8
product/internal/config/config.go
Normal file
8
product/internal/config/config.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package config
|
||||
|
||||
import "github.com/zeromicro/go-zero/rest"
|
||||
|
||||
type Config struct {
|
||||
rest.RestConf
|
||||
DataSource string
|
||||
}
|
||||
28
product/internal/handler/getproductlisthandler.go
Normal file
28
product/internal/handler/getproductlisthandler.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"fusenapi/product/internal/logic"
|
||||
"fusenapi/product/internal/svc"
|
||||
"fusenapi/product/internal/types"
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
)
|
||||
|
||||
func GetProductListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.GetProductListReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
|
||||
l := logic.NewGetProductListLogic(r.Context(), svcCtx)
|
||||
resp, err := l.GetProductList(&req)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
} else {
|
||||
httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
22
product/internal/handler/routes.go
Normal file
22
product/internal/handler/routes.go
Normal file
@@ -0,0 +1,22 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"fusenapi/product/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
)
|
||||
|
||||
func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
||||
server.AddRoutes(
|
||||
[]rest.Route{
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/product/list",
|
||||
Handler: GetProductListHandler(serverCtx),
|
||||
},
|
||||
},
|
||||
)
|
||||
}
|
||||
35
product/internal/logic/getproductlistlogic.go
Normal file
35
product/internal/logic/getproductlistlogic.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fusenapi/utils/image"
|
||||
|
||||
"fusenapi/product/internal/svc"
|
||||
"fusenapi/product/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type GetProductListLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewGetProductListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetProductListLogic {
|
||||
return &GetProductListLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
// 获取产品列表
|
||||
func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq) (resp *types.Response, err error) {
|
||||
//获取合适尺寸
|
||||
if req.Size > 0 {
|
||||
req.Size = image.GetCurrentSize(req.Size)
|
||||
}
|
||||
//获取是否存在千人千面
|
||||
return
|
||||
}
|
||||
19
product/internal/svc/servicecontext.go
Normal file
19
product/internal/svc/servicecontext.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package svc
|
||||
|
||||
import (
|
||||
"fusenapi/model"
|
||||
"fusenapi/product/internal/config"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
)
|
||||
|
||||
type ServiceContext struct {
|
||||
Config config.Config
|
||||
FsProductModel model.FsProductModel
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config) *ServiceContext {
|
||||
return &ServiceContext{
|
||||
Config: c,
|
||||
FsProductModel: model.NewFsProductModel(sqlx.NewMysql(c.DataSource)),
|
||||
}
|
||||
}
|
||||
15
product/internal/types/types.go
Normal file
15
product/internal/types/types.go
Normal file
@@ -0,0 +1,15 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
package types
|
||||
|
||||
type GetProductListReq struct {
|
||||
Cid uint32 `form:"cid"`
|
||||
Size uint32 `form:"size"`
|
||||
Page uint32 `form:"page"`
|
||||
IsDemo uint32 `form:"is_demo" , options=0|1"`
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"msg"`
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
31
product/product.go
Normal file
31
product/product.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
|
||||
"fusenapi/product/internal/config"
|
||||
"fusenapi/product/internal/handler"
|
||||
"fusenapi/product/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/conf"
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
)
|
||||
|
||||
var configFile = flag.String("f", "etc/product.yaml", "the config file")
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
var c config.Config
|
||||
conf.MustLoad(*configFile, &c)
|
||||
|
||||
server := rest.MustNewServer(c.RestConf)
|
||||
defer server.Stop()
|
||||
|
||||
ctx := svc.NewServiceContext(c)
|
||||
handler.RegisterHandlers(server, ctx)
|
||||
|
||||
fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
|
||||
server.Start()
|
||||
}
|
||||
Reference in New Issue
Block a user