From dae46d9cee01a04c103babe12d1fe63fdbe89dd8 Mon Sep 17 00:00:00 2001 From: Charlie Egan Date: Mon, 11 Mar 2024 07:19:43 +0000 Subject: [PATCH] Correct lens ordering Signed-off-by: Charlie Egan --- internal/pkg/database/lenses.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/internal/pkg/database/lenses.go b/internal/pkg/database/lenses.go index a0c58713..551a9df2 100644 --- a/internal/pkg/database/lenses.go +++ b/internal/pkg/database/lenses.go @@ -113,9 +113,18 @@ func AllLenses(db *sql.DB) (results []models.Lens, err error) { goquDB := goqu.New("postgres", db) selectLenses := goquDB.From("photos.lenses"). - Select("*"). - Order(goqu.I("name").Asc()). + FullOuterJoin(goqu.T("medias").Schema("photos"), goqu.On(goqu.Ex{"medias.lens_id": goqu.I("lenses.id")})). + FullOuterJoin(goqu.T("posts").Schema("photos"), goqu.On(goqu.Ex{"posts.media_id": goqu.I("medias.id")})). + Select( + "lenses.*", + ). + Where(goqu.L("lenses.id IS NOT NULL")). + Order( + goqu.L("MAX(coalesce(posts.publish_date, timestamp with time zone 'epoch'))").Desc(), + ). + GroupBy(goqu.I("lenses.id")). Executor() + if err := selectLenses.ScanStructs(&dbLenses); err != nil { return results, errors.Wrap(err, "failed to select lenses") }