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>
42 lines
765 B
Go
42 lines
765 B
Go
package model
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
mysqldriver "github.com/go-sql-driver/mysql"
|
|
"gorm.io/gorm"
|
|
|
|
"spark-server/internal/pkg/errs"
|
|
)
|
|
|
|
// Repo is MySQL persistence for friend tables.
|
|
type Repo struct {
|
|
gdb *gorm.DB
|
|
}
|
|
|
|
func NewRepo(gdb *gorm.DB) *Repo {
|
|
return &Repo{gdb: gdb}
|
|
}
|
|
|
|
func (m *Repo) session(ctx context.Context) (*gorm.DB, error) {
|
|
if m == nil || m.gdb == nil {
|
|
return nil, errs.ErrAvailable
|
|
}
|
|
return m.gdb.WithContext(ctx), nil
|
|
}
|
|
|
|
func mapDBErr(err error) error {
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return errs.ErrNotFound
|
|
}
|
|
var me *mysqldriver.MySQLError
|
|
if errors.As(err, &me) && me.Number == 1062 {
|
|
return errs.ErrDuplicate
|
|
}
|
|
return errs.ErrInner.WithError("friend-mysql", err)
|
|
}
|