* Backend: Add API endpoint for photo count per month * URL: /api/v1/moments/time
This commit is contained in:
parent
5c913a422d
commit
f6a611bded
3 changed files with 57 additions and 0 deletions
31
internal/api/moments_time.go
Normal file
31
internal/api/moments_time.go
Normal file
|
@ -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)
|
||||
})
|
||||
}
|
25
internal/query/moments_time.go
Normal file
25
internal/query/moments_time.go
Normal file
|
@ -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
|
||||
}
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue