update
This commit is contained in:
+94
-1
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user