用户关系功能
This commit is contained in:
@@ -3,6 +3,8 @@ package mysql
|
||||
import (
|
||||
"acquaintances/biz/model"
|
||||
"acquaintances/biz/model/user"
|
||||
"errors"
|
||||
|
||||
"github.com/cloudwego/hertz/pkg/common/hlog"
|
||||
)
|
||||
|
||||
@@ -29,7 +31,7 @@ func UpdatesUser(user *user.UpdateUserRequest) error {
|
||||
if user.Address != "" {
|
||||
maps["address"] = user.Address
|
||||
}
|
||||
if user.Area != "" {
|
||||
if user.Area != 0 {
|
||||
maps["area"] = user.Area
|
||||
}
|
||||
if user.Mobile != "" {
|
||||
@@ -51,6 +53,9 @@ func UpdatesUser(user *user.UpdateUserRequest) error {
|
||||
return db.Where("user_id = ?", user.UserID).Updates(maps).Error
|
||||
}
|
||||
|
||||
// InfoUser 查询单个用户
|
||||
// 参数:用户ID
|
||||
// 返回:用户信息指针,错误
|
||||
func InfoUser(id int64) (*user.UserInfoReq, error) {
|
||||
db := DB.Model(&model.User{})
|
||||
db.Where("user_id = ?", id)
|
||||
@@ -60,3 +65,23 @@ func InfoUser(id int64) (*user.UserInfoReq, error) {
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// GetUsersById 批量查询用户信息
|
||||
// 参数:用户ID切片
|
||||
// 返回:用户信息切片指针,错误
|
||||
func GetUsersById(ids []string) ([]*user.UserInfoReq, error) {
|
||||
db := DB.Model(&model.User{})
|
||||
// 参数校验
|
||||
if len(ids) == 0 {
|
||||
return nil, errors.New("user IDs cannot be empty")
|
||||
}
|
||||
query := db.
|
||||
Where("user_id IN (?)", ids)
|
||||
|
||||
var users []*user.UserInfoReq
|
||||
if err := query.Find(&users).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return users, nil
|
||||
}
|
||||
|
@@ -3,23 +3,119 @@ package mysql
|
||||
import (
|
||||
"acquaintances/biz/model"
|
||||
"acquaintances/biz/model/user"
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/cloudwego/hertz/pkg/common/hlog"
|
||||
)
|
||||
|
||||
func CreateUserRelations(relations user.CreateUserRelationsReq) error {
|
||||
db := DB.Model(&model.UserRelations{})
|
||||
return db.Create(relations).Error
|
||||
var data model.UserRelations
|
||||
friends, err := getFriend(relations.UserID)
|
||||
if err != nil {
|
||||
hlog.Error("DeleteUserRelations:", err)
|
||||
return err
|
||||
}
|
||||
// 检查是否已存在
|
||||
for _, friend := range friends {
|
||||
if friend.FriendID == relations.FriendID {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
// 不存在
|
||||
newFriend := model.Friend{
|
||||
FriendID: relations.FriendID,
|
||||
Remark: relations.Remark,
|
||||
CreateTime: time.Now().Unix(),
|
||||
}
|
||||
friends = append(friends, newFriend)
|
||||
data.FriendList, err = json.Marshal(friends)
|
||||
if err != nil {
|
||||
hlog.Error("DeleteUserRelations:", err)
|
||||
return err
|
||||
}
|
||||
data.FriendCount = int64(len(friends))
|
||||
return db.Create(data).Error
|
||||
}
|
||||
|
||||
func DeleteUserRelations(relations user.DeleteUserRelationsReq) error {
|
||||
db := DB.Model(&model.UserRelations{})
|
||||
return db.Delete(&model.UserRelations{UserID: relations.UserID, FriendID: relations.FriendID}).Error
|
||||
friends, err := getFriend(relations.UserID)
|
||||
if err != nil {
|
||||
hlog.Error("DeleteUserRelations:", err)
|
||||
return err
|
||||
}
|
||||
var newFriends []model.Friend
|
||||
for _, friend := range friends {
|
||||
if friend.FriendID != relations.FriendID {
|
||||
newFriends = append(newFriends, friend)
|
||||
}
|
||||
}
|
||||
|
||||
maps := make(map[string]interface{})
|
||||
maps["friend_count"] = int64(len(newFriends))
|
||||
maps["friend_list"], err = json.Marshal(newFriends)
|
||||
if err != nil {
|
||||
hlog.Error("DeleteUserRelations:", err)
|
||||
return err
|
||||
}
|
||||
return db.Where("user_id = ?", relations.UserID).Updates(maps).Error
|
||||
}
|
||||
|
||||
func GetUserListRelations(relations user.ListUserRelationsReq) (*user.ListUserRelationsResponse, error) {
|
||||
func UpdateUserRelations(relations user.UpdateUserRelationsReq) error {
|
||||
db := DB.Model(&model.UserRelations{})
|
||||
var res *user.ListUserRelationsResponse
|
||||
if err := db.Where("user_id = ? or friend_id= ?", relations.UserID).Find(&res).Error; err != nil {
|
||||
return nil, err
|
||||
friends, err := getFriend(relations.UserID)
|
||||
if err != nil {
|
||||
hlog.Error("UpdateUserRelations:", err)
|
||||
return err
|
||||
}
|
||||
for _, friend := range friends {
|
||||
if friend.FriendID == relations.FriendID {
|
||||
friend.Remark = relations.Remark
|
||||
}
|
||||
}
|
||||
maps := make(map[string]interface{})
|
||||
maps["friend_list"], err = json.Marshal(friends)
|
||||
if err != nil {
|
||||
hlog.Error("UpdateUserRelations:", err)
|
||||
return err
|
||||
}
|
||||
return db.Where("user_id = ?", relations.UserID).Updates(maps).Error
|
||||
}
|
||||
|
||||
|
||||
func GetUserListRelations(relations user.ListUserRelationsReq) (*user.ListUserRelationsRes, error) {
|
||||
var res *user.ListUserRelationsRes
|
||||
friends, err := getFriend(relations.UserID)
|
||||
if err != nil {
|
||||
hlog.Error("GetUserListRelations:", err)
|
||||
return nil,err
|
||||
}
|
||||
var ids []string
|
||||
for _,v :=range friends{
|
||||
ids = append(ids, v.FriendID)
|
||||
}
|
||||
res.Users,err = GetUsersById(ids)
|
||||
if err != nil{
|
||||
hlog.Error("GetUserListRelations:", err)
|
||||
return nil,err
|
||||
}
|
||||
res.Total = int64(len(friends))
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func getFriend(userId string) ([]model.Friend, error) {
|
||||
db := DB.Model(&model.UserRelations{})
|
||||
var beforeRes model.UserRelations
|
||||
if err := db.Where("user_id = ?", userId).Find(&beforeRes).Error; err != nil {
|
||||
hlog.Error("getFriend:", err)
|
||||
return nil, err
|
||||
}
|
||||
var friends []model.Friend
|
||||
if err := json.Unmarshal(beforeRes.FriendList, &friends); err != nil {
|
||||
hlog.Error("getFriend:", err)
|
||||
return nil, err
|
||||
}
|
||||
return friends, nil
|
||||
}
|
||||
|
@@ -13,6 +13,6 @@ import (
|
||||
// Ping .
|
||||
func Ping(ctx context.Context, c *app.RequestContext) {
|
||||
c.JSON(consts.StatusOK, utils.H{
|
||||
"message": "pong",
|
||||
"message": "acquaintances pong",
|
||||
})
|
||||
}
|
||||
|
@@ -7,7 +7,9 @@ import (
|
||||
"context"
|
||||
|
||||
user "acquaintances/biz/model/user"
|
||||
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
"github.com/cloudwego/hertz/pkg/common/hlog"
|
||||
"github.com/cloudwego/hertz/pkg/protocol/consts"
|
||||
)
|
||||
|
||||
@@ -60,7 +62,7 @@ func ListUserRelations(ctx context.Context, c *app.RequestContext) {
|
||||
return
|
||||
}
|
||||
|
||||
resp := new(user.ListUserRelationsResponse)
|
||||
resp := new(user.ListUserRelationsRes)
|
||||
resp, err = mysql.GetUserListRelations(req)
|
||||
if err != nil {
|
||||
c.JSON(consts.StatusInternalServerError, err.Error())
|
||||
@@ -69,3 +71,23 @@ func ListUserRelations(ctx context.Context, c *app.RequestContext) {
|
||||
|
||||
c.JSON(consts.StatusOK, resp)
|
||||
}
|
||||
|
||||
// UpdateUserRelations .
|
||||
// @router /v1/user/relation/ [PUT]
|
||||
func UpdateUserRelations(ctx context.Context, c *app.RequestContext) {
|
||||
var err error
|
||||
var req user.UpdateUserRelationsReq
|
||||
err = c.BindAndValidate(&req)
|
||||
if err != nil {
|
||||
c.String(consts.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
resp := new(user.UpdateUserRelationsRes)
|
||||
if err = mysql.UpdateUserRelations(req); err != nil {
|
||||
hlog.Error("UpdateUserRelations:", err)
|
||||
c.JSON(consts.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(consts.StatusOK, resp)
|
||||
}
|
||||
|
@@ -96,7 +96,7 @@ func CreateUser(ctx context.Context, c *app.RequestContext) {
|
||||
Gender: req.Gender,
|
||||
Age: uint8(req.Age),
|
||||
Mobile: req.Mobile,
|
||||
Area: req.Area,
|
||||
Area: uint8(req.Area),
|
||||
UserPassword: req.UserPassword,
|
||||
},
|
||||
}); err != nil {
|
||||
|
@@ -9,6 +9,7 @@ import (
|
||||
|
||||
// Country 国家和地区信息结构体(基于ISO 3166-1标准)
|
||||
type Country struct {
|
||||
gorm.Model
|
||||
ChineseName string `gorm:"column:chinese_name;type:varchar(50);not null;comment:'中文简称'"`
|
||||
EnglishName string `gorm:"column:english_name;type:varchar(100);not null;comment:'英文简称'"`
|
||||
ISO2 string `gorm:"column:iso2;type:char(2);not null;uniqueIndex:idx_iso2;comment:'ISO 3166-1两位字母码'"`
|
||||
|
@@ -17,7 +17,7 @@ type User struct {
|
||||
Introduce string `gorm:"column:introduce;type:varchar(200);default:'';comment:'个性签名'"`
|
||||
AvatarImageURL string `gorm:"column:avatar_image_url;type:varchar(255);default:'';comment:'头像URL'"`
|
||||
Birthday *time.Time `gorm:"column:birthday;type:date;comment:'出生日期'"`
|
||||
Area string `gorm:"column:area;type:varchar(10);comment:'国家/地区编码(ISO 3166-1)'"`
|
||||
Area uint8 `gorm:"column:area;type:tinyint;comment:'国家/地区编码(ISO 3166-1)'"`
|
||||
Mobile string `gorm:"column:mobile;type:char(11);uniqueIndex:idx_mobile;not null;unique;comment:'手机号码'"`
|
||||
UserPassword string `gorm:"column:password;type:varchar(100);not null;comment:'加密密码'"`
|
||||
UserStatus user.UserStatus `gorm:"column:user_status;type:tinyint unsigned;default:1;comment:'用户状态;0:禁用,1:正常,2:注销'"`
|
||||
|
File diff suppressed because it is too large
Load Diff
@@ -1,19 +1,23 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// Friendship 好友关系表
|
||||
// UserRelations 好友关系表
|
||||
type UserRelations struct {
|
||||
gorm.Model
|
||||
UserID string `gorm:"type:bigint;not null;index:idx_user_status;column:用户id;"`
|
||||
FriendID string `gorm:"type:bigint;not null;index:idx_friend_status;column:好友id"`
|
||||
Status uint8 `gorm:"type:tinyint;default:0;column:关系状态"` // 0: 待确认, 1: 已确认, 2: 已拒绝, 3: 已删除
|
||||
UserID string `gorm:"column:user_id;type:varchar(32);not null;index:idx_user_friend,unique;comment:用户id"`
|
||||
FriendList json.RawMessage `gorm:"column:friend_list;type:json;comment:'好友列表'"`
|
||||
FriendCount int64 `gorm:"column:friend_count;type:int;default:0;comment:好友数量"`
|
||||
}
|
||||
|
||||
// 关联用户
|
||||
User *User `gorm:"foreignKey:UserID;references:UserID"`
|
||||
Friend *User `gorm:"foreignKey:FriendID;references:UserID"`
|
||||
type Friend struct{
|
||||
FriendID string `json:"friend_id"`
|
||||
Remark string `json:"remark"` // 好友备注
|
||||
CreateTime int64 `json:"create_time"` // 创建时间
|
||||
}
|
||||
|
||||
func (u *UserRelations) TableName() string {
|
||||
|
@@ -115,3 +115,8 @@ func _listuserrelationsMw() []app.HandlerFunc {
|
||||
// your code...
|
||||
return nil
|
||||
}
|
||||
|
||||
func _updateuserrelationsMw() []app.HandlerFunc {
|
||||
// your code...
|
||||
return nil
|
||||
}
|
||||
|
@@ -30,6 +30,7 @@ func Register(r *server.Hertz) {
|
||||
_relation.DELETE("/", append(_deleteuserrelationsMw(), user.DeleteUserRelations)...)
|
||||
_relation.GET("/", append(_listuserrelationsMw(), user.ListUserRelations)...)
|
||||
_relation.POST("/", append(_createuserrelationsMw(), user.CreateUserRelations)...)
|
||||
_relation.PUT("/", append(_updateuserrelationsMw(), user.UpdateUserRelations)...)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user