You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.2 KiB
50 lines
1.2 KiB
package logic
|
|
|
|
import (
|
|
"context"
|
|
|
|
"healthapi/internal/model"
|
|
"healthapi/internal/svc"
|
|
"healthapi/internal/types"
|
|
"healthapi/pkg/errorx"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type SetDefaultAddressLogic struct {
|
|
logx.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
func NewSetDefaultAddressLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SetDefaultAddressLogic {
|
|
return &SetDefaultAddressLogic{
|
|
Logger: logx.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
func (l *SetDefaultAddressLogic) SetDefaultAddress(req *types.AddressIdReq) (resp *types.CommonResp, err error) {
|
|
userID, err := GetUserIDFromCtx(l.ctx)
|
|
if err != nil {
|
|
return nil, errorx.ErrUnauthorized
|
|
}
|
|
|
|
// 验证地址存在
|
|
var addr model.Address
|
|
if err := l.svcCtx.DB.Where("id = ? AND user_id = ?", req.Id, userID).First(&addr).Error; err != nil {
|
|
return nil, errorx.NewCodeError(404, "地址不存在")
|
|
}
|
|
|
|
// 取消所有默认地址
|
|
l.svcCtx.DB.Model(&model.Address{}).Where("user_id = ?", userID).Update("is_default", false)
|
|
|
|
// 设置新的默认地址
|
|
l.svcCtx.DB.Model(&addr).Update("is_default", true)
|
|
|
|
return &types.CommonResp{
|
|
Code: 0,
|
|
Message: "设置成功",
|
|
}, nil
|
|
}
|
|
|