First implementation of liking a photo (#48)

First implementation of liking a photo
This commit is contained in:
Philipp Knobel 2018-10-31 03:33:18 +01:00 committed by Michael Mayer
parent df98ed162f
commit 00f79b2d01
5 changed files with 55 additions and 0 deletions

View file

@ -515,6 +515,7 @@
}, },
likePhoto(photo) { likePhoto(photo) {
photo.PhotoFavorite = !photo.PhotoFavorite; photo.PhotoFavorite = !photo.PhotoFavorite;
photo.like(photo.PhotoFavorite);
}, },
deletePhoto(photo) { deletePhoto(photo) {
this.$alert.success('Photo deleted'); this.$alert.success('Photo deleted');

View file

@ -1,4 +1,5 @@
import Abstract from 'model/abstract'; import Abstract from 'model/abstract';
import Api from 'common/api';
class Photo extends Abstract { class Photo extends Abstract {
getEntityName() { getEntityName() {
@ -117,6 +118,14 @@ class Photo extends Abstract {
return 'Unknown' return 'Unknown'
} }
like(liked) {
if (liked === true) {
return Api.post(this.getEntityResource() + "/like");
} else {
return Api.delete(this.getEntityResource() + "/like");
}
}
static getCollectionResource() { static getCollectionResource() {
return 'photos'; return 'photos';
} }

View file

@ -5,6 +5,7 @@ import (
"github.com/gin-gonic/gin/binding" "github.com/gin-gonic/gin/binding"
"github.com/photoprism/photoprism/internal/forms" "github.com/photoprism/photoprism/internal/forms"
"github.com/photoprism/photoprism/internal/photoprism" "github.com/photoprism/photoprism/internal/photoprism"
"log"
"net/http" "net/http"
"strconv" "strconv"
) )
@ -30,3 +31,39 @@ func GetPhotos(router *gin.RouterGroup, conf *photoprism.Config) {
c.JSON(http.StatusOK, result) c.JSON(http.StatusOK, result)
}) })
} }
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 {
photo := search.FindPhotoById(photoId)
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(""))
}
})
}
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 {
photo := search.FindPhotoById(photoId)
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(""))
}
})
}

View file

@ -193,3 +193,9 @@ func (s *Search) FindFileByHash(fileHash string) (file File) {
return file return file
} }
func (s *Search) FindPhotoById(photoId uint64) (photo Photo) {
s.db.Where("id = ?", photoId).First(&photo)
return photo
}

View file

@ -20,6 +20,8 @@ func registerRoutes(app *gin.Engine, conf *photoprism.Config) {
{ {
api.GetPhotos(v1, conf) api.GetPhotos(v1, conf)
api.GetThumbnail(v1, conf) api.GetThumbnail(v1, conf)
api.LikePhoto(v1, conf)
api.DislikePhoto(v1, conf)
} }
// Default HTML page (client-side routing implemented via Vue.js) // Default HTML page (client-side routing implemented via Vue.js)