From 7c4691e8465edd624761107e77fba1625b1a95a8 Mon Sep 17 00:00:00 2001 From: Pancakes Date: Tue, 19 Nov 2024 18:19:19 -0500 Subject: [PATCH] New scraped data table --- api/packages/list.go | 6 +++--- db/package.go | 22 +++++++++++++++++++++- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/api/packages/list.go b/api/packages/list.go index 682891f..e9b0e12 100644 --- a/api/packages/list.go +++ b/api/packages/list.go @@ -45,11 +45,11 @@ func List(w http.ResponseWriter, r *http.Request) { var sort string // must NOT be user input switch r.URL.Query().Get("sort") { case "mostfavs": - sort = "favorites" + sort = "COALESCE(p.favorites, s.favorites, 0)" case "mostlikes": - sort = "goods" + sort = "COALESCE(p.goods, s.goods, 0)" case "mostdls": - sort = "downloads" + sort = "COALESCE(p.downloads, s.downloads, 0)" case "random": sort = "RAND()" default: // newest diff --git a/db/package.go b/db/package.go index 1e373a3..3dc9f21 100644 --- a/db/package.go +++ b/db/package.go @@ -99,7 +99,27 @@ func FetchPackage(id int, rev int) (common.Package, error) { func FetchPackageList(category string, author string, search string, offset int, count int, sort string, safemode bool) ([]common.Package, error) { var args []any - q := "SELECT p.id, p.rev, p.type, p.name, p.dataname, p.author, IFNULL(profiles.personaname, p.legacyauthor), IFNULL(profiles.avatarmedium, \"\"), p.description, p.downloads, p.favorites, p.goods, p.bads, p.time FROM packages p LEFT JOIN profiles ON p.author = profiles.steamid WHERE p.rev = (SELECT MAX(p2.rev) FROM packages p2 WHERE p2.id = p.id)" + q := `SELECT + p.id, + p.rev, + p.type, + p.name, + p.dataname, + COALESCE(p.author, ""), + COALESCE(pr.personaname, s.author, ""), + COALESCE(pr.avatarmedium, ""), + COALESCE(p.description, s.description, ""), + COALESCE(p.downloads, s.downloads, 0), + COALESCE(p.favorites, s.favorites, 0), + COALESCE(p.goods, s.goods, 0), + COALESCE(p.bads, s.bads, 0), + p.time + FROM packages p + LEFT JOIN profiles pr + ON p.author = pr.steamid + LEFT JOIN scraped s + ON p.id = s.id + WHERE p.rev = (SELECT MAX(p2.rev) FROM packages p2 WHERE p2.id = p.id)` if category != "" { q += " AND p.type = ?"