Skip to content

Commit

Permalink
photoprism#152 total count API implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
capraynor committed Jan 29, 2024
1 parent ee61d98 commit 36b1f6f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
13 changes: 13 additions & 0 deletions internal/api/photos_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,20 @@ func SearchPhotos(router *gin.RouterGroup) {
c.JSON(http.StatusOK, result)
}

countHandler := func(c *gin.Context) {
f, s, err := searchForm(c)
if err != nil {
return
}

photoTotalCount, err := search.UserPhotosCount(f, s)

c.JSON(http.StatusOK, photoTotalCount)

}

// Register route handlers.
router.GET("/photos", defaultHandler)
router.GET("/photos/count", countHandler)
router.GET("/photos/view", viewHandler)
}
21 changes: 19 additions & 2 deletions internal/search/photos.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,17 @@ func UserPhotos(f form.SearchPhotos, sess *entity.Session) (results PhotoResults
return searchPhotos(f, sess, PhotosColsAll)
}

func UserPhotosCount(f form.SearchPhotos, sess *entity.Session) (count PhotoCountResult, err error) {
return getPhotosTotalCount(f, sess, PhotosColsAll)
}

// PhotoIds finds photo and file ids based on the search form provided and returns them as PhotoResults.
func PhotoIds(f form.SearchPhotos) (files PhotoResults, count int, err error) {
f.Merged = false
f.Primary = true
return searchPhotos(f, nil, "photos.id, photos.photo_uid, files.file_uid")
}
func buildBasicFilters(f form.SearchPhotos, sess *entity.Session, resultCols string) (db *gorm.DB, returnZeroResultsImmediately bool, err error) {
func buildBasicQuery(f form.SearchPhotos, sess *entity.Session, resultCols string) (db *gorm.DB, returnZeroResultsImmediately bool, err error) {

// Parse query string and filter.
if err = f.ParseQueryString(); err != nil {
Expand Down Expand Up @@ -722,11 +726,24 @@ func buildBasicFilters(f form.SearchPhotos, sess *entity.Session, resultCols str
return s, false, nil
}

// Given the search form, and return the total count (discard f.Merged, f.Count and f.Offset)
func getPhotosTotalCount(f form.SearchPhotos, sess *entity.Session, resultCols string) (count PhotoCountResult, err error) {
s, returnZeroResultsImmediately, err := buildBasicQuery(f, sess, resultCols)
var countResult PhotoCountResult

if returnZeroResultsImmediately {
countResult.TotalCount = 0
return countResult, nil
}
s.Count(&countResult.TotalCount)
return countResult, nil
}

// searchPhotos finds photos based on the search form and user session then returns them as PhotoResults.
func searchPhotos(f form.SearchPhotos, sess *entity.Session, resultCols string) (results PhotoResults, count int, err error) {
start := time.Now()

s, returnZeroResultsImmediately, err := buildBasicFilters(f, sess, resultCols)
s, returnZeroResultsImmediately, err := buildBasicQuery(f, sess, resultCols)

if err != nil {
return PhotoResults{}, 0, err
Expand Down
4 changes: 4 additions & 0 deletions internal/search/photos_results.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,7 @@ func (photos PhotoResults) Merge() (merged PhotoResults, count int, err error) {

return merged, count, nil
}

type PhotoCountResult struct {
TotalCount uint `json:"TotalCount"`
}

0 comments on commit 36b1f6f

Please sign in to comment.