2022-09-18 19:28:56 +03:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2022-10-02 12:21:17 +03:00
|
|
|
"fmt"
|
2022-09-18 19:28:56 +03:00
|
|
|
"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-10-02 12:21:17 +03:00
|
|
|
var startup, wallpaper int
|
2022-09-18 19:28:56 +03:00
|
|
|
|
|
|
|
for query.Next() {
|
2022-10-02 12:21:17 +03:00
|
|
|
err := query.Scan(&startup, &wallpaper)
|
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-10-02 12:21:17 +03:00
|
|
|
var data = map[string]interface{}{"startup": startup, "wallpaper": wallpaper}
|
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-10-02 12:21:17 +03:00
|
|
|
wallpaper := r.FormValue("wallpaper")
|
2022-09-25 11:04:32 +03:00
|
|
|
startup := r.FormValue("startup")
|
2022-09-18 19:28:56 +03:00
|
|
|
|
2022-10-02 12:21:17 +03:00
|
|
|
if startup == "" && wallpaper == "" {
|
2022-09-25 11:04:32 +03:00
|
|
|
utils.Respond(w, utils.Message(false, "All fields are required."))
|
2022-09-18 19:28:56 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-02 12:21:17 +03:00
|
|
|
if wallpaper != "" {
|
|
|
|
_, err := db.Exec("UPDATE settings SET wallpaper = ?", wallpaper)
|
2022-09-19 16:55:29 +03:00
|
|
|
if err != nil {
|
2022-09-25 11:04:32 +03:00
|
|
|
functions.Logger(err.Error())
|
2022-09-19 16:55:29 +03:00
|
|
|
}
|
|
|
|
|
2022-10-02 12:21:17 +03:00
|
|
|
if wallpaper == "1" {
|
2022-10-09 12:03:53 +03:00
|
|
|
fmt.Println("Wallpaper enabled")
|
2022-09-19 16:55:29 +03:00
|
|
|
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
|
|
|
}
|