todo: add-address

This commit is contained in:
eson 2023-06-19 20:10:32 +08:00
parent 640d3261e0
commit db155a71a0
7 changed files with 89 additions and 13 deletions

View File

@ -19,7 +19,7 @@ type FsAddress struct {
Country *string `gorm:"default:'';" json:"country"` // 国家
ZipCode *string `gorm:"default:'';" json:"zip_code"` // 邮编
Status *int64 `gorm:"default:0;" json:"status"` // 1正常 0异常
IsDefault *int64 `gorm:"default:0;" json:"is_default"` // 1默认地址0非默认地址
IsDefault *int64 `gorm:"index;default:0;" json:"is_default"` // 1默认地址0非默认地址
}
type FsAddressModel struct{ db *gorm.DB }

View File

@ -15,6 +15,14 @@ func (a *FsAddressModel) GetOne(ctx context.Context, id int64, userId int64) (re
return resp, nil
}
func (a *FsAddressModel) GetUserAllAddress(ctx context.Context, userId int64) (resp []FsAddress, err error) {
err = a.db.WithContext(ctx).Model(&FsAddress{}).Where("`user_id` = ? and `status` = ?", userId, 1).Order("`id` DESC").Find(&resp).Error
if err != nil {
return nil, err
}
return
}
func (a *FsAddressModel) CreateOne(ctx context.Context, address *FsAddress) (resp *FsAddress, err error) {
err = a.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
@ -45,10 +53,15 @@ func (a *FsAddressModel) CreateOne(ctx context.Context, address *FsAddress) (res
return resp, nil
}
func (a *FsAddressModel) GetUserAllAddress(ctx context.Context, userId int64) (resp []FsAddress, err error) {
err = a.db.WithContext(ctx).Model(&FsAddress{}).Where("`user_id` = ? and `status` = ?", userId, 1).Order("`id` DESC").Find(&resp).Error
if err != nil {
return nil, err
}
return
func (a *FsAddressModel) UpdateAddAddress(ctx context.Context, address *FsAddress) (err error) {
err = a.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
if *address.IsDefault > 0 {
err = tx.Where("user_id = ? and is_default = 1", *address.UserId).Update("is_default", 0).Error
if err != nil {
return err
}
}
return a.db.WithContext(ctx).Where("id = ? and user_id = ?", address.Id, address.UserId).Omit("id", "user_id").Updates(address).Error
})
return err
}

View File

@ -1,6 +1,7 @@
package logic
import (
"fusenapi/model/gmodel"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
@ -38,5 +39,62 @@ func (l *UserAddAddressLogic) UserAddAddress(req *types.RequestAddAddress, useri
return resp.SetStatus(basic.CodeSafeValueRangeErr)
}
return resp.SetStatus(basic.CodeOK)
m := gmodel.NewFsAddressModel(l.svcCtx.MysqlConn)
var status int64 = 1
if req.Id == 0 {
// $address->country = 'USA';
// $address->status = 1;
// $address->user_id = $uid;
var (
country string = "USA"
isDefautl int64 = 1
)
createOne := &gmodel.FsAddress{
Name: &req.Name,
FirstName: &req.FirstName,
LastName: &req.LastName,
Mobile: &req.Mobile,
Street: &req.Street,
Suite: &req.Suite,
City: &req.City,
State: &req.State,
Country: &country,
Status: &status,
UserId: &userinfo.UserId,
ZipCode: &req.ZipCode,
IsDefault: &isDefautl,
}
_, err := m.CreateOne(l.ctx, createOne)
if err != nil {
logx.Error(err)
return resp.SetStatus(basic.CodeDbCreateErr)
}
return resp.SetStatus(basic.CodeOK, map[string]int64{"id": createOne.Id})
}
address := &gmodel.FsAddress{
Id: req.Id,
Name: &req.Name,
FirstName: &req.FirstName,
LastName: &req.LastName,
Mobile: &req.Mobile,
Street: &req.Street,
Suite: &req.Suite,
City: &req.City,
State: &req.State,
Status: &status,
UserId: &userinfo.UserId,
ZipCode: &req.ZipCode,
IsDefault: &req.IsDefault,
}
err := m.UpdateAddAddress(l.ctx, address)
if err != nil {
logx.Error(err)
return resp.SetStatus(basic.CodeDbUpdateErr)
}
return resp.SetStatus(basic.CodeOK, map[string]int64{"id": address.Id})
}

View File

@ -54,7 +54,7 @@ func (l *UserSaveBasicInfoLogic) UserSaveBasicInfo(req *types.RequestBasicInfoFo
case gmodel.ErrRecordNotFound:
return resp.SetStatus(basic.CodeUserIdNotFoundErr)
default:
return resp.SetStatusWithMessage(basic.CodeUpdateErr, err.Error())
return resp.SetStatusWithMessage(basic.CodeDbUpdateErr, err.Error())
}
}

View File

@ -31,7 +31,7 @@ type RequestUserLogin struct {
type RequestAddAddress struct {
Id int64 `json:"id"` // address_id 地址id
IsDefault int `json:"is_default"` //是否默认
IsDefault int64 `json:"is_default"` //是否默认
Name string `json:"name"` //收货人
FirstName string `json:"first_name"` //first_name
LastName string `json:"last_name"` //last_name

View File

@ -69,7 +69,7 @@ type RequestUserLogin {
type RequestAddAddress {
Id int64 `json:"id"` // address_id 地址id
IsDefault int `json:"is_default"` //是否默认
IsDefault int64 `json:"is_default"` //是否默认
Name string `json:"name"` //收货人
FirstName string `json:"first_name"` //first_name
LastName string `json:"last_name"` //last_name

View File

@ -22,10 +22,11 @@ var (
CodeGuestDupErr = &StatusResponse{5010, "the user is already a guest user and does not need to apply again"} // 用户已经是guest用户不需要重复申请 错误
CodeGuestGenErr = &StatusResponse{5011, "serialization failed for guest_id of the visitor"} // 游客guest_id序列化失败
CodeUpdateErr = &StatusResponse{5000, "update database error"} // update database logic 错误
CodeDbUpdateErr = &StatusResponse{5000, "update database error"} // update database logic 错误
CodeDupGuestErr = &StatusResponse{5001, "the user is already a guest user and does not need to apply again"} // 用户已经是guest用户不需要重复申请 错误
CodeRequestParamsErr = &StatusResponse{5002, "invalid request param"} // api参数校验 错误
CodeDbRecordNotFoundErr = &StatusResponse{5003, "db record not found"}
CodeDbCreateErr = &StatusResponse{5004, "create one in database error"}
)
type Response struct {
@ -58,7 +59,11 @@ func (resp *Response) SetStatus(sr *StatusResponse, data ...interface{}) *Respon
Message: sr.Message,
}
if len(data) == 1 {
newResp.Data = data[0]
if err, ok := data[0].(error); ok {
newResp.Message = err.Error()
} else {
newResp.Data = data[0]
}
}
return newResp
}