Albums: Add BatchPhotosAlbum API #15

Signed-off-by: Michael Mayer <michael@liquidbytes.net>
This commit is contained in:
Michael Mayer 2019-12-04 01:46:17 +01:00
parent e214ae2921
commit ded37fc486
5 changed files with 47 additions and 6 deletions

View file

@ -124,3 +124,43 @@ func BatchPhotosStory(router *gin.RouterGroup, conf *config.Config) {
c.JSON(http.StatusOK, gin.H{"message": fmt.Sprintf("photos marked as story in %s", elapsed)})
})
}
type BatchPhotosAlbumParams struct {
Ids []int `json:"ids"`
AlbumID int `json:"albumId"`
}
// POST /api/v1/batch/photos/album
func BatchPhotosAlbum(router *gin.RouterGroup, conf *config.Config) {
router.POST("/batch/photos/album", func(c *gin.Context) {
if Unauthorized(c, conf) {
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
return
}
var params BatchPhotosAlbumParams
if err := c.BindJSON(&params); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": util.UcFirst(err.Error())})
return
}
if params.AlbumID < 1 {
log.Error("no album selected")
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": util.UcFirst("no album selected")})
return
}
if len(params.Ids) == 0 {
log.Error("no photos selected")
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": util.UcFirst("no photos selected")})
return
}
log.Infof("adding photos to album %d: %#v", params.AlbumID, params.Ids)
/* TODO: Add photos to album */
c.JSON(http.StatusOK, gin.H{"message": "photos added to album"})
})
}

View file

@ -20,7 +20,7 @@ type Album struct {
AlbumPhoto *Photo
AlbumPhotoID uint
AlbumFavorite bool
Photos []Photo `gorm:"many2many:album_photos;"`
Photos []Photo `gorm:"many2many:albums_photos;"`
}
func (m *Album) BeforeCreate(scope *gorm.Scope) error {

View file

@ -47,7 +47,7 @@ type Photo struct {
TimeZone string
Labels []*PhotoLabel
Files []*File
Albums []*Album `gorm:"many2many:album_photos;"`
Albums []*Album `gorm:"many2many:albums_photos;"`
}
func (m *Photo) BeforeCreate(scope *gorm.Scope) error {

View file

@ -122,7 +122,7 @@ func (s *Search) Photos(form forms.PhotoSearchForm) (results []PhotoSearchResult
}
if form.Album > 0 {
q = q.Joins("JOIN album_photos ON album_photos.photo_id = photos.id").Where("album_photos.album_id = ?", form.Album)
q = q.Joins("JOIN albums_photos ON albums_photos.photo_id = photos.id").Where("albums_photos.album_id = ?", form.Album)
}
if form.Camera > 0 {
@ -384,7 +384,7 @@ func (s *Search) FindAlbumThumbByUUID(albumUUID string) (file models.File, err e
if err := s.db.Where("files.file_primary AND files.deleted_at IS NULL").
Joins("JOIN albums ON albums.album_uuid = ?", albumUUID).
Joins("JOIN album_photos ON album_photos.album_id = albums.id AND album_photos.photo_id = files.photo_id").
Joins("JOIN albums_photos ON albums_photos.album_id = albums.id AND albums_photos.photo_id = files.photo_id").
First(&file).Error; err != nil {
return file, err
}
@ -405,8 +405,8 @@ func (s *Search) Albums(form forms.AlbumSearchForm) (results []AlbumSearchResult
// q.LogMode(true)
q = q.Table("albums").
Select(`albums.*, COUNT(album_photos.album_id) AS album_count`).
Joins("LEFT JOIN album_photos ON album_photos.album_id = albums.id").
Select(`albums.*, COUNT(albums_photos.album_id) AS album_count`).
Joins("LEFT JOIN albums_photos ON albums_photos.album_id = albums.id").
Where("albums.deleted_at IS NULL").
Group("albums.id")

View file

@ -40,6 +40,7 @@ func registerRoutes(router *gin.Engine, conf *config.Config) {
api.BatchPhotosDelete(v1, conf)
api.BatchPhotosPrivate(v1, conf)
api.BatchPhotosStory(v1, conf)
api.BatchPhotosAlbum(v1, conf)
api.GetAlbums(v1, conf)
api.LikeAlbum(v1, conf)