photoprism/internal/api/albums.go

170 lines
4.2 KiB
Go
Raw Normal View History

2019-06-17 21:45:06 +02:00
package api
import (
"fmt"
2019-06-17 21:45:06 +02:00
"net/http"
"strconv"
"github.com/photoprism/photoprism/internal/event"
"github.com/photoprism/photoprism/internal/models"
2019-06-17 21:45:06 +02:00
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/photoprism/photoprism/internal/config"
"github.com/photoprism/photoprism/internal/forms"
"github.com/photoprism/photoprism/internal/photoprism"
"github.com/photoprism/photoprism/internal/util"
)
// GET /api/v1/albums
func GetAlbums(router *gin.RouterGroup, conf *config.Config) {
router.GET("/albums", func(c *gin.Context) {
var form forms.AlbumSearchForm
search := photoprism.NewSearch(conf.OriginalsPath(), conf.Db())
err := c.MustBindWith(&form, binding.Form)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": util.UcFirst(err.Error())})
return
}
result, err := search.Albums(form)
if err != nil {
c.AbortWithStatusJSON(400, gin.H{"error": util.UcFirst(err.Error())})
return
}
c.Header("X-Result-Count", strconv.Itoa(form.Count))
c.Header("X-Result-Offset", strconv.Itoa(form.Offset))
2019-06-17 21:45:06 +02:00
c.JSON(http.StatusOK, result)
})
}
type AlbumParams struct {
2019-06-17 21:45:06 +02:00
AlbumName string `json:"AlbumName"`
}
// POST /api/v1/albums
func CreateAlbum(router *gin.RouterGroup, conf *config.Config) {
router.POST("/albums", func(c *gin.Context) {
if Unauthorized(c, conf) {
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
return
}
var params AlbumParams
2019-06-17 21:45:06 +02:00
if err := c.BindJSON(&params); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": util.UcFirst(err.Error())})
return
2019-06-17 21:45:06 +02:00
}
m := models.NewAlbum(params.AlbumName)
2019-06-17 21:45:06 +02:00
if res := conf.Db().Create(m); res.Error != nil {
log.Error(res.Error.Error())
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("\"%s\" already exists", m.AlbumName)})
return
2019-06-17 21:45:06 +02:00
}
event.Success(fmt.Sprintf("Album %s created", m.AlbumName))
c.JSON(http.StatusOK, m)
2019-06-17 21:45:06 +02:00
})
}
// PUT /api/v1/albums/:uuid
func UpdateAlbum(router *gin.RouterGroup, conf *config.Config) {
router.PUT("/albums/:uuid", func(c *gin.Context) {
if Unauthorized(c, conf) {
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
return
}
var params AlbumParams
if err := c.BindJSON(&params); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": util.UcFirst(err.Error())})
return
}
id := c.Param("uuid")
search := photoprism.NewSearch(conf.OriginalsPath(), conf.Db())
m, err := search.FindAlbumByUUID(id)
if err != nil {
c.AbortWithStatusJSON(404, gin.H{"error": util.UcFirst(err.Error())})
return
}
m.Rename(params.AlbumName)
conf.Db().Save(&m)
event.Publish("config.updated", event.Data(conf.ClientConfig()))
event.Success(fmt.Sprintf("Album %s updated", m.AlbumName))
c.JSON(http.StatusOK, m)
})
}
2019-06-17 21:45:06 +02:00
// POST /api/v1/albums/:uuid/like
//
// Parameters:
// uuid: string Album UUID
func LikeAlbum(router *gin.RouterGroup, conf *config.Config) {
router.POST("/albums/:uuid/like", func(c *gin.Context) {
if Unauthorized(c, conf) {
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
return
}
2019-06-17 21:45:06 +02:00
search := photoprism.NewSearch(conf.OriginalsPath(), conf.Db())
album, err := search.FindAlbumByUUID(c.Param("uuid"))
if err != nil {
c.AbortWithStatusJSON(404, gin.H{"error": util.UcFirst(err.Error())})
return
}
album.AlbumFavorite = true
conf.Db().Save(&album)
event.Publish("config.updated", event.Data(conf.ClientConfig()))
2019-06-17 21:45:06 +02:00
c.JSON(http.StatusOK, http.Response{})
})
}
// DELETE /api/v1/albums/:uuid/like
//
// Parameters:
// uuid: string Album UUID
func DislikeAlbum(router *gin.RouterGroup, conf *config.Config) {
router.DELETE("/albums/:uuid/like", func(c *gin.Context) {
if Unauthorized(c, conf) {
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
return
}
2019-06-17 21:45:06 +02:00
search := photoprism.NewSearch(conf.OriginalsPath(), conf.Db())
album, err := search.FindAlbumByUUID(c.Param("uuid"))
if err != nil {
c.AbortWithStatusJSON(404, gin.H{"error": util.UcFirst(err.Error())})
return
}
album.AlbumFavorite = false
conf.Db().Save(&album)
event.Publish("config.updated", event.Data(conf.ClientConfig()))
2019-06-17 21:45:06 +02:00
c.JSON(http.StatusOK, http.Response{})
})
}