+ 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"`
}
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
func getViewsFilePath() (string, error) {
// Get the path to the storage then append the const name for the storage file
@ -94,7 +101,7 @@ func LoadViewsCache() error {
return nil
}
func AddViewToPost(postName string) uint32 {
func AddViewToPost(postName string, incrementViews bool) uint32 {
// Lock the viewers mapping
viewersLock.Lock()
@ -104,8 +111,10 @@ func AddViewToPost(postName string) uint32 {
ViewsCache[postName] = 0
}
if incrementViews {
// Add to the counter
ViewsCache[postName]++
}
// Unlock
viewersLock.Unlock()
@ -175,7 +184,7 @@ func getStorageDirectory() string {
}
// 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
storageDir := getStorageDirectory()
@ -183,7 +192,7 @@ func GetPost(fileName string) Post {
filePath := fmt.Sprintf("%s%s", storageDir, fileName)
// Get the post views and add 1 to them
postViews := AddViewToPost(fileName)
postViews := AddViewToPost(fileName, incrementViews)
p := Post{
Name: fileName,
@ -316,7 +325,7 @@ func GetAllPosts() []Post {
// Go through all of the files
for _, v := range files {
// Process the file into a pad
postList = append(postList, GetPost(v.Name()))
postList = append(postList, GetPost(v.Name(), false))
}
// Return the post list

View File

@ -64,7 +64,18 @@ func AdminRoutes(router *gin.RouterGroup) {
})
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
@ -73,8 +84,6 @@ func AdminRoutes(router *gin.RouterGroup) {
// Get all of the pads as a listing
padList := objects.GetAllPosts()
fmt.Println(padList)
ctx.HTML(200, "admin_view.html", gin.H{
"title": "Admin",
"padList": padList,

View File

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

View File

@ -37,7 +37,10 @@
<div class="pad-name col-5">
Pad Name
</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
</div>
<div class="col-2">
@ -54,11 +57,14 @@
{{ $element.Name }}
</a>
</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 }}
</div>
<div class="col-2">
<div onclick="doDelete(`{{ $element.Name }}`)" class="btn btn-danger">
<div onclick="doDelete({{ $element.Name }})" class="btn btn-danger">
Delete
</div>
</div>