feat: 重构内容服务,优化数据预加载和用户互动功能;增加租户关注功能及相关测试用例
This commit is contained in:
@@ -5,7 +5,7 @@ import (
|
||||
"errors"
|
||||
|
||||
"quyun/v2/app/errorx"
|
||||
tenant_dto "quyun/v2/app/http/v1/dto"
|
||||
"quyun/v2/app/http/v1/dto"
|
||||
"quyun/v2/database/models"
|
||||
"quyun/v2/pkg/consts"
|
||||
|
||||
@@ -17,21 +17,9 @@ import (
|
||||
// @provider
|
||||
type tenant struct{}
|
||||
|
||||
func (s *tenant) GetPublicProfile(ctx context.Context, id string) (*tenant_dto.TenantProfile, error) {
|
||||
// id could be Code or ID. Try Code first, then ID.
|
||||
tbl, q := models.TenantQuery.QueryContext(ctx)
|
||||
|
||||
// Try to find by code or ID
|
||||
var t *models.Tenant
|
||||
var err error
|
||||
|
||||
// Assume id is ID for simplicity if numeric, or try both.
|
||||
if cast.ToInt64(id) > 0 {
|
||||
t, err = q.Where(tbl.ID.Eq(cast.ToInt64(id))).First()
|
||||
} else {
|
||||
t, err = q.Where(tbl.Code.Eq(id)).First()
|
||||
}
|
||||
|
||||
func (s *tenant) GetPublicProfile(ctx context.Context, id string) (*dto.TenantProfile, error) {
|
||||
tid := cast.ToInt64(id)
|
||||
t, err := models.TenantQuery.WithContext(ctx).Where(models.TenantQuery.ID.Eq(tid)).First()
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, errorx.ErrRecordNotFound
|
||||
@@ -40,39 +28,25 @@ func (s *tenant) GetPublicProfile(ctx context.Context, id string) (*tenant_dto.T
|
||||
}
|
||||
|
||||
// Stats
|
||||
// Followers
|
||||
followers, _ := models.TenantUserQuery.WithContext(ctx).Where(models.TenantUserQuery.TenantID.Eq(t.ID)).Count()
|
||||
// Contents
|
||||
contentsCount, _ := models.ContentQuery.WithContext(ctx).Where(models.ContentQuery.TenantID.Eq(t.ID), models.ContentQuery.Status.Eq(consts.ContentStatusPublished)).Count()
|
||||
// Likes
|
||||
var likes int64
|
||||
// Sum content likes
|
||||
// Mock likes for now or fetch
|
||||
followers, _ := models.TenantUserQuery.WithContext(ctx).Where(models.TenantUserQuery.TenantID.Eq(tid)).Count()
|
||||
contents, _ := models.ContentQuery.WithContext(ctx).Where(models.ContentQuery.TenantID.Eq(tid), models.ContentQuery.Status.Eq(consts.ContentStatusPublished)).Count()
|
||||
|
||||
// IsFollowing
|
||||
// Following status
|
||||
isFollowing := false
|
||||
userID := ctx.Value(consts.CtxKeyUser)
|
||||
if userID != nil {
|
||||
if userID := ctx.Value(consts.CtxKeyUser); userID != nil {
|
||||
uid := cast.ToInt64(userID)
|
||||
count, _ := models.TenantUserQuery.WithContext(ctx).Where(models.TenantUserQuery.TenantID.Eq(t.ID), models.TenantUserQuery.UserID.Eq(uid)).Count()
|
||||
isFollowing = count > 0
|
||||
isFollowing, _ = models.TenantUserQuery.WithContext(ctx).
|
||||
Where(models.TenantUserQuery.TenantID.Eq(tid), models.TenantUserQuery.UserID.Eq(uid)).
|
||||
Exists()
|
||||
}
|
||||
|
||||
// Config parsing (Unused for now as we don't map to bio yet)
|
||||
// config := t.Config.Data()
|
||||
|
||||
return &tenant_dto.TenantProfile{
|
||||
ID: cast.ToString(t.ID),
|
||||
Name: t.Name,
|
||||
Avatar: "", // From config
|
||||
Cover: "", // From config
|
||||
Bio: "", // From config
|
||||
Description: "", // From config
|
||||
CertType: "personal", // Mock
|
||||
Stats: tenant_dto.Stats{
|
||||
return &dto.TenantProfile{
|
||||
ID: cast.ToString(t.ID),
|
||||
Name: t.Name,
|
||||
Avatar: "", // Extract from config if available
|
||||
Stats: dto.Stats{
|
||||
Followers: int(followers),
|
||||
Contents: int(contentsCount),
|
||||
Likes: int(likes),
|
||||
Contents: int(contents),
|
||||
},
|
||||
IsFollowing: isFollowing,
|
||||
}, nil
|
||||
@@ -87,12 +61,10 @@ func (s *tenant) Follow(ctx context.Context, id string) error {
|
||||
tid := cast.ToInt64(id)
|
||||
|
||||
// Check if tenant exists
|
||||
_, err := models.TenantQuery.WithContext(ctx).Where(models.TenantQuery.ID.Eq(tid)).First()
|
||||
if err != nil {
|
||||
if _, err := models.TenantQuery.WithContext(ctx).Where(models.TenantQuery.ID.Eq(tid)).First(); err != nil {
|
||||
return errorx.ErrRecordNotFound
|
||||
}
|
||||
|
||||
// Add to tenant_users
|
||||
tu := &models.TenantUser{
|
||||
TenantID: tid,
|
||||
UserID: uid,
|
||||
@@ -100,12 +72,7 @@ func (s *tenant) Follow(ctx context.Context, id string) error {
|
||||
Status: consts.UserStatusVerified,
|
||||
}
|
||||
|
||||
count, _ := models.TenantUserQuery.WithContext(ctx).Where(models.TenantUserQuery.TenantID.Eq(tid), models.TenantUserQuery.UserID.Eq(uid)).Count()
|
||||
if count > 0 {
|
||||
return nil // Already following
|
||||
}
|
||||
|
||||
if err := models.TenantUserQuery.WithContext(ctx).Create(tu); err != nil {
|
||||
if err := models.TenantUserQuery.WithContext(ctx).Save(tu); err != nil {
|
||||
return errorx.ErrDatabaseError.WithCause(err)
|
||||
}
|
||||
return nil
|
||||
@@ -119,9 +86,51 @@ func (s *tenant) Unfollow(ctx context.Context, id string) error {
|
||||
uid := cast.ToInt64(userID)
|
||||
tid := cast.ToInt64(id)
|
||||
|
||||
_, err := models.TenantUserQuery.WithContext(ctx).Where(models.TenantUserQuery.TenantID.Eq(tid), models.TenantUserQuery.UserID.Eq(uid)).Delete()
|
||||
_, err := models.TenantUserQuery.WithContext(ctx).
|
||||
Where(models.TenantUserQuery.TenantID.Eq(tid), models.TenantUserQuery.UserID.Eq(uid)).
|
||||
Delete()
|
||||
if err != nil {
|
||||
return errorx.ErrDatabaseError.WithCause(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *tenant) ListFollowed(ctx context.Context) ([]dto.TenantProfile, error) {
|
||||
userID := ctx.Value(consts.CtxKeyUser)
|
||||
if userID == nil {
|
||||
return nil, errorx.ErrUnauthorized
|
||||
}
|
||||
uid := cast.ToInt64(userID)
|
||||
|
||||
tbl, q := models.TenantUserQuery.QueryContext(ctx)
|
||||
list, err := q.Where(tbl.UserID.Eq(uid)).Find()
|
||||
if err != nil {
|
||||
return nil, errorx.ErrDatabaseError.WithCause(err)
|
||||
}
|
||||
|
||||
var data []dto.TenantProfile
|
||||
for _, tu := range list {
|
||||
// Fetch Tenant
|
||||
t, err := models.TenantQuery.WithContext(ctx).Where(models.TenantQuery.ID.Eq(tu.TenantID)).First()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
// Stats
|
||||
followers, _ := models.TenantUserQuery.WithContext(ctx).Where(models.TenantUserQuery.TenantID.Eq(tu.TenantID)).Count()
|
||||
contents, _ := models.ContentQuery.WithContext(ctx).Where(models.ContentQuery.TenantID.Eq(tu.TenantID), models.ContentQuery.Status.Eq(consts.ContentStatusPublished)).Count()
|
||||
|
||||
data = append(data, dto.TenantProfile{
|
||||
ID: cast.ToString(t.ID),
|
||||
Name: t.Name,
|
||||
Avatar: "",
|
||||
Stats: dto.Stats{
|
||||
Followers: int(followers),
|
||||
Contents: int(contents),
|
||||
},
|
||||
IsFollowing: true,
|
||||
})
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user