Clear table button

This commit is contained in:
aceberg 2023-01-03 00:17:35 +07:00
parent b05ca57cbe
commit 201250e3db
4 changed files with 23 additions and 1 deletions

View file

@ -64,3 +64,9 @@ func Delete(path string, id uint16) {
sqlStatement = fmt.Sprintf(sqlStatement, id)
dbExec(path, sqlStatement)
}
// Clear - delete all hosts from table
func Clear(path string) {
sqlStatement := `DELETE FROM "now";`
dbExec(path, sqlStatement)
}

View file

@ -2,11 +2,13 @@ package web
import (
"html/template"
"log"
"net/http"
"strconv"
"github.com/aceberg/WatchYourLAN/internal/check"
"github.com/aceberg/WatchYourLAN/internal/conf"
"github.com/aceberg/WatchYourLAN/internal/db"
"github.com/aceberg/WatchYourLAN/internal/models"
"github.com/aceberg/WatchYourLAN/internal/scan"
)
@ -50,3 +52,12 @@ func saveConfigHandler(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, r.Header.Get("Referer"), 302)
}
func clearHandler(w http.ResponseWriter, r *http.Request) {
log.Println("INFO: delting all hosts from DB")
db.Clear(AppConfig.DbPath)
http.Redirect(w, r, r.Header.Get("Referer"), 302)
}

View file

@ -50,7 +50,11 @@
<div class="alert alert-info" role="alert">
<h4 class="alert-heading">Warning!</h4>
<p>After changing <b>Host</b> or <b>Port</b> you need to restart the app</p>
</div>
</div>
<br>
<form action="/clear/" method="post">
<button type="submit" class="btn btn-danger">Clear table</button>
</form>
</div>
</div>
</div>

View file

@ -55,6 +55,7 @@ func Gui(configPath string) {
log.Println("=================================== ")
http.HandleFunc("/", indexHandler)
http.HandleFunc("/clear/", clearHandler)
http.HandleFunc("/config/", configHandler)
http.HandleFunc("/del_host/", delHandler)
http.HandleFunc("/home/", homeHandler)