StarlioX/api/controllers/settings.go

101 lines
2.1 KiB
Go
Raw Normal View History

2022-09-18 19:28:56 +03:00
package controllers
import (
"database/sql"
"github.com/Redume/EveryNasa/api/utils"
2022-09-25 11:04:32 +03:00
"github.com/Redume/EveryNasa/functions"
"github.com/gofiber/fiber/v2"
2022-09-18 19:28:56 +03:00
_ "github.com/mattn/go-sqlite3"
)
var SettingsGet = func(c *fiber.Ctx) error {
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
}
querySettings, err := db.Query("SELECT * FROM settings")
2022-09-25 11:04:32 +03:00
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
}
}(querySettings)
2022-09-18 19:28:56 +03:00
var startup, wallpaper, save_logg, analytics int
2022-09-18 19:28:56 +03:00
for querySettings.Next() {
err := querySettings.Scan(&startup, &wallpaper, &save_logg, &analytics)
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-12-15 21:10:57 +03:00
var data = map[string]interface{}{
"startup": startup,
"wallpaper": wallpaper,
"save_logg": save_logg,
"analytics": analytics}
2022-12-15 21:10:57 +03:00
utils.Respond(c, data)
2022-09-18 19:28:56 +03:00
}
return nil
2022-09-18 19:28:56 +03:00
}
var SettingsUpdate = func(c *fiber.Ctx) error {
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
}
startup := c.FormValue("startup")
wallpaper := c.FormValue("wallpaper")
save_logg := c.FormValue("save_logg")
analytics := c.FormValue("analytics")
2022-09-18 19:28:56 +03:00
if startup == "" && wallpaper == "" && save_logg == "" && analytics == "" {
utils.Respond(c, utils.Message(false, "All fields are required."))
return nil
2022-09-18 19:28:56 +03:00
}
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-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())
}
}
if save_logg != "" {
_, err := db.Exec("UPDATE settings SET save_logg = ?", save_logg)
if err != nil {
functions.Logger(err.Error())
}
}
if analytics != "" {
_, err := db.Exec("UPDATE settings SET analytics = ?", analytics)
if err != nil {
functions.Logger(err.Error())
}
}
utils.Respond(c, utils.Message(true, "The settings have been applied successfully."))
return nil
2022-09-18 19:28:56 +03:00
}