StarlioX/api/controllers/settings.go

76 lines
1.7 KiB
Go
Raw Normal View History

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