go-gallery/routers/album/photo.go

64 lines
1.2 KiB
Go
Raw Normal View History

2018-04-30 02:13:01 +00:00
package album
import (
"git.trj.tw/golang/go-gallery/models"
"git.trj.tw/golang/go-gallery/modules/context"
2018-05-08 08:35:09 +00:00
"git.trj.tw/golang/go-gallery/modules/utils"
2018-04-30 02:13:01 +00:00
)
2018-05-08 08:35:09 +00:00
// GetAlbumPhotos -
func GetAlbumPhotos(c *context.Context) {
2018-05-08 08:19:51 +00:00
data, ok := c.Get("album")
2018-04-30 02:13:01 +00:00
if !ok {
c.NotFound("album not found")
return
}
2018-05-25 09:26:29 +00:00
album := data.(*models.Album)
data = nil
2018-05-08 08:19:51 +00:00
2018-05-08 08:35:09 +00:00
photos, err := models.GetPhotos(album.ID)
2018-04-30 02:13:01 +00:00
if err != nil {
c.ServerError(nil)
return
}
2018-05-08 08:35:09 +00:00
res := make(map[string]interface{})
res["photos"] = make([]map[string]interface{}, 0)
for _, v := range photos {
res["photos"] = append(res["photos"].([]map[string]interface{}), utils.ToMap(v))
}
c.Success(res)
}
// GetPhoto -
func GetPhoto(c *context.Context) {
2018-05-25 09:26:29 +00:00
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)
2018-05-08 08:35:09 +00:00
2018-05-25 09:26:29 +00:00
c.Success(res)
2018-04-30 02:13:01 +00:00
}