spark-server/internal/module/friend/model/friend_relation.go
zhr ae9a71034d feat: implement Spark IM V3 modular monolith
Add HTTP /v1 MVC, TCP send path, groups with server-side member-avatar collage, object-storage upload, and friends/favorites around the architecture-v3 layout.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-17 19:52:11 +08:00

57 lines
1.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package model
import (
"context"
"time"
"spark-server/internal/module/friend/entity"
)
// FriendRelation maps table friend_relations.
type FriendRelation struct {
UserSmall int64 `gorm:"column:user_small;primaryKey;comment:两人中较小的 user_id,保证同一对用户只有一行"`
UserBig int64 `gorm:"column:user_big;primaryKey;comment:两人中较大的 user_id"`
CreatedAt time.Time `gorm:"column:created_at;comment:成为好友时间"`
}
func (FriendRelation) TableName() string { return "friend_relations" }
func (r *FriendRelation) entity() *entity.Relation {
if r == nil {
return nil
}
return &entity.Relation{UserSmall: r.UserSmall, UserBig: r.UserBig, CreatedAt: r.CreatedAt}
}
func (m *Repo) GetRelation(ctx context.Context, a, b int64) (*entity.Relation, error) {
db, err := m.session(ctx)
if err != nil {
return nil, err
}
small, big := entity.OrderUsers(a, b)
var row FriendRelation
if err := db.Where("user_small = ? AND user_big = ?", small, big).Take(&row).Error; err != nil {
return nil, mapDBErr(err)
}
return row.entity(), nil
}
func (m *Repo) ListFriends(ctx context.Context, userID int64, limit int) ([]*entity.Relation, error) {
db, err := m.session(ctx)
if err != nil {
return nil, err
}
limit = entity.ClampLimit(limit)
var rows []FriendRelation
err = db.Where("user_small = ? OR user_big = ?", userID, userID).
Order("created_at DESC").Limit(limit).Find(&rows).Error
if err != nil {
return nil, mapDBErr(err)
}
out := make([]*entity.Relation, 0, len(rows))
for i := range rows {
out = append(out, rows[i].entity())
}
return out, nil
}