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
+94 -1
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)
}