feat: add admin apis

This commit is contained in:
Krispire Admin
2026-06-16 14:47:45 +05:30
parent aa61f4f0b1
commit a49bf7591e
24 changed files with 1164 additions and 22 deletions
+7 -7
View File
@@ -49,7 +49,7 @@ const commentSelect = `
u.name, u.handle, u.avatar_url,
ru.handle,
(vl.comment_id IS NOT NULL),
(SELECT count(*) FROM post_comments rc WHERE rc.parent_id = c.id)`
(SELECT count(*) FROM post_comments rc WHERE rc.parent_id = c.id AND rc.deleted_at IS NULL)`
// The viewer id always rides in parameter slot $2 across the read queries
// below, so the shared viewer-scoped like join can hardcode it.
@@ -120,7 +120,7 @@ func (r *CommentRepo) Create(ctx context.Context, postID, userID string, parentI
// Get returns one comment hydrated with author + viewer-like + reply count.
func (r *CommentRepo) Get(ctx context.Context, id, viewerID string) (*model.Comment, error) {
q := `SELECT ` + commentSelect + commentFrom + ` WHERE c.id = $1`
q := `SELECT ` + commentSelect + commentFrom + ` WHERE c.id = $1 AND c.deleted_at IS NULL`
c := &model.Comment{}
err := scanComment(r.pg.Pool.QueryRow(ctx, q, id, nullUUIDArg(viewerID)), c)
if errors.Is(err, pgx.ErrNoRows) {
@@ -137,7 +137,7 @@ func (r *CommentRepo) Get(ctx context.Context, id, viewerID string) (*model.Comm
func (r *CommentRepo) ListTopLevel(ctx context.Context, postID, viewerID string, limit, offset int) ([]model.Comment, int, error) {
var total int
if err := r.pg.Pool.QueryRow(ctx,
`SELECT count(*) FROM post_comments WHERE post_id = $1 AND parent_id IS NULL`,
`SELECT count(*) FROM post_comments WHERE post_id = $1 AND parent_id IS NULL AND deleted_at IS NULL`,
postID).Scan(&total); err != nil {
return nil, 0, fmt.Errorf("count comments: %w", err)
}
@@ -145,7 +145,7 @@ func (r *CommentRepo) ListTopLevel(ctx context.Context, postID, viewerID string,
return []model.Comment{}, 0, nil
}
q := `SELECT ` + commentSelect + commentFrom + `
WHERE c.post_id = $1 AND c.parent_id IS NULL
WHERE c.post_id = $1 AND c.parent_id IS NULL AND c.deleted_at IS NULL
ORDER BY (c.pinned_at IS NOT NULL) DESC, c.pinned_at DESC NULLS LAST, c.created_at DESC
LIMIT $3 OFFSET $4`
rows, err := r.pg.Pool.Query(ctx, q, postID, nullUUIDArg(viewerID), limit, offset)
@@ -160,14 +160,14 @@ func (r *CommentRepo) ListTopLevel(ctx context.Context, postID, viewerID string,
func (r *CommentRepo) ListReplies(ctx context.Context, parentID, viewerID string, limit, offset int) ([]model.Comment, int, error) {
var total int
if err := r.pg.Pool.QueryRow(ctx,
`SELECT count(*) FROM post_comments WHERE parent_id = $1`, parentID).Scan(&total); err != nil {
`SELECT count(*) FROM post_comments WHERE parent_id = $1 AND deleted_at IS NULL`, parentID).Scan(&total); err != nil {
return nil, 0, fmt.Errorf("count replies: %w", err)
}
if total == 0 {
return []model.Comment{}, 0, nil
}
q := `SELECT ` + commentSelect + commentFrom + `
WHERE c.parent_id = $1
WHERE c.parent_id = $1 AND c.deleted_at IS NULL
ORDER BY c.created_at ASC
LIMIT $3 OFFSET $4`
rows, err := r.pg.Pool.Query(ctx, q, parentID, nullUUIDArg(viewerID), limit, offset)
@@ -211,7 +211,7 @@ func (r *CommentRepo) Hydrate(ctx context.Context, viewerID string, posts []*mod
// Total comment count per post (top-level + replies).
counts := make(map[string]int, len(ids))
crows, err := r.pg.Pool.Query(ctx,
`SELECT post_id, count(*) FROM post_comments WHERE post_id = ANY($1) GROUP BY post_id`,
`SELECT post_id, count(*) FROM post_comments WHERE post_id = ANY($1) AND deleted_at IS NULL GROUP BY post_id`,
ids)
if err != nil {
return fmt.Errorf("hydrate comment counts: %w", err)