move common code to pkg
This commit is contained in:
@@ -5,6 +5,7 @@ go 1.25.2
|
||||
require (
|
||||
github.com/jackc/pgx/v5 v5.7.6
|
||||
github.com/stretchr/testify v1.11.1
|
||||
pkg v0.0.0-00010101000000-000000000000
|
||||
)
|
||||
|
||||
require (
|
||||
@@ -20,3 +21,5 @@ require (
|
||||
golang.org/x/text v0.24.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
replace pkg => ../pkg
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
package domains
|
||||
|
||||
var (
|
||||
SQLSTATE_25P02 = "25P02"
|
||||
SQLSTATE_23505 = "23505"
|
||||
)
|
||||
@@ -1,36 +0,0 @@
|
||||
package domains
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type (
|
||||
BaseHandler struct{}
|
||||
)
|
||||
|
||||
func (bh *BaseHandler) ToJSON(w http.ResponseWriter, statusCode int, data any) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(statusCode)
|
||||
json.NewEncoder(w).Encode(data)
|
||||
}
|
||||
|
||||
func (bh *BaseHandler) ParamToInt(param string, defaultValue ...int) int {
|
||||
if param == "" {
|
||||
if len(defaultValue) > 0 {
|
||||
return defaultValue[0]
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
value, err := strconv.Atoi(param)
|
||||
if err != nil {
|
||||
if len(defaultValue) > 0 {
|
||||
return defaultValue[0]
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
@@ -8,13 +8,13 @@ import (
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"pkg"
|
||||
"servicea/internal/app"
|
||||
"servicea/internal/domains"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
domains.BaseHandler
|
||||
pkg.BaseHandler
|
||||
s *Service
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ package meteo
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"servicea/internal/domains"
|
||||
"pkg"
|
||||
"strings"
|
||||
|
||||
b "github.com/jackc/pgx/v5"
|
||||
@@ -78,7 +78,7 @@ func (pgx *pgxRepo) insertBatch(ctx context.Context, tx b.Tx, fileChecksum strin
|
||||
var batchID int
|
||||
err := tx.QueryRow(ctx, insertBatch, 0, fileChecksum).Scan(&batchID)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), domains.SQLSTATE_23505) {
|
||||
if strings.Contains(err.Error(), pkg.SQLSTATE_23505) {
|
||||
return 0, ErrRecordAlreadyExists
|
||||
}
|
||||
return 0, fmt.Errorf("error inserting batch: %w", err)
|
||||
@@ -107,7 +107,7 @@ func (pgx *pgxRepo) insertAcceptedMeteoData(ctx context.Context, tx b.Tx, batchI
|
||||
rowsInserted++
|
||||
if err != nil {
|
||||
results.Close()
|
||||
if strings.Contains(err.Error(), domains.SQLSTATE_23505) {
|
||||
if strings.Contains(err.Error(), pkg.SQLSTATE_23505) {
|
||||
return 0, ErrRecordAlreadyExists
|
||||
}
|
||||
return 0, fmt.Errorf("error executing batch command %d: %w", i, err)
|
||||
|
||||
Reference in New Issue
Block a user