diff --git a/api/controllers/favorite.go b/api/controllers/favorite.go new file mode 100644 index 0000000..80fca3f --- /dev/null +++ b/api/controllers/favorite.go @@ -0,0 +1,133 @@ +package controllers + +import ( + "database/sql" + "fmt" + "github.com/Redume/EveryNasa/api/utils" + "github.com/Redume/EveryNasa/functions" + "github.com/gofiber/fiber/v2" +) + +var GetFavorites = func(c *fiber.Ctx) error { + FavoriteTitle := c.Query("title") + + type Favorite struct { + Title string `json:"title"` + Explanation string `json:"explanation"` + Date string `json:"date"` + URL string `json:"url"` + HDURL string `json:"hdurl"` + MediaType string `json:"media_type"` + } + + var title, explanation, date, url, hdurl, media_type string + db := functions.GetDatabase() + + if FavoriteTitle != "" { + queryFavorites, err := db.Query("SELECT * FROM favorite WHERE title LIKE ?", FavoriteTitle) + if err != nil { + functions.Logger(err.Error()) + } + + defer func(query *sql.Rows) { + err := query.Close() + if err != nil { + functions.Logger(err.Error()) + } + }(queryFavorites) + + for queryFavorites.Next() { + err := queryFavorites.Scan(&title, &explanation, &date, &url, &hdurl, &media_type) + if err != nil { + functions.Logger(err.Error()) + } + + return c.JSON(fiber.Map{ + "title": title, + "explanation": explanation, + "date": date, + "url": url, + "hdurl": hdurl, + "media_type": media_type, + }) + } + } else { + queryFavorite, err := db.Query("SELECT * FROM favorite") + if err != nil { + functions.Logger(err.Error()) + } + + defer func(query *sql.Rows) { + err := query.Close() + if err != nil { + functions.Logger(err.Error()) + } + }(queryFavorite) + var favorites []Favorite + for queryFavorite.Next() { + err := queryFavorite.Scan(&title, &explanation, &date, &url, &hdurl, &media_type) + if err != nil { + functions.Logger(err.Error()) + } + + favorites = append(favorites, Favorite{ + Title: title, + Explanation: explanation, + Date: date, + URL: url, + HDURL: hdurl, + MediaType: media_type}) + } + + return c.JSON(favorites) + } + + return c.SendString("No favorites found") +} + +var AddFavorite = func(c *fiber.Ctx) error { + title := c.FormValue("title") + explanation := c.FormValue("explanation") + date := c.FormValue("date") + url := c.FormValue("url") + hdurl := c.FormValue("hdurl") + media_type := c.FormValue("media_type") + + fmt.Println(title, explanation, date, url, hdurl, media_type) + + if title == "" && explanation == "" && date == "" && url == "" && hdurl == "" && media_type == "" { + utils.Respond(c, utils.Message(false, "All fields are required")) + return nil + } + + db := functions.GetDatabase() + + _, err := db.Exec("INSERT INTO favorite (title, explanation, date, url, hdurl, media_type) VALUES (?, ?, ?, ?, ?, ?)", + title, + explanation, + date, + url, + hdurl, + media_type) + + if err != nil { + functions.Logger(err.Error()) + } + + utils.Respond(c, utils.Message(true, "Favorite added")) + return nil +} + +var DeleteFavorite = func(c *fiber.Ctx) error { + title := c.FormValue("title") + + db := functions.GetDatabase() + + _, err := db.Exec("DELETE FROM favorite WHERE title = ?", title) + if err != nil { + functions.Logger(err.Error()) + } + + utils.Respond(c, utils.Message(true, "Favorite deleted")) + return nil +} diff --git a/web/page/favorite.go b/web/page/favorite.go new file mode 100644 index 0000000..22de2f5 --- /dev/null +++ b/web/page/favorite.go @@ -0,0 +1,15 @@ +package page + +import ( + "github.com/Redume/EveryNasa/functions" + "github.com/gofiber/fiber/v2" +) + +func Favorite(c *fiber.Ctx) error { + con := functions.Connected() + if con == false { + return c.SendFile("./web/errors/500.html") + } + + return c.SendFile("./web/src/favorite.html") +} diff --git a/web/src/favorite.html b/web/src/favorite.html new file mode 100644 index 0000000..e3f3946 --- /dev/null +++ b/web/src/favorite.html @@ -0,0 +1,98 @@ + + + + + EveryNasa + + + + + + + + +
+
+
+
+ +
+ + + + + + + + + \ No newline at end of file diff --git a/web/static/style/favorite.css b/web/static/style/favorite.css new file mode 100644 index 0000000..4a8788d --- /dev/null +++ b/web/static/style/favorite.css @@ -0,0 +1,9 @@ +body { + background-color: #131313; +} + +lottie-player { + width: 30%; + height: 30%; + margin-top: -10%; +} \ No newline at end of file