StarlioX/api/controllers/settings.go

92 lines
1.8 KiB
Go
Raw Permalink Normal View History

2022-09-18 19:28:56 +03:00
package controllers
import (
"database/sql"
2023-06-05 18:54:14 +03:00
"github.com/Redume/Starlio/utils"
"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 {
2023-06-05 18:54:14 +03:00
db, err := sql.Open("sqlite3", "Starlio.db")
2022-09-25 11:04:32 +03:00
if err != nil {
utils.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 {
utils.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 {
utils.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 int
2022-09-18 19:28:56 +03:00
for querySettings.Next() {
err := querySettings.Scan(&startup, &wallpaper, &save_logg)
2022-09-18 19:28:56 +03:00
if err != nil {
utils.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,
}
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 {
2023-06-05 18:54:14 +03:00
db, err := sql.Open("sqlite3", "Starlio.db")
2022-09-25 11:04:32 +03:00
if err != nil {
utils.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")
2022-09-18 19:28:56 +03:00
if startup == "" && wallpaper == "" && save_logg == "" {
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 {
utils.Logger(err.Error())
2022-09-19 16:55:29 +03:00
}
2022-10-02 12:21:17 +03:00
if wallpaper == "1" {
go utils.StartWallpaper()
2022-09-19 16:55:29 +03:00
}
}
2022-09-25 11:04:32 +03:00
if startup != "" {
_, err := db.Exec("UPDATE settings SET startup = ?", startup)
if err != nil {
utils.Logger(err.Error())
2022-09-25 11:04:32 +03:00
}
}
if save_logg != "" {
_, err := db.Exec("UPDATE settings SET save_logg = ?", save_logg)
if err != nil {
utils.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
}