friend update

This commit is contained in:
Gordon 2023-02-01 15:56:17 +08:00
parent 4067d7e4b9
commit e550c9a874
3 changed files with 34 additions and 25 deletions

View File

@ -45,8 +45,8 @@ func NewFriendServer(port int) *friendServer {
}
//mysql init
var mysql relation.Mysql
var model relation.Friend
err := mysql.InitConn().AutoMigrateModel(&model)
var model relation.FriendGorm
err := mysql.InitConn().AutoMigrateModel(&relation.FriendModel{})
if err != nil {
panic("db init err:" + err.Error())
}

View File

@ -117,11 +117,11 @@ type FriendDatabaseInterface interface {
}
type FriendDatabase struct {
sqlDB *relation.Friend
sqlDB relation.FriendDB
}
func NewFriendDatabase(db *gorm.DB) *FriendDatabase {
sqlDB := relation.NewFriendDB(db)
sqlDB := relation.NewFriendGorm(db)
database := &FriendDatabase{
sqlDB: sqlDB,
}

View File

@ -8,7 +8,16 @@ import (
"time"
)
type Friend struct {
type FriendDB interface {
Create(ctx context.Context, friends []*FriendModel) (err error)
Delete(ctx context.Context, ownerUserID string, friendUserIDs string) (err error)
UpdateByMap(ctx context.Context, ownerUserID string, args map[string]interface{}) (err error)
Update(ctx context.Context, friends []*FriendModel) (err error)
UpdateRemark(ctx context.Context, ownerUserID, friendUserID, remark string) (err error)
FindOwnerUserID(ctx context.Context, ownerUserID string) (friends []*FriendModel, err error)
}
type FriendModel struct {
OwnerUserID string `gorm:"column:owner_user_id;primary_key;size:64"`
FriendUserID string `gorm:"column:friend_user_id;primary_key;size:64"`
Remark string `gorm:"column:remark;size:255"`
@ -16,77 +25,77 @@ type Friend struct {
AddSource int32 `gorm:"column:add_source"`
OperatorUserID string `gorm:"column:operator_user_id;size:64"`
Ex string `gorm:"column:ex;size:1024"`
DB *gorm.DB `gorm:"-"`
}
type FriendGorm struct {
DB *gorm.DB `gorm:"-"`
}
func NewFriendGorm(DB *gorm.DB) *FriendGorm {
return &FriendGorm{DB: DB}
}
type FriendUser struct {
Friend
FriendGorm
Nickname string `gorm:"column:name;size:255"`
}
func NewFriendDB(db *gorm.DB) *Friend {
var friend Friend
friend.DB = db
return &friend
}
func (f *Friend) Create(ctx context.Context, friends []*Friend) (err error) {
func (f *FriendGorm) Create(ctx context.Context, friends []*FriendModel) (err error) {
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "friends", friends)
}()
return utils.Wrap(f.DB.Create(&friends).Error, "")
}
func (f *Friend) Delete(ctx context.Context, ownerUserID string, friendUserIDs string) (err error) {
func (f *FriendGorm) Delete(ctx context.Context, ownerUserID string, friendUserIDs string) (err error) {
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "ownerUserID", ownerUserID, "friendUserIDs", friendUserIDs)
}()
err = utils.Wrap(f.DB.Where("owner_user_id = ? and friend_user_id = ?", ownerUserID, friendUserIDs).Delete(&Friend{}).Error, "")
err = utils.Wrap(f.DB.Where("owner_user_id = ? and friend_user_id = ?", ownerUserID, friendUserIDs).Delete(&FriendModel{}).Error, "")
return err
}
func (f *Friend) UpdateByMap(ctx context.Context, ownerUserID string, args map[string]interface{}) (err error) {
func (f *FriendGorm) UpdateByMap(ctx context.Context, ownerUserID string, args map[string]interface{}) (err error) {
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "ownerUserID", ownerUserID, "args", args)
}()
return utils.Wrap(f.DB.Where("owner_user_id = ?", ownerUserID).Updates(args).Error, "")
}
func (f *Friend) Update(ctx context.Context, friends []*Friend) (err error) {
func (f *FriendGorm) Update(ctx context.Context, friends []*FriendModel) (err error) {
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "friends", friends)
}()
return utils.Wrap(f.DB.Updates(&friends).Error, "")
}
func (f *Friend) UpdateRemark(ctx context.Context, ownerUserID, friendUserID, remark string) (err error) {
func (f *FriendGorm) UpdateRemark(ctx context.Context, ownerUserID, friendUserID, remark string) (err error) {
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "ownerUserID", ownerUserID, "friendUserID", friendUserID, "remark", remark)
}()
return utils.Wrap(f.DB.Model(f).Where("owner_user_id = ? and friend_user_id = ?", ownerUserID, friendUserID).Update("remark", remark).Error, "")
return utils.Wrap(f.DB.Model(&FriendModel{}).Where("owner_user_id = ? and friend_user_id = ?", ownerUserID, friendUserID).Update("remark", remark).Error, "")
}
func (f *Friend) FindOwnerUserID(ctx context.Context, ownerUserID string) (friends []*Friend, err error) {
func (f *FriendGorm) FindOwnerUserID(ctx context.Context, ownerUserID string) (friends []*FriendModel, err error) {
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "ownerUserID", ownerUserID, "friends", friends)
}()
return friends, utils.Wrap(f.DB.Where("owner_user_id = ?", ownerUserID).Find(&friends).Error, "")
}
func (f *Friend) FindFriendUserID(ctx context.Context, friendUserID string) (friends []*Friend, err error) {
func (f *FriendGorm) FindFriendUserID(ctx context.Context, friendUserID string) (friends []*FriendModel, err error) {
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "friendUserID", friendUserID, "friends", friends)
}()
return friends, utils.Wrap(f.DB.Where("friend_user_id = ?", friendUserID).Find(&friends).Error, "")
}
func (f *Friend) Take(ctx context.Context, ownerUserID, friendUserID string) (friend *Friend, err error) {
friend = &Friend{}
func (f *FriendGorm) Take(ctx context.Context, ownerUserID, friendUserID string) (friend *FriendModel, err error) {
friend = &FriendModel{}
defer tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "ownerUserID", ownerUserID, "friendUserID", friendUserID, "friend", friend)
return friend, utils.Wrap(f.DB.Where("owner_user_id = ? and friend_user_id", ownerUserID, friendUserID).Take(friend).Error, "")
}
func (f *Friend) FindUserState(ctx context.Context, userID1, userID2 string) (friends []*Friend, err error) {
func (f *FriendGorm) FindUserState(ctx context.Context, userID1, userID2 string) (friends []*FriendModel, err error) {
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetSelfFuncName(), err, "userID1", userID1, "userID2", userID2)
}()