update
This commit is contained in:
parent
1b8416dba3
commit
e33c51727a
@ -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
|
||||
}
|
||||
|
37
modules/middleware/album.go
Normal file
37
modules/middleware/album.go
Normal file
@ -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()
|
||||
}
|
25
routers/album/photo.go
Normal file
25
routers/album/photo.go
Normal file
@ -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
|
||||
}
|
||||
}
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user