更新用户地址接口的实现
1. 接口功能说明
根据文档说明,更新用户地址的接口接收以下参数:
-
rest
:请求体。 -
location
:用户的位置信息,包括纬度、经度、省份和城市。 -
update_user_data
:一个额外的字段,用于指示客户端是否需要更新缓存信息。
接口的主要功能是更新用户的地址信息,并在更新完成后返回更新后的地址信息。客户端在收到响应后,会根据 update_user_data
字段决定是否更新本地缓存。
2. 实现步骤
2.1 定义请求和响应模型
在 models
目录下定义请求和响应的数据结构:
models/update_user_address.go
:
package models// UpdateUserAddressRequest 定义更新用户地址的请求参数
type UpdateUserAddressRequest struct {Address struct {Latitude string `json:"latitude"`Longitude string `json:"longitude"`Province string `json:"province"`City string `json:"city"`} `json:"address"`
}// UpdateUserAddressResponse 定义更新用户地址的响应参数
type UpdateUserAddressResponse struct {UID string `json:"uid"`Address Address `json:"address"`UpdateUserData bool `json:"update_user_data"`
}// Address 定义地址信息
type Address struct {Latitude string `json:"latitude"`Longitude string `json:"longitude"`Province string `json:"province"`City string `json:"city"`
}
2.2 实现更新逻辑
在 service
层实现更新用户地址的逻辑:
service/user_service.go
:
package serviceimport ("context""github.com/gin-gonic/gin""go.mongodb.org/mongo-driver/bson""go.mongodb.org/mongo-driver/mongo""your_project/models"
)type UserService struct {DB *mongo.Database
}func NewUserService(db *mongo.Database) *UserService {return &UserService{DB: db}
}// UpdateUserAddress 更新用户的地址信息
func (s *UserService) UpdateUserAddress(uid string, address models.Address) error {collection := s.DB.Collection("users")filter := bson.M{"uid": uid}update := bson.M{"$set": bson.M{"address.latitude": address.Latitude,"address.longitude": address.Longitude,"address.province": address.Province,"address.city": address.City,}}_, err := collection.UpdateOne(context.TODO(), filter, update)return err
}
2.3 处理请求和响应
在 handler
层处理请求和返回响应:
handler/user_handler.go
:
package handlerimport ("encoding/json""net/http""your_project/models""your_project/service"
)type UserHandler struct {UserService *service.UserService
}func NewUserHandler(userService *service.UserService) *UserHandler {return &UserHandler{UserService: userService,}
}// UpdateUserAddress 更新用户地址
func (h *UserHandler) UpdateUserAddress(c *gin.Context) {var req models.UpdateUserAddressRequestif err := c.ShouldBindJSON(&req); err != nil {c.JSON(http.StatusBadRequest, gin.H{"code": 400,"message": "Invalid request parameters",})return}// 获取用户IDuid := c.Param("uid")if uid == "" {c.JSON(http.StatusBadRequest, gin.H{"code": 400,"message": "User ID is required",})return}// 更新用户地址if err := h.UserService.UpdateUserAddress(uid, req.Address); err != nil {c.JSON(http.StatusInternalServerError, gin.H{"code": 500,"message": "Failed to update user address",})return}// 返回更新后的地址信息resp := models.UpdateUserAddressResponse{UID: uid,Address: req.Address,UpdateUserData: true,}c.JSON(http.StatusOK, gin.H{"code": 200,"data": resp,})
}
2.4 注册路由
在 main
函数中注册路由:
main.go
:
package mainimport ("github.com/gin-gonic/gin""your_project/handler""your_project/service""go.mongodb.org/mongo-driver/mongo""go.mongodb.org/mongo-driver/mongo/options"
)func main() {// 连接到MongoDBclient, err := mongo.Connect(context.TODO(), options.Client().ApplyURI("mongodb://localhost:27017"))if err != nil {log.Fatalf("Failed to connect to MongoDB: %v", err)}defer client.Disconnect(context.TODO())// 初始化UserServiceuserService := service.NewUserService(client.Database("your_db"))// 初始化UserHandleruserHandler := handler.NewUserHandler(userService)// 初始化Gin引擎router := gin.Default()// 注册更新用户地址的路由router.PUT("/users/:uid/address", userHandler.UpdateUserAddress)// 启动服务router.Run(":8080")
}