Mirror of @tangled.org/core. Running on a Raspberry Pi Zero 2 (Please be gentle).
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

appview/db: add db helpers to count entries

Signed-off-by: oppiliappan <me@oppi.li>

authored by

oppiliappan and committed by
Tangled
38397294 6fc9fb30

+79 -68
+4 -4
appview/db/follow.go
··· 56 56 } 57 57 58 58 type FollowStats struct { 59 - Followers int 60 - Following int 59 + Followers int64 60 + Following int64 61 61 } 62 62 63 63 func GetFollowerFollowingCount(e Execer, did string) (FollowStats, error) { 64 - followers, following := 0, 0 64 + var followers, following int64 65 65 err := e.QueryRow( 66 66 `SELECT 67 67 COUNT(CASE WHEN subject_did = ? THEN 1 END) AS followers, ··· 122 122 123 123 for rows.Next() { 124 124 var did string 125 - var followers, following int 125 + var followers, following int64 126 126 if err := rows.Scan(&did, &followers, &following); err != nil { 127 127 return nil, err 128 128 }
+25 -64
appview/db/repos.go
··· 2 2 3 3 import ( 4 4 "database/sql" 5 + "errors" 5 6 "fmt" 6 7 "log" 7 8 "slices" ··· 37 36 func (r Repo) DidSlashRepo() string { 38 37 p, _ := securejoin.SecureJoin(r.Did, r.Name) 39 38 return p 40 - } 41 - 42 - func GetAllRepos(e Execer, limit int) ([]Repo, error) { 43 - var repos []Repo 44 - 45 - rows, err := e.Query( 46 - `select did, name, knot, rkey, description, created, source 47 - from repos 48 - order by created desc 49 - limit ? 50 - `, 51 - limit, 52 - ) 53 - if err != nil { 54 - return nil, err 55 - } 56 - defer rows.Close() 57 - 58 - for rows.Next() { 59 - var repo Repo 60 - err := scanRepo( 61 - rows, &repo.Did, &repo.Name, &repo.Knot, &repo.Rkey, &repo.Description, &repo.Created, &repo.Source, 62 - ) 63 - if err != nil { 64 - return nil, err 65 - } 66 - repos = append(repos, repo) 67 - } 68 - 69 - if err := rows.Err(); err != nil { 70 - return nil, err 71 - } 72 - 73 - return repos, nil 74 39 } 75 40 76 41 func GetRepos(e Execer, limit int, filters ...filter) ([]Repo, error) { ··· 283 316 }) 284 317 285 318 return repos, nil 319 + } 320 + 321 + func CountRepos(e Execer, filters ...filter) (int64, error) { 322 + var conditions []string 323 + var args []any 324 + for _, filter := range filters { 325 + conditions = append(conditions, filter.Condition()) 326 + args = append(args, filter.Arg()...) 327 + } 328 + 329 + whereClause := "" 330 + if conditions != nil { 331 + whereClause = " where " + strings.Join(conditions, " and ") 332 + } 333 + 334 + repoQuery := fmt.Sprintf(`select count(1) from repos %s`, whereClause) 335 + var count int64 336 + err := e.QueryRow(repoQuery, args...).Scan(&count) 337 + 338 + if !errors.Is(err, sql.ErrNoRows) && err != nil { 339 + return 0, err 340 + } 341 + 342 + return count, nil 286 343 } 287 344 288 345 func GetAllReposByDid(e Execer, did string) ([]Repo, error) { ··· 560 569 StarCount int 561 570 IssueCount IssueCount 562 571 PullCount PullCount 563 - } 564 - 565 - func scanRepo(rows *sql.Rows, did, name, knot, rkey, description *string, created *time.Time, source *string) error { 566 - var createdAt string 567 - var nullableDescription sql.NullString 568 - var nullableSource sql.NullString 569 - if err := rows.Scan(did, name, knot, rkey, &nullableDescription, &createdAt, &nullableSource); err != nil { 570 - return err 571 - } 572 - 573 - if nullableDescription.Valid { 574 - *description = nullableDescription.String 575 - } else { 576 - *description = "" 577 - } 578 - 579 - createdAtTime, err := time.Parse(time.RFC3339, createdAt) 580 - if err != nil { 581 - *created = time.Now() 582 - } else { 583 - *created = createdAtTime 584 - } 585 - 586 - if nullableSource.Valid { 587 - *source = nullableSource.String 588 - } else { 589 - *source = "" 590 - } 591 - 592 - return nil 593 572 }
+26
appview/db/star.go
··· 1 1 package db 2 2 3 3 import ( 4 + "database/sql" 5 + "errors" 4 6 "fmt" 5 7 "log" 6 8 "strings" ··· 183 181 } 184 182 185 183 return stars, nil 184 + } 185 + 186 + func CountStars(e Execer, filters ...filter) (int64, error) { 187 + var conditions []string 188 + var args []any 189 + for _, filter := range filters { 190 + conditions = append(conditions, filter.Condition()) 191 + args = append(args, filter.Arg()...) 192 + } 193 + 194 + whereClause := "" 195 + if conditions != nil { 196 + whereClause = " where " + strings.Join(conditions, " and ") 197 + } 198 + 199 + repoQuery := fmt.Sprintf(`select count(1) from stars %s`, whereClause) 200 + var count int64 201 + err := e.QueryRow(repoQuery, args...).Scan(&count) 202 + 203 + if !errors.Is(err, sql.ErrNoRows) && err != nil { 204 + return 0, err 205 + } 206 + 207 + return count, nil 186 208 } 187 209 188 210 func GetAllStars(e Execer, limit int) ([]Star, error) {
+24
appview/db/strings.go
··· 206 206 return all, nil 207 207 } 208 208 209 + func CountStrings(e Execer, filters ...filter) (int64, error) { 210 + var conditions []string 211 + var args []any 212 + for _, filter := range filters { 213 + conditions = append(conditions, filter.Condition()) 214 + args = append(args, filter.Arg()...) 215 + } 216 + 217 + whereClause := "" 218 + if conditions != nil { 219 + whereClause = " where " + strings.Join(conditions, " and ") 220 + } 221 + 222 + repoQuery := fmt.Sprintf(`select count(1) from strings %s`, whereClause) 223 + var count int64 224 + err := e.QueryRow(repoQuery, args...).Scan(&count) 225 + 226 + if !errors.Is(err, sql.ErrNoRows) && err != nil { 227 + return 0, err 228 + } 229 + 230 + return count, nil 231 + } 232 + 209 233 func DeleteString(e Execer, filters ...filter) error { 210 234 var conditions []string 211 235 var args []any