finish address-list 接口

This commit is contained in:
eson 2023-06-08 15:41:49 +08:00
parent 80f0abe037
commit 8c3ce4e896
13 changed files with 379 additions and 12 deletions

View File

@ -44,10 +44,14 @@
1. sh fs_gen_api.sh home-user-auth # 序列化api
2. sh fs_gen_mysql_model.sh fs_canteen_type # 根据ddl序列化mysql model
3. xxxx
3. 生成后就是go-zero框架的实现. 主要在logic目录下实现代码.
4. Response.Data的结构体 在api文件下定义. 方便rpc以后的接入. 参考现有的例子
#### 规范
model的自定义函数前缀使用self_的文件命名 eg: self_fsfont.go
sql 的值只能用 ?, 传入. 不能自己拼接. 涉及到sql安全和防注入.
例子: QueryRow("SELECT * FROM userinfo WHERE username = ? AND password = ?", sename, partname).Scan(&uid, &username, &password)
#### 参与贡献

20
ddl/fs_address.sql Normal file
View File

@ -0,0 +1,20 @@
-- fusentest.fs_address definition
CREATE TABLE `fs_address` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(10) unsigned NOT NULL COMMENT '用户ID',
`name` varchar(64) NOT NULL DEFAULT '' COMMENT '地址名称',
`first_name` varchar(64) NOT NULL COMMENT 'FirstName',
`last_name` varchar(64) NOT NULL COMMENT 'LastName',
`mobile` varchar(16) NOT NULL COMMENT '手机号码',
`street` varchar(128) NOT NULL COMMENT '街道',
`suite` varchar(128) NOT NULL COMMENT '房号',
`city` varchar(128) NOT NULL COMMENT '城市',
`state` varchar(128) NOT NULL COMMENT '州名',
`country` varchar(128) NOT NULL COMMENT '国家',
`zip_code` varchar(128) NOT NULL COMMENT '邮编',
`status` tinyint(1) NOT NULL COMMENT '1正常 0异常',
`is_default` tinyint(1) NOT NULL COMMENT '1默认地址0非默认地址',
PRIMARY KEY (`id`) USING BTREE,
KEY `user_id` (`user_id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=129 DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='用户地址表';

67
model/fsaddressmodel.go Executable file
View File

@ -0,0 +1,67 @@
package model
import (
"context"
"fmt"
"strings"
"github.com/zeromicro/go-zero/core/stores/sqlc"
"github.com/zeromicro/go-zero/core/stores/sqlx"
"github.com/zeromicro/go-zero/core/stringx"
)
var _ FsAddressModel = (*customFsAddressModel)(nil)
type (
// FsAddressModel is an interface to be customized, add more methods here,
// and implement the added methods in customFsAddressModel.
FsAddressModel interface {
fsAddressModel
FindDataAddressList(ctx context.Context, userid int64) (*DataAddressList, error)
}
customFsAddressModel struct {
*defaultFsAddressModel
}
DataAddressList struct {
Id int64 `db:"id"`
UserId int64 `db:"user_id"` // 用户ID
Name string `db:"name"` // 地址名称
FirstName string `db:"first_name"` // FirstName
LastName string `db:"last_name"` // LastName
Mobile string `db:"mobile"` // 手机号码
Street string `db:"street"` // 街道
Suite string `db:"suite"` // 房号
City string `db:"city"` // 城市
State string `db:"state"` // 州名
ZipCode string `db:"zip_code"` // 邮编
IsDefault int64 `db:"is_default"` // 1默认地址0非默认地址
}
)
var (
// DataAddressList 结构需要的字段
fsDataAddressListRows = strings.Join(stringx.Remove(fsAddressFieldNames, "`status`", "`country`"), ",")
)
// NewFsAddressModel returns a model for the database table.
func NewFsAddressModel(conn sqlx.SqlConn) FsAddressModel {
return &customFsAddressModel{
defaultFsAddressModel: newFsAddressModel(conn),
}
}
func (m *defaultFsAddressModel) FindDataAddressList(ctx context.Context, userid int64) (*DataAddressList, error) {
query := fmt.Sprintf("select %s from %s where `user_id` = ? ", fsDataAddressListRows, m.table)
var resp DataAddressList
err := m.conn.QueryRowCtx(ctx, &resp, query, userid)
switch err {
case nil:
return &resp, nil
case sqlc.ErrNotFound:
return nil, ErrNotFound
default:
return nil, err
}
}

96
model/fsaddressmodel_gen.go Executable file
View File

@ -0,0 +1,96 @@
// Code generated by goctl. DO NOT EDIT.
package model
import (
"context"
"database/sql"
"fmt"
"strings"
"github.com/zeromicro/go-zero/core/stores/builder"
"github.com/zeromicro/go-zero/core/stores/sqlc"
"github.com/zeromicro/go-zero/core/stores/sqlx"
"github.com/zeromicro/go-zero/core/stringx"
)
var (
fsAddressFieldNames = builder.RawFieldNames(&FsAddress{})
fsAddressRows = strings.Join(fsAddressFieldNames, ",")
fsAddressRowsExpectAutoSet = strings.Join(stringx.Remove(fsAddressFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
fsAddressRowsWithPlaceHolder = strings.Join(stringx.Remove(fsAddressFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
)
type (
fsAddressModel interface {
Insert(ctx context.Context, data *FsAddress) (sql.Result, error)
FindOne(ctx context.Context, id int64) (*FsAddress, error)
Update(ctx context.Context, data *FsAddress) error
Delete(ctx context.Context, id int64) error
}
defaultFsAddressModel struct {
conn sqlx.SqlConn
table string
}
FsAddress struct {
Id int64 `db:"id"`
UserId int64 `db:"user_id"` // 用户ID
Name string `db:"name"` // 地址名称
FirstName string `db:"first_name"` // FirstName
LastName string `db:"last_name"` // LastName
Mobile string `db:"mobile"` // 手机号码
Street string `db:"street"` // 街道
Suite string `db:"suite"` // 房号
City string `db:"city"` // 城市
State string `db:"state"` // 州名
Country string `db:"country"` // 国家
ZipCode string `db:"zip_code"` // 邮编
Status int64 `db:"status"` // 1正常 0异常
IsDefault int64 `db:"is_default"` // 1默认地址0非默认地址
}
)
func newFsAddressModel(conn sqlx.SqlConn) *defaultFsAddressModel {
return &defaultFsAddressModel{
conn: conn,
table: "`fs_address`",
}
}
func (m *defaultFsAddressModel) Delete(ctx context.Context, id int64) error {
query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
_, err := m.conn.ExecCtx(ctx, query, id)
return err
}
func (m *defaultFsAddressModel) FindOne(ctx context.Context, id int64) (*FsAddress, error) {
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", fsAddressRows, m.table)
var resp FsAddress
err := m.conn.QueryRowCtx(ctx, &resp, query, id)
switch err {
case nil:
return &resp, nil
case sqlc.ErrNotFound:
return nil, ErrNotFound
default:
return nil, err
}
}
func (m *defaultFsAddressModel) Insert(ctx context.Context, data *FsAddress) (sql.Result, error) {
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, fsAddressRowsExpectAutoSet)
ret, err := m.conn.ExecCtx(ctx, query, data.UserId, data.Name, data.FirstName, data.LastName, data.Mobile, data.Street, data.Suite, data.City, data.State, data.Country, data.ZipCode, data.Status, data.IsDefault)
return ret, err
}
func (m *defaultFsAddressModel) Update(ctx context.Context, data *FsAddress) error {
query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, fsAddressRowsWithPlaceHolder)
_, err := m.conn.ExecCtx(ctx, query, data.UserId, data.Name, data.FirstName, data.LastName, data.Mobile, data.Street, data.Suite, data.City, data.State, data.Country, data.ZipCode, data.Status, data.IsDefault, data.Id)
return err
}
func (m *defaultFsAddressModel) tableName() string {
return m.table
}

View File

@ -1,7 +1,18 @@
package main
import "testing"
import (
"testing"
)
type FsCanteenType struct {
Id int64 `db:"id"` // ID
Name string `db:"name"` // 餐厅名字
Sort int64 `db:"sort"` // 排序
Status int64 `db:"status"` // 状态位 1启用0停用
Ctime int64 `db:"ctime"` // 添加时间
}
func TestMain(t *testing.T) {
// log.Println(model.RawFieldNames[FsCanteenType]())
main()
}

View File

@ -22,10 +22,15 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Path: "/user/fonts",
Handler: UserFontsHandler(serverCtx),
},
{
Method: http.MethodGet,
Path: "/user/address-list",
Handler: UserAddressListHandler(serverCtx),
},
{
Method: http.MethodGet,
Path: "/user/get-type",
Handler: GetTypeHandler(serverCtx),
Handler: UserGetTypeHandler(serverCtx),
},
{
Method: http.MethodPost,

View File

@ -0,0 +1,37 @@
package handler
import (
"errors"
"net/http"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
"fusenapi/server/home-user-auth/internal/logic"
"fusenapi/server/home-user-auth/internal/svc"
"fusenapi/server/home-user-auth/internal/types"
)
func UserAddressListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.Request
if err := httpx.Parse(r, &req); err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 510,
Message: "parameter error",
})
logx.Info(err)
return
}
l := logic.NewUserAddressListLogic(r.Context(), svcCtx)
resp := l.UserAddressList(&req)
if resp != nil {
httpx.OkJsonCtx(r.Context(), w, resp)
} else {
err := errors.New("server logic is error, resp must not be nil")
httpx.ErrorCtx(r.Context(), w, err)
logx.Error(err)
}
}
}

View File

@ -12,7 +12,7 @@ import (
"fusenapi/server/home-user-auth/internal/types"
)
func GetTypeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
func UserGetTypeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.Request
if err := httpx.Parse(r, &req); err != nil {
@ -24,8 +24,8 @@ func GetTypeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return
}
l := logic.NewGetTypeLogic(r.Context(), svcCtx)
resp := l.GetType(&req)
l := logic.NewUserGetTypeLogic(r.Context(), svcCtx)
resp := l.UserGetType(&req)
if resp != nil {
httpx.OkJsonCtx(r.Context(), w, resp)
} else {

View File

@ -0,0 +1,40 @@
package logic
import (
"context"
"fusenapi/model"
"fusenapi/server/home-user-auth/internal/svc"
"fusenapi/server/home-user-auth/internal/types"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"github.com/zeromicro/go-zero/core/logx"
)
type UserAddressListLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewUserAddressListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserAddressListLogic {
return &UserAddressListLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *UserAddressListLogic) UserAddressList(req *types.Request) (resp *types.Response) {
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
m := model.NewFsAddressModel(l.svcCtx.MysqlConn)
user := auth.GetUserInfoFormCtx(l.ctx)
// model里设置了 返回值. 和 types 里的一至可以直接返回
data, err := m.FindDataAddressList(l.ctx, user.UserId)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, err.Error())
}
return resp.SetStatus(basic.CodeOK, data)
}

View File

@ -11,21 +11,22 @@ import (
"github.com/zeromicro/go-zero/core/logx"
)
type GetTypeLogic struct {
type UserGetTypeLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewGetTypeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetTypeLogic {
return &GetTypeLogic{
func NewUserGetTypeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserGetTypeLogic {
return &UserGetTypeLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *GetTypeLogic) GetType(req *types.Request) (resp *types.Response) {
func (l *UserGetTypeLogic) UserGetType(req *types.Request) (resp *types.Response) {
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
data, err := model.NewFsCanteenTypeModel(l.svcCtx.MysqlConn).FindGetType(l.ctx)
if err != nil {
logx.Error(err)

View File

@ -91,6 +91,21 @@ type DataStatusConfig struct {
LogisticsStatus []KeyNameButton `json:"logistics_status"` //物流状态筛选项
}
type DataAddressList struct {
Id int64 `db:"id"`
UserId int64 `db:"user_id"` // 用户ID
Name string `db:"name"` // 地址名称
FirstName string `db:"first_name"` // FirstName
LastName string `db:"last_name"` // LastName
Mobile string `db:"mobile"` // 手机号码
Street string `db:"street"` // 街道
Suite string `db:"suite"` // 房号
City string `db:"city"` // 城市
State string `db:"state"` // 州名
ZipCode string `db:"zip_code"` // 邮编
IsDefault int64 `db:"is_default"` // 1默认地址0非默认地址
}
type Response struct {
Code int `json:"code"`
Message string `json:"msg"`

View File

@ -21,7 +21,10 @@ service home-user-auth {
@handler UserFontsHandler
get /user/fonts(request) returns (response);
@handler GetTypeHandler
@handler UserAddressListHandler
get /user/address-list(request) returns (response);
@handler UserGetTypeHandler
get /user/get-type(request) returns (response);
@handler UserSaveBasicInfoHandler
@ -29,6 +32,7 @@ service home-user-auth {
@handler UserStatusConfigHandler
post /user/status-config(request) returns (response);
}
@server(
@ -128,4 +132,22 @@ type DataStatusConfig {
Time []KeyName `json:"time"` //返回订单时间筛选项
RefundReason []KeyName `json:"refund_reason"` //退款原因说明项
LogisticsStatus []KeyNameButton `json:"logistics_status"` //物流状态筛选项
}
// DataAddressList /user/address-list 返回值
type DataAddressList {
Id int64 `db:"id"`
UserId int64 `db:"user_id"` // 用户ID
Name string `db:"name"` // 地址名称
FirstName string `db:"first_name"` // FirstName
LastName string `db:"last_name"` // LastName
Mobile string `db:"mobile"` // 手机号码
Street string `db:"street"` // 街道
Suite string `db:"suite"` // 房号
City string `db:"city"` // 城市
State string `db:"state"` // 州名
// Country string `db:"country"` // 国家
ZipCode string `db:"zip_code"` // 邮编
// Status int64 `db:"status"` // 1正常 0异常
IsDefault int64 `db:"is_default"` // 1默认地址0非默认地址
}

49
utils/sqlfs/builder.go Normal file
View File

@ -0,0 +1,49 @@
package sqlfs
import (
"reflect"
"strings"
)
var dbTag = "db"
// RawFieldNames converts golang struct field into slice string.
func RawFieldNames[T any]() []string {
var out []string
var a T
v := reflect.ValueOf(a)
typ := v.Type()
for i := 0; i < v.NumField(); i++ {
// gets us a StructField
fi := typ.Field(i)
tagv := fi.Tag.Get(dbTag)
switch tagv {
case "-":
continue
default:
// get tag name with the tag opton, e.g.:
// `db:"id"`
// `db:"id,type=char,length=16"`
// `db:",type=char,length=16"`
// `db:"-,type=char,length=16"`
if strings.Contains(tagv, ",") {
tagv = strings.TrimSpace(strings.Split(tagv, ",")[0])
}
if tagv == "-" {
continue
}
if len(tagv) == 0 {
tagv = fi.Name
}
out = append(out, tagv)
}
}
return out
}