From e33c51727a6bab039b456e0c166fd0eb4cc2f1f7 Mon Sep 17 00:00:00 2001 From: Jay Date: Mon, 30 Apr 2018 10:13:01 +0800 Subject: [PATCH] update --- models/photo.go | 41 ++++++++++++++++++++++++++++++++++++- modules/middleware/album.go | 37 +++++++++++++++++++++++++++++++++ routers/album/photo.go | 25 ++++++++++++++++++++++ routers/routes/routes.go | 4 ++++ 4 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 modules/middleware/album.go create mode 100644 routers/album/photo.go diff --git a/models/photo.go b/models/photo.go index 4f1fc4f..ea9aa2c 100644 --- a/models/photo.go +++ b/models/photo.go @@ -1,6 +1,10 @@ package models -import "time" +import ( + "time" + + "git.trj.tw/golang/utils/dbtool" +) // Photo model type Photo struct { @@ -11,3 +15,38 @@ type Photo struct { Ctime time.Time `sql:"ctime" cc:"ctime"` Mtime time.Time `sql:"mtime" cc:"mtime"` } + +// GetPhotos - +func GetPhotos(album string) (photo []Photo, err error) { + rows, err := x.Query(`select "id", "album", "name", "thumbnail", "ctime", "mtime" from "storage"."photo" where "album" = $1`, album) + if err != nil { + return nil, err + } + + err = dbtool.ScanToStructAll(rows, &photo) + if err != nil { + return nil, err + } + + return +} + +// 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) + if err != nil { + return nil, err + } + + photo = &Photo{} + ok, err := dbtool.ScanToStruct(rows, photo) + if err != nil { + return nil, err + } + + if !ok { + return nil, nil + } + + return +} diff --git a/modules/middleware/album.go b/modules/middleware/album.go new file mode 100644 index 0000000..a353960 --- /dev/null +++ b/modules/middleware/album.go @@ -0,0 +1,37 @@ +package middleware + +import ( + "git.trj.tw/golang/go-gallery/models" + "git.trj.tw/golang/go-gallery/modules/context" + "git.trj.tw/golang/go-gallery/modules/utils" +) + +// GetAlbumToNext - +func GetAlbumToNext(c *context.Context) { + id := c.Param("album") + if len(id) == 0 { + c.DataFormat("no album id") + return + } + + token, ok := c.Get("token") + if !ok { + c.CustomRes("Foridden", nil) + return + } + + album, err := models.GetAlbum(id, token.(map[string]interface{})["user"].(map[string]interface{})["id"].(string)) + if err != nil { + c.ServerError(nil) + return + } + if album == nil { + c.NotFound("album not found") + return + } + + albumMap := utils.ToMap(album) + c.Set("album", albumMap) + + c.Next() +} diff --git a/routers/album/photo.go b/routers/album/photo.go new file mode 100644 index 0000000..7221736 --- /dev/null +++ b/routers/album/photo.go @@ -0,0 +1,25 @@ +package album + +import ( + "git.trj.tw/golang/go-gallery/models" + "git.trj.tw/golang/go-gallery/modules/context" +) + +// GetAllAlbumPhotos - +func GetAllAlbumPhotos(c *context.Context) { + +} + +// GetPhoto - +func GetPhoto(c *context.Context) { + album, ok := c.Get("album") + if !ok { + c.NotFound("album not found") + return + } + _, err := models.GetPhoto(album.(map[string]interface{})["id"].(string)) + if err != nil { + c.ServerError(nil) + return + } +} diff --git a/routers/routes/routes.go b/routers/routes/routes.go index e4a8654..da19b4c 100644 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -55,4 +55,8 @@ func SetDefaultRoutes(r *gin.Engine) { albumAPI.PUT("/:id", context.PatchContext(album.UpdateAlbum)) albumAPI.DELETE("/:id", context.PatchContext(album.DeleteAlbum)) } + photoAPI := albumAPI.Group("/:album/photo", context.PatchContext(middleware.VerifyToken), context.PatchContext(middleware.GetAlbumToNext)) + { + photoAPI.GET("/", context.PatchContext(album.GetPhoto)) + } }