add delete album

This commit is contained in:
Jay 2018-04-25 17:10:29 +08:00
parent 0da02160f0
commit 1b8416dba3
3 changed files with 47 additions and 5 deletions

View File

@ -56,14 +56,15 @@ func GetUserAlbums(uid string) (album []Album, err error) {
// GetAlbum -
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)
rows, err := x.Query(`select "id", "uid", "name", "public", "ctime", "mtime" from "storage"."album" where "id" = $1 and "uid" = $2 limit 1`, id, uid)
if err != nil {
return
return nil, err
}
defer rows.Close()
album = &Album{}
ok, err := dbtool.ScanToStruct(rows, album)
if err != nil {
return
return nil, err
}
if !ok {
return nil, nil
@ -119,3 +120,9 @@ func (a *Album) Update() (err error) {
return
}
// Delete -
func (a *Album) Delete() (err error) {
_, err = x.Exec(`delete from "storage"."album" where "id" = $1`, a.ID)
return
}

View File

@ -52,9 +52,9 @@ func GetAlbum(c *context.Context) {
c.NotFound("album not found")
return
}
log.Println(album)
data := make(map[string]interface{})
data["album"] = album
data["album"] = utils.ToMap(album)
c.Success(data)
}
@ -143,3 +143,37 @@ func UpdateAlbum(c *context.Context) {
m["album"] = res
c.Success(m)
}
// DeleteAlbum -
func DeleteAlbum(c *context.Context) {
id := c.Param("id")
if len(id) == 0 {
c.NotFound(nil)
return
}
token, ok := c.Get("token")
if !ok {
c.CustomRes("Foridden", "user token data not found")
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
}
err = album.Delete()
if err != nil {
c.ServerError(nil)
return
}
c.Success(nil)
}

View File

@ -53,5 +53,6 @@ func SetDefaultRoutes(r *gin.Engine) {
albumAPI.POST("/", context.PatchContext(album.CreateAlbum))
albumAPI.GET("/:id", context.PatchContext(album.GetAlbum))
albumAPI.PUT("/:id", context.PatchContext(album.UpdateAlbum))
albumAPI.DELETE("/:id", context.PatchContext(album.DeleteAlbum))
}
}