fix utils
This commit is contained in:
@@ -5,7 +5,6 @@ import (
|
||||
"crypto/sha512"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"log"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
@@ -40,7 +39,6 @@ func UserLogin(c *context.Context) {
|
||||
acc, err := models.GetAccount(loginArg.Account)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
c.ServerError(nil)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
package album
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"git.trj.tw/golang/go-gallery/modules/context"
|
||||
"git.trj.tw/golang/go-gallery/modules/utils"
|
||||
|
||||
"git.trj.tw/golang/go-gallery/models"
|
||||
)
|
||||
|
||||
// GetAllAlbums -
|
||||
func GetAllAlbums(c *context.Context) {
|
||||
val, ok := c.Get("token")
|
||||
|
||||
if !ok {
|
||||
c.CustomRes("Foridden", "User token data not found")
|
||||
}
|
||||
|
||||
albums, err := models.GetUserAlbums(val.(map[string]interface{})["user"].(map[string]interface{})["id"].(string))
|
||||
if err != nil {
|
||||
c.ServerError(nil)
|
||||
return
|
||||
}
|
||||
|
||||
data := make(map[string]interface{})
|
||||
data["albums"] = make([]map[string]interface{}, 0)
|
||||
for _, v := range albums {
|
||||
log.Println(v)
|
||||
data["albums"] = append(data["albums"].([]map[string]interface{}), utils.ToMap(v))
|
||||
}
|
||||
c.Success(data)
|
||||
}
|
||||
|
||||
// GetAlbum -
|
||||
func GetAlbum(c *context.Context) {
|
||||
id := c.Param("id")
|
||||
|
||||
album, err := models.GetAlbum(id)
|
||||
if err != nil {
|
||||
c.ServerError(nil)
|
||||
return
|
||||
}
|
||||
if album == nil {
|
||||
c.NotFound("album not found")
|
||||
return
|
||||
}
|
||||
|
||||
data := make(map[string]interface{})
|
||||
data["album"] = album
|
||||
c.Success(data)
|
||||
}
|
||||
@@ -3,7 +3,9 @@ package routes
|
||||
import (
|
||||
"git.trj.tw/golang/go-gallery/models"
|
||||
"git.trj.tw/golang/go-gallery/modules/context"
|
||||
"git.trj.tw/golang/go-gallery/modules/middleware"
|
||||
"git.trj.tw/golang/go-gallery/routers/account"
|
||||
"git.trj.tw/golang/go-gallery/routers/album"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
@@ -43,4 +45,9 @@ func SetDefaultRoutes(r *gin.Engine) {
|
||||
accountAPI.POST("/logout", context.PatchContext(account.UserLogout))
|
||||
accountAPI.POST("/signup", context.PatchContext(account.UserSignup))
|
||||
}
|
||||
api.GET("/albums", context.PatchContext(middleware.VerifyToken), context.PatchContext(album.GetAllAlbums))
|
||||
albumAPI := api.Group("/album", context.PatchContext(middleware.VerifyToken))
|
||||
{
|
||||
albumAPI.GET("/:id", context.PatchContext(album.GetAlbum))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user