public home page with community activity overview
This commit is contained in:
+36
-4
@@ -160,10 +160,42 @@ export function listActivities(userId: number): Omit<ActivityRow, 'points'>[] {
|
||||
.all(userId) as Omit<ActivityRow, 'points'>[];
|
||||
}
|
||||
|
||||
export function getActivity(id: number, userId: number): ActivityRow | undefined {
|
||||
return db.prepare('SELECT * FROM activities WHERE id = ? AND user_id = ?').get(id, userId) as
|
||||
| ActivityRow
|
||||
| undefined;
|
||||
export function getActivity(id: number): (ActivityRow & { username: string }) | undefined {
|
||||
return db
|
||||
.prepare(
|
||||
`SELECT a.*, u.username FROM activities a JOIN users u ON u.id = a.user_id WHERE a.id = ?`
|
||||
)
|
||||
.get(id) as (ActivityRow & { username: string }) | undefined;
|
||||
}
|
||||
|
||||
/** All users' activities, newest first, with uploader attribution. */
|
||||
export function listAllActivities(): (Omit<ActivityRow, 'points'> & { username: string })[] {
|
||||
return db
|
||||
.prepare(
|
||||
`SELECT a.id, a.user_id, a.name, a.type, a.date, a.distance_m, a.duration_s, a.moving_s,
|
||||
a.elev_gain_m, a.elev_loss_m, a.elev_min_m, a.elev_max_m, a.bounds, a.created_at,
|
||||
u.username
|
||||
FROM activities a JOIN users u ON u.id = a.user_id
|
||||
ORDER BY a.date DESC, a.id DESC`
|
||||
)
|
||||
.all() as (Omit<ActivityRow, 'points'> & { username: string })[];
|
||||
}
|
||||
|
||||
export function communityTotals(): {
|
||||
users: number;
|
||||
activities: number;
|
||||
distance_m: number;
|
||||
elev_gain_m: number;
|
||||
} {
|
||||
return db
|
||||
.prepare(
|
||||
`SELECT (SELECT count(*) FROM users) users,
|
||||
count(*) activities,
|
||||
coalesce(sum(distance_m), 0) distance_m,
|
||||
coalesce(sum(elev_gain_m), 0) elev_gain_m
|
||||
FROM activities`
|
||||
)
|
||||
.get() as { users: number; activities: number; distance_m: number; elev_gain_m: number };
|
||||
}
|
||||
|
||||
export function deleteActivity(id: number, userId: number): void {
|
||||
|
||||
Reference in New Issue
Block a user