new framework
This commit is contained in:
@@ -1,91 +1,59 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"context"
|
||||
"net/http"
|
||||
"strings"
|
||||
"ron"
|
||||
|
||||
"gopher-toolbox/config"
|
||||
"gopher-toolbox/app"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/zepyrshut/rating-orama/internal/repository"
|
||||
)
|
||||
|
||||
// TODO: Extract to toolbox
|
||||
const (
|
||||
InvalidRequest string = "invalid_request"
|
||||
InternalError string = "internal_error"
|
||||
RequestID string = "request_id"
|
||||
NotFound string = "not_found"
|
||||
Created string = "created"
|
||||
Updated string = "updated"
|
||||
Deleted string = "deleted"
|
||||
Enabled string = "enabled"
|
||||
Disabled string = "disabled"
|
||||
Retrieved string = "retrieved"
|
||||
ErrorCreating string = "error_creating"
|
||||
ErrorUpdating string = "error_updating"
|
||||
ErrorEnabling string = "error_enabling"
|
||||
ErrorDisabling string = "error_disabling"
|
||||
ErrorGetting string = "error_getting"
|
||||
ErrorGettingAll string = "error_getting_all"
|
||||
InvalidEntityID string = "invalid_entity_id"
|
||||
NotImplemented string = "not_implemented"
|
||||
|
||||
UserUsernameKey string = "user_username_key"
|
||||
UserEmailKey string = "user_email_key"
|
||||
UsernameAlReadyExists string = "username_already_exists"
|
||||
EmailAlreadyExists string = "email_already_exists"
|
||||
IncorrectPassword string = "incorrect_password"
|
||||
ErrorGeneratingToken string = "error_generating_token"
|
||||
LoggedIn string = "logged_in"
|
||||
|
||||
CategoryNameKey string = "category_name_key"
|
||||
CategoryAlreadyExists string = "category_already_exists"
|
||||
|
||||
ItemsNameKey string = "items_name_key"
|
||||
NameAlreadyExists string = "name_already_exists"
|
||||
)
|
||||
|
||||
type Handlers struct {
|
||||
App *config.App
|
||||
Queries repository.ExtendedQuerier
|
||||
app *app.App
|
||||
queries repository.ExtendedQuerier
|
||||
}
|
||||
|
||||
func New(q repository.ExtendedQuerier, app *config.App) *Handlers {
|
||||
func New(app *app.App, q repository.ExtendedQuerier) *Handlers {
|
||||
return &Handlers{
|
||||
Queries: q,
|
||||
App: app,
|
||||
app: app,
|
||||
queries: q,
|
||||
}
|
||||
}
|
||||
|
||||
func (hq *Handlers) ToBeImplemented(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "Not implemented yet",
|
||||
func (hq *Handlers) ToBeImplemented(c *ron.CTX, ctx context.Context) {
|
||||
c.JSON(http.StatusOK, ron.Data{
|
||||
"message": "To be implemented",
|
||||
})
|
||||
}
|
||||
|
||||
<<<<<<< Updated upstream
|
||||
func (hq *Handlers) Ping(c *gin.Context) {
|
||||
slog.Info("ping", RequestID, c.Request.Context().Value(RequestID))
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
=======
|
||||
func (hq *Handlers) Ping(c *ron.CTX, ctx context.Context) {
|
||||
c.JSON(http.StatusOK, ron.Data{
|
||||
>>>>>>> Stashed changes
|
||||
"message": "pong",
|
||||
})
|
||||
}
|
||||
|
||||
// TODO: Extract to toolbox
|
||||
func handleQueryError(c *gin.Context, err error, errorMap map[string]string, logMessage string, defaultErrorMessage string) bool {
|
||||
if err != nil {
|
||||
for key, message := range errorMap {
|
||||
if strings.Contains(err.Error(), key) {
|
||||
slog.Error(logMessage, "error", message, RequestID, c.Request.Context().Value(RequestID))
|
||||
c.JSON(http.StatusConflict, gin.H{"error": message})
|
||||
return true
|
||||
}
|
||||
}
|
||||
// // TODO: Extract to toolbox
|
||||
// func handleQueryError(c *gin.Context, err error, errorMap map[string]string, logMessage string, defaultErrorMessage string) bool {
|
||||
// if err != nil {
|
||||
// for key, message := range errorMap {
|
||||
// if strings.Contains(err.Error(), key) {
|
||||
// slog.Error(logMessage, "error", message, RequestID, c.Request.Context().Value(RequestID))
|
||||
// c.JSON(http.StatusConflict, gin.H{"error": message})
|
||||
// return true
|
||||
// }
|
||||
// }
|
||||
|
||||
slog.Error(logMessage, "error", err.Error(), RequestID, c.Request.Context().Value(RequestID))
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": defaultErrorMessage})
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
// slog.Error(logMessage, "error", err.Error(), RequestID, c.Request.Context().Value(RequestID))
|
||||
// c.JSON(http.StatusInternalServerError, gin.H{"error": defaultErrorMessage})
|
||||
// return true
|
||||
// }
|
||||
// return false
|
||||
// }
|
||||
|
||||
@@ -1,46 +1,73 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"gopher-toolbox/app"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"ron"
|
||||
|
||||
"github.com/zepyrshut/rating-orama/internal/scraper"
|
||||
"github.com/zepyrshut/rating-orama/internal/sqlc"
|
||||
)
|
||||
|
||||
func (hq *Handlers) GetTVShow(c *gin.Context) {
|
||||
func (hq *Handlers) GetTVShow(c *ron.CTX, ctx context.Context) {
|
||||
ttShowID := c.Query("ttid")
|
||||
slog.Info("", "ttid", ttShowID, RequestID, c.Request.Context().Value(RequestID))
|
||||
//slog.Info("", "ttid", ttShowID, RequestID, ctx.Value(RequestID))
|
||||
|
||||
var title string
|
||||
var scraperEpisodes []scraper.Episode
|
||||
var sqlcEpisodes []sqlc.Episode
|
||||
|
||||
tvShow, err := hq.Queries.CheckTVShowExists(c, ttShowID)
|
||||
tvShow, err := hq.queries.CheckTVShowExists(ctx, ttShowID)
|
||||
if err != nil {
|
||||
title, scraperEpisodes = scraper.ScrapeEpisodes(ttShowID)
|
||||
<<<<<<< Updated upstream
|
||||
|
||||
sqlcEpisodes, err = hq.Queries.CreateTvShowWithEpisodes(c, sqlc.CreateTVShowParams{
|
||||
=======
|
||||
// TODO: make transactional
|
||||
ttShow, err := hq.queries.CreateTVShow(ctx, sqlc.CreateTVShowParams{
|
||||
>>>>>>> Stashed changes
|
||||
TtImdb: ttShowID,
|
||||
Name: title,
|
||||
}, scraperEpisodes)
|
||||
if err != nil {
|
||||
<<<<<<< Updated upstream
|
||||
slog.Error("failed to create tv show with episodes", "ttid", ttShowID, "error", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": ErrorCreating})
|
||||
return
|
||||
=======
|
||||
slog.Error("failed to create tv show", "ttid", ttShowID, "error", err)
|
||||
c.JSON(http.StatusInternalServerError, ron.Data{"error": app.ErrorCreating})
|
||||
}
|
||||
|
||||
slog.Info("ttshowid", "id", ttShow.ID)
|
||||
for _, episode := range scraperEpisodes {
|
||||
sqlcEpisodesParams := episode.ToEpisodeParams(ttShow.ID)
|
||||
sqlcEpisode, err := hq.queries.CreateEpisodes(ctx, sqlcEpisodesParams)
|
||||
if err != nil {
|
||||
slog.Error("failed to create episodes", "ttid", ttShowID, "error", err)
|
||||
c.JSON(http.StatusInternalServerError, ron.Data{"error": app.ErrorCreating})
|
||||
return
|
||||
}
|
||||
|
||||
sqlcEpisodes = append(sqlcEpisodes, sqlcEpisode)
|
||||
>>>>>>> Stashed changes
|
||||
}
|
||||
|
||||
slog.Info("scraped seasons", "ttid", ttShowID, "title", title)
|
||||
} else {
|
||||
title = tvShow.Name
|
||||
sqlcEpisodes, err = hq.Queries.GetEpisodes(c, tvShow.ID)
|
||||
sqlcEpisodes, err = hq.queries.GetEpisodes(ctx, tvShow.ID)
|
||||
if err != nil {
|
||||
slog.Error("failed to get episodes", "ttid", ttShowID, "error", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": ErrorGetting})
|
||||
c.JSON(http.StatusInternalServerError, ron.Data{"error": app.ErrorGetting})
|
||||
return
|
||||
}
|
||||
|
||||
<<<<<<< Updated upstream
|
||||
if err := hq.Queries.IncreasePopularity(c, ttShowID); err != nil {
|
||||
slog.Error("failed to increase popularity", "ttid", ttShowID, "error", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": ErrorUpdating})
|
||||
@@ -59,5 +86,15 @@ func (hq *Handlers) GetTVShow(c *gin.Context) {
|
||||
"seasons": sqlcEpisodes,
|
||||
"tvShowMedian": tvShowMedian,
|
||||
"tvShowAverage": tvShowAverage,
|
||||
=======
|
||||
hq.queries.IncreasePopularity(ctx, ttShowID)
|
||||
slog.Info("tv show exists", "ttid", ttShowID, "title", tvShow.Name)
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, ron.Data{
|
||||
"popularity": tvShow.Popularity,
|
||||
"title": title,
|
||||
"seasons": sqlcEpisodes,
|
||||
>>>>>>> Stashed changes
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user