Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

完成 添加收获地址接口, 效果已验证 #20

Merged
merged 3 commits into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions apps/app/api/api.api
Original file line number Diff line number Diff line change
Expand Up @@ -199,15 +199,15 @@ service api-api {

@doc "add user receiveAddress"
@handler addReceiveAddress
get /addReceiveAddress (UserReceiveAddressAddReq) returns (UserReceiveAddressAddRes)
post /addReceiveAddress (UserReceiveAddressAddReq) returns (UserReceiveAddressAddRes)

@doc "edit user receiveAddress list"
@doc "edit user receiveAddress"
@handler editReceiveAddress
get /editReceiveAddress (UserReceiveAddressEditReq) returns (UserReceiveAddressEditRes)
post /editReceiveAddress (UserReceiveAddressEditReq) returns (UserReceiveAddressEditRes)

@doc "del user receiveAddress list"
@handler delReceiveAddress
get /delReceiveAddress (UserReceiveAddressDelReq) returns (UserReceiveAddressDelRes)
post /delReceiveAddress (UserReceiveAddressDelReq) returns (UserReceiveAddressDelRes)

@doc "get user receiveAddress list"
@handler userReceiveAddressList
Expand Down
8 changes: 4 additions & 4 deletions apps/app/api/desc/userReceiveAddress.api
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ type UserReceiveAddress {
Uid uint64 `json:"uid"` //用户id
Name string `json:"name"` //收货人名称
Phone string `json:"phone"` //手机号
IsDefault uint8 `json:"is_default"` //是否为默认地址
IsDefault int32 `json:"is_default"` //是否为默认地址
PostCode string `json:"post_code"` //邮政编码
Province string `json:"province"` //省份/直辖市
City string `json:"city"` //城市
Region string `json:"region"` //区
DetailAddress string `json:"detail_address"` //详细地址(街道)
IsDelete uint8 `json:"is_delete"` //是否删除
IsDelete int32 `json:"is_delete"` //是否删除
CreateTime int64 `json:"create_time"` //数据创建时间[禁止在代码中赋值]
UpdateTime int64 `json:"update_time"` //数据更新时间[禁止在代码中赋值]
}
Expand All @@ -39,7 +39,7 @@ type (
UserReceiveAddressAddReq {
Name string `json:"name"` //收货人名称
Phone string `json:"phone"` //手机号
IsDefault uint8 `json:"is_default"` //是否为默认地址
IsDefault int32 `json:"is_default"` //是否为默认地址
PostCode string `json:"post_code"` //邮政编码
Province string `json:"province"` //省份/直辖市
City string `json:"city"` //城市
Expand All @@ -57,7 +57,7 @@ type (
Id int64 `json:"id"`
Name string `json:"name"` //收货人名称
Phone string `json:"phone"` //手机号
IsDefault uint8 `json:"is_default"` //是否为默认地址
IsDefault int32 `json:"is_default"` //是否为默认地址
PostCode string `json:"post_code"` //邮政编码
Province string `json:"province"` //省份/直辖市
City string `json:"city"` //城市
Expand Down
6 changes: 3 additions & 3 deletions apps/app/api/internal/handler/routes.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func AddReceiveAddressHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.UserReceiveAddressAddReq
if err := httpx.Parse(r, &req); err != nil {
httpx.Error(w, err)
result.ParamErrorResult(r, w, err)
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func DelReceiveAddressHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.UserReceiveAddressDelReq
if err := httpx.Parse(r, &req); err != nil {
httpx.Error(w, err)
result.ParamErrorResult(r, w, err)
return
}

Expand Down
2 changes: 1 addition & 1 deletion apps/app/api/internal/handler/user/detailhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func DetailHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.UserInfoReq
if err := httpx.Parse(r, &req); err != nil {
httpx.Error(w, err)
result.ParamErrorResult(r, w, err)
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func EditReceiveAddressHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.UserReceiveAddressEditReq
if err := httpx.Parse(r, &req); err != nil {
httpx.Error(w, err)
result.ParamErrorResult(r, w, err)
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func UserReceiveAddressListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc
return func(w http.ResponseWriter, r *http.Request) {
var req types.UserReceiveAddressListReq
if err := httpx.Parse(r, &req); err != nil {
httpx.Error(w, err)
result.ParamErrorResult(r, w, err)
return
}

Expand Down
15 changes: 13 additions & 2 deletions apps/app/api/internal/logic/user/addreceiveaddresslogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package user

import (
"context"
"encoding/json"

"github.com/jinzhu/copier"
"github.com/zhoushuguang/lebron/apps/app/api/internal/svc"
"github.com/zhoushuguang/lebron/apps/app/api/internal/types"
"github.com/zhoushuguang/lebron/apps/user/rpc/user"

"github.com/zeromicro/go-zero/core/logx"
)
Expand All @@ -24,6 +27,14 @@ func NewAddReceiveAddressLogic(ctx context.Context, svcCtx *svc.ServiceContext)
}

func (l *AddReceiveAddressLogic) AddReceiveAddress(req *types.UserReceiveAddressAddReq) (resp *types.UserReceiveAddressAddRes, err error) {

return
//get jwt token uid
uid, _ := l.ctx.Value("uid").(json.Number).Int64()
var addRpcReq user.UserReceiveAddressAddReq
addRpcReq.Uid = int32(uid)
copier.Copy(&addRpcReq, req)
_, err = l.svcCtx.UserRPC.AddUserReceiveAddress(l.ctx, &addRpcReq)
if err != nil {
return nil, err
}
return resp, nil
}
9 changes: 6 additions & 3 deletions apps/app/api/internal/logic/user/delreceiveaddresslogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/zhoushuguang/lebron/apps/app/api/internal/svc"
"github.com/zhoushuguang/lebron/apps/app/api/internal/types"
"github.com/zhoushuguang/lebron/apps/user/rpc/user"

"github.com/zeromicro/go-zero/core/logx"
)
Expand All @@ -24,7 +25,9 @@ func NewDelReceiveAddressLogic(ctx context.Context, svcCtx *svc.ServiceContext)
}

func (l *DelReceiveAddressLogic) DelReceiveAddress(req *types.UserReceiveAddressDelReq) (resp *types.UserReceiveAddressDelRes, err error) {
// todo: add your logic here and delete this line

return
_, err = l.svcCtx.UserRPC.DelUserReceiveAddress(l.ctx, &user.UserReceiveAddressDelReq{Id: req.Id})
if err != nil {
return nil, err
}
return resp, nil
}
12 changes: 9 additions & 3 deletions apps/app/api/internal/logic/user/editreceiveaddresslogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package user
import (
"context"

"github.com/jinzhu/copier"
"github.com/zhoushuguang/lebron/apps/app/api/internal/svc"
"github.com/zhoushuguang/lebron/apps/app/api/internal/types"
"github.com/zhoushuguang/lebron/apps/user/rpc/user"

"github.com/zeromicro/go-zero/core/logx"
)
Expand All @@ -24,7 +26,11 @@ func NewEditReceiveAddressLogic(ctx context.Context, svcCtx *svc.ServiceContext)
}

func (l *EditReceiveAddressLogic) EditReceiveAddress(req *types.UserReceiveAddressEditReq) (resp *types.UserReceiveAddressEditRes, err error) {
// todo: add your logic here and delete this line

return
var editRpcReq user.UserReceiveAddressEditReq
copier.Copy(&editRpcReq, req)
_, err = l.svcCtx.UserRPC.EditUserReceiveAddress(l.ctx, &editRpcReq)
if err != nil {
return nil, err
}
return resp, nil
}
3 changes: 2 additions & 1 deletion apps/app/api/internal/logic/user/loginlogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"time"

"github.com/pkg/errors"
"github.com/zhoushuguang/lebron/apps/user/rpc/user"
"github.com/zhoushuguang/lebron/pkg/jwtx"

Expand Down Expand Up @@ -33,7 +34,7 @@ func (l *LoginLogic) Login(req *types.LoginReq) (resp *types.LoginResp, err erro
Password: req.Password,
})
if err != nil {
return nil, err
return nil, errors.Wrapf(err, "req: %+v", req)
}

//generate token
Expand Down
8 changes: 4 additions & 4 deletions apps/app/api/internal/types/types.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions apps/user/model/userreceiveaddressmodel.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package model

import (
"context"
"database/sql"
"fmt"

"github.com/zeromicro/go-zero/core/stores/cache"
"github.com/zeromicro/go-zero/core/stores/sqlx"
)
Expand All @@ -12,13 +16,23 @@ type (
// and implement the added methods in customUserReceiveAddressModel.
UserReceiveAddressModel interface {
userReceiveAddressModel
UpdateIsDelete(ctx context.Context, data *UserReceiveAddress) error
}

customUserReceiveAddressModel struct {
*defaultUserReceiveAddressModel
}
)

func (m customUserReceiveAddressModel) UpdateIsDelete(ctx context.Context, data *UserReceiveAddress) error {
userReceiveAddressIdKey := fmt.Sprintf("%s%v", cacheUserReceiveAddressIdPrefix, data.Id)
_, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
query := fmt.Sprintf("update %s set is_delete = 1 where `id` = ?", m.table)
return conn.ExecCtx(ctx, query, data.Id)
}, userReceiveAddressIdKey)
return err
}

// NewUserReceiveAddressModel returns a model for the database table.
func NewUserReceiveAddressModel(conn sqlx.SqlConn, c cache.CacheConf) UserReceiveAddressModel {
return &customUserReceiveAddressModel{
Expand Down
4 changes: 2 additions & 2 deletions apps/user/model/userreceiveaddressmodel_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 17 additions & 2 deletions apps/user/rpc/internal/logic/adduserreceiveaddresslogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package logic
import (
"context"

"github.com/pkg/errors"
"github.com/zhoushuguang/lebron/apps/user/model"
"github.com/zhoushuguang/lebron/apps/user/rpc/internal/svc"
"github.com/zhoushuguang/lebron/apps/user/rpc/user"
"github.com/zhoushuguang/lebron/pkg/xerr"

"github.com/zeromicro/go-zero/core/logx"
)
Expand All @@ -25,7 +28,19 @@ func NewAddUserReceiveAddressLogic(ctx context.Context, svcCtx *svc.ServiceConte

// AddUserReceiveAddress 添加收获地址
func (l *AddUserReceiveAddressLogic) AddUserReceiveAddress(in *user.UserReceiveAddressAddReq) (*user.UserReceiveAddressAddRes, error) {
// todo: add your logic here and delete this line

dbAddress := new(model.UserReceiveAddress)
dbAddress.Uid = int64(in.GetUid())
dbAddress.Name = in.GetName()
dbAddress.Phone = in.GetPhone()
dbAddress.Province = in.GetProvince()
dbAddress.City = in.GetCity()
dbAddress.IsDefault = in.GetIsDefault()
dbAddress.PostCode = in.GetPostCode()
dbAddress.Region = in.GetRegion()
dbAddress.DetailAddress = in.GetDetailAddress()
_, err := l.svcCtx.UserReceiveAddressModel.Insert(l.ctx, dbAddress)
if err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DbError), "AddUserReceiveAddress Database Exception : %+v , err: %v", dbAddress, err)
}
return &user.UserReceiveAddressAddRes{}, nil
}
21 changes: 18 additions & 3 deletions apps/user/rpc/internal/logic/deluserreceiveaddresslogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package logic
import (
"context"

"github.com/pkg/errors"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zhoushuguang/lebron/apps/user/model"
"github.com/zhoushuguang/lebron/apps/user/rpc/internal/svc"
"github.com/zhoushuguang/lebron/apps/user/rpc/user"

"github.com/zeromicro/go-zero/core/logx"
"github.com/zhoushuguang/lebron/pkg/xerr"
)

type DelUserReceiveAddressLogic struct {
Expand All @@ -25,7 +27,20 @@ func NewDelUserReceiveAddressLogic(ctx context.Context, svcCtx *svc.ServiceConte

// DelUserReceiveAddress 删除收获地址
func (l *DelUserReceiveAddressLogic) DelUserReceiveAddress(in *user.UserReceiveAddressDelReq) (*user.UserReceiveAddressDelRes, error) {
// todo: add your logic here and delete this line
_, err := l.svcCtx.UserReceiveAddressModel.FindOne(l.ctx,in.Id)
if err != nil {
if err == model.ErrNotFound {
return nil, errors.Wrap(xerr.NewErrMsg("数据不存在"), "收获地址不存在")
}
return nil, err
}

dbAddress := new(model.UserReceiveAddress)
dbAddress.Id = in.Id
dbAddress.IsDelete = 1
err = l.svcCtx.UserReceiveAddressModel.UpdateIsDelete(l.ctx, dbAddress)
if err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DbError), "DelUserReceiveAddress Database Exception : %+v , err: %v", dbAddress, err)
}
return &user.UserReceiveAddressDelRes{}, nil
}
Loading