feat: 🎉 Rating Orama!
This commit is contained in:
@@ -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
|
||||
}
|
||||
@@ -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!")
|
||||
}
|
||||
@@ -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),
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user