StarlioX/api/controllers/settings.go

79 lines
1.6 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
2022-10-02 12:21:17 +03:00
var startup, wallpaper int
2022-09-18 19:28:56 +03:00
for querySettings.Next() {
err := querySettings.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}
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")
2022-09-18 19:28:56 +03:00
2022-10-02 12:21:17 +03:00
if startup == "" && wallpaper == "" {
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())
}
}
utils.Respond(c, utils.Message(true, "The settings have been applied successfully."))
return nil
2022-09-18 19:28:56 +03:00
}