change name folder, project file optimization

This commit is contained in:
Данил 2023-03-18 16:40:05 +03:00
parent b9ffe9611e
commit 3463c8af8c
No known key found for this signature in database
GPG key ID: BB6531316DD682D0
9 changed files with 43 additions and 57 deletions

View file

@ -2,8 +2,7 @@ package controllers
import ( import (
"database/sql" "database/sql"
"github.com/Redume/EveryNasa/api/utils" "github.com/Redume/EveryNasa/utils"
"github.com/Redume/EveryNasa/functions"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
) )
@ -21,25 +20,25 @@ var GetFavorites = func(c *fiber.Ctx) error {
} }
var title, explanation, copyright, date, url, hdurl, media_type string var title, explanation, copyright, date, url, hdurl, media_type string
db := functions.GetDatabase() db := utils.GetDatabase()
if FavoriteTitle != "" { if FavoriteTitle != "" {
queryFavorites, err := db.Query("SELECT * FROM favorite WHERE title LIKE ?", FavoriteTitle) queryFavorites, err := db.Query("SELECT * FROM favorite WHERE title LIKE ?", FavoriteTitle)
if err != nil { if err != nil {
functions.Logger(err.Error()) utils.Logger(err.Error())
} }
defer func(query *sql.Rows) { defer func(query *sql.Rows) {
err := query.Close() err := query.Close()
if err != nil { if err != nil {
functions.Logger(err.Error()) utils.Logger(err.Error())
} }
}(queryFavorites) }(queryFavorites)
for queryFavorites.Next() { for queryFavorites.Next() {
err := queryFavorites.Scan(&title, &explanation, &copyright, &date, &url, &hdurl, &media_type) err := queryFavorites.Scan(&title, &explanation, &copyright, &date, &url, &hdurl, &media_type)
if err != nil { if err != nil {
functions.Logger(err.Error()) utils.Logger(err.Error())
} }
return c.JSON(fiber.Map{ return c.JSON(fiber.Map{
@ -55,13 +54,13 @@ var GetFavorites = func(c *fiber.Ctx) error {
} else { } else {
queryFavorite, err := db.Query("SELECT * FROM favorite") queryFavorite, err := db.Query("SELECT * FROM favorite")
if err != nil { if err != nil {
functions.Logger(err.Error()) utils.Logger(err.Error())
} }
defer func(query *sql.Rows) { defer func(query *sql.Rows) {
err := query.Close() err := query.Close()
if err != nil { if err != nil {
functions.Logger(err.Error()) utils.Logger(err.Error())
} }
}(queryFavorite) }(queryFavorite)
@ -69,7 +68,7 @@ var GetFavorites = func(c *fiber.Ctx) error {
for queryFavorite.Next() { for queryFavorite.Next() {
err := queryFavorite.Scan(&title, &explanation, &copyright, &date, &url, &hdurl, &media_type) err := queryFavorite.Scan(&title, &explanation, &copyright, &date, &url, &hdurl, &media_type)
if err != nil { if err != nil {
functions.Logger(err.Error()) utils.Logger(err.Error())
} }
favorites = append(favorites, Favorite{ favorites = append(favorites, Favorite{
@ -102,7 +101,7 @@ var AddFavorite = func(c *fiber.Ctx) error {
return nil return nil
} }
db := functions.GetDatabase() db := utils.GetDatabase()
_, err := db.Exec("INSERT INTO favorite (title, explanation, copyright, date, url, hdurl, media_type) VALUES (?, ?, ?, ?, ?, ?, ?)", _, err := db.Exec("INSERT INTO favorite (title, explanation, copyright, date, url, hdurl, media_type) VALUES (?, ?, ?, ?, ?, ?, ?)",
title, title,
@ -114,7 +113,7 @@ var AddFavorite = func(c *fiber.Ctx) error {
media_type) media_type)
if err != nil { if err != nil {
functions.Logger(err.Error()) utils.Logger(err.Error())
} }
utils.Respond(c, utils.Message(true, "Favorite added")) utils.Respond(c, utils.Message(true, "Favorite added"))
@ -124,11 +123,11 @@ var AddFavorite = func(c *fiber.Ctx) error {
var DeleteFavorite = func(c *fiber.Ctx) error { var DeleteFavorite = func(c *fiber.Ctx) error {
title := c.FormValue("title") title := c.FormValue("title")
db := functions.GetDatabase() db := utils.GetDatabase()
_, err := db.Exec("DELETE FROM favorite WHERE title = ?", title) _, err := db.Exec("DELETE FROM favorite WHERE title = ?", title)
if err != nil { if err != nil {
functions.Logger(err.Error()) utils.Logger(err.Error())
} }
utils.Respond(c, utils.Message(true, "Favorite deleted")) utils.Respond(c, utils.Message(true, "Favorite deleted"))

View file

@ -2,8 +2,7 @@ package controllers
import ( import (
"database/sql" "database/sql"
"github.com/Redume/EveryNasa/api/utils" "github.com/Redume/EveryNasa/utils"
"github.com/Redume/EveryNasa/functions"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
) )
@ -11,35 +10,35 @@ import (
var SettingsGet = func(c *fiber.Ctx) error { var SettingsGet = func(c *fiber.Ctx) error {
db, err := sql.Open("sqlite3", "EveryNasa.db") db, err := sql.Open("sqlite3", "EveryNasa.db")
if err != nil { if err != nil {
functions.Logger(err.Error()) utils.Logger(err.Error())
} }
querySettings, err := db.Query("SELECT * FROM settings") querySettings, err := db.Query("SELECT * FROM settings")
if err != nil { if err != nil {
functions.Logger(err.Error()) utils.Logger(err.Error())
} }
defer func(query *sql.Rows) { defer func(query *sql.Rows) {
err := query.Close() err := query.Close()
if err != nil { if err != nil {
functions.Logger(err.Error()) utils.Logger(err.Error())
} }
}(querySettings) }(querySettings)
var startup, wallpaper, save_logg, analytics int var startup, wallpaper, save_logg int
for querySettings.Next() { for querySettings.Next() {
err := querySettings.Scan(&startup, &wallpaper, &save_logg, &analytics) err := querySettings.Scan(&startup, &wallpaper, &save_logg)
if err != nil { if err != nil {
functions.Logger(err.Error()) utils.Logger(err.Error())
} }
var data = map[string]interface{}{ var data = map[string]interface{}{
"startup": startup, "startup": startup,
"wallpaper": wallpaper, "wallpaper": wallpaper,
"save_logg": save_logg, "save_logg": save_logg,
"analytics": analytics} }
utils.Respond(c, data) utils.Respond(c, data)
} }
@ -50,15 +49,14 @@ var SettingsGet = func(c *fiber.Ctx) error {
var SettingsUpdate = func(c *fiber.Ctx) error { var SettingsUpdate = func(c *fiber.Ctx) error {
db, err := sql.Open("sqlite3", "EveryNasa.db") db, err := sql.Open("sqlite3", "EveryNasa.db")
if err != nil { if err != nil {
functions.Logger(err.Error()) utils.Logger(err.Error())
} }
startup := c.FormValue("startup") startup := c.FormValue("startup")
wallpaper := c.FormValue("wallpaper") wallpaper := c.FormValue("wallpaper")
save_logg := c.FormValue("save_logg") save_logg := c.FormValue("save_logg")
analytics := c.FormValue("analytics")
if startup == "" && wallpaper == "" && save_logg == "" && analytics == "" { if startup == "" && wallpaper == "" && save_logg == "" {
utils.Respond(c, utils.Message(false, "All fields are required.")) utils.Respond(c, utils.Message(false, "All fields are required."))
return nil return nil
} }
@ -66,32 +64,25 @@ var SettingsUpdate = func(c *fiber.Ctx) error {
if wallpaper != "" { if wallpaper != "" {
_, err := db.Exec("UPDATE settings SET wallpaper = ?", wallpaper) _, err := db.Exec("UPDATE settings SET wallpaper = ?", wallpaper)
if err != nil { if err != nil {
functions.Logger(err.Error()) utils.Logger(err.Error())
} }
if wallpaper == "1" { if wallpaper == "1" {
go functions.StartWallpaper() go utils.StartWallpaper()
} }
} }
if startup != "" { if startup != "" {
_, err := db.Exec("UPDATE settings SET startup = ?", startup) _, err := db.Exec("UPDATE settings SET startup = ?", startup)
if err != nil { if err != nil {
functions.Logger(err.Error()) utils.Logger(err.Error())
} }
} }
if save_logg != "" { if save_logg != "" {
_, err := db.Exec("UPDATE settings SET save_logg = ?", save_logg) _, err := db.Exec("UPDATE settings SET save_logg = ?", save_logg)
if err != nil { if err != nil {
functions.Logger(err.Error()) utils.Logger(err.Error())
}
}
if analytics != "" {
_, err := db.Exec("UPDATE settings SET analytics = ?", analytics)
if err != nil {
functions.Logger(err.Error())
} }
} }

View file

@ -6,8 +6,7 @@ import (
"os/user" "os/user"
"strings" "strings"
"github.com/Redume/EveryNasa/api/utils" "github.com/Redume/EveryNasa/utils"
"github.com/Redume/EveryNasa/functions"
) )
var Startup = func(c *fiber.Ctx) error { var Startup = func(c *fiber.Ctx) error {
@ -32,19 +31,19 @@ var Startup = func(c *fiber.Ctx) error {
var SetStartup = func(c *fiber.Ctx) error { var SetStartup = func(c *fiber.Ctx) error {
u, err := user.Current() u, err := user.Current()
if err != nil { if err != nil {
functions.Logger(err.Error()) utils.Logger(err.Error())
} }
dir, err := os.Getwd() dir, err := os.Getwd()
if err != nil { if err != nil {
functions.Logger(err.Error()) utils.Logger(err.Error())
} }
dir = strings.Replace(dir, "\\", "\\\\", -1) + "\\EveryNasa.exe" dir = strings.Replace(dir, "\\", "\\\\", -1) + "\\EveryNasa.exe"
err = functions.CreateLnk(dir, strings.Replace(u.HomeDir, "\\", "\\\\", -1)+"\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\EveryNasa.lnk") err = utils.CreateLnk(dir, strings.Replace(u.HomeDir, "\\", "\\\\", -1)+"\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\EveryNasa.lnk")
if err != nil { if err != nil {
functions.Logger(err.Error()) utils.Logger(err.Error())
} }
utils.Respond(c, utils.Message(true, "The settings have been applied successfully.")) utils.Respond(c, utils.Message(true, "The settings have been applied successfully."))
@ -54,12 +53,12 @@ var SetStartup = func(c *fiber.Ctx) error {
var RemoveStartup = func(c *fiber.Ctx) error { var RemoveStartup = func(c *fiber.Ctx) error {
u, err := user.Current() u, err := user.Current()
if err != nil { if err != nil {
functions.Logger(err.Error()) utils.Logger(err.Error())
} }
err = os.Remove(strings.Replace(u.HomeDir, "\\", "\\\\", -1) + "\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\EveryNasa.lnk") err = os.Remove(strings.Replace(u.HomeDir, "\\", "\\\\", -1) + "\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\EveryNasa.lnk")
if err != nil { if err != nil {
functions.Logger(err.Error()) utils.Logger(err.Error())
} }
utils.Respond(c, utils.Message(true, "The settings have been applied successfully.")) utils.Respond(c, utils.Message(true, "The settings have been applied successfully."))

View file

@ -1,8 +1,7 @@
package controllers package controllers
import ( import (
"github.com/Redume/EveryNasa/api/utils" "github.com/Redume/EveryNasa/utils"
"github.com/Redume/EveryNasa/functions"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"github.com/reujab/wallpaper" "github.com/reujab/wallpaper"
) )
@ -17,7 +16,7 @@ var WallpaperUpdate = func(c *fiber.Ctx) error {
err := wallpaper.SetFromURL(url) err := wallpaper.SetFromURL(url)
if err != nil { if err != nil {
functions.Logger(err.Error()) utils.Logger(err.Error())
utils.Respond(c, utils.Message(false, "An error occurred while setting the wallpaper")) utils.Respond(c, utils.Message(false, "An error occurred while setting the wallpaper"))
return nil return nil
} }

View file

@ -1,4 +1,4 @@
package functions package utils
import "net/http" import "net/http"

View file

@ -1,4 +1,4 @@
package functions package utils
import ( import (
"os" "os"

View file

@ -1,4 +1,4 @@
package functions package utils
import ( import (
"database/sql" "database/sql"
@ -16,8 +16,7 @@ func Database() {
CREATE TABLE IF NOT EXISTS settings ( CREATE TABLE IF NOT EXISTS settings (
startup INTEGER DEFAULT 0, startup INTEGER DEFAULT 0,
wallpaper INTEGER DEFAULT 0, wallpaper INTEGER DEFAULT 0,
save_logg INTEGER DEFAULT 1, save_logg INTEGER DEFAULT 1
analytics INTEGER DEFAULT 1
);` );`
_, err = db.Exec(sqlTable) _, err = db.Exec(sqlTable)
@ -25,12 +24,12 @@ func Database() {
Logger(err.Error()) Logger(err.Error())
} }
stmt, err := db.Prepare("INSERT INTO settings(startup, wallpaper, save_logg, analytics) values(?,?,?,?)") stmt, err := db.Prepare("INSERT INTO settings(startup, wallpaper, save_logg) values(?,?,?)")
if err != nil { if err != nil {
Logger(err.Error()) Logger(err.Error())
} }
_, err = stmt.Exec(0, 0, 1, 1) _, err = stmt.Exec(0, 0, 1)
if err != nil { if err != nil {
Logger(err.Error()) Logger(err.Error())
} }

View file

@ -3,7 +3,6 @@ package utils
import ( import (
"encoding/json" "encoding/json"
"github.com/Redume/EveryNasa/functions"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
) )
@ -15,6 +14,6 @@ func Respond(c *fiber.Ctx, data map[string]interface{}) {
c.Set("Content-Type", "application/json") c.Set("Content-Type", "application/json")
err := json.NewEncoder(c).Encode(data) err := json.NewEncoder(c).Encode(data)
if err != nil { if err != nil {
functions.Logger(err.Error()) Logger(err.Error())
} }
} }

View file

@ -1,4 +1,4 @@
package functions package utils
import ( import (
"encoding/json" "encoding/json"