mirror of
				https://github.com/3ybactuk/marketplace-go-service-project.git
				synced 2025-10-30 05:53:45 +03:00 
			
		
		
		
	[hw-8] add: comment service
This commit is contained in:
		
							
								
								
									
										22
									
								
								comments/Dockerfile
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								comments/Dockerfile
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,22 @@ | ||||
| FROM golang:1.23.9-alpine as builder | ||||
|  | ||||
| WORKDIR /build | ||||
|  | ||||
| COPY comments/go.mod go.mod | ||||
| COPY comments/go.sum go.sum | ||||
|  | ||||
| RUN go mod download | ||||
|  | ||||
| COPY . . | ||||
|  | ||||
| WORKDIR comments | ||||
|  | ||||
| RUN CGO_ENABLED=0 GOOS=linux go build -o /server ./cmd/server/main.go | ||||
|  | ||||
| FROM scratch | ||||
| COPY --from=builder server /bin/server | ||||
| COPY comments/configs/values_local.yaml /bin/config/values_local.yaml | ||||
|  | ||||
| ENV CONFIG_FILE=/bin/config/values_local.yaml | ||||
|  | ||||
| ENTRYPOINT ["/bin/server"] | ||||
| @@ -5,9 +5,9 @@ MIGRATIONS_FOLDER	:= ./db/migrations/ | ||||
| LOCAL_DB_NAME		:= route256 | ||||
| LOCAL_DB_DSN		:= postgresql://user:password@localhost:5433/route256?sslmode=disable | ||||
|  | ||||
| PROD_USER := loms-user | ||||
| PROD_PASS := loms-password | ||||
| PROD_DB   := postgres-master | ||||
| PROD_USER := comments-user | ||||
| PROD_PASS := comments-password | ||||
| PROD_DB   := postgres-comments-shard | ||||
|  | ||||
| PROD_MIGRATIONS := ./comments/db/migrations/ | ||||
|  | ||||
| @@ -20,7 +20,8 @@ bindir: | ||||
|  | ||||
| # Used for CI | ||||
| run-migrations: | ||||
| 	$(GOOSE) -dir $(PROD_MIGRATIONS) postgres "postgresql://$(PROD_USER):$(PROD_PASS)@$(PROD_DB):5432/comments_db?sslmode=disable" up | ||||
| 	$(GOOSE) -dir $(PROD_MIGRATIONS) postgres "postgresql://$(PROD_USER)-1:$(PROD_PASS)-1@$(PROD_DB)-1:5432/comments_db?sslmode=disable" up | ||||
| 	$(GOOSE) -dir $(PROD_MIGRATIONS) postgres "postgresql://$(PROD_USER)-2:$(PROD_PASS)-2@$(PROD_DB)-2:5432/comments_db?sslmode=disable" up | ||||
|  | ||||
| db-create-migration: | ||||
| 	$(BINDIR)/goose -dir $(MIGRATIONS_FOLDER) create -s $(n) sql | ||||
|   | ||||
							
								
								
									
										42
									
								
								comments/cmd/server/main.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								comments/cmd/server/main.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,42 @@ | ||||
| package main | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"net/http" | ||||
| 	"os" | ||||
| 	"os/signal" | ||||
| 	"syscall" | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/rs/zerolog/log" | ||||
|  | ||||
| 	"route256/comments/internal/app" | ||||
| ) | ||||
|  | ||||
| func main() { | ||||
| 	srv, err := app.NewApp(os.Getenv("CONFIG_FILE")) | ||||
| 	if err != nil { | ||||
| 		log.Fatal().Err(err).Msg("failed creating app") | ||||
| 	} | ||||
|  | ||||
| 	ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) | ||||
| 	defer stop() | ||||
|  | ||||
| 	go func() { | ||||
| 		if err := srv.ListenAndServe(ctx); err != nil && err != http.ErrServerClosed { | ||||
| 			log.Fatal().Err(err).Msg("serving error") | ||||
| 		} | ||||
| 	}() | ||||
|  | ||||
| 	<-ctx.Done() | ||||
| 	log.Info().Msg("shutdown signal received") | ||||
|  | ||||
| 	shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||||
| 	defer cancel() | ||||
|  | ||||
| 	if err := srv.Shutdown(shutdownCtx); err != nil { | ||||
| 		log.Error().Err(err).Msg("graceful shutdown failed") | ||||
| 	} else { | ||||
| 		log.Info().Msg("server stopped gracefully") | ||||
| 	} | ||||
| } | ||||
| @@ -1,3 +1,4 @@ | ||||
| -- +goose Up | ||||
| CREATE TABLE comments ( | ||||
|   id          BIGSERIAL PRIMARY KEY, | ||||
|   user_id     BIGINT NOT NULL, | ||||
| @@ -6,5 +7,10 @@ CREATE TABLE comments ( | ||||
|   created_at  TIMESTAMP(3) NOT NULL DEFAULT now() | ||||
| ); | ||||
|  | ||||
| CREATE INDEX ON comments(sku, created_at DESC, user_id ASC); | ||||
| CREATE INDEX ON comments(user_id, created_at DESC); | ||||
| CREATE INDEX sku_idx ON comments(sku, created_at DESC, user_id ASC); | ||||
| CREATE INDEX user_id_idx ON comments(user_id, created_at DESC); | ||||
|  | ||||
| -- +goose Down | ||||
| DROP TABLE comments; | ||||
| DROP INDEX sku_idx; | ||||
| DROP INDEX user_id_idx; | ||||
| @@ -2,15 +2,29 @@ module route256/comments | ||||
|  | ||||
| go 1.23.1 | ||||
|  | ||||
| require github.com/go-playground/validator/v10 v10.27.0 | ||||
| require ( | ||||
| 	github.com/go-playground/validator/v10 v10.27.0 | ||||
| 	github.com/jackc/pgx/v5 v5.7.5 | ||||
| 	github.com/opentracing/opentracing-go v1.2.0 | ||||
| 	github.com/rs/zerolog v1.34.0 | ||||
| 	google.golang.org/grpc v1.74.2 | ||||
| 	google.golang.org/protobuf v1.36.6 | ||||
| ) | ||||
|  | ||||
| require ( | ||||
| 	github.com/gabriel-vasile/mimetype v1.4.8 // indirect | ||||
| 	github.com/go-playground/locales v0.14.1 // indirect | ||||
| 	github.com/go-playground/universal-translator v0.18.1 // indirect | ||||
| 	github.com/jackc/pgpassfile v1.0.0 // indirect | ||||
| 	github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect | ||||
| 	github.com/jackc/puddle/v2 v2.2.2 // indirect | ||||
| 	github.com/leodido/go-urn v1.4.0 // indirect | ||||
| 	golang.org/x/crypto v0.33.0 // indirect | ||||
| 	golang.org/x/net v0.34.0 // indirect | ||||
| 	golang.org/x/sys v0.30.0 // indirect | ||||
| 	golang.org/x/text v0.22.0 // indirect | ||||
| 	github.com/mattn/go-colorable v0.1.13 // indirect | ||||
| 	github.com/mattn/go-isatty v0.0.19 // indirect | ||||
| 	golang.org/x/crypto v0.38.0 // indirect | ||||
| 	golang.org/x/net v0.40.0 // indirect | ||||
| 	golang.org/x/sync v0.14.0 // indirect | ||||
| 	golang.org/x/sys v0.33.0 // indirect | ||||
| 	golang.org/x/text v0.25.0 // indirect | ||||
| 	google.golang.org/genproto/googleapis/rpc v0.0.0-20250528174236-200df99c418a // indirect | ||||
| ) | ||||
|   | ||||
| @@ -1,3 +1,5 @@ | ||||
| github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= | ||||
| 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= | ||||
| github.com/gabriel-vasile/mimetype v1.4.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3GqacKw1NM= | ||||
| @@ -10,19 +12,57 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn | ||||
| github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= | ||||
| github.com/go-playground/validator/v10 v10.27.0 h1:w8+XrWVMhGkxOaaowyKH35gFydVHOvC0/uWoy2Fzwn4= | ||||
| github.com/go-playground/validator/v10 v10.27.0/go.mod h1:I5QpIEbmr8On7W0TktmJAumgzX4CA1XNl4ZmDuVHKKo= | ||||
| github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= | ||||
| github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= | ||||
| github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= | ||||
| github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= | ||||
| github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= | ||||
| github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo= | ||||
| github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= | ||||
| github.com/jackc/pgx/v5 v5.7.5 h1:JHGfMnQY+IEtGM63d+NGMjoRpysB2JBwDr5fsngwmJs= | ||||
| github.com/jackc/pgx/v5 v5.7.5/go.mod h1:aruU7o91Tc2q2cFp5h4uP3f6ztExVpyVv88Xl/8Vl8M= | ||||
| 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/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= | ||||
| github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= | ||||
| github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= | ||||
| github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= | ||||
| github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= | ||||
| github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= | ||||
| github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+1B0VhjKrZUs= | ||||
| github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc= | ||||
| 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/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0= | ||||
| github.com/rs/zerolog v1.34.0 h1:k43nTLIwcTVQAncfCw4KZ2VY6ukYoZaBPNOE8txlOeY= | ||||
| github.com/rs/zerolog v1.34.0/go.mod h1:bJsvje4Z08ROH4Nhs5iH600c3IkWhwp44iRc54W6wYQ= | ||||
| github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||||
| github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= | ||||
| github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||||
| github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= | ||||
| github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= | ||||
| golang.org/x/crypto v0.33.0 h1:IOBPskki6Lysi0lo9qQvbxiQ+FvsCC/YWOecCHAixus= | ||||
| golang.org/x/crypto v0.33.0/go.mod h1:bVdXmD7IV/4GdElGPozy6U7lWdRXA4qyRVGJV57uQ5M= | ||||
| golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0= | ||||
| golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k= | ||||
| golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc= | ||||
| golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= | ||||
| golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM= | ||||
| golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY= | ||||
| golang.org/x/crypto v0.38.0 h1:jt+WWG8IZlBnVbomuhg2Mdq0+BBQaHbtqHEFEigjUV8= | ||||
| golang.org/x/crypto v0.38.0/go.mod h1:MvrbAqul58NNYPKnOra203SB9vpuZW0e+RRZV+Ggqjw= | ||||
| golang.org/x/net v0.40.0 h1:79Xs7wF06Gbdcg4kdCCIQArK11Z1hr5POQ6+fIYHNuY= | ||||
| golang.org/x/net v0.40.0/go.mod h1:y0hY0exeL2Pku80/zKK7tpntoX23cqL3Oa6njdgRtds= | ||||
| golang.org/x/sync v0.14.0 h1:woo0S4Yywslg6hp4eUFjTVOyKt0RookbpAHG4c1HmhQ= | ||||
| golang.org/x/sync v0.14.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA= | ||||
| golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||||
| golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||||
| golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||||
| golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw= | ||||
| golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= | ||||
| golang.org/x/text v0.25.0 h1:qVyWApTSYLk/drJRO5mDlNYskwQznZmkpV2c8q9zls4= | ||||
| golang.org/x/text v0.25.0/go.mod h1:WEdwpYrmk1qmdHvhkSTNPm3app7v4rsT8F2UD6+VHIA= | ||||
| google.golang.org/genproto/googleapis/rpc v0.0.0-20250528174236-200df99c418a h1:v2PbRU4K3llS09c7zodFpNePeamkAwG3mPrAery9VeE= | ||||
| google.golang.org/genproto/googleapis/rpc v0.0.0-20250528174236-200df99c418a/go.mod h1:qQ0YXyHHx3XkvlzUtpXDkS29lDSafHMZBAZDc03LQ3A= | ||||
| google.golang.org/grpc v1.74.2 h1:WoosgB65DlWVC9FqI82dGsZhWFNBSLjQ84bjROOpMu4= | ||||
| google.golang.org/grpc v1.74.2/go.mod h1:CtQ+BGjaAIXHs/5YS3i473GqwBBa1zGQNevxdeBEXrM= | ||||
| google.golang.org/protobuf v1.36.6 h1:z1NpPI8ku2WgiWnf+t9wTPsn6eP1L7ksHUlkfLvd9xY= | ||||
| google.golang.org/protobuf v1.36.6/go.mod h1:jduwjTPXsFjZGTmRluh+L6NjiWu7pchiJ2/5YcXBHnY= | ||||
| gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||||
| 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= | ||||
|   | ||||
							
								
								
									
										59
									
								
								comments/infra/config/config.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										59
									
								
								comments/infra/config/config.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,59 @@ | ||||
| package config | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"os" | ||||
| 	"path/filepath" | ||||
| 	"strings" | ||||
| 	"time" | ||||
|  | ||||
| 	"gopkg.in/yaml.v3" | ||||
| ) | ||||
|  | ||||
| type Config struct { | ||||
| 	App struct { | ||||
| 		EditInterval time.Duration `yaml:"edit_interval"` | ||||
| 	} | ||||
|  | ||||
| 	Service struct { | ||||
| 		Host     string `yaml:"host"` | ||||
| 		GRPCPort string `yaml:"grpc_port"` | ||||
| 		HTTPPort string `yaml:"http_port"` | ||||
| 		LogLevel string `yaml:"log_level"` | ||||
| 	} `yaml:"service"` | ||||
|  | ||||
| 	DbShards []struct { | ||||
| 		Host     string `yaml:"host"` | ||||
| 		Port     string `yaml:"port"` | ||||
| 		User     string `yaml:"user"` | ||||
| 		Password string `yaml:"password"` | ||||
| 		DBName   string `yaml:"db_name"` | ||||
| 	} `yaml:"db_shards"` | ||||
| } | ||||
|  | ||||
| func LoadConfig(filename string) (*Config, error) { | ||||
| 	workDir, err := os.Getwd() | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	cfgRoot := filepath.Join(workDir, "configs") | ||||
| 	absCfgRoot, _ := filepath.Abs(cfgRoot) | ||||
|  | ||||
| 	filePath := filepath.Join(absCfgRoot, filepath.Clean(filename)) | ||||
| 	if !strings.HasPrefix(filePath, absCfgRoot) { | ||||
| 		return nil, fmt.Errorf("invalid path") | ||||
| 	} | ||||
|  | ||||
| 	f, err := os.Open(filename) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	defer f.Close() | ||||
|  | ||||
| 	config := &Config{} | ||||
| 	if err := yaml.NewDecoder(f).Decode(config); err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
|  | ||||
| 	return config, nil | ||||
| } | ||||
							
								
								
									
										40
									
								
								comments/infra/db/postgres/postgres.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								comments/infra/db/postgres/postgres.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,40 @@ | ||||
| package postgres | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
|  | ||||
| 	"github.com/jackc/pgx/v5/pgxpool" | ||||
| 	"github.com/pkg/errors" | ||||
| ) | ||||
|  | ||||
| func NewPools(ctx context.Context, DSNs ...string) ([]*pgxpool.Pool, error) { | ||||
| 	pools := make([]*pgxpool.Pool, len(DSNs)) | ||||
|  | ||||
| 	for i, dsn := range DSNs { | ||||
| 		cfg, err := pgxpool.ParseConfig(dsn) | ||||
| 		if err != nil { | ||||
| 			closeOpened(pools[:i]) | ||||
|  | ||||
| 			return nil, errors.Wrap(err, "pgxpool.ParseConfig") | ||||
| 		} | ||||
|  | ||||
| 		pool, err := pgxpool.NewWithConfig(ctx, cfg) | ||||
| 		if err != nil { | ||||
| 			closeOpened(pools[:i]) | ||||
|  | ||||
| 			return nil, errors.Wrap(err, "pgxpool.NewWithConfig") | ||||
| 		} | ||||
|  | ||||
| 		pools[i] = pool | ||||
| 	} | ||||
|  | ||||
| 	return pools, nil | ||||
| } | ||||
|  | ||||
| func closeOpened(pools []*pgxpool.Pool) { | ||||
| 	for _, p := range pools { | ||||
| 		if p != nil { | ||||
| 			p.Close() | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										24
									
								
								comments/infra/grpc/middleware/logging.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								comments/infra/grpc/middleware/logging.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,24 @@ | ||||
| package mw | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
|  | ||||
| 	"github.com/rs/zerolog/log" | ||||
| 	"google.golang.org/grpc" | ||||
| 	"google.golang.org/protobuf/encoding/protojson" | ||||
| 	"google.golang.org/protobuf/proto" | ||||
| ) | ||||
|  | ||||
| func Logging(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp any, err error) { | ||||
| 	raw, _ := protojson.Marshal((req).(proto.Message)) | ||||
| 	log.Debug().Msgf("request: method: %v, req: %s", info.FullMethod, string(raw)) | ||||
|  | ||||
| 	if resp, err = handler(ctx, req); err != nil { | ||||
| 		log.Debug().Msgf("response: method: %v, err: %s", info.FullMethod, err.Error()) | ||||
| 		return | ||||
| 	} | ||||
| 	rawResp, _ := protojson.Marshal((resp).(proto.Message)) | ||||
| 	log.Debug().Msgf("response: method: %v, resp: %s", info.FullMethod, string(rawResp)) | ||||
|  | ||||
| 	return | ||||
| } | ||||
							
								
								
									
										18
									
								
								comments/infra/grpc/middleware/validate.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								comments/infra/grpc/middleware/validate.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,18 @@ | ||||
| package mw | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
|  | ||||
| 	"google.golang.org/grpc" | ||||
| 	"google.golang.org/grpc/codes" | ||||
| 	"google.golang.org/grpc/status" | ||||
| ) | ||||
|  | ||||
| func Validate(ctx context.Context, req any, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) { | ||||
| 	if v, ok := req.(interface{ Validate() error }); ok { | ||||
| 		if err := v.Validate(); err != nil { | ||||
| 			return nil, status.Error(codes.InvalidArgument, err.Error()) | ||||
| 		} | ||||
| 	} | ||||
| 	return handler(ctx, req) | ||||
| } | ||||
| @@ -3,14 +3,13 @@ package sqlc | ||||
| import ( | ||||
| 	"context" | ||||
| 	"errors" | ||||
| 	"sort" | ||||
|  | ||||
| 	"route256/comments/internal/domain/entity" | ||||
| 	"route256/comments/internal/domain/model" | ||||
|  | ||||
| 	"github.com/jackc/pgx/v5" | ||||
| 	"github.com/jackc/pgx/v5/pgxpool" | ||||
| 	"github.com/rs/zerolog/log" | ||||
|  | ||||
| 	"route256/comments/internal/domain/entity" | ||||
| 	"route256/comments/internal/domain/model" | ||||
| ) | ||||
|  | ||||
| type commentsRepo struct { | ||||
| @@ -33,12 +32,22 @@ func (r *commentsRepo) pickShard(sku int64) *pgxpool.Pool { | ||||
| 	return r.shard2 | ||||
| } | ||||
|  | ||||
| func (r *commentsRepo) GetCommentByID(ctx context.Context, id int64) (*Comment, error) { | ||||
| func mapComment(c *Comment) *entity.Comment { | ||||
| 	return &entity.Comment{ | ||||
| 		ID:        c.ID, | ||||
| 		UserID:    c.UserID, | ||||
| 		SKU:       c.Sku, | ||||
| 		CreatedAt: c.CreatedAt.Time, | ||||
| 		Text:      c.Text, | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func (r *commentsRepo) GetCommentByID(ctx context.Context, id int64) (*entity.Comment, error) { | ||||
| 	q1 := New(r.shard1) | ||||
| 	c, err := q1.GetCommentByID(ctx, id) | ||||
| 	switch { | ||||
| 	case err == nil: | ||||
| 		return c, nil | ||||
| 		return mapComment(c), nil | ||||
| 	case errors.Is(err, pgx.ErrNoRows): | ||||
| 		log.Trace().Msgf("comment with id %d not found in shard 1", id) | ||||
| 	default: | ||||
| @@ -49,7 +58,7 @@ func (r *commentsRepo) GetCommentByID(ctx context.Context, id int64) (*Comment, | ||||
| 	c2, err2 := q2.GetCommentByID(ctx, id) | ||||
| 	switch { | ||||
| 	case err2 == nil: | ||||
| 		return c2, nil | ||||
| 		return mapComment(c2), nil | ||||
| 	case errors.Is(err2, pgx.ErrNoRows): | ||||
| 		return nil, model.ErrCommentNotFound | ||||
| 	default: | ||||
| @@ -57,7 +66,7 @@ func (r *commentsRepo) GetCommentByID(ctx context.Context, id int64) (*Comment, | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func (r *commentsRepo) InsertComment(ctx context.Context, comment *entity.Comment) (*Comment, error) { | ||||
| func (r *commentsRepo) InsertComment(ctx context.Context, comment *entity.Comment) (*entity.Comment, error) { | ||||
| 	shard := r.pickShard(comment.SKU) | ||||
| 	q := New(shard) | ||||
|  | ||||
| @@ -72,10 +81,10 @@ func (r *commentsRepo) InsertComment(ctx context.Context, comment *entity.Commen | ||||
| 		return nil, err | ||||
| 	} | ||||
|  | ||||
| 	return c, nil | ||||
| 	return mapComment(c), nil | ||||
| } | ||||
|  | ||||
| func (r *commentsRepo) ListCommentsBySku(ctx context.Context, sku int64) ([]*Comment, error) { | ||||
| func (r *commentsRepo) ListCommentsBySku(ctx context.Context, sku int64) ([]*entity.Comment, error) { | ||||
| 	shard := r.pickShard(sku) | ||||
| 	q := New(shard) | ||||
|  | ||||
| @@ -84,13 +93,15 @@ func (r *commentsRepo) ListCommentsBySku(ctx context.Context, sku int64) ([]*Com | ||||
| 		return nil, err | ||||
| 	} | ||||
|  | ||||
| 	out := make([]*Comment, len(list)) | ||||
| 	copy(out, list) | ||||
| 	out := make([]*entity.Comment, 0, len(list)) | ||||
| 	for _, com := range list { | ||||
| 		out = append(out, mapComment(com)) | ||||
| 	} | ||||
|  | ||||
| 	return out, nil | ||||
| } | ||||
|  | ||||
| func (r *commentsRepo) ListCommentsByUser(ctx context.Context, userID int64) ([]*Comment, error) { | ||||
| func (r *commentsRepo) ListCommentsByUser(ctx context.Context, userID int64) ([]*entity.Comment, error) { | ||||
| 	q1 := New(r.shard1) | ||||
| 	l1, err1 := q1.ListCommentsByUser(ctx, userID) | ||||
| 	if err1 != nil { | ||||
| @@ -103,22 +114,18 @@ func (r *commentsRepo) ListCommentsByUser(ctx context.Context, userID int64) ([] | ||||
| 		return nil, err2 | ||||
| 	} | ||||
|  | ||||
| 	merged := make([]*Comment, 0, len(l1)+len(l2)) | ||||
| 	merged = append(merged, l1...) | ||||
| 	merged = append(merged, l2...) | ||||
|  | ||||
| 	sort.Slice(merged, func(i, j int) bool { | ||||
| 		if merged[i].CreatedAt.Time.Equal(merged[j].CreatedAt.Time) { | ||||
| 			return merged[i].UserID < merged[j].UserID | ||||
| 		} | ||||
|  | ||||
| 		return merged[i].CreatedAt.Time.After(merged[j].CreatedAt.Time) | ||||
| 	}) | ||||
| 	merged := make([]*entity.Comment, 0, len(l1)+len(l2)) | ||||
| 	for _, com := range l1 { | ||||
| 		merged = append(merged, mapComment(com)) | ||||
| 	} | ||||
| 	for _, com := range l2 { | ||||
| 		merged = append(merged, mapComment(com)) | ||||
| 	} | ||||
|  | ||||
| 	return merged, nil | ||||
| } | ||||
|  | ||||
| func (r *commentsRepo) UpdateComment(ctx context.Context, comment *entity.Comment) (*Comment, error) { | ||||
| func (r *commentsRepo) UpdateComment(ctx context.Context, comment *entity.Comment) (*entity.Comment, error) { | ||||
| 	req := &UpdateCommentParams{ | ||||
| 		ID:   comment.ID, | ||||
| 		Text: comment.Text, | ||||
| @@ -128,7 +135,7 @@ func (r *commentsRepo) UpdateComment(ctx context.Context, comment *entity.Commen | ||||
| 	c, err := q1.UpdateComment(ctx, req) | ||||
| 	switch { | ||||
| 	case err == nil: | ||||
| 		return c, nil | ||||
| 		return mapComment(c), nil | ||||
| 	case errors.Is(err, pgx.ErrNoRows): | ||||
| 		log.Trace().Msgf("comment with id %d not found in shard 1", req.ID) | ||||
| 	default: | ||||
| @@ -139,7 +146,7 @@ func (r *commentsRepo) UpdateComment(ctx context.Context, comment *entity.Commen | ||||
| 	c2, err2 := q2.UpdateComment(ctx, req) | ||||
| 	switch { | ||||
| 	case err2 == nil: | ||||
| 		return c2, nil | ||||
| 		return mapComment(c2), nil | ||||
| 	case errors.Is(err2, pgx.ErrNoRows): | ||||
| 		return nil, model.ErrCommentNotFound | ||||
| 	default: | ||||
|   | ||||
							
								
								
									
										190
									
								
								comments/internal/app/app.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										190
									
								
								comments/internal/app/app.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,190 @@ | ||||
| package app | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"fmt" | ||||
| 	"net" | ||||
| 	"net/http" | ||||
| 	"os" | ||||
| 	"route256/comments/infra/config" | ||||
| 	"route256/comments/infra/db/postgres" | ||||
| 	"route256/comments/internal/app/server" | ||||
| 	"route256/comments/internal/domain/service" | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/grpc-ecosystem/grpc-gateway/v2/runtime" | ||||
| 	"github.com/jackc/pgx/v5/pgxpool" | ||||
| 	"github.com/prometheus/client_golang/prometheus/promhttp" | ||||
| 	"github.com/rs/zerolog" | ||||
| 	"github.com/rs/zerolog/log" | ||||
| 	"google.golang.org/grpc" | ||||
| 	"google.golang.org/grpc/credentials/insecure" | ||||
| 	"google.golang.org/grpc/reflection" | ||||
|  | ||||
| 	mw "route256/comments/infra/grpc/middleware" | ||||
| 	repository "route256/comments/infra/repository/sqlc" | ||||
|  | ||||
| 	pb "route256/pkg/api/comments/v1" | ||||
| ) | ||||
|  | ||||
| type App struct { | ||||
| 	config     *config.Config | ||||
| 	controller *server.Server | ||||
|  | ||||
| 	grpcServer *grpc.Server | ||||
| 	httpServer *http.Server | ||||
| 	gwConn     *grpc.ClientConn | ||||
| } | ||||
|  | ||||
| func NewApp(configPath string) (*App, error) { | ||||
| 	c, err := config.LoadConfig(configPath) | ||||
| 	if err != nil { | ||||
| 		return nil, fmt.Errorf("unable to load config: %w", err) | ||||
| 	} | ||||
|  | ||||
| 	log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}) | ||||
| 	zerolog.SetGlobalLevel(zerolog.InfoLevel) | ||||
|  | ||||
| 	if c.Service.LogLevel != "" { | ||||
| 		level, logErr := zerolog.ParseLevel(c.Service.LogLevel) | ||||
| 		if logErr != nil { | ||||
| 			return nil, fmt.Errorf("unknown log level `%s` provided: %w", c.Service.LogLevel, logErr) | ||||
| 		} | ||||
|  | ||||
| 		zerolog.SetGlobalLevel(level) | ||||
| 	} | ||||
|  | ||||
| 	log.WithLevel(zerolog.GlobalLevel()).Msgf("using logging level=`%s`", zerolog.GlobalLevel().String()) | ||||
|  | ||||
| 	shards, err := getPostgresPools(c) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
|  | ||||
| 	repo := repository.NewCommentsRepository(shards[0], shards[1]) | ||||
|  | ||||
| 	service := service.NewCommentsService(repo, c.App.EditInterval) | ||||
| 	controller := server.NewServer(service) | ||||
|  | ||||
| 	app := &App{ | ||||
| 		config:     c, | ||||
| 		controller: controller, | ||||
| 	} | ||||
|  | ||||
| 	return app, nil | ||||
| } | ||||
|  | ||||
| func (app *App) Shutdown(ctx context.Context) (err error) { | ||||
| 	if app.httpServer != nil { | ||||
| 		err = app.httpServer.Shutdown(ctx) | ||||
| 		if err != nil { | ||||
| 			log.Error().Err(err).Msgf("failed http gateway server shutdown") | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	done := make(chan struct{}) | ||||
| 	if app.grpcServer != nil { | ||||
| 		go func() { | ||||
| 			app.grpcServer.GracefulStop() | ||||
|  | ||||
| 			close(done) | ||||
| 		}() | ||||
| 	} | ||||
|  | ||||
| 	select { | ||||
| 	case <-done: | ||||
| 	case <-ctx.Done(): | ||||
| 		if app.grpcServer != nil { | ||||
| 			app.grpcServer.Stop() | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	if app.gwConn != nil { | ||||
| 		err2 := app.gwConn.Close() | ||||
|  | ||||
| 		if err2 != nil { | ||||
| 			err = err2 | ||||
|  | ||||
| 			log.Error().Err(err).Msgf("failed gateway connection close") | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	return err | ||||
| } | ||||
|  | ||||
| func (app *App) ListenAndServe(ctx context.Context) error { | ||||
| 	grpcAddr := fmt.Sprintf("%s:%s", app.config.Service.Host, app.config.Service.GRPCPort) | ||||
| 	l, err := net.Listen("tcp", grpcAddr) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	app.grpcServer = grpc.NewServer( | ||||
| 		grpc.ChainUnaryInterceptor( | ||||
| 			mw.Logging, | ||||
| 			mw.Validate, | ||||
| 		), | ||||
| 	) | ||||
| 	reflection.Register(app.grpcServer) | ||||
|  | ||||
| 	pb.RegisterCommentsServer(app.grpcServer, app.controller) | ||||
|  | ||||
| 	go func() { | ||||
| 		if err = app.grpcServer.Serve(l); err != nil { | ||||
| 			log.Fatal().Err(err).Msg("failed to serve") | ||||
| 		} | ||||
| 	}() | ||||
|  | ||||
| 	log.Info().Msgf("Serving grpc loms at grpc://%s", l.Addr()) | ||||
|  | ||||
| 	// Setup HTTP gateway | ||||
|  | ||||
| 	conn, err := grpc.NewClient( | ||||
| 		grpcAddr, | ||||
| 		grpc.WithTransportCredentials(insecure.NewCredentials()), | ||||
| 	) | ||||
| 	if err != nil { | ||||
| 		return fmt.Errorf("grpc.NewClient: %w", err) | ||||
| 	} | ||||
| 	app.gwConn = conn | ||||
|  | ||||
| 	gwmux := runtime.NewServeMux() | ||||
| 	if err = pb.RegisterCommentsHandler(ctx, gwmux, conn); err != nil { | ||||
| 		return fmt.Errorf("pb.RegisterLOMSHandler: %w", err) | ||||
| 	} | ||||
|  | ||||
| 	root := http.NewServeMux() | ||||
| 	root.Handle("/metrics", promhttp.Handler()) | ||||
| 	root.Handle("/", gwmux) | ||||
|  | ||||
| 	app.httpServer = &http.Server{ | ||||
| 		Addr:        fmt.Sprintf("%s:%s", app.config.Service.Host, app.config.Service.HTTPPort), | ||||
| 		Handler:     root, | ||||
| 		ReadTimeout: 10 * time.Second, | ||||
| 	} | ||||
|  | ||||
| 	log.Info().Msgf("Serving http loms at http://%s", app.httpServer.Addr) | ||||
|  | ||||
| 	return app.httpServer.ListenAndServe() | ||||
| } | ||||
|  | ||||
| func getPostgresPools(c *config.Config) ([]*pgxpool.Pool, error) { | ||||
| 	conns := make([]string, len(c.DbShards)) | ||||
| 	for i, shard := range c.DbShards { | ||||
| 		conns[i] = fmt.Sprintf( | ||||
| 			"postgresql://%s:%s@%s:%s/%s?sslmode=disable", | ||||
| 			shard.User, | ||||
| 			shard.Password, | ||||
| 			shard.Host, | ||||
| 			shard.Port, | ||||
| 			shard.DBName, | ||||
| 		) | ||||
| 	} | ||||
|  | ||||
| 	pools, err := postgres.NewPools(context.Background(), conns...) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
|  | ||||
| 	return pools, nil | ||||
| } | ||||
							
								
								
									
										133
									
								
								comments/internal/app/server/server.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										133
									
								
								comments/internal/app/server/server.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,133 @@ | ||||
| package server | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"errors" | ||||
| 	"fmt" | ||||
|  | ||||
| 	"route256/comments/internal/domain/entity" | ||||
| 	"route256/comments/internal/domain/model" | ||||
|  | ||||
| 	pb "route256/pkg/api/comments/v1" | ||||
|  | ||||
| 	"google.golang.org/grpc/codes" | ||||
| 	"google.golang.org/grpc/status" | ||||
| 	"google.golang.org/protobuf/types/known/emptypb" | ||||
| 	"google.golang.org/protobuf/types/known/timestamppb" | ||||
| ) | ||||
|  | ||||
| var _ pb.CommentsServer = (*Server)(nil) | ||||
|  | ||||
| type CommentsService interface { | ||||
| 	CommentGetByID(ctx context.Context, id int64) (*entity.Comment, error) | ||||
| 	CommentCreate(ctx context.Context, comment *entity.Comment) (int64, error) | ||||
| 	CommentListBySKU(ctx context.Context, sku int64) ([]*entity.Comment, error) | ||||
| 	CommentListByUser(ctx context.Context, userID int64) ([]*entity.Comment, error) | ||||
| 	CommentEdit(ctx context.Context, comment *entity.Comment) error | ||||
| } | ||||
|  | ||||
| type Server struct { | ||||
| 	pb.UnimplementedCommentsServer | ||||
|  | ||||
| 	service CommentsService | ||||
| } | ||||
|  | ||||
| func NewServer(commentsService CommentsService) *Server { | ||||
| 	return &Server{ | ||||
| 		service: commentsService, | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func (s *Server) CommentAdd(ctx context.Context, req *pb.CreateCommentRequest) (*pb.CreateCommentResponse, error) { | ||||
| 	id, err := s.service.CommentCreate(ctx, &entity.Comment{ | ||||
| 		UserID: req.UserId, | ||||
| 		SKU:    req.Sku, | ||||
| 		Text:   req.Comment, | ||||
| 	}) | ||||
| 	if err != nil { | ||||
| 		return nil, fmt.Errorf("service.InsertComment: %w", err) | ||||
| 	} | ||||
|  | ||||
| 	return &pb.CreateCommentResponse{ | ||||
| 		Id: id, | ||||
| 	}, nil | ||||
| } | ||||
|  | ||||
| func (s *Server) CommentEdit(ctx context.Context, req *pb.EditCommentRequest) (*emptypb.Empty, error) { | ||||
| 	err := s.service.CommentEdit(ctx, &entity.Comment{ | ||||
| 		ID:     req.CommentId, | ||||
| 		UserID: req.UserId, | ||||
| 		Text:   req.NewComment, | ||||
| 	}) | ||||
| 	switch { | ||||
| 	case errors.Is(err, model.ErrCommentEditUserMismatch): | ||||
| 		return &emptypb.Empty{}, status.Error(codes.PermissionDenied, err.Error()) | ||||
| 	case errors.Is(err, model.ErrCommentEditTimeout): | ||||
| 		return &emptypb.Empty{}, status.Error(codes.FailedPrecondition, err.Error()) | ||||
| 	case err != nil: | ||||
| 		return &emptypb.Empty{}, status.Error(codes.Internal, err.Error()) | ||||
| 	} | ||||
|  | ||||
| 	return &emptypb.Empty{}, nil | ||||
| } | ||||
|  | ||||
| func (s *Server) CommentGetByID(ctx context.Context, req *pb.GetCommentRequest) (*pb.GetCommentResponse, error) { | ||||
| 	comm, err := s.service.CommentGetByID(ctx, req.Id) | ||||
| 	if err != nil { | ||||
| 		return nil, fmt.Errorf("service.GetCommentByID: %w", err) | ||||
| 	} | ||||
|  | ||||
| 	return &pb.GetCommentResponse{ | ||||
| 		Comment: &pb.Comment{ | ||||
| 			Id:        comm.ID, | ||||
| 			UserId:    comm.UserID, | ||||
| 			Sku:       comm.SKU, | ||||
| 			Text:      comm.Text, | ||||
| 			CreatedAt: timestamppb.New(comm.CreatedAt), | ||||
| 		}, | ||||
| 	}, nil | ||||
| } | ||||
|  | ||||
| func (s *Server) CommentListBySKU(ctx context.Context, req *pb.ListBySkuRequest) (*pb.ListBySkuResponse, error) { | ||||
| 	comms, err := s.service.CommentListBySKU(ctx, req.Sku) | ||||
| 	if err != nil { | ||||
| 		return nil, fmt.Errorf("service.ListCommentsBySku: %w", err) | ||||
| 	} | ||||
|  | ||||
| 	comments := make([]*pb.Comment, len(comms)) | ||||
| 	for i, comm := range comms { | ||||
| 		comments[i] = &pb.Comment{ | ||||
| 			Id:        comm.ID, | ||||
| 			UserId:    comm.UserID, | ||||
| 			Sku:       comm.SKU, | ||||
| 			Text:      comm.Text, | ||||
| 			CreatedAt: timestamppb.New(comm.CreatedAt), | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	return &pb.ListBySkuResponse{ | ||||
| 		Comments: comments, | ||||
| 	}, nil | ||||
| } | ||||
|  | ||||
| func (s *Server) CommentListByUser(ctx context.Context, req *pb.ListByUserRequest) (*pb.ListByUserResponse, error) { | ||||
| 	comms, err := s.service.CommentListByUser(ctx, req.UserId) | ||||
| 	if err != nil { | ||||
| 		return nil, fmt.Errorf("service.ListCommentsByUser: %w", err) | ||||
| 	} | ||||
|  | ||||
| 	comments := make([]*pb.Comment, len(comms)) | ||||
| 	for i, comm := range comms { | ||||
| 		comments[i] = &pb.Comment{ | ||||
| 			Id:        comm.ID, | ||||
| 			UserId:    comm.UserID, | ||||
| 			Sku:       comm.SKU, | ||||
| 			Text:      comm.Text, | ||||
| 			CreatedAt: timestamppb.New(comm.CreatedAt), | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	return &pb.ListByUserResponse{ | ||||
| 		Comments: comments, | ||||
| 	}, nil | ||||
| } | ||||
| @@ -1,9 +1,11 @@ | ||||
| package entity | ||||
|  | ||||
| import "time" | ||||
|  | ||||
| type Comment struct { | ||||
| 	ID        int64 | ||||
| 	UserID    int64 | ||||
| 	SKU       int64 | ||||
| 	CreatedAt string | ||||
| 	CreatedAt time.Time | ||||
| 	Text      string | ||||
| } | ||||
|   | ||||
| @@ -1,22 +0,0 @@ | ||||
| package model | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"time" | ||||
| ) | ||||
|  | ||||
| type Comment struct { | ||||
| 	ID        int64 `validate:"gt=0"` | ||||
| 	UserID    int64 `validate:"gt=0"` | ||||
| 	SKU       int64 `validate:"gt=0"` | ||||
| 	CreatedAt time.Time | ||||
| 	Text      string `validate:"lte=255,gt=0"` | ||||
| } | ||||
|  | ||||
| func (c *Comment) Validate() error { | ||||
| 	if err := validate.Struct(c); err != nil { | ||||
| 		return fmt.Errorf("invalid requested values: %w", err) | ||||
| 	} | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
| @@ -3,3 +3,5 @@ package model | ||||
| import "errors" | ||||
|  | ||||
| var ErrCommentNotFound = errors.New("comment not found") | ||||
| var ErrCommentEditUserMismatch = errors.New("comment edit user mismatch") | ||||
| var ErrCommentEditTimeout = errors.New("comment edit timeout") | ||||
|   | ||||
| @@ -1,9 +0,0 @@ | ||||
| package model | ||||
|  | ||||
| import "github.com/go-playground/validator/v10" | ||||
|  | ||||
| var validate *validator.Validate | ||||
|  | ||||
| func init() { | ||||
| 	validate = validator.New() | ||||
| } | ||||
| @@ -1,3 +1,134 @@ | ||||
| package service | ||||
|  | ||||
| // TODO | ||||
| import ( | ||||
| 	"context" | ||||
| 	"fmt" | ||||
| 	"sort" | ||||
| 	"time" | ||||
|  | ||||
| 	"route256/comments/internal/domain/entity" | ||||
| 	"route256/comments/internal/domain/model" | ||||
| ) | ||||
|  | ||||
| type CommentRepository interface { | ||||
| 	GetCommentByID(ctx context.Context, id int64) (*entity.Comment, error) | ||||
| 	InsertComment(ctx context.Context, comment *entity.Comment) (*entity.Comment, error) | ||||
| 	ListCommentsBySku(ctx context.Context, sku int64) ([]*entity.Comment, error) | ||||
| 	ListCommentsByUser(ctx context.Context, userID int64) ([]*entity.Comment, error) | ||||
| 	UpdateComment(ctx context.Context, comment *entity.Comment) (*entity.Comment, error) | ||||
| } | ||||
|  | ||||
| type CommentsService struct { | ||||
| 	comments CommentRepository | ||||
|  | ||||
| 	timeout time.Duration | ||||
| } | ||||
|  | ||||
| func NewCommentsService(commRepo CommentRepository, timeout time.Duration) *CommentsService { | ||||
| 	return &CommentsService{ | ||||
| 		comments: commRepo, | ||||
| 		timeout:  timeout, | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func (s *CommentsService) checkTimeout(newTime, oldTime time.Time) error { | ||||
| 	if newTime.Sub(oldTime) >= s.timeout { | ||||
| 		return model.ErrCommentEditTimeout | ||||
| 	} | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (s *CommentsService) CommentCreate(ctx context.Context, comment *entity.Comment) (int64, error) { | ||||
| 	comm, err := s.comments.InsertComment(ctx, &entity.Comment{ | ||||
| 		UserID:    comment.UserID, | ||||
| 		SKU:       comment.SKU, | ||||
| 		CreatedAt: time.Now(), | ||||
| 		Text:      comment.Text, | ||||
| 	}) | ||||
| 	if err != nil { | ||||
| 		return 0, fmt.Errorf("repository.InsertComment: %w", err) | ||||
| 	} | ||||
|  | ||||
| 	return comm.ID, nil | ||||
| } | ||||
|  | ||||
| func (s *CommentsService) CommentGetByID(ctx context.Context, id int64) (*entity.Comment, error) { | ||||
| 	if id <= 0 { | ||||
| 		return nil, fmt.Errorf("comment id must be greater than 0") | ||||
| 	} | ||||
|  | ||||
| 	comm, err := s.comments.GetCommentByID(ctx, id) | ||||
| 	if err != nil { | ||||
| 		return nil, fmt.Errorf("repository.InsertComment: %w", err) | ||||
| 	} | ||||
|  | ||||
| 	return comm, nil | ||||
| } | ||||
|  | ||||
| func (s *CommentsService) CommentEdit(ctx context.Context, newComment *entity.Comment) error { | ||||
| 	if newComment.ID <= 0 { | ||||
| 		return fmt.Errorf("comment id must be greater than 0") | ||||
| 	} | ||||
|  | ||||
| 	oldComment, err := s.comments.GetCommentByID(ctx, newComment.ID) | ||||
| 	if err != nil { | ||||
| 		return fmt.Errorf("repository.GetCommentByID: %w", err) | ||||
| 	} | ||||
|  | ||||
| 	if oldComment.UserID != newComment.UserID { | ||||
| 		return model.ErrCommentEditUserMismatch | ||||
| 	} | ||||
|  | ||||
| 	comment := &entity.Comment{ | ||||
| 		ID:        newComment.ID, | ||||
| 		UserID:    newComment.UserID, | ||||
| 		SKU:       newComment.SKU, | ||||
| 		CreatedAt: time.Now(), | ||||
| 		Text:      newComment.Text, | ||||
| 	} | ||||
|  | ||||
| 	if err := s.checkTimeout(comment.CreatedAt, oldComment.CreatedAt); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	if _, err := s.comments.UpdateComment(ctx, comment); err != nil { | ||||
| 		return fmt.Errorf("repository.UpdateComment: %w", err) | ||||
| 	} | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (s *CommentsService) CommentListBySKU(ctx context.Context, sku int64) ([]*entity.Comment, error) { | ||||
| 	if sku <= 0 { | ||||
| 		return nil, fmt.Errorf("sku must be greater than 0") | ||||
| 	} | ||||
|  | ||||
| 	comms, err := s.comments.ListCommentsBySku(ctx, sku) | ||||
| 	if err != nil { | ||||
| 		return nil, fmt.Errorf("repository.ListCommentsBySku: %w", err) | ||||
| 	} | ||||
|  | ||||
| 	return comms, nil | ||||
| } | ||||
|  | ||||
| func (s *CommentsService) CommentListByUser(ctx context.Context, userID int64) ([]*entity.Comment, error) { | ||||
| 	if userID <= 0 { | ||||
| 		return nil, fmt.Errorf("userID must be greater than 0") | ||||
| 	} | ||||
|  | ||||
| 	comms, err := s.comments.ListCommentsByUser(ctx, userID) | ||||
| 	if err != nil { | ||||
| 		return nil, fmt.Errorf("repository.ListCommentsByUser: %w", err) | ||||
| 	} | ||||
|  | ||||
| 	sort.Slice(comms, func(i, j int) bool { | ||||
| 		if comms[i].CreatedAt.Equal(comms[j].CreatedAt) { | ||||
| 			return comms[i].UserID < comms[j].UserID | ||||
| 		} | ||||
|  | ||||
| 		return comms[i].CreatedAt.After(comms[j].CreatedAt) | ||||
| 	}) | ||||
|  | ||||
| 	return comms, nil | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 3ybacTuK
					3ybacTuK