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

View file

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

View file

@ -6,8 +6,7 @@ import (
"os/user"
"strings"
"github.com/Redume/EveryNasa/api/utils"
"github.com/Redume/EveryNasa/functions"
"github.com/Redume/EveryNasa/utils"
)
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 {
u, err := user.Current()
if err != nil {
functions.Logger(err.Error())
utils.Logger(err.Error())
}
dir, err := os.Getwd()
if err != nil {
functions.Logger(err.Error())
utils.Logger(err.Error())
}
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 {
functions.Logger(err.Error())
utils.Logger(err.Error())
}
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 {
u, err := user.Current()
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")
if err != nil {
functions.Logger(err.Error())
utils.Logger(err.Error())
}
utils.Respond(c, utils.Message(true, "The settings have been applied successfully."))

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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