diff --git a/models/photo.go b/models/photo.go index ea9aa2c..b7cb000 100644 --- a/models/photo.go +++ b/models/photo.go @@ -32,8 +32,8 @@ func GetPhotos(album string) (photo []Photo, err error) { } // GetPhoto - -func GetPhoto(id string) (photo *Photo, err error) { - rows, err := x.Query(`select "id", "album", "name", "thumbnail", "ctime", "mtime" from "storage"."photo" where "id" = $1`, id) +func GetPhoto(album, id string) (photo *Photo, err error) { + 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 { return nil, err } diff --git a/routers/album/album.go b/routers/album/album.go index d547d8a..0c67e7a 100644 --- a/routers/album/album.go +++ b/routers/album/album.go @@ -52,7 +52,6 @@ func GetAlbum(c *context.Context) { c.NotFound("album not found") return } - log.Println(album) data := make(map[string]interface{}) data["album"] = utils.ToMap(album) c.Success(data) diff --git a/routers/album/photo.go b/routers/album/photo.go index 8f07a37..392fc39 100644 --- a/routers/album/photo.go +++ b/routers/album/photo.go @@ -9,11 +9,12 @@ import ( // GetAlbumPhotos - func GetAlbumPhotos(c *context.Context) { data, ok := c.Get("album") - album := data.(models.Album) if !ok { c.NotFound("album not found") return } + album := data.(*models.Album) + data = nil photos, err := models.GetPhotos(album.ID) if err != nil { @@ -32,5 +33,31 @@ func GetAlbumPhotos(c *context.Context) { // GetPhoto - 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) } diff --git a/routers/routes/routes.go b/routers/routes/routes.go index 0f2806d..0d724ac 100644 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -54,9 +54,10 @@ func SetDefaultRoutes(r *gin.Engine) { albumAPI.GET("/:album", context.PatchContext(album.GetAlbum)) albumAPI.PUT("/:album", context.PatchContext(album.UpdateAlbum)) 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.GET("/", context.PatchContext(album.GetAlbumPhotos)) + photoAPI.GET("/:photo", context.PatchContext(album.GetPhoto)) } }