diff --git a/models/album.go b/models/album.go index 91f2538..ce6dd02 100644 --- a/models/album.go +++ b/models/album.go @@ -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 +} diff --git a/routers/album/album.go b/routers/album/album.go index 7c4d7b5..0f8582b 100644 --- a/routers/album/album.go +++ b/routers/album/album.go @@ -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) +} diff --git a/routers/routes/routes.go b/routers/routes/routes.go index 9c5de55..e4a8654 100644 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -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)) } }