-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add API endpoint for renaming files (#27)
Signed-off-by: Erik Westra <[email protected]>
- Loading branch information
1 parent
f722ec9
commit 3a1853a
Showing
5 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package database | ||
|
||
import "github.com/go-fast-cdn/models" | ||
|
||
func RenameDoc(oldFileName, newFileName string) error { | ||
doc := models.Doc{} | ||
return DB.Model(&doc).Where("file_name = ?", oldFileName).Update("file_name", newFileName).Error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package database | ||
|
||
import "github.com/go-fast-cdn/models" | ||
|
||
func RenameImage(oldFileName, newFileName string) error { | ||
image := models.Image{} | ||
return DB.Model(&image).Where("file_name = ?", oldFileName).Update("file_name", newFileName).Error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package handlers | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/go-fast-cdn/database" | ||
) | ||
|
||
func HandleDocsRename(c *gin.Context) { | ||
oldName := c.PostForm("filename") | ||
newName := c.PostForm("newname") | ||
|
||
if oldName == "" || newName == "" { | ||
c.String(http.StatusBadRequest, "Invalid request") | ||
return | ||
} | ||
|
||
err := database.RenameDoc(oldName, newName) | ||
if err != nil { | ||
c.String(http.StatusInternalServerError, "Failed to rename file: %s", err.Error()) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, gin.H{"status": "File renamed successfully"}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package handlers | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/go-fast-cdn/database" | ||
) | ||
|
||
func HandleImageRename(c *gin.Context) { | ||
oldName := c.PostForm("filename") | ||
newName := c.PostForm("newname") | ||
|
||
if oldName == "" || newName == "" { | ||
c.String(http.StatusBadRequest, "Invalid request") | ||
return | ||
} | ||
|
||
err := database.RenameImage(oldName, newName) | ||
if err != nil { | ||
c.String(http.StatusInternalServerError, "Failed to rename file: %s", err.Error()) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, gin.H{"status": "File renamed successfully"}) | ||
} |