From 3badce1bc8fc06a1875df8e3cb47c49367063e3d Mon Sep 17 00:00:00 2001 From: Krassimir Valev Date: Thu, 1 Sep 2022 00:04:00 +0300 Subject: [PATCH] Photos: Search by city --- internal/form/search_photos.go | 1 + internal/search/photos.go | 5 +++++ internal/search/photos_test.go | 37 ++++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/internal/form/search_photos.go b/internal/form/search_photos.go index d92e04570..0fc8290b2 100644 --- a/internal/form/search_photos.go +++ b/internal/form/search_photos.go @@ -53,6 +53,7 @@ type SearchPhotos struct { Category string `form:"category" notes:"Location Category Name"` // Moments Country string `form:"country" example:"country:\"de|us\"" notes:"Country Code, OR search with |"` // Moments State string `form:"state" example:"state:\"Baden-Württemberg\"" notes:"Name of State (Location), OR search with |"` // Moments + City string `form:"city" example:"city:\"Berlin\"" notes:"Name of City (Location), OR search with |"` // Moments Year string `form:"year" example:"year:1990|2003" notes:"Year Number, OR search with |"` // Moments Month string `form:"month" example:"month:7|10" notes:"Month (1-12), OR search with |"` // Moments Day string `form:"day" example:"day:3|13" notes:"Day of Month (1-31), OR search with |"` // Moments diff --git a/internal/search/photos.go b/internal/search/photos.go index 9671c43a5..aee50b19a 100644 --- a/internal/search/photos.go +++ b/internal/search/photos.go @@ -401,6 +401,11 @@ func searchPhotos(f form.SearchPhotos, resultCols string) (results PhotoResults, s = s.Where("places.place_state IN (?)", SplitOr(f.State)) } + // Filter by location city? + if txt.NotEmpty(f.City) { + s = s.Where("places.place_city IN (?)", SplitOr(f.City)) + } + // Filter by location category? if txt.NotEmpty(f.Category) { s = s.Joins("JOIN cells ON photos.cell_id = cells.id"). diff --git a/internal/search/photos_test.go b/internal/search/photos_test.go index cf4e10b65..464ce6d7a 100644 --- a/internal/search/photos_test.go +++ b/internal/search/photos_test.go @@ -479,6 +479,26 @@ func TestPhotos(t *testing.T) { assert.LessOrEqual(t, 1, len(photos)) + }) + t.Run("form.city", func(t *testing.T) { + var f form.SearchPhotos + f.Query = "city:Mandeni" + f.Count = 10 + f.Offset = 0 + + // Parse query string and filter. + if err := f.ParseQueryString(); err != nil { + t.Fatal(err) + } + + photos, _, err := Photos(f) + + if err != nil { + t.Fatal(err) + } + + assert.LessOrEqual(t, 1, len(photos)) + }) t.Run("form.title", func(t *testing.T) { var f form.SearchPhotos @@ -976,6 +996,23 @@ func TestPhotos(t *testing.T) { assert.LessOrEqual(t, 1, len(photos)) }) + t.Run("search for city", func(t *testing.T) { + var f form.SearchPhotos + f.City = "Mandeni" + + // Parse query string and filter. + if err := f.ParseQueryString(); err != nil { + t.Fatal(err) + } + + photos, _, err := Photos(f) + + if err != nil { + t.Fatal(err) + } + + assert.LessOrEqual(t, 1, len(photos)) + }) t.Run("search for category", func(t *testing.T) { var f form.SearchPhotos f.Category = "botanical garden"