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"
|
2022-10-23 15:27:36 +03:00
|
|
|
"github.com/gofiber/fiber/v2"
|
2022-09-18 19:28:56 +03:00
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
)
|
|
|
|
|
2022-10-23 15:27:36 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-10-23 15:27:36 +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
|
|
|
}
|
2022-10-23 15:27:36 +03:00
|
|
|
}(querySettings)
|
2022-09-18 19:28:56 +03:00
|
|
|
|
2022-11-22 21:36:35 +03:00
|
|
|
var startup, wallpaper, save_logg, analytics int
|
2022-09-18 19:28:56 +03:00
|
|
|
|
2022-10-23 15:27:36 +03:00
|
|
|
for querySettings.Next() {
|
2022-11-22 21:36:35 +03:00
|
|
|
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,
|
2022-11-22 21:36:35 +03:00
|
|
|
"wallpaper": wallpaper,
|
|
|
|
"save_logg": save_logg,
|
|
|
|
"analytics": analytics}
|
2022-12-15 21:10:57 +03:00
|
|
|
|
2022-10-23 15:27:36 +03:00
|
|
|
utils.Respond(c, data)
|
2022-09-18 19:28:56 +03:00
|
|
|
}
|
2022-10-23 15:27:36 +03:00
|
|
|
|
|
|
|
return nil
|
2022-09-18 19:28:56 +03:00
|
|
|
}
|
|
|
|
|
2022-10-23 15:27:36 +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
|
|
|
}
|
|
|
|
|
2022-10-23 15:27:36 +03:00
|
|
|
startup := c.FormValue("startup")
|
|
|
|
wallpaper := c.FormValue("wallpaper")
|
2022-11-21 22:22:00 +03:00
|
|
|
save_logg := c.FormValue("save_logg")
|
2022-11-22 21:36:35 +03:00
|
|
|
analytics := c.FormValue("analytics")
|
2022-09-18 19:28:56 +03:00
|
|
|
|
2022-11-22 21:36:35 +03:00
|
|
|
if startup == "" && wallpaper == "" && save_logg == "" && analytics == "" {
|
2022-10-23 15:27:36 +03:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-21 22:22:00 +03:00
|
|
|
if save_logg != "" {
|
|
|
|
_, err := db.Exec("UPDATE settings SET save_logg = ?", save_logg)
|
|
|
|
if err != nil {
|
|
|
|
functions.Logger(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-22 21:36:35 +03:00
|
|
|
if analytics != "" {
|
|
|
|
_, err := db.Exec("UPDATE settings SET analytics = ?", analytics)
|
|
|
|
if err != nil {
|
|
|
|
functions.Logger(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-23 15:27:36 +03:00
|
|
|
utils.Respond(c, utils.Message(true, "The settings have been applied successfully."))
|
|
|
|
return nil
|
2022-09-18 19:28:56 +03:00
|
|
|
}
|