diff --git a/internal/api/moments_time.go b/internal/api/moments_time.go new file mode 100644 index 000000000..d65a3c29c --- /dev/null +++ b/internal/api/moments_time.go @@ -0,0 +1,31 @@ +package api + +import ( + "net/http" + + "github.com/photoprism/photoprism/internal/config" + "github.com/photoprism/photoprism/internal/query" + "github.com/photoprism/photoprism/pkg/txt" + + "github.com/gin-gonic/gin" +) + +// GET /api/v1/moments/time +func GetMomentsTime(router *gin.RouterGroup, conf *config.Config) { + router.GET("/moments/time", func(c *gin.Context) { + if Unauthorized(c, conf) { + c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized) + return + } + + q := query.New(conf.OriginalsPath(), conf.Db()) + + result, err := q.GetMomentsTime() + if err != nil { + c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": txt.UcFirst(err.Error())}) + return + } + + c.JSON(http.StatusOK, result) + }) +} diff --git a/internal/query/moments_time.go b/internal/query/moments_time.go new file mode 100644 index 000000000..e73435e81 --- /dev/null +++ b/internal/query/moments_time.go @@ -0,0 +1,25 @@ +package query + +// MomentsTimeResult contains photo counts per month and year +type MomentsTimeResult struct { + PhotoYear int + PhotoMonth int + Count int +} + +// GetMomentsTime counts photos per month and year +func (s *Repo) GetMomentsTime() (results []MomentsTimeResult, err error) { + q := s.db.NewScope(nil).DB() + + q = q.Table("photos"). + Where("deleted_at IS NULL"). + Select("photos.photo_year, photos.photo_month, COUNT(*) AS count"). + Group("photos.photo_year, photos.photo_month"). + Order("photos.photo_year DESC, photos.photo_month DESC") + + if result := q.Scan(&results); result.Error != nil { + return results, result.Error + } + + return results, nil +} diff --git a/internal/server/routes.go b/internal/server/routes.go index ca5ba3593..4229a5ae6 100644 --- a/internal/server/routes.go +++ b/internal/server/routes.go @@ -34,6 +34,7 @@ func registerRoutes(router *gin.Engine, conf *config.Config) { api.GetPhotoDownload(v1, conf) api.LikePhoto(v1, conf) api.DislikePhoto(v1, conf) + api.GetMomentsTime(v1, conf) api.GetLabels(v1, conf) api.LikeLabel(v1, conf)