This commit is contained in:
Jay 2018-05-25 17:26:29 +08:00
parent 59df5ce6a1
commit 70d8971e64
4 changed files with 32 additions and 5 deletions

View File

@ -32,8 +32,8 @@ func GetPhotos(album string) (photo []Photo, err error) {
} }
// GetPhoto - // GetPhoto -
func GetPhoto(id string) (photo *Photo, err error) { func GetPhoto(album, id string) (photo *Photo, err error) {
rows, err := x.Query(`select "id", "album", "name", "thumbnail", "ctime", "mtime" from "storage"."photo" where "id" = $1`, id) rows, err := x.Query(`select "id", "album", "name", "thumbnail", "ctime", "mtime" from "storage"."photo" where "album" = $1 and "id" = $2`, album, id)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -52,7 +52,6 @@ func GetAlbum(c *context.Context) {
c.NotFound("album not found") c.NotFound("album not found")
return return
} }
log.Println(album)
data := make(map[string]interface{}) data := make(map[string]interface{})
data["album"] = utils.ToMap(album) data["album"] = utils.ToMap(album)
c.Success(data) c.Success(data)

View File

@ -9,11 +9,12 @@ import (
// GetAlbumPhotos - // GetAlbumPhotos -
func GetAlbumPhotos(c *context.Context) { func GetAlbumPhotos(c *context.Context) {
data, ok := c.Get("album") data, ok := c.Get("album")
album := data.(models.Album)
if !ok { if !ok {
c.NotFound("album not found") c.NotFound("album not found")
return return
} }
album := data.(*models.Album)
data = nil
photos, err := models.GetPhotos(album.ID) photos, err := models.GetPhotos(album.ID)
if err != nil { if err != nil {
@ -32,5 +33,31 @@ func GetAlbumPhotos(c *context.Context) {
// GetPhoto - // GetPhoto -
func GetPhoto(c *context.Context) { func GetPhoto(c *context.Context) {
data, ok := c.Get("album")
if !ok {
c.NotFound("album not found")
return
}
album := data.(*models.Album)
data = nil
var photoId string
photoId = c.Param("photo")
if len(photoId) == 0 {
c.DataFormat("photo id not found")
return
}
photo, err := models.GetPhoto(album.ID, photoId)
if err != nil {
c.ServerError(nil)
return
}
if photo == nil {
c.NotFound("photo not found")
return
}
res := make(map[string]interface{})
res["photo"] = utils.ToMap(photo)
c.Success(res)
} }

View File

@ -54,9 +54,10 @@ func SetDefaultRoutes(r *gin.Engine) {
albumAPI.GET("/:album", context.PatchContext(album.GetAlbum)) albumAPI.GET("/:album", context.PatchContext(album.GetAlbum))
albumAPI.PUT("/:album", context.PatchContext(album.UpdateAlbum)) albumAPI.PUT("/:album", context.PatchContext(album.UpdateAlbum))
albumAPI.DELETE("/:album", context.PatchContext(album.DeleteAlbum)) albumAPI.DELETE("/:album", context.PatchContext(album.DeleteAlbum))
albumAPI.GET("/:album/photos", context.PatchContext(middleware.VerifyToken), context.PatchContext(middleware.GetAlbumToNext), context.PatchContext(album.GetAlbumPhotos))
} }
photoAPI := albumAPI.Group("/:album/photo", context.PatchContext(middleware.VerifyToken), context.PatchContext(middleware.GetAlbumToNext)) photoAPI := albumAPI.Group("/:album/photo", context.PatchContext(middleware.VerifyToken), context.PatchContext(middleware.GetAlbumToNext))
{ {
photoAPI.GET("/", context.PatchContext(album.GetAlbumPhotos)) photoAPI.GET("/:photo", context.PatchContext(album.GetPhoto))
} }
} }