2022-09-18 19:28:56 +03:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2022-09-19 16:55:29 +03:00
|
|
|
"github.com/Redume/EveryNasa/functions"
|
2022-09-18 19:28:56 +03:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/Redume/EveryNasa/api/utils"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
)
|
|
|
|
|
|
|
|
var SettingsGet = func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
db, errOpen := sql.Open("sqlite3", "EveryNasa.db")
|
|
|
|
if errOpen != nil {
|
|
|
|
panic(errOpen)
|
|
|
|
}
|
|
|
|
|
|
|
|
query, errQuery := db.Query("SELECT * FROM settings")
|
|
|
|
if errQuery != nil {
|
|
|
|
panic(errQuery)
|
|
|
|
}
|
|
|
|
|
2022-09-19 02:07:40 +03:00
|
|
|
defer func(query *sql.Rows) {
|
|
|
|
err := query.Close()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}(query)
|
2022-09-18 19:28:56 +03:00
|
|
|
|
2022-09-21 10:47:18 +03:00
|
|
|
var autostart, autochangewallpaper int
|
2022-09-18 19:28:56 +03:00
|
|
|
|
|
|
|
for query.Next() {
|
2022-09-21 10:47:18 +03:00
|
|
|
err := query.Scan(&autostart, &autochangewallpaper)
|
2022-09-18 19:28:56 +03:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-09-21 10:47:18 +03:00
|
|
|
var data = map[string]interface{}{"autostart": autostart, "autochangewallpaper": autochangewallpaper}
|
2022-09-18 19:28:56 +03:00
|
|
|
utils.Respond(w, data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var SettingsUpdate = func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
db, errOpen := sql.Open("sqlite3", "EveryNasa.db")
|
|
|
|
if errOpen != nil {
|
|
|
|
panic(errOpen)
|
|
|
|
}
|
|
|
|
|
|
|
|
autostart := r.FormValue("autostart")
|
2022-09-19 16:55:29 +03:00
|
|
|
autochangewallpaper := r.FormValue("autochangewallpaper")
|
2022-09-18 19:28:56 +03:00
|
|
|
|
2022-09-21 10:47:18 +03:00
|
|
|
if autostart == "" && autochangewallpaper == "" {
|
2022-09-18 19:28:56 +03:00
|
|
|
utils.Respond(w, utils.Message(false, "All fields are required"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if autostart != "" {
|
|
|
|
_, err := db.Exec("UPDATE settings SET autostart = ?", autostart)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-19 16:55:29 +03:00
|
|
|
if autochangewallpaper != "" {
|
|
|
|
_, err := db.Exec("UPDATE settings SET autochangewallpaper = ?", autochangewallpaper)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if autochangewallpaper == "1" {
|
|
|
|
go functions.StartWallpaper()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-18 19:28:56 +03:00
|
|
|
utils.Respond(w, utils.Message(true, "Settings updated"))
|
|
|
|
}
|