StarlioX/api/controllers/settings.go

78 lines
1.6 KiB
Go

package controllers
import (
"database/sql"
"github.com/Redume/EveryNasa/api/utils"
"github.com/Redume/EveryNasa/functions"
"github.com/gofiber/fiber/v2"
_ "github.com/mattn/go-sqlite3"
)
var SettingsGet = func(c *fiber.Ctx) error {
db, err := sql.Open("sqlite3", "EveryNasa.db")
if err != nil {
functions.Logger(err.Error())
}
querySettings, err := db.Query("SELECT * FROM settings")
if err != nil {
functions.Logger(err.Error())
}
defer func(query *sql.Rows) {
err := query.Close()
if err != nil {
functions.Logger(err.Error())
}
}(querySettings)
var startup, wallpaper int
for querySettings.Next() {
err := querySettings.Scan(&startup, &wallpaper)
if err != nil {
functions.Logger(err.Error())
}
var data = map[string]interface{}{"startup": startup, "wallpaper": wallpaper}
utils.Respond(c, data)
}
return nil
}
var SettingsUpdate = func(c *fiber.Ctx) error {
db, err := sql.Open("sqlite3", "EveryNasa.db")
if err != nil {
functions.Logger(err.Error())
}
startup := c.FormValue("startup")
wallpaper := c.FormValue("wallpaper")
if startup == "" && wallpaper == "" {
utils.Respond(c, utils.Message(false, "All fields are required."))
return nil
}
if wallpaper != "" {
_, err := db.Exec("UPDATE settings SET wallpaper = ?", wallpaper)
if err != nil {
functions.Logger(err.Error())
}
if wallpaper == "1" {
go functions.StartWallpaper()
}
}
if startup != "" {
_, err := db.Exec("UPDATE settings SET startup = ?", startup)
if err != nil {
functions.Logger(err.Error())
}
}
utils.Respond(c, utils.Message(true, "The settings have been applied successfully."))
return nil
}