diff --git a/core/.env.example b/core/.env.example new file mode 100644 index 0000000..1af2e7b --- /dev/null +++ b/core/.env.example @@ -0,0 +1,5 @@ +DRIVERNAME=pgx +MIGRATE=true +DATASOURCE=postgresql://developer:secret@localhost:5432/db?sslmode=disable +ASYMMETRICKEY= +DURATION= \ No newline at end of file diff --git a/core/Makefile b/core/Makefile index fa083aa..2cb4ade 100644 --- a/core/Makefile +++ b/core/Makefile @@ -1,40 +1,145 @@ +GO ?= go +GOFMT ?= gofmt "-s" +GO_VERSION=$(shell $(GO) version | cut -c 14- | cut -d' ' -f1 | cut -d'.' -f2) +PACKAGES ?= $(shell $(GO) list ./...) +VETPACKAGES ?= $(shell $(GO) list ./...) +GOFILES := $(shell find . -name "*.go") + +.PHONY: sayhello +# Print Hello World sayhello: @echo "Hello World" +.PHONY: dockerize +# Creates a development database. dockerize: docker run --name rating-db-dev -e POSTGRES_PASSWORD=secret -e POSTGRES_USER=developer -e POSTGRES_DB=rating -p 5432:5432 -d postgres:16.3-alpine3.20 +.PHONY: undockerize +# Destroy a development database. undockerize: docker rm -f rating-db-dev +.PHONY: migrateup +# Migrate all schemas, triggers and data located in cmd/database/migrations. migrateup: - migrate -path database/migrations -database "postgresql://developer:secret@localhost:5432/rating?sslmode=disable" -verbose up - -migratedown: - migrate -path database/migrations -database "postgresql://developer:secret@localhost:5432/rating?sslmode=disable" -verbose down + migrate -path cmd/database/migrations -database "postgresql://developer:secret@localhost:5432/rating?sslmode=disable" -verbose up +.PHONY: sqlc +# Generate or recreate SQLC queries. sqlc: sqlc generate +.PHONY: test +# Test all files and generate coverage file. test: - go test -v -cover ./... + $(GO) test -v -covermode=count -coverprofile=coverage.out $(PACKAGES) +.PHONY: gomock +# Generate mock files. gomock: - mockgen -package mock -destination internal/repository/mock/querier.go github.com/zepyrshut/rating-orama/internal/repository ExtendedQuerier + mockgen -package mock -destination internal/repository/mock/querier.go arena-coins/internal/repository ExtendedQuerier +.PHONY: run +# Run project. run: - go run ./cmd/. + $(GO) run ./cmd/. +.PHONY: recreate +# Destroy development DB and generate ones. recreate: make undockerize make dockerize sleep 2 make migrateup +.PHONY: tidy +# Runs a go mod tidy +tidy: + $(GO) mod tidy + +.PHONY: build-linux +# Build and generate linux executable. build-linux: + make tidy + make remove-debug CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o ./tmp/arena ./cmd/. +.PHONY: pack-docker +# Run docker build for pack binary and assets to Docker container. pack-docker: make test make build-linux - docker build -t rating:${version} -t rating:latest . + docker build -t arena-coins:${version} -t arena-coins:latest . + +.PHONY: remove-debug +# Remove all debug entries for reduce size binary. +remove-debug: + find . -name "*.go" -type f -exec sed -i '/slog\.Debug/d' {} + + +.PHONY: fmt +# Ensure consistent code formatting. +fmt: + $(GOFMT) -w $(GOFILES) + +.PHONY: fmt-check +# format (check only). +fmt-check: + @diff=$$($(GOFMT) -d $(GOFILES)); \ + if [ -n "$$diff" ]; then \ + echo "Please run 'make fmt' and commit the result:"; \ + echo "$${diff}"; \ + exit 1; \ + fi; + +.PHONY: vet +# Examine packages and report suspicious constructs if any. +vet: + $(GO) vet $(VETPACKAGES) + +.PHONY: tools +# Install tools (migrate and sqlc). +tools: + @if [ $(GO_VERSION) -gt 16 ]; then \ + $(GO) install -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@latest; \ + $(GO) install github.com/sqlc-dev/sqlc/cmd/sqlc@latest; \ + fi + +.PHONY: env +# Copy .env.example to .env if .env does not already exist +env: + @if [ ! -f .env ]; then \ + cp .env.example .env; \ + echo ".env file created from .env.example"; \ + else \ + echo ".env file already exists"; \ + fi + + +.PHONY: first-run +# Runs for the first time +first-run: + make tools + make env + make recreate + make run + +.PHONY: help +# Help. +help: + @echo '' + @echo 'Usage:' + @echo ' make [target]' + @echo '' + @echo 'Targets:' + @awk '/^[a-zA-Z\-\0-9]+:/ { \ + helpMessage = match(lastLine, /^# (.*)/); \ + if (helpMessage) { \ + helpCommand = substr($$1, 0, index($$1, ":")-1); \ + helpMessage = substr(lastLine, RSTART + 2, RLENGTH); \ + printf " - \033[36m%-20s\033[0m %s\n", helpCommand, helpMessage; \ + } \ + } \ + { lastLine = $$0 }' $(MAKEFILE_LIST) + +.DEFAULT_GOAL := help \ No newline at end of file diff --git a/core/database/migrations/001_rating_schema.up.sql b/core/cmd/database/migrations/001_rating_schema.up.sql similarity index 100% rename from core/database/migrations/001_rating_schema.up.sql rename to core/cmd/database/migrations/001_rating_schema.up.sql diff --git a/core/database/queries/tv_show.sql b/core/cmd/database/queries/tv_show.sql similarity index 100% rename from core/database/queries/tv_show.sql rename to core/cmd/database/queries/tv_show.sql diff --git a/core/cmd/main.go b/core/cmd/main.go index d601a26..f8f2b34 100644 --- a/core/cmd/main.go +++ b/core/cmd/main.go @@ -1,79 +1,41 @@ package main import ( + "embed" "encoding/gob" - "errors" - "gopher-toolbox/config" + "gopher-toolbox/app" + "gopher-toolbox/db" "log/slog" "net/http" - "os" + "ron" - "gopher-toolbox/db" - "gopher-toolbox/utils" - - "github.com/golang-migrate/migrate/v4" - _ "github.com/golang-migrate/migrate/v4/database/postgres" - _ "github.com/golang-migrate/migrate/v4/source/file" - "github.com/joho/godotenv" "github.com/zepyrshut/rating-orama/internal/handlers" "github.com/zepyrshut/rating-orama/internal/repository" ) -const version = "0.2.0-beta.20241116-4" +//go:embed database/migrations +var database embed.FS -var app *config.App +const version = "0.2.0-beta.20241116-4" func init() { gob.Register(map[string]string{}) - - err := godotenv.Load() - if err != nil { - slog.Error("cannot load .env file", "error", err) - } - config.NewLogger(config.LogLevel(os.Getenv("LOG_LEVEL"))) - slog.Info("starting server") - - if os.Getenv("MIGRATE") == "true" { - migrateDB() - } -} - -func migrateDB() { - slog.Info("migrating database") - m, err := migrate.New("file://database/migrations", os.Getenv("DATASOURCE")) - if err != nil { - slog.Error("cannot create migration", "error", err) - } - - err = m.Up() - if err != nil && !errors.Is(err, migrate.ErrNoChange) { - slog.Error("cannot migrate", "error", err) - panic(err) - } - if errors.Is(err, migrate.ErrNoChange) { - slog.Info("migration has no changes") - } - - slog.Info("migration done") } func main() { + app := app.New(version) + app.Migrate(database) + r := ron.New(func(e *ron.Engine) { + e.Render = ron.NewHTMLRender() + e.Config.LogLevel = slog.LevelDebug + }) - app = &config.App{ - DataSource: os.Getenv("DATASOURCE"), - UseCache: utils.GetBool(os.Getenv("USE_CACHE")), - AppInfo: config.AppInfo{ - GinMode: os.Getenv("GIN_MODE"), - Version: version, - }, - } - - dbPool := db.NewPostgresPool(app.DataSource) + dbPool := db.NewPGXPool(app.Database.DataSource) defer dbPool.Close() q := repository.NewPGXRepo(dbPool) - h := handlers.New(q, app) - r := Router(h, app) + h := handlers.New(app, q) + router(h, r) slog.Info("server started", "port", "8080", "version", version) err := http.ListenAndServe(":8080", r) diff --git a/core/cmd/routes.go b/core/cmd/routes.go index 2e9e562..c0ee97f 100644 --- a/core/cmd/routes.go +++ b/core/cmd/routes.go @@ -1,18 +1,14 @@ package main import ( - "github.com/gin-gonic/gin" - "github.com/zepyrshut/rating-orama/internal/handlers" + "ron" - "gopher-toolbox/config" + "github.com/zepyrshut/rating-orama/internal/handlers" ) -func Router(h *handlers.Handlers, app *config.App) *gin.Engine { - gin.SetMode(app.AppInfo.GinMode) - r := gin.New() +func router(h *handlers.Handlers, r *ron.Engine) { r.GET("/ping", h.Ping) r.GET("/tvshow", h.GetTVShow) - return r } diff --git a/core/go.mod b/core/go.mod index 93cc5b7..322adb1 100644 --- a/core/go.mod +++ b/core/go.mod @@ -2,17 +2,26 @@ module github.com/zepyrshut/rating-orama go 1.23.2 -require github.com/jackc/pgx/v5 v5.7.1 +require ( + github.com/jackc/pgx/v5 v5.7.1 + ron v0.0.0-00010101000000-000000000000 +) require ( + aidanwoods.dev/go-paseto v1.5.2 // indirect + aidanwoods.dev/go-result v0.1.0 // indirect github.com/PuerkitoBio/goquery v1.10.0 // indirect +<<<<<<< Updated upstream github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da // indirect github.com/aead/chacha20poly1305 v0.0.0-20201124145622-1a5aba2a8b29 // indirect github.com/aead/poly1305 v0.0.0-20180717145839-3fee0db0b635 // indirect +======= +>>>>>>> Stashed changes github.com/andybalholm/cascadia v1.3.2 // indirect github.com/antchfx/htmlquery v1.3.3 // indirect github.com/antchfx/xmlquery v1.4.2 // indirect github.com/antchfx/xpath v1.3.2 // indirect +<<<<<<< Updated upstream github.com/bytedance/sonic v1.12.4 // indirect github.com/bytedance/sonic/loader v0.2.1 // indirect github.com/cloudwego/base64x v0.1.4 // indirect @@ -24,14 +33,17 @@ require ( github.com/go-playground/validator/v10 v10.22.1 // indirect github.com/gobwas/glob v0.2.3 // indirect github.com/goccy/go-json v0.10.3 // indirect +======= + github.com/gobwas/glob v0.2.3 // indirect +>>>>>>> Stashed changes github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect github.com/golang/protobuf v1.5.4 // indirect github.com/google/go-cmp v0.6.0 // indirect github.com/hashicorp/errwrap v1.1.0 // indirect github.com/hashicorp/go-multierror v1.1.1 // indirect github.com/jackc/pgconn v1.14.3 // indirect - github.com/json-iterator/go v1.1.12 // indirect github.com/kennygrant/sanitize v1.2.4 // indirect +<<<<<<< Updated upstream github.com/klauspost/cpuid/v2 v2.2.8 // indirect github.com/leodido/go-urn v1.4.0 // indirect github.com/lib/pq v1.10.9 // indirect @@ -50,21 +62,26 @@ require ( google.golang.org/appengine v1.6.8 // indirect google.golang.org/protobuf v1.35.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect +======= + github.com/lib/pq v1.10.9 // indirect + github.com/saintfish/chardet v0.0.0-20230101081208-5e3ef4b5456d // indirect + github.com/temoto/robotstxt v1.1.2 // indirect + go.uber.org/atomic v1.7.0 // indirect + golang.org/x/net v0.30.0 // indirect + google.golang.org/appengine v1.6.8 // indirect + google.golang.org/protobuf v1.34.2 // indirect +>>>>>>> Stashed changes ) require ( - github.com/gin-gonic/gin v1.10.0 github.com/gocolly/colly v1.2.0 - github.com/golang-migrate/migrate/v4 v4.18.1 - github.com/google/uuid v1.6.0 // indirect + github.com/golang-migrate/migrate/v4 v4.18.1 // indirect github.com/jackc/chunkreader/v2 v2.0.1 // indirect github.com/jackc/pgio v1.0.0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgproto3/v2 v2.3.3 // indirect github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect github.com/jackc/puddle/v2 v2.2.2 // indirect - github.com/joho/godotenv v1.5.1 - github.com/mattn/go-isatty v0.0.20 // indirect golang.org/x/crypto v0.28.0 // indirect golang.org/x/sync v0.8.0 // indirect golang.org/x/sys v0.26.0 // indirect @@ -72,4 +89,6 @@ require ( gopher-toolbox v0.0.0-00010101000000-000000000000 ) +replace ron => ./../../ron-gola + replace gopher-toolbox => ./../../gopher-toolbox diff --git a/core/go.sum b/core/go.sum index 3f0c198..7009357 100644 --- a/core/go.sum +++ b/core/go.sum @@ -1,9 +1,14 @@ +aidanwoods.dev/go-paseto v1.5.2 h1:9aKbCQQUeHCqis9Y6WPpJpM9MhEOEI5XBmfTkFMSF/o= +aidanwoods.dev/go-paseto v1.5.2/go.mod h1:7eEJZ98h2wFi5mavCcbKfv9h86oQwut4fLVeL/UBFnw= +aidanwoods.dev/go-result v0.1.0 h1:y/BMIRX6q3HwaorX1Wzrjo3WUdiYeyWbvGe18hKS3K8= +aidanwoods.dev/go-result v0.1.0/go.mod h1:yridkWghM7AXSFA6wzx0IbsurIm1Lhuro3rYef8FBHM= github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 h1:L/gRVlceqvL25UVaW/CKtUDjefjrs0SPonmDGUVOYP0= github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E= github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY= github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU= github.com/PuerkitoBio/goquery v1.10.0 h1:6fiXdLuUvYs2OJSvNRqlNPoBm6YABE226xrbavY5Wv4= github.com/PuerkitoBio/goquery v1.10.0/go.mod h1:TjZZl68Q3eGHNBA8CWaxAN7rOU1EbDz3CWuolcO5Yu4= +<<<<<<< Updated upstream github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da h1:KjTM2ks9d14ZYCvmHS9iAKVt9AyzRSqNU1qabPih5BY= github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da/go.mod h1:eHEWzANqSiWQsof+nXEI9bUVUyV6F53Fp89EuCh2EAA= github.com/aead/chacha20poly1305 v0.0.0-20170617001512-233f39982aeb/go.mod h1:UzH9IX1MMqOcwhoNOIjmTQeAxrFgzs50j4golQtXXxU= @@ -11,6 +16,8 @@ github.com/aead/chacha20poly1305 v0.0.0-20201124145622-1a5aba2a8b29 h1:1DcvRPZOd github.com/aead/chacha20poly1305 v0.0.0-20201124145622-1a5aba2a8b29/go.mod h1:UzH9IX1MMqOcwhoNOIjmTQeAxrFgzs50j4golQtXXxU= github.com/aead/poly1305 v0.0.0-20180717145839-3fee0db0b635 h1:52m0LGchQBBVqJRyYYufQuIbVqRawmubW3OFGqK1ekw= github.com/aead/poly1305 v0.0.0-20180717145839-3fee0db0b635/go.mod h1:lmLxL+FV291OopO93Bwf9fQLQeLyt33VJRUg5VJ30us= +======= +>>>>>>> Stashed changes github.com/andybalholm/cascadia v1.3.2 h1:3Xi6Dw5lHF15JtdcmAHD3i1+T8plmv7BQ/nsViSLyss= github.com/andybalholm/cascadia v1.3.2/go.mod h1:7gtRlve5FxPPgIgX36uWBX58OdBsSS6lUvCFb+h7KvU= github.com/antchfx/htmlquery v1.3.3 h1:x6tVzrRhVNfECDaVxnZi1mEGrQg3mjE/rxbH2Pe6dNE= @@ -19,6 +26,7 @@ github.com/antchfx/xmlquery v1.4.2 h1:MZKd9+wblwxfQ1zd1AdrTsqVaMjMCwow3IqkCSe00K github.com/antchfx/xmlquery v1.4.2/go.mod h1:QXhvf5ldTuGqhd1SHNvvtlhhdQLks4dD0awIVhXIDTA= github.com/antchfx/xpath v1.3.2 h1:LNjzlsSjinu3bQpw9hWMY9ocB80oLOWuQqFvO6xt51U= github.com/antchfx/xpath v1.3.2/go.mod h1:i54GszH55fYfBmoZXapTHN8T8tkcHfRgLyVwwqzXNcs= +<<<<<<< Updated upstream github.com/bytedance/sonic v1.12.4 h1:9Csb3c9ZJhfUWeMtpCDCq6BUoH5ogfDFLUgQ/jG+R0k= github.com/bytedance/sonic v1.12.4/go.mod h1:B8Gt/XvtZ3Fqj+iSKMypzymZxw/FVwgIGKzMzT9r/rk= github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= @@ -28,6 +36,8 @@ github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/ github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w= github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg= github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY= +======= +>>>>>>> Stashed changes github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -43,28 +53,26 @@ github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4 github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg= github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= +<<<<<<< Updated upstream github.com/gabriel-vasile/mimetype v1.4.6 h1:3+PzJTKLkvgjeTbts6msPJt4DixhT4YtFNf1gtGe3zc= github.com/gabriel-vasile/mimetype v1.4.6/go.mod h1:JX1qVKqZd40hUPpAfiNTe0Sne7hdfKSbOqqmkq8GCXc= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU= github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y= +======= +>>>>>>> Stashed changes github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY= github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= -github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= -github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= -github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= -github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= -github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= -github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= -github.com/go-playground/validator/v10 v10.22.1 h1:40JcKH+bBNGFczGuoBYgX4I6m/i27HYW8P9FDk5PbgA= -github.com/go-playground/validator/v10 v10.22.1/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y= github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8= +<<<<<<< Updated upstream github.com/goccy/go-json v0.10.3 h1:KZ5WoDbxAIgm2HNbYckL0se1fHD6rz5j4ywS6ebzDqA= github.com/goccy/go-json v0.10.3/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M= +======= +>>>>>>> Stashed changes github.com/gocolly/colly v1.2.0 h1:qRz9YAn8FIH0qzgNUw+HT9UN7wm1oF9OBAilwEWpyrI= github.com/gocolly/colly v1.2.0/go.mod h1:Hof5T3ZswNVsOHYmba1u03W65HDWgpV5HifSuueE0EA= github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= @@ -78,11 +86,16 @@ github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiu github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +<<<<<<< Updated upstream github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +======= +github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= +github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +>>>>>>> Stashed changes github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I= github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= @@ -107,12 +120,9 @@ github.com/jackc/pgx/v5 v5.7.1 h1:x7SYsPBYDkHDksogeSmZZ5xzThcTgRz++I5E+ePFUcs= github.com/jackc/pgx/v5 v5.7.1/go.mod h1:e7O26IywZZ+naJtWWos6i6fvWK+29etgITqrqHLfoZA= github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo= github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= -github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= -github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= -github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= -github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/kennygrant/sanitize v1.2.4 h1:gN25/otpP5vAsO2djbMhF/LQX6R7+O1TB4yv8NzpJ3o= github.com/kennygrant/sanitize v1.2.4/go.mod h1:LGsjYYtgxbetdg5owWB2mpgUL6e2nfw2eObZ0u0qvak= +<<<<<<< Updated upstream github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= github.com/klauspost/cpuid/v2 v2.2.8 h1:+StwCXwm9PdpiEkPyzBXIy+M9KUb4ODm0Zarf1kS5BM= github.com/klauspost/cpuid/v2 v2.2.8/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= @@ -123,39 +133,34 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= +======= +>>>>>>> Stashed changes github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= -github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= -github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3Nl2EsFP0= github.com/moby/docker-image-spec v1.3.1/go.mod h1:eKmb5VW8vQEh/BAr2yvVNvuiJuY6UIocYsFu/DxxRpo= github.com/moby/term v0.5.0 h1:xt8Q1nalod/v7BqbG21f8mQPqH+xAaC9C3N3wfWbVP0= github.com/moby/term v0.5.0/go.mod h1:8FzsFHVUBGZdbDsJw/ot+X+d5HLUbvklYLJ9uGfcI3Y= -github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= -github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= -github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= -github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= -github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/morikuni/aec v1.0.0 h1:nP9CBfwrvYnBRgY6qfDQkygYDmYwOilePFkwzv4dU8A= github.com/morikuni/aec v1.0.0/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc= -github.com/o1egl/paseto v1.0.0 h1:bwpvPu2au176w4IBlhbyUv/S5VPptERIA99Oap5qUd0= -github.com/o1egl/paseto v1.0.0/go.mod h1:5HxsZPmw/3RI2pAwGo1HhOOwSdvBpcuVzO7uDkm+CLU= github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U= github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM= github.com/opencontainers/image-spec v1.1.0 h1:8SG7/vwALn54lVB/0yZ/MMwhFrPYtpEHQb2IpWsCzug= github.com/opencontainers/image-spec v1.1.0/go.mod h1:W4s4sFTMaBeK1BQLXbG4AdM2szdn85PY75RI83NrTrM= +<<<<<<< Updated upstream github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M= github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +======= +>>>>>>> Stashed changes github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII= -github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o= github.com/saintfish/chardet v0.0.0-20230101081208-5e3ef4b5456d h1:hrujxIzL1woJ7AwssoOcM/tq5JjjG2yYOc8odClEiXA= github.com/saintfish/chardet v0.0.0-20230101081208-5e3ef4b5456d/go.mod h1:uugorj2VCxiV1x+LzaIdVa9b4S4qGAcH6cbhh4qVxOU= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +<<<<<<< Updated upstream github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= @@ -165,17 +170,16 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +======= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +>>>>>>> Stashed changes github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/temoto/robotstxt v1.1.2 h1:W2pOjSJ6SWvldyEuiFXNxz3xZ8aiWX5LbfDiOFd7Fxg= github.com/temoto/robotstxt v1.1.2/go.mod h1:+1AmkuG3IYkh1kv0d2qEB9Le88ehNO0zwOr3ujewlOo= -github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= -github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= -github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE= -github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= -github.com/zepyrshut/esfaker v0.0.0-20241017072233-b4a5efb1f24d h1:o52tUkQBIDD6s2v2OHmXIsZQIKTEiVMPeov2SmvXJWk= -github.com/zepyrshut/esfaker v0.0.0-20241017072233-b4a5efb1f24d/go.mod h1:HgsPkO8n/XumWNHKfMZNV9UgC9/sUghpxVFuQcPJd2o= go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.54.0 h1:TT4fX+nBOA/+LUkobKGW1ydGcn+G3vRw9+g5HwCphpk= go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.54.0/go.mod h1:L7UH0GbB0p47T4Rri3uHjbpCFYrVrwc1I25QhNPiGK8= go.opentelemetry.io/otel v1.29.0 h1:PdomN/Al4q/lN6iBJEN3AwPvUiHPMlt93c8bqTG5Llw= @@ -184,11 +188,16 @@ go.opentelemetry.io/otel/metric v1.29.0 h1:vPf/HFWTNkPu1aYeIsc98l4ktOQaL6LeSoeV2 go.opentelemetry.io/otel/metric v1.29.0/go.mod h1:auu/QWieFVWx+DmQOUMgj0F8LHWdgalxXqvp7BII/W8= go.opentelemetry.io/otel/trace v1.29.0 h1:J/8ZNK4XgR7a21DZUAsbF8pZ5Jcw1VhACmnYt39JTi4= go.opentelemetry.io/otel/trace v1.29.0/go.mod h1:eHl3w0sp3paPkYstJOmAimxhiFXPg+MMTlEh3nsQgWQ= +<<<<<<< Updated upstream go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE= go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0= golang.org/x/arch v0.11.0 h1:KXV8WWKCXm6tRpLirl2szsO5j/oOODwZf4hATmGVNs4= golang.org/x/arch v0.11.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= golang.org/x/crypto v0.0.0-20181025213731-e84da0312774/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +======= +go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw= +go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= +>>>>>>> Stashed changes golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw= @@ -208,14 +217,12 @@ golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= -golang.org/x/sys v0.0.0-20181026203630-95b1ffbd15a5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo= golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= @@ -244,10 +251,11 @@ google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFytA= google.golang.org/protobuf v1.35.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= -gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +<<<<<<< Updated upstream nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50= +======= +>>>>>>> Stashed changes diff --git a/core/internal/handlers/handlers.go b/core/internal/handlers/handlers.go index 6f34115..9ea5af4 100644 --- a/core/internal/handlers/handlers.go +++ b/core/internal/handlers/handlers.go @@ -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 +// } diff --git a/core/internal/handlers/tvshow.go b/core/internal/handlers/tvshow.go index d12f5f2..b0d74dd 100644 --- a/core/internal/handlers/tvshow.go +++ b/core/internal/handlers/tvshow.go @@ -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 }) }