This commit is contained in:
Jay 2018-04-25 15:50:45 +08:00
parent 8c83ceb9ea
commit 2b17fa04be
5 changed files with 152 additions and 18 deletions

View File

@ -16,7 +16,6 @@ type Album struct {
Public bool `sql:"public" cc:"public"`
Ctime time.Time `sql:"ctime" cc:"ctime"`
Mtime time.Time `sql:"mtime" cc:"mtime"`
Photos []*Photo `sql:"-" cc:"-"`
}
// GetAllAlbums -
@ -56,8 +55,8 @@ func GetUserAlbums(uid string) (album []Album, err error) {
}
// GetAlbum -
func GetAlbum(id string) (album *Album, err error) {
rows, err := x.Query(`selecy "id", "uid", "name", "public", "ctime", "mtime" where "id" = $1 limit 1`, id)
func GetAlbum(id, uid string) (album *Album, err error) {
rows, err := x.Query(`selecy "id", "uid", "name", "public", "ctime", "mtime" where "id" = $1 and "uid" = $2 limit 1`, id, uid)
if err != nil {
return
}
@ -72,3 +71,51 @@ func GetAlbum(id string) (album *Album, err error) {
return
}
// Get -
func (a *Album) Get() (err error) {
rows, err := x.Query(`select "id", "uid", "name", "public", "ctime", "mtime" from "storage"."album" where "id" = $1`, a.ID)
if err != nil {
return err
}
ok, err := dbtool.ScanToStruct(rows, a)
if err != nil {
return err
}
if !ok {
return errors.New("album data not found")
}
return
}
// Create -
func (a *Album) Create() (err error) {
if len(a.UID) == 0 || len(a.Name) == 0 {
return errors.New("album data empty")
}
rows, err := x.Query(`insert into "storage"."album" ("uid", "name", "public") values ($1, $2, $3) returning *`, a.UID, a.Name, a.Public)
if err != nil {
return err
}
defer rows.Close()
_, err = dbtool.ScanToStruct(rows, a)
if err != nil {
return err
}
return
}
// Update -
func (a *Album) Update() (err error) {
_, err = x.Exec(`update "storage"."album" set
"name" = $1,
"public" = $2
where
"id" = $3`, a.Name, a.Public, a.ID)
return
}

View File

@ -4,10 +4,10 @@ import "time"
// Photo model
type Photo struct {
ID string `xorm:"id"`
Album string `xorm:"album"`
Name string `xorm:"name"`
Thumbnail string `xorm:"thumbnail"`
Ctime time.Time `xorm:"ctime"`
Mtime time.Time `xorm:"mtime"`
ID string `sql:"id" cc:"id"`
Album string `sql:"album" cc:"album"`
Name string `sql:"name" cc:"name"`
Thumbnail string `sql:"thumbnail" cc:"thumbnail"`
Ctime time.Time `sql:"ctime" cc:"ctime"`
Mtime time.Time `sql:"mtime" cc:"mtime"`
}

View File

@ -1,8 +0,0 @@
package middleware
import "github.com/gin-gonic/gin"
// CORS middleware
func CORS(c *gin.Context) {
}

View File

@ -18,6 +18,7 @@ func GetAllAlbums(c *context.Context) {
}
albums, err := models.GetUserAlbums(val.(map[string]interface{})["user"].(map[string]interface{})["id"].(string))
log.Println(err)
if err != nil {
c.ServerError(nil)
return
@ -35,8 +36,14 @@ func GetAllAlbums(c *context.Context) {
// GetAlbum -
func GetAlbum(c *context.Context) {
id := c.Param("id")
val, ok := c.Get("token")
if !ok {
c.CustomRes("Foridden", "user token data not found")
return
}
uid := val.(map[string]interface{})["user"].(map[string]interface{})["id"].(string)
album, err := models.GetAlbum(id)
album, err := models.GetAlbum(id, uid)
if err != nil {
c.ServerError(nil)
return
@ -50,3 +57,89 @@ func GetAlbum(c *context.Context) {
data["album"] = album
c.Success(data)
}
// CreateAlbum -
func CreateAlbum(c *context.Context) {
postData := struct {
Name string `json:"name" binding:"required"`
Public bool `json:"public" binding:"exists"`
}{
Public: false,
}
token, ok := c.Get("token")
if !ok {
c.CustomRes("Foridden", "user token data not found")
return
}
uid, ok := token.(map[string]interface{})["user"].(map[string]interface{})["id"].(string)
err := c.BindJSON(&postData)
if err != nil {
c.DataFormat(nil)
return
}
album := &models.Album{
UID: uid,
Name: postData.Name,
Public: postData.Public,
}
err = album.Create()
if err != nil {
c.ServerError(nil)
return
}
res := utils.ToMap(album)
m := make(map[string]interface{})
m["album"] = res
c.Success(m)
}
// UpdateAlbum -
func UpdateAlbum(c *context.Context) {
id := c.Param("id")
postData := struct {
Name string `json:"name" binding:"required"`
Public bool `json:"public" binding:"required"`
}{}
if len(id) == 0 {
c.NotFound(nil)
return
}
token, ok := c.Get("token")
if !ok {
c.CustomRes("Foridden", "user token data not found")
return
}
uid, ok := token.(map[string]interface{})["user"].(map[string]interface{})["id"].(string)
err := c.BindData(&postData)
if err != nil {
c.DataFormat(nil)
return
}
album, err := models.GetAlbum(id, uid)
if err != nil {
c.ServerError(nil)
return
}
if album == nil {
c.NotFound("album not found")
return
}
album.Name = postData.Name
album.Public = postData.Public
err = album.Update()
if err != nil {
c.ServerError(nil)
return
}
res := utils.ToMap(album)
m := make(map[string]interface{})
m["album"] = res
c.Success(m)
}

View File

@ -50,6 +50,8 @@ func SetDefaultRoutes(r *gin.Engine) {
api.GET("/albums", context.PatchContext(middleware.VerifyToken), context.PatchContext(album.GetAllAlbums))
albumAPI := api.Group("/album", context.PatchContext(middleware.VerifyToken))
{
albumAPI.POST("/", context.PatchContext(album.CreateAlbum))
albumAPI.GET("/:id", context.PatchContext(album.GetAlbum))
albumAPI.PUT("/:id", context.PatchContext(album.UpdateAlbum))
}
}