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>
56 lines
1.3 KiB
Go
56 lines
1.3 KiB
Go
package model
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"spark-server/internal/module/friend/entity"
|
|
"spark-server/internal/pkg/errs"
|
|
)
|
|
|
|
func (m *Repo) AcceptFriend(ctx context.Context, in entity.AcceptParams) error {
|
|
if in.Relation == nil {
|
|
return errs.ErrInvalidArgument
|
|
}
|
|
db, err := m.session(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
now := in.Relation.CreatedAt
|
|
if now.IsZero() {
|
|
now = time.Now()
|
|
in.Relation.CreatedAt = now
|
|
}
|
|
return db.Transaction(func(tx *gorm.DB) error {
|
|
row := FriendRelation{UserSmall: in.Relation.UserSmall, UserBig: in.Relation.UserBig, CreatedAt: now}
|
|
if err := tx.Where("user_small = ? AND user_big = ?", row.UserSmall, row.UserBig).
|
|
Attrs(row).FirstOrCreate(&row).Error; err != nil {
|
|
return mapDBErr(err)
|
|
}
|
|
if err := markRequestsAccepted(tx, in.Requests, now); err != nil {
|
|
return err
|
|
}
|
|
return upsertRemarks(tx, in.Remarks)
|
|
})
|
|
}
|
|
|
|
func (m *Repo) DeleteFriend(ctx context.Context, a, b int64) error {
|
|
db, err := m.session(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return db.Transaction(func(tx *gorm.DB) error {
|
|
small, big := entity.OrderUsers(a, b)
|
|
res := tx.Where("user_small = ? AND user_big = ?", small, big).Delete(&FriendRelation{})
|
|
if res.Error != nil {
|
|
return mapDBErr(res.Error)
|
|
}
|
|
if res.RowsAffected == 0 {
|
|
return errs.ErrNotFound
|
|
}
|
|
return deleteRemarksBetween(tx, a, b)
|
|
})
|
|
}
|