+ Deletion implemented

This commit is contained in:
Daniel Legt 2022-06-03 23:15:20 +03:00
parent faff1ab527
commit bf1d032e68
4 changed files with 36 additions and 12 deletions

View File

@ -26,6 +26,13 @@ type Post struct {
Views uint32 `json:"views"` Views uint32 `json:"views"`
} }
func (p *Post) Delete() error {
filePath := path.Join(getStorageDirectory(), p.Name)
// Remove the file and return the result
return os.Remove(filePath)
}
// Get the path to the views JSON // Get the path to the views JSON
func getViewsFilePath() (string, error) { func getViewsFilePath() (string, error) {
// Get the path to the storage then append the const name for the storage file // Get the path to the storage then append the const name for the storage file
@ -94,7 +101,7 @@ func LoadViewsCache() error {
return nil return nil
} }
func AddViewToPost(postName string) uint32 { func AddViewToPost(postName string, incrementViews bool) uint32 {
// Lock the viewers mapping // Lock the viewers mapping
viewersLock.Lock() viewersLock.Lock()
@ -104,8 +111,10 @@ func AddViewToPost(postName string) uint32 {
ViewsCache[postName] = 0 ViewsCache[postName] = 0
} }
// Add to the counter if incrementViews {
ViewsCache[postName]++ // Add to the counter
ViewsCache[postName]++
}
// Unlock // Unlock
viewersLock.Unlock() viewersLock.Unlock()
@ -175,7 +184,7 @@ func getStorageDirectory() string {
} }
// Get a post from the file system // Get a post from the file system
func GetPost(fileName string) Post { func GetPost(fileName string, incrementViews bool) Post {
// Get the base storage directory and make sure it exists // Get the base storage directory and make sure it exists
storageDir := getStorageDirectory() storageDir := getStorageDirectory()
@ -183,7 +192,7 @@ func GetPost(fileName string) Post {
filePath := fmt.Sprintf("%s%s", storageDir, fileName) filePath := fmt.Sprintf("%s%s", storageDir, fileName)
// Get the post views and add 1 to them // Get the post views and add 1 to them
postViews := AddViewToPost(fileName) postViews := AddViewToPost(fileName, incrementViews)
p := Post{ p := Post{
Name: fileName, Name: fileName,
@ -316,7 +325,7 @@ func GetAllPosts() []Post {
// Go through all of the files // Go through all of the files
for _, v := range files { for _, v := range files {
// Process the file into a pad // Process the file into a pad
postList = append(postList, GetPost(v.Name())) postList = append(postList, GetPost(v.Name(), false))
} }
// Return the post list // Return the post list

View File

@ -64,7 +64,18 @@ func AdminRoutes(router *gin.RouterGroup) {
}) })
router.GET("/delete/:padname", func(ctx *gin.Context) { router.GET("/delete/:padname", func(ctx *gin.Context) {
// Get the pad name that we bout' to delete
padName := ctx.Param("padname")
// Try and get the pad, check if valid
pad := objects.GetPost(padName, false)
// Delete the pad
err := pad.Delete()
fmt.Println(err)
// Redirect the user to the admin page
ctx.Redirect(http.StatusFound, "/admin/view")
}) })
// Admin view route // Admin view route
@ -73,8 +84,6 @@ func AdminRoutes(router *gin.RouterGroup) {
// Get all of the pads as a listing // Get all of the pads as a listing
padList := objects.GetAllPosts() padList := objects.GetAllPosts()
fmt.Println(padList)
ctx.HTML(200, "admin_view.html", gin.H{ ctx.HTML(200, "admin_view.html", gin.H{
"title": "Admin", "title": "Admin",
"padList": padList, "padList": padList,

View File

@ -41,7 +41,7 @@ func HomeRoutes(router *gin.Engine) {
} }
postName = sanitize.XSS(sanitize.SingleLine(postName)) postName = sanitize.XSS(sanitize.SingleLine(postName))
post := objects.GetPost(postName) post := objects.GetPost(postName, true)
c.HTML(200, "page.html", gin.H{ c.HTML(200, "page.html", gin.H{
"title": postName, "title": postName,

View File

@ -37,7 +37,10 @@
<div class="pad-name col-5"> <div class="pad-name col-5">
Pad Name Pad Name
</div> </div>
<div class="pad-last-modified col-5"> <div class="pad-last-view col-1">
Views
</div>
<div class="pad-last-modified col-4">
Create Date Create Date
</div> </div>
<div class="col-2"> <div class="col-2">
@ -54,11 +57,14 @@
{{ $element.Name }} {{ $element.Name }}
</a> </a>
</div> </div>
<div class="pad-last-modified col-5"> <div class="pad-last-view col-1">
{{ $element.Views }}
</div>
<div class="pad-last-modified col-4">
{{ $element.LastModified }} {{ $element.LastModified }}
</div> </div>
<div class="col-2"> <div class="col-2">
<div onclick="doDelete(`{{ $element.Name }}`)" class="btn btn-danger"> <div onclick="doDelete({{ $element.Name }})" class="btn btn-danger">
Delete Delete
</div> </div>
</div> </div>