feat: 🎉 Rating Orama!

This commit is contained in:
2023-04-09 06:45:27 +02:00
commit b2f4b573f3
61 changed files with 8130 additions and 0 deletions
+25
View File
@@ -0,0 +1,25 @@
package handlers
import (
"github.com/jackc/pgx/v5/pgxpool"
"github.com/zepyrshut/rating-orama/app"
"github.com/zepyrshut/rating-orama/repository"
)
type Repository struct {
DB repository.DBRepo
App *app.Application
}
var Repo *Repository
func NewRepo(db *pgxpool.Pool, app *app.Application) *Repository {
return &Repository{
DB: repository.NewPostgresRepo(db),
App: app,
}
}
func NewHandlers(r *Repository) {
Repo = r
}
+28
View File
@@ -0,0 +1,28 @@
package handlers
import (
"github.com/gofiber/fiber/v2"
)
func (rp Repository) Index(c *fiber.Ctx) error {
return c.Render("index", fiber.Map{
"Title": "Template engine is working! We are in the index page!",
"SomeData": []string{"Some", "Data", "Here"},
})
}
func (rp Repository) Another(c *fiber.Ctx) error {
return c.Render("another", fiber.Map{
"Title": "Template engine is working! We are in the another page!",
"SomeData": []string{"Some", "Data", "Here"},
})
}
func (rp Repository) Ping(c *fiber.Ctx) error {
rp.App.Info("Ping!")
return c.SendString("Pong!")
}
func (rp Repository) Panic(c *fiber.Ctx) error {
panic("Panic!")
}
+55
View File
@@ -0,0 +1,55 @@
package handlers
import (
"encoding/json"
"fmt"
"github.com/gofiber/fiber/v2"
"github.com/zepyrshut/rating-orama/models"
"io"
"net/http"
)
func (rp Repository) GetAllChapters(c *fiber.Ctx) error {
tvShow := models.TvShow{}
ttShowID := c.Query("id")
if ttShowID[0:2] == "tt" {
ttShowID = ttShowID[2:]
}
exist := rp.DB.CheckIfTvShowExists(ttShowID)
if !exist {
url := fmt.Sprintf(rp.App.Environment.HarvesterApi, ttShowID)
response, _ := http.Get(url)
body, _ := io.ReadAll(response.Body)
err := json.Unmarshal(body, &tvShow)
if err != nil {
rp.App.Error(err.Error())
return c.Status(http.StatusInternalServerError).JSON(err)
}
err = rp.DB.InsertTvShow(tvShow)
if err != nil {
rp.App.Error(err.Error())
return c.Status(http.StatusInternalServerError).JSON(err)
}
}
tvShow, err := rp.DB.FetchTvShow(ttShowID)
if err != nil {
rp.App.Error(err.Error())
return c.Status(http.StatusInternalServerError).JSON(err)
}
tvShowJSON, err := json.Marshal(tvShow)
if err != nil {
rp.App.Error(err.Error())
return c.Status(http.StatusInternalServerError).JSON(err)
}
return c.Render("charts", fiber.Map{
"TvShow": tvShow,
"TvShowJSON": string(tvShowJSON),
})
}