2018-09-24 21:14:15 +02:00
|
|
|
package api
|
2018-09-24 11:27:46 +02:00
|
|
|
|
|
|
|
import (
|
2018-10-31 07:14:33 +01:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
2018-09-24 11:27:46 +02:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/gin-gonic/gin/binding"
|
|
|
|
"github.com/photoprism/photoprism/internal/forms"
|
|
|
|
"github.com/photoprism/photoprism/internal/photoprism"
|
|
|
|
)
|
|
|
|
|
2018-11-06 09:56:40 +01:00
|
|
|
// GET /api/v1/photos
|
|
|
|
// Parameters see https://github.com/photoprism/photoprism/blob/develop/internal/forms/photo-search.go
|
2018-09-27 08:59:53 +02:00
|
|
|
func GetPhotos(router *gin.RouterGroup, conf *photoprism.Config) {
|
2018-09-24 11:27:46 +02:00
|
|
|
router.GET("/photos", func(c *gin.Context) {
|
|
|
|
var form forms.PhotoSearchForm
|
|
|
|
|
|
|
|
search := photoprism.NewSearch(conf.OriginalsPath, conf.GetDb())
|
|
|
|
|
|
|
|
c.MustBindWith(&form, binding.Form)
|
|
|
|
|
|
|
|
result, err := search.Photos(form)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(400, gin.H{"error": err.Error()})
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Header("x-result-count", strconv.Itoa(form.Count))
|
|
|
|
c.Header("x-result-offset", strconv.Itoa(form.Offset))
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, result)
|
|
|
|
})
|
2018-09-27 08:59:53 +02:00
|
|
|
}
|
2018-10-31 03:33:18 +01:00
|
|
|
|
2018-11-06 09:56:40 +01:00
|
|
|
// POST /api/v1/photos/:photoId/like
|
2018-10-31 03:33:18 +01:00
|
|
|
func LikePhoto(router *gin.RouterGroup, conf *photoprism.Config) {
|
|
|
|
router.POST("/photos/:photoId/like", func(c *gin.Context) {
|
|
|
|
search := photoprism.NewSearch(conf.OriginalsPath, conf.GetDb())
|
|
|
|
|
|
|
|
photoId, err := strconv.ParseUint(c.Param("photoId"), 10, 64)
|
|
|
|
|
|
|
|
if err == nil {
|
2018-11-01 17:01:45 +01:00
|
|
|
photo := search.FindPhotoByID(photoId)
|
2018-10-31 03:33:18 +01:00
|
|
|
photo.PhotoFavorite = true
|
|
|
|
conf.GetDb().Save(&photo)
|
|
|
|
c.JSON(http.StatusAccepted, http.Response{})
|
|
|
|
} else {
|
|
|
|
log.Printf("could not find image for id: %s", err.Error())
|
|
|
|
c.Data(http.StatusNotFound, "image", []byte(""))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-11-06 09:56:40 +01:00
|
|
|
// DELETE /api/v1/photos/:photoId/like
|
2018-10-31 03:33:18 +01:00
|
|
|
func DislikePhoto(router *gin.RouterGroup, conf *photoprism.Config) {
|
|
|
|
router.DELETE("/photos/:photoId/like", func(c *gin.Context) {
|
|
|
|
search := photoprism.NewSearch(conf.OriginalsPath, conf.GetDb())
|
|
|
|
|
|
|
|
photoId, err := strconv.ParseUint(c.Param("photoId"), 10, 64)
|
|
|
|
|
|
|
|
if err == nil {
|
2018-11-01 17:01:45 +01:00
|
|
|
photo := search.FindPhotoByID(photoId)
|
2018-10-31 03:33:18 +01:00
|
|
|
photo.PhotoFavorite = false
|
|
|
|
conf.GetDb().Save(&photo)
|
|
|
|
c.JSON(http.StatusAccepted, http.Response{})
|
|
|
|
} else {
|
|
|
|
log.Printf("could not find image for id: %s", err.Error())
|
|
|
|
c.Data(http.StatusNotFound, "image", []byte(""))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|