Refactor code

pull/131/head
wenxu12345 3 years ago
parent 9b2fde6ea2
commit da7d187612

@ -28,7 +28,7 @@ type UserChat struct {
Msg []MsgInfo
}
type GroupMember struct {
type GroupMember_x struct {
GroupID string
UIDList []string
}
@ -248,7 +248,7 @@ func (d *DataBases) MgoSkipUID(count int) (string, error) {
}
func (d *DataBases) GetGroupMember(groupID string) []string {
groupInfo := GroupMember{}
groupInfo := GroupMember_x{}
groupInfo.GroupID = groupID
groupInfo.UIDList = make([]string, 0)
@ -282,7 +282,7 @@ func (d *DataBases) AddGroupMember(groupID, uid string) error {
}
if n == 0 {
groupInfo := GroupMember{}
groupInfo := GroupMember_x{}
groupInfo.GroupID = groupID
groupInfo.UIDList = append(groupInfo.UIDList, uid)
err = c.Insert(&groupInfo)

@ -23,19 +23,22 @@ func initMysqlDB() {
var err1 error
db, err := gorm.Open("mysql", dsn)
if err != nil {
log.Error("", "", dsn)
log.NewError("0", "Open failed ", err.Error(), dsn)
}
if err != nil {
time.Sleep(time.Duration(30) * time.Second)
db, err1 = gorm.Open("mysql", dsn)
if err1 != nil {
log.NewError("0", "Open failed ", err1.Error(), dsn)
panic(err1.Error())
}
}
//Check the database and table during initialization
sql := fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s ;", config.Config.Mysql.DBDatabaseName)
sql := fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s ;", config.Config.Mysql.DBDatabaseName+"test1")
err = db.Exec(sql).Error
if err != nil {
log.NewError("0", "Exec failed ", err.Error(), sql)
panic(err.Error())
}
db.Close()
@ -44,9 +47,22 @@ func initMysqlDB() {
config.Config.Mysql.DBUserName, config.Config.Mysql.DBPassword, config.Config.Mysql.DBAddress[0], config.Config.Mysql.DBDatabaseName)
db, err = gorm.Open("mysql", dsn)
if err != nil {
log.NewError("0", "Open failed ", err.Error(), dsn)
panic(err.Error())
}
log.NewInfo("open db ok ", dsn)
db.AutoMigrate(&Friend{},
&FriendRequest{},
&Group{},
&GroupMember{},
&GroupRequest{},
&User{},
&Black{})
return
sqlTable := "CREATE TABLE IF NOT EXISTS `user` (" +
" `uid` varchar(64) NOT NULL," +
" `name` varchar(64) DEFAULT NULL," +

@ -6,7 +6,7 @@ import (
"time"
)
func InsertToFriend(toInsertFollow *Friend) error {
func InsertToFriend(toInsertFollow *db.Friend) error {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return err
@ -20,12 +20,12 @@ func InsertToFriend(toInsertFollow *Friend) error {
return nil
}
func GetFriendRelationshipFromFriend(OwnerUserID, FriendUserID string) (*Friend, error) {
func GetFriendRelationshipFromFriend(OwnerUserID, FriendUserID string) (*db.Friend, error) {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return nil, err
}
var friend Friend
var friend db.Friend
err = dbConn.Table("friend").Where("owner_user_id=? and friend_user_id=?", OwnerUserID, FriendUserID).Find(&friend).Error
if err != nil {
return nil, err
@ -33,12 +33,12 @@ func GetFriendRelationshipFromFriend(OwnerUserID, FriendUserID string) (*Friend,
return &friend, err
}
func GetFriendListByUserID(OwnerUserID string) ([]Friend, error) {
func GetFriendListByUserID(OwnerUserID string) ([]db.Friend, error) {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return nil, err
}
var friends []Friend
var friends []db.Friend
err = dbConn.Table("friend").Where("owner_user_id=?", OwnerUserID).Find(&friends).Error
if err != nil {
return nil, err

@ -19,12 +19,12 @@ import (
//}
// who apply to add me
func GetReceivedFriendsApplicationListByUserID(ToUserID string) ([]FriendRequest, error) {
func GetReceivedFriendsApplicationListByUserID(ToUserID string) ([]db.FriendRequest, error) {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return nil, err
}
var usersInfo []FriendRequest
var usersInfo []db.FriendRequest
err = dbConn.Table("friend_request").Where("to_user_id=?", ToUserID).Find(&usersInfo).Error
if err != nil {
return nil, err
@ -33,12 +33,12 @@ func GetReceivedFriendsApplicationListByUserID(ToUserID string) ([]FriendRequest
}
//I apply to add somebody
func GetSendFriendApplicationListByUserID(FromUserID string) ([]FriendRequest, error) {
func GetSendFriendApplicationListByUserID(FromUserID string) ([]db.FriendRequest, error) {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return nil, err
}
var usersInfo []FriendRequest
var usersInfo []db.FriendRequest
err = dbConn.Table("friend_request").Where("from_user_id=?", FromUserID).Find(&usersInfo).Error
if err != nil {
return nil, err
@ -47,12 +47,12 @@ func GetSendFriendApplicationListByUserID(FromUserID string) ([]FriendRequest, e
}
//FromUserId apply to add ToUserID
func GetFriendApplicationByBothUserID(FromUserID, ToUserID string) (*FriendRequest, error) {
func GetFriendApplicationByBothUserID(FromUserID, ToUserID string) (*db.FriendRequest, error) {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return nil, err
}
var friendRequest FriendRequest
var friendRequest db.FriendRequest
err = dbConn.Table("friend_request").Where("from_user_id=? and to_user_id=?", FromUserID, ToUserID).Find(&friendRequest).Error
if err != nil {
return nil, err
@ -60,7 +60,7 @@ func GetFriendApplicationByBothUserID(FromUserID, ToUserID string) (*FriendReque
return &friendRequest, nil
}
func UpdateFriendApplication(friendRequest *FriendRequest) error {
func UpdateFriendApplication(friendRequest *db.FriendRequest) error {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return err
@ -71,7 +71,7 @@ func UpdateFriendApplication(friendRequest *FriendRequest) error {
friendRequest.FromUserID, friendRequest.ToUserID).Update(&friendRequest).Error
}
func InsertFriendApplication(friendRequest *FriendRequest) error {
func InsertFriendApplication(friendRequest *db.FriendRequest) error {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return err

@ -18,7 +18,7 @@ import (
// Ex string `gorm:"column:ex"`
//}
func InsertIntoGroupMember(toInsertInfo GroupMember) error {
func InsertIntoGroupMember(toInsertInfo db.GroupMember) error {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return err
@ -34,12 +34,12 @@ func InsertIntoGroupMember(toInsertInfo GroupMember) error {
return nil
}
func GetGroupMemberListByUserID(userID string) ([]GroupMember, error) {
func GetGroupMemberListByUserID(userID string) ([]db.GroupMember, error) {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return nil, err
}
var groupMemberList []GroupMember
var groupMemberList []db.GroupMember
err = dbConn.Table("group_member").Where("user_id=?", userID).Find(&groupMemberList).Error
if err != nil {
return nil, err
@ -47,12 +47,12 @@ func GetGroupMemberListByUserID(userID string) ([]GroupMember, error) {
return groupMemberList, nil
}
func GetGroupMemberListByGroupID(groupID string) ([]GroupMember, error) {
func GetGroupMemberListByGroupID(groupID string) ([]db.GroupMember, error) {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return nil, err
}
var groupMemberList []GroupMember
var groupMemberList []db.GroupMember
err = dbConn.Table("group_member").Where("group_id=?", groupID).Find(&groupMemberList).Error
if err != nil {
return nil, err
@ -60,12 +60,12 @@ func GetGroupMemberListByGroupID(groupID string) ([]GroupMember, error) {
return groupMemberList, nil
}
func GetGroupMemberListByGroupIDAndRoleLevel(groupID string, roleLevel int32) ([]GroupMember, error) {
func GetGroupMemberListByGroupIDAndRoleLevel(groupID string, roleLevel int32) ([]db.GroupMember, error) {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return nil, err
}
var groupMemberList []GroupMember
var groupMemberList []db.GroupMember
err = dbConn.Table("group_member").Where("group_id=? and role_level=?", groupID, roleLevel).Find(&groupMemberList).Error
if err != nil {
return nil, err
@ -73,12 +73,12 @@ func GetGroupMemberListByGroupIDAndRoleLevel(groupID string, roleLevel int32) ([
return groupMemberList, nil
}
func GetGroupMemberInfoByGroupIDAndUserID(groupID, userID string) (*GroupMember, error) {
func GetGroupMemberInfoByGroupIDAndUserID(groupID, userID string) (*db.GroupMember, error) {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return nil, err
}
var groupMember GroupMember
var groupMember db.GroupMember
err = dbConn.Table("group_member").Where("group_id=? and user_id=? ", groupID, userID).Limit(1).Find(&groupMember).Error
if err != nil {
return nil, err
@ -91,14 +91,14 @@ func DeleteGroupMemberByGroupIDAndUserID(groupID, userID string) error {
if err != nil {
return err
}
err = dbConn.Table("group_member").Where("group_id=? and user_id=? ", groupID, userID).Delete(&GroupMember{}).Error
err = dbConn.Table("group_member").Where("group_id=? and user_id=? ", groupID, userID).Delete(&db.GroupMember{}).Error
if err != nil {
return err
}
return nil
}
func UpdateGroupMemberInfo(groupMemberInfo GroupMember) error {
func UpdateGroupMemberInfo(groupMemberInfo db.GroupMember) error {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return err
@ -110,12 +110,12 @@ func UpdateGroupMemberInfo(groupMemberInfo GroupMember) error {
return nil
}
func GetOwnerManagerByGroupID(groupID string) ([]GroupMember, error) {
func GetOwnerManagerByGroupID(groupID string) ([]db.GroupMember, error) {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return nil, err
}
var groupMemberList []GroupMember
var groupMemberList []db.GroupMember
err = dbConn.Table("group_member").Where("group_id=? and role_level>?", groupID, constant.GroupOrdinaryUsers).Find(&groupMemberList).Error
if err != nil {
return nil, err
@ -136,7 +136,7 @@ func GetGroupMemberNumByGroupID(groupID string) uint32 {
return number
}
func GetGroupOwnerInfoByGroupID(groupID string) (*GroupMember, error) {
func GetGroupOwnerInfoByGroupID(groupID string) (*db.GroupMember, error) {
omList, err := GetOwnerManagerByGroupID(groupID)
if err != nil {
return nil, err
@ -169,12 +169,12 @@ func RemoveGroupMember(groupID string, UserID string) error {
return DeleteGroupMemberByGroupIDAndUserID(groupID, UserID)
}
func GetMemberInfoByID(groupID string, userID string) (*GroupMember, error) {
func GetMemberInfoByID(groupID string, userID string) (*db.GroupMember, error) {
return GetGroupMemberInfoByGroupIDAndUserID(groupID, userID)
}
func GetGroupMemberByGroupID(groupID string, filter int32, begin int32, maxNumber int32) ([]GroupMember, error) {
var memberList []GroupMember
func GetGroupMemberByGroupID(groupID string, filter int32, begin int32, maxNumber int32) ([]db.GroupMember, error) {
var memberList []db.GroupMember
var err error
if filter >= 0 {
memberList, err = GetGroupMemberListByGroupIDAndRoleLevel(groupID, filter) //sorted by join time

@ -18,7 +18,7 @@ import (
// Ex string `gorm:"column:ex"`
//}
func InsertIntoGroup(groupInfo Group) error {
func InsertIntoGroup(groupInfo db.Group) error {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return err
@ -34,12 +34,12 @@ func InsertIntoGroup(groupInfo Group) error {
return nil
}
func GetGroupInfoByGroupID(groupId string) (*Group, error) {
func GetGroupInfoByGroupID(groupId string) (*db.Group, error) {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return nil, err
}
var groupInfo Group
var groupInfo db.Group
err = dbConn.Table("group").Where("group_id=?", groupId).Find(&groupInfo).Error
if err != nil {
return nil, err
@ -47,7 +47,7 @@ func GetGroupInfoByGroupID(groupId string) (*Group, error) {
return &groupInfo, nil
}
func SetGroupInfo(groupInfo Group) error {
func SetGroupInfo(groupInfo db.Group) error {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return err

@ -19,7 +19,7 @@ import (
// Ex string `gorm:"column:ex"`
//}
func UpdateGroupRequest(groupRequest GroupRequest) error {
func UpdateGroupRequest(groupRequest db.GroupRequest) error {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return err
@ -30,7 +30,7 @@ func UpdateGroupRequest(groupRequest GroupRequest) error {
return dbConn.Table("group_request").Where("group_id=? and user_id=?", groupRequest.GroupID, groupRequest.UserID).Update(&groupRequest).Error
}
func InsertIntoGroupRequest(toInsertInfo GroupRequest) error {
func InsertIntoGroupRequest(toInsertInfo db.GroupRequest) error {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return err
@ -48,12 +48,12 @@ func InsertIntoGroupRequest(toInsertInfo GroupRequest) error {
return nil
}
func GetGroupRequestByGroupIDAndUserID(groupID, userID string) (*GroupRequest, error) {
func GetGroupRequestByGroupIDAndUserID(groupID, userID string) (*db.GroupRequest, error) {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return nil, err
}
var groupRequest GroupRequest
var groupRequest db.GroupRequest
err = dbConn.Table("group_request").Where("user_id=? and group_id=?", userID, groupID).Find(&groupRequest).Error
if err != nil {
return nil, err
@ -66,19 +66,19 @@ func DelGroupRequestByGroupIDAndUserID(groupID, userID string) error {
if err != nil {
return err
}
err = dbConn.Table("group_request").Where("group_id=? and user_id=?", groupID, userID).Delete(&GroupRequest{}).Error
err = dbConn.Table("group_request").Where("group_id=? and user_id=?", groupID, userID).Delete(&db.GroupRequest{}).Error
if err != nil {
return err
}
return nil
}
func GetGroupRequestByGroupID(groupID string) ([]GroupRequest, error) {
func GetGroupRequestByGroupID(groupID string) ([]db.GroupRequest, error) {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return nil, err
}
var groupRequestList []GroupRequest
var groupRequestList []db.GroupRequest
err = dbConn.Table("group_request").Where("group_id=?", groupID).Find(&groupRequestList).Error
if err != nil {
return nil, err
@ -87,8 +87,8 @@ func GetGroupRequestByGroupID(groupID string) ([]GroupRequest, error) {
}
//received
func GetGroupApplicationList(userID string) ([]GroupRequest, error) {
var groupRequestList []GroupRequest
func GetGroupApplicationList(userID string) ([]db.GroupRequest, error) {
var groupRequestList []db.GroupRequest
memberList, err := GetGroupMemberListByUserID(userID)
if err != nil {
return nil, err

@ -5,7 +5,7 @@ import (
"time"
)
func InsertInToUserBlackList(black Black) error {
func InsertInToUserBlackList(black db.Black) error {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return err
@ -29,7 +29,7 @@ func CheckBlack(ownerUserID, blockUserID string) error {
if err != nil {
return err
}
var black Black
var black db.Black
err = dbConn.Table("user_black_list").Where("owner_user_id=? and block_user_id=?", ownerUserID, blockUserID).Find(&black).Error
return err
}
@ -44,12 +44,12 @@ func RemoveBlackList(ownerUserID, blockUserID string) error {
return err
}
func GetBlackListByUserID(ownerUserID string) ([]Black, error) {
func GetBlackListByUserID(ownerUserID string) ([]db.Black, error) {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return nil, err
}
var blackListUsersInfo []Black
var blackListUsersInfo []db.Black
err = dbConn.Table("user_black_list").Where("owner_user_id=?", ownerUserID).Find(&blackListUsersInfo).Error
if err != nil {
return nil, err

@ -19,7 +19,7 @@ func init() {
} else {
continue
}
var appMgr User
var appMgr db.User
appMgr.UserID = v
appMgr.Nickname = "AppManager" + utils.IntToString(k+1)
appMgr.AppMangerLevel = constant.AppAdmin
@ -31,7 +31,7 @@ func init() {
}
}
func UserRegister(user User) error {
func UserRegister(user db.User) error {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return err
@ -67,16 +67,16 @@ func DeleteUser(userID string) (i int64) {
if err != nil {
return 0
}
i = dbConn.Table("user").Where("user_id=?", userID).Delete(User{}).RowsAffected
i = dbConn.Table("user").Where("user_id=?", userID).Delete(db.User{}).RowsAffected
return i
}
func GetUserByUserID(userID string) (*User, error) {
func GetUserByUserID(userID string) (*db.User, error) {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return nil, err
}
var user User
var user db.User
err = dbConn.Table("user").Where("user_id=?", userID).First(&user).Error
if err != nil {
return nil, err
@ -84,7 +84,7 @@ func GetUserByUserID(userID string) (*User, error) {
return &user, nil
}
func UpdateUserInfo(user User) error {
func UpdateUserInfo(user db.User) error {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return err

Loading…
Cancel
Save