update authorization tokens and add/fix create album endpoint

This commit is contained in:
wisplite
2025-11-22 21:19:44 -06:00
parent 062e0d6066
commit 432a9e5229
7 changed files with 40 additions and 12 deletions
+1
View File
@@ -22,6 +22,7 @@ func Init() bool {
&models.Album{},
&models.User{},
&models.AccessToken{},
&models.UserAlbumAccess{},
)
if err != nil {
log.Fatal("failed to migrate database: ", err)
+1 -1
View File
@@ -19,7 +19,7 @@ type Album struct {
UpdatedAt time.Time
}
type UserAccess struct {
type UserAlbumAccess struct {
UserID string `gorm:"not null"`
AlbumID string `gorm:"not null"`
AccessLevel int `gorm:"not null"` // 0: View, 1: Upload, 2: Edit, 3: Edit/Delete, 4: Admin (manage other users)
+4
View File
@@ -40,6 +40,10 @@ func RegisterAlbumRoutes(rg *gin.RouterGroup) {
Description string `json:"description"`
ParentID string `json:"parentId"`
}
if err := c.ShouldBindJSON(&request); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
result, err := services.CreateAlbum(accessToken, request.Title, request.Description, request.ParentID)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
+4 -4
View File
@@ -62,11 +62,11 @@ func RegisterUserRoutes(rg *gin.RouterGroup) {
})
user.GET("/getUserData", func(c *gin.Context) {
authHeader := c.GetHeader("Authorization")
token := authHeader
if len(authHeader) > 7 && authHeader[:7] == "Bearer " {
token = authHeader[7:]
if authHeader == "" {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
return
}
userData, err := services.GetUserData(token)
userData, err := services.GetUserData(authHeader)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
+1 -1
View File
@@ -82,7 +82,7 @@ func CreateAlbum(accessToken string, title string, description string, parentID
}
func CheckUserAlbumAccess(userID string, albumID string) (int, error) {
userAccess := models.UserAccess{}
userAccess := models.UserAlbumAccess{}
result := db.GetDB().First(&userAccess, "user_id = ? AND album_id = ?", userID, albumID)
if result.Error != nil {
if result.Error == gorm.ErrRecordNotFound {