You've already forked reloading-manager
fix tpyo (#8)
Reviewed-on: Siteworxpro/reloading-manager#8 Co-authored-by: Ron Rise <ron@siteworxpro.com> Co-committed-by: Ron Rise <ron@siteworxpro.com>
This commit was merged in pull request #8.
This commit is contained in:
@@ -38,6 +38,29 @@ jobs:
|
||||
cd frontend
|
||||
npm run build
|
||||
|
||||
golangci-lint:
|
||||
name: 🧪 🐹 GolangCI-Lint
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: 🛡️ 🔒 Add Siteworx CA Certificates
|
||||
run: |
|
||||
apt update && apt install -yq ca-certificates curl
|
||||
curl -Ls https://siteworxpro.com/hosted/Siteworx+Root+CA.pem -o /usr/local/share/ca-certificates/sw.crt
|
||||
update-ca-certificates
|
||||
- name: 📖 🔍 Checkout Repository Code
|
||||
uses: actions/checkout@v2
|
||||
with:
|
||||
fetch-depth: 1
|
||||
- name: ⚙️ 🐹 Set up Go Environment
|
||||
uses: actions/setup-go@v2
|
||||
with:
|
||||
go-version: '1.24.3'
|
||||
cache: true
|
||||
- name: ✅ 🧪 Run GolangCI-Lint
|
||||
uses: golangci/golangci-lint-action@v8.0.0
|
||||
with:
|
||||
working-directory: backend
|
||||
|
||||
test-go:
|
||||
env:
|
||||
GOPRIVATE: 'git.siteworxpro.com'
|
||||
|
||||
17
backend/.golangci.yml
Normal file
17
backend/.golangci.yml
Normal file
@@ -0,0 +1,17 @@
|
||||
version: "2"
|
||||
linters:
|
||||
default: standard
|
||||
enable:
|
||||
- whitespace
|
||||
- tagalign
|
||||
- reassign
|
||||
- bodyclose
|
||||
- contextcheck
|
||||
- containedctx
|
||||
- godot
|
||||
- usestdlibvars
|
||||
|
||||
formatters:
|
||||
settings:
|
||||
gofmt:
|
||||
simplify: true
|
||||
@@ -20,6 +20,10 @@ const (
|
||||
DbPassword Env.EnvironmentVariable = "DB_PASSWORD"
|
||||
)
|
||||
|
||||
type contextKeyType string
|
||||
|
||||
const dbContextKey contextKeyType = "dbcontext"
|
||||
|
||||
type Database struct {
|
||||
Db *pgx.Conn
|
||||
connected bool
|
||||
@@ -50,6 +54,24 @@ func (*Database) DSN(hidePassword bool) string {
|
||||
return fmt.Sprintf("postgres://%s:%s@%s:5432/%s%s", dbUser, dbPassword, dbHost, dbDatabase, extraParams)
|
||||
}
|
||||
|
||||
func NewWithContext(ctx context.Context) context.Context {
|
||||
db := GetNewDatabase()
|
||||
|
||||
return context.WithValue(ctx, dbContextKey, db)
|
||||
}
|
||||
|
||||
func NewFromContext(ctx context.Context) *Database {
|
||||
if ok := ctx.Value(dbContextKey); ok != nil {
|
||||
return ctx.Value(dbContextKey).(*Database)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func WithContext(ctx context.Context, database *Database) context.Context {
|
||||
return context.WithValue(ctx, dbContextKey, database)
|
||||
}
|
||||
|
||||
func GetNewDatabase() *Database {
|
||||
var dbSingleton Database
|
||||
|
||||
|
||||
@@ -11,6 +11,9 @@ import (
|
||||
|
||||
func (db *Database) Migrate() {
|
||||
sqlDB, err := sql.Open("postgres", db.DSN(false))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
driver, err := postgres.WithInstance(sqlDB, &postgres.Config{
|
||||
MigrationsTable: "schema_migrations",
|
||||
|
||||
@@ -9,8 +9,8 @@ require (
|
||||
github.com/go-sql-driver/mysql v1.9.2
|
||||
github.com/golang-migrate/migrate v3.5.4+incompatible
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/jackc/pgx/v5 v5.7.4
|
||||
github.com/labstack/echo/v4 v4.13.3
|
||||
github.com/jackc/pgx/v5 v5.7.5
|
||||
github.com/labstack/echo/v4 v4.13.4
|
||||
github.com/labstack/gommon v0.4.2
|
||||
)
|
||||
|
||||
@@ -41,11 +41,11 @@ require (
|
||||
github.com/stretchr/testify v1.10.0 // indirect
|
||||
github.com/valyala/bytebufferpool v1.0.0 // indirect
|
||||
github.com/valyala/fasttemplate v1.2.2 // indirect
|
||||
golang.org/x/crypto v0.37.0 // indirect
|
||||
golang.org/x/net v0.39.0 // indirect
|
||||
golang.org/x/sys v0.32.0 // indirect
|
||||
golang.org/x/text v0.24.0 // indirect
|
||||
golang.org/x/time v0.11.0 // indirect
|
||||
golang.org/x/crypto v0.39.0 // indirect
|
||||
golang.org/x/net v0.41.0 // indirect
|
||||
golang.org/x/sys v0.33.0 // indirect
|
||||
golang.org/x/text v0.26.0 // indirect
|
||||
golang.org/x/time v0.12.0 // indirect
|
||||
gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
@@ -42,6 +42,8 @@ github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7Ulw
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
|
||||
github.com/jackc/pgx/v5 v5.7.4 h1:9wKznZrhWa2QiHL+NjTSPP6yjl3451BX3imWDnokYlg=
|
||||
github.com/jackc/pgx/v5 v5.7.4/go.mod h1:ncY89UGWxg82EykZUwSpUKEfccBGGYq1xjrOpsbsfGQ=
|
||||
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/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
|
||||
@@ -52,6 +54,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/labstack/echo/v4 v4.13.3 h1:pwhpCPrTl5qry5HRdM5FwdXnhXSLSY+WE+YQSeCaafY=
|
||||
github.com/labstack/echo/v4 v4.13.3/go.mod h1:o90YNEeQWjDozo584l7AwhJMHN0bOC4tAfg+Xox9q5g=
|
||||
github.com/labstack/echo/v4 v4.13.4 h1:oTZZW+T3s9gAu5L8vmzihV7/lkXGZuITzTQkTEhcXEA=
|
||||
github.com/labstack/echo/v4 v4.13.4/go.mod h1:g63b33BZ5vZzcIUF8AtRH40DrTlXnx4UMC8rBdndmjQ=
|
||||
github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0=
|
||||
github.com/labstack/gommon v0.4.2/go.mod h1:QlUFxVM+SNXhDL/Z7YhocGIBYOiwB0mXm1+1bAPHPyU=
|
||||
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
|
||||
@@ -88,33 +92,45 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U
|
||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/crypto v0.37.0 h1:kJNSjF/Xp7kU0iB2Z+9viTPMW4EqqsrywMXLJOOsXSE=
|
||||
golang.org/x/crypto v0.37.0/go.mod h1:vg+k43peMZ0pUMhYmVAWysMK35e6ioLh3wB8ZCAfbVc=
|
||||
golang.org/x/crypto v0.39.0 h1:SHs+kF4LP+f+p14esP5jAoDpHU8Gu/v9lFRK6IT5imM=
|
||||
golang.org/x/crypto v0.39.0/go.mod h1:L+Xg3Wf6HoL4Bn4238Z6ft6KfEpN0tJGo53AAPC632U=
|
||||
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/mod v0.21.0 h1:vvrHzRwRfVKSiLrG+d4FMl/Qi4ukBCE6kZlTUkDYRT0=
|
||||
golang.org/x/mod v0.21.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
|
||||
golang.org/x/mod v0.25.0 h1:n7a+ZbQKQA/Ysbyb0/6IbB1H/X41mKgbhfv7AfG/44w=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
||||
golang.org/x/net v0.39.0 h1:ZCu7HMWDxpXpaiKdhzIfaltL9Lp31x/3fCP11bc6/fY=
|
||||
golang.org/x/net v0.39.0/go.mod h1:X7NRbYVEA+ewNkCNyJ513WmMdQ3BineSwVtN2zD/d+E=
|
||||
golang.org/x/net v0.41.0 h1:vBTly1HeNPEn3wtREYfy4GZ/NECgw2Cnl+nK6Nz3uvw=
|
||||
golang.org/x/net v0.41.0/go.mod h1:B/K4NNqkfmg07DQYrbwvSluqCJOOXwUjeb/5lOisjbA=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.13.0 h1:AauUjRAJ9OSnvULf/ARrrVywoJDy0YS2AwQ98I37610=
|
||||
golang.org/x/sync v0.13.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
|
||||
golang.org/x/sync v0.15.0 h1:KWH3jNZsfyT6xfAfKiz6MRNmd46ByHDYaZ7KSkCtdW8=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.32.0 h1:s77OFDvIQeibCmezSnk/q6iAfkdiQaJi4VzroCFrN20=
|
||||
golang.org/x/sys v0.32.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
||||
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.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.24.0 h1:dd5Bzh4yt5KYA8f9CJHCP4FB4D51c2c6JvN37xJJkJ0=
|
||||
golang.org/x/text v0.24.0/go.mod h1:L8rBsPeo2pSS+xqN0d5u2ikmjtmoJbDBT1b7nHvFCdU=
|
||||
golang.org/x/text v0.26.0 h1:P42AVeLghgTYr4+xUnTRKDMqpar+PtX7KWuNQL21L8M=
|
||||
golang.org/x/text v0.26.0/go.mod h1:QK15LZJUUQVJxhz7wXgxSy/CJaTFjd0G+YLonydOVQA=
|
||||
golang.org/x/time v0.11.0 h1:/bpjEDfN9tkoN/ryeYHnv5hcMlc8ncjMcM4XBk5NWV0=
|
||||
golang.org/x/time v0.11.0/go.mod h1:CDIdPxbZBQxdj6cxyCIdrNogrJKMJ7pr37NYpMcMDSg=
|
||||
golang.org/x/time v0.12.0 h1:ScB/8o8olJvc+CQPWrK3fPZNfh7qgwCrY0zJmoEQLSE=
|
||||
golang.org/x/time v0.12.0/go.mod h1:CDIdPxbZBQxdj6cxyCIdrNogrJKMJ7pr37NYpMcMDSg=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
|
||||
|
||||
@@ -21,12 +21,15 @@ type BulletResponse struct {
|
||||
|
||||
func Photo(c echo.Context) error {
|
||||
db := c.(*database.CustomContext).Db
|
||||
defer db.Db.Close(context.Background())
|
||||
|
||||
defer func() {
|
||||
_ = db.Db.Close(context.Background())
|
||||
}()
|
||||
|
||||
id := c.Param("id")
|
||||
uid := handlers.ParseUuidOrBadRequest(c, id)
|
||||
if uid == nil {
|
||||
return nil
|
||||
uid, err := handlers.ParseUuidOrBadRequest(c, id)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Invalid UUID format")
|
||||
}
|
||||
|
||||
byId, err := db.Bullets.GetBulletById(context.Background(), *uid)
|
||||
@@ -48,15 +51,18 @@ func Photo(c echo.Context) error {
|
||||
|
||||
func Delete(c echo.Context) error {
|
||||
db := c.(*database.CustomContext).Db
|
||||
defer db.Db.Close(context.Background())
|
||||
|
||||
defer func() {
|
||||
_ = db.Db.Close(context.Background())
|
||||
}()
|
||||
|
||||
id := c.Param("id")
|
||||
uid := handlers.ParseUuidOrBadRequest(c, id)
|
||||
if uid == nil {
|
||||
return nil
|
||||
uid, err := handlers.ParseUuidOrBadRequest(c, id)
|
||||
if err != nil || uid == nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Invalid UUID format")
|
||||
}
|
||||
|
||||
err := db.Bullets.DeleteBullet(context.Background(), *uid)
|
||||
err = db.Bullets.DeleteBullet(context.Background(), *uid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -66,12 +72,14 @@ func Delete(c echo.Context) error {
|
||||
|
||||
func Put(c echo.Context) error {
|
||||
db := c.(*database.CustomContext).Db
|
||||
defer db.Db.Close(context.Background())
|
||||
defer func() {
|
||||
_ = db.Db.Close(context.Background())
|
||||
}()
|
||||
|
||||
id := c.Param("id")
|
||||
uid := handlers.ParseUuidOrBadRequest(c, id)
|
||||
if uid == nil {
|
||||
return nil
|
||||
uid, err := handlers.ParseUuidOrBadRequest(c, id)
|
||||
if err != nil || uid == nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Invalid UUID format")
|
||||
}
|
||||
|
||||
byId, err := db.Bullets.GetBulletById(context.Background(), *uid)
|
||||
@@ -101,9 +109,9 @@ func Put(c echo.Context) error {
|
||||
weight, _ := strconv.ParseInt(c.FormValue("weight"), 10, 32)
|
||||
diameter, _ := strconv.ParseInt(c.FormValue("diameter"), 10, 32)
|
||||
manufacturerId := c.FormValue("manufacturer_id")
|
||||
manufacturerUid := handlers.ParseUuidOrBadRequest(c, manufacturerId)
|
||||
if manufacturerUid == nil {
|
||||
return nil
|
||||
manufacturerUid, err := handlers.ParseUuidOrBadRequest(c, manufacturerId)
|
||||
if err == nil || manufacturerUid == nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Invalid UUID format")
|
||||
}
|
||||
|
||||
name := c.FormValue("name")
|
||||
@@ -147,13 +155,15 @@ func Put(c echo.Context) error {
|
||||
|
||||
func Get(c echo.Context) error {
|
||||
db := c.(*database.CustomContext).Db
|
||||
defer db.Db.Close(context.Background())
|
||||
defer func() {
|
||||
_ = db.Db.Close(context.Background())
|
||||
}()
|
||||
|
||||
if c.Param("id") != "" {
|
||||
id := c.Param("id")
|
||||
uid := handlers.ParseUuidOrBadRequest(c, id)
|
||||
if uid == nil {
|
||||
return nil
|
||||
uid, err := handlers.ParseUuidOrBadRequest(c, id)
|
||||
if err != nil || uid == nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Invalid UUID format")
|
||||
}
|
||||
|
||||
byId, err := db.Bullets.GetBulletById(context.Background(), *uid)
|
||||
@@ -210,7 +220,6 @@ func Get(c echo.Context) error {
|
||||
}
|
||||
|
||||
func Post(c echo.Context) error {
|
||||
|
||||
file, err := c.FormFile("photo")
|
||||
if err != nil {
|
||||
c.Logger().Error(err)
|
||||
@@ -245,11 +254,13 @@ func Post(c echo.Context) error {
|
||||
}
|
||||
|
||||
db := c.(*database.CustomContext).Db
|
||||
defer db.Db.Close(context.Background())
|
||||
defer func() {
|
||||
_ = db.Db.Close(context.Background())
|
||||
}()
|
||||
|
||||
manufacturerUid := handlers.ParseUuidOrBadRequest(c, manufacturerId)
|
||||
if manufacturerUid == nil {
|
||||
return nil
|
||||
manufacturerUid, err := handlers.ParseUuidOrBadRequest(c, manufacturerId)
|
||||
if err != nil || manufacturerUid == nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Invalid UUID format")
|
||||
}
|
||||
|
||||
manufacturer, err := db.Manufacturer.GetById(context.Background(), *manufacturerUid)
|
||||
|
||||
@@ -22,7 +22,9 @@ type postRequest struct {
|
||||
|
||||
func Get(c echo.Context) error {
|
||||
db := c.(*database.CustomContext).Db
|
||||
defer db.Db.Close(context.Background())
|
||||
defer func() {
|
||||
_ = db.Db.Close(context.Background())
|
||||
}()
|
||||
|
||||
cartridges, err := db.Loads.GetCartridges(context.Background())
|
||||
if err != nil {
|
||||
@@ -45,7 +47,9 @@ func Get(c echo.Context) error {
|
||||
|
||||
func Post(c echo.Context) error {
|
||||
db := c.(*database.CustomContext).Db
|
||||
defer db.Db.Close(context.Background())
|
||||
defer func() {
|
||||
_ = db.Db.Close(context.Background())
|
||||
}()
|
||||
|
||||
req := postRequest{}
|
||||
|
||||
@@ -79,14 +83,16 @@ func Post(c echo.Context) error {
|
||||
|
||||
func Delete(c echo.Context) error {
|
||||
db := c.(*database.CustomContext).Db
|
||||
defer db.Db.Close(context.Background())
|
||||
defer func() {
|
||||
_ = db.Db.Close(context.Background())
|
||||
}()
|
||||
|
||||
uid := handlers.ParseUuidOrBadRequest(c, c.Param("id"))
|
||||
if uid == nil {
|
||||
return nil
|
||||
uid, err := handlers.ParseUuidOrBadRequest(c, c.Param("id"))
|
||||
if err != nil || uid == nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "invalid id")
|
||||
}
|
||||
|
||||
err := db.Loads.DeleteCartridge(context.Background(), *uid)
|
||||
err = db.Loads.DeleteCartridge(context.Background(), *uid)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -66,7 +66,9 @@ type ResultChan[T any] struct {
|
||||
|
||||
func Post(c echo.Context) error {
|
||||
db := c.(*database.CustomContext).Db
|
||||
defer db.Db.Close(context.Background())
|
||||
defer func() {
|
||||
_ = db.Db.Close(context.Background())
|
||||
}()
|
||||
|
||||
cartridgeID, err := handlers.ParseUuid(c.FormValue("cartridge_id"))
|
||||
if err != nil {
|
||||
@@ -136,7 +138,6 @@ func Post(c echo.Context) error {
|
||||
}
|
||||
|
||||
func Get(c echo.Context) error {
|
||||
|
||||
id := c.Param("id")
|
||||
cResults := make(chan ResultChan[[]loadResponseResults])
|
||||
|
||||
@@ -182,7 +183,6 @@ func Get(c echo.Context) error {
|
||||
} else {
|
||||
ch <- ResultChan[int64]{Result: total}
|
||||
}
|
||||
|
||||
}(cTotal)
|
||||
go execResultsQuery(cResults, c)
|
||||
|
||||
|
||||
@@ -21,13 +21,15 @@ type manufacturerResponse struct {
|
||||
|
||||
func Get(c echo.Context) error {
|
||||
db := c.(*database.CustomContext).Db
|
||||
defer db.Db.Close(context.Background())
|
||||
defer func() {
|
||||
_ = db.Db.Close(context.Background())
|
||||
}()
|
||||
|
||||
if c.Param("id") != "" {
|
||||
id := c.Param("id")
|
||||
uid := handlers.ParseUuidOrBadRequest(c, id)
|
||||
if uid == nil {
|
||||
return nil
|
||||
uid, err := handlers.ParseUuidOrBadRequest(c, id)
|
||||
if err != nil || uid == nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Invalid UUID format")
|
||||
}
|
||||
|
||||
byId, err := db.Manufacturer.GetById(context.Background(), *uid)
|
||||
@@ -73,15 +75,17 @@ func Get(c echo.Context) error {
|
||||
|
||||
func Delete(c echo.Context) error {
|
||||
db := c.(*database.CustomContext).Db
|
||||
defer db.Db.Close(context.Background())
|
||||
defer func() {
|
||||
_ = db.Db.Close(context.Background())
|
||||
}()
|
||||
|
||||
id := c.Param("id")
|
||||
uid := handlers.ParseUuidOrBadRequest(c, id)
|
||||
if uid == nil {
|
||||
return nil
|
||||
uid, err := handlers.ParseUuidOrBadRequest(c, id)
|
||||
if err != nil || uid == nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Invalid UUID format")
|
||||
}
|
||||
|
||||
_, err := db.Manufacturer.GetById(context.Background(), *uid)
|
||||
_, err = db.Manufacturer.GetById(context.Background(), *uid)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusNotFound, "Not found")
|
||||
}
|
||||
@@ -97,16 +101,18 @@ func Delete(c echo.Context) error {
|
||||
|
||||
func Post(c echo.Context) error {
|
||||
db := c.(*database.CustomContext).Db
|
||||
defer db.Db.Close(context.Background())
|
||||
defer func() {
|
||||
_ = db.Db.Close(context.Background())
|
||||
}()
|
||||
|
||||
if c.Param("id") != "" {
|
||||
id := c.Param("id")
|
||||
uid := handlers.ParseUuidOrBadRequest(c, id)
|
||||
if uid == nil {
|
||||
return nil
|
||||
uid, err := handlers.ParseUuidOrBadRequest(c, id)
|
||||
if err != nil || uid == nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Invalid UUID format")
|
||||
}
|
||||
|
||||
_, err := db.Manufacturer.GetById(context.Background(), *uid)
|
||||
_, err = db.Manufacturer.GetById(context.Background(), *uid)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusNotFound, "Not found")
|
||||
}
|
||||
|
||||
@@ -13,7 +13,9 @@ import (
|
||||
|
||||
func Photo(c echo.Context) error {
|
||||
db := c.(*database.CustomContext).Db
|
||||
defer db.Db.Close(context.Background())
|
||||
defer func() {
|
||||
_ = db.Db.Close(context.Background())
|
||||
}()
|
||||
|
||||
id := c.Param("id")
|
||||
uid, err := handlers.ParseUuid(id)
|
||||
@@ -41,7 +43,9 @@ func Photo(c echo.Context) error {
|
||||
func Delete(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
db := c.(*database.CustomContext).Db
|
||||
defer db.Db.Close(context.Background())
|
||||
defer func() {
|
||||
_ = db.Db.Close(context.Background())
|
||||
}()
|
||||
|
||||
uid, err := handlers.ParseUuid(id)
|
||||
if err != nil {
|
||||
@@ -58,7 +62,9 @@ func Delete(c echo.Context) error {
|
||||
|
||||
func Post(c echo.Context) error {
|
||||
db := c.(*database.CustomContext).Db
|
||||
defer db.Db.Close(context.Background())
|
||||
defer func() {
|
||||
_ = db.Db.Close(context.Background())
|
||||
}()
|
||||
|
||||
if c.Param("id") != "" {
|
||||
id := c.Param("id")
|
||||
@@ -163,7 +169,9 @@ func Post(c echo.Context) error {
|
||||
|
||||
func Get(c echo.Context) error {
|
||||
db := c.(*database.CustomContext).Db
|
||||
defer db.Db.Close(context.Background())
|
||||
defer func() {
|
||||
_ = db.Db.Close(context.Background())
|
||||
}()
|
||||
|
||||
if c.Param("id") != "" {
|
||||
id := c.Param("id")
|
||||
|
||||
@@ -3,6 +3,7 @@ package primers
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"git.siteworxpro.com/reloading-manager/backend/database"
|
||||
"git.siteworxpro.com/reloading-manager/backend/handlers"
|
||||
"git.siteworxpro.com/reloading-manager/backend/models/primers"
|
||||
@@ -19,7 +20,9 @@ type PrimerResponse struct {
|
||||
|
||||
func Delete(c echo.Context) error {
|
||||
db := c.(*database.CustomContext).Db
|
||||
defer db.Db.Close(context.Background())
|
||||
defer func() {
|
||||
_ = db.Db.Close(context.Background())
|
||||
}()
|
||||
|
||||
id := c.Param("id")
|
||||
uid, err := handlers.ParseUuid(id)
|
||||
@@ -37,13 +40,15 @@ func Delete(c echo.Context) error {
|
||||
|
||||
func Post(c echo.Context) error {
|
||||
db := c.(*database.CustomContext).Db
|
||||
defer db.Db.Close(context.Background())
|
||||
defer func() {
|
||||
_ = db.Db.Close(context.Background())
|
||||
}()
|
||||
|
||||
if c.Param("id") != "" {
|
||||
id := c.Param("id")
|
||||
uid := handlers.ParseUuidOrBadRequest(c, id)
|
||||
if uid == nil {
|
||||
return nil
|
||||
uid, err := handlers.ParseUuidOrBadRequest(c, id)
|
||||
if err != nil || uid == nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Invalid UUID.")
|
||||
}
|
||||
|
||||
p, err := db.Primer.GetPrimerById(context.Background(), *uid)
|
||||
@@ -56,9 +61,9 @@ func Post(c echo.Context) error {
|
||||
}
|
||||
|
||||
if c.FormValue("manufacturer_id") != "" {
|
||||
mid := handlers.ParseUuidOrBadRequest(c, c.FormValue("manufacturer_id"))
|
||||
if mid == nil {
|
||||
return nil
|
||||
mid, err := handlers.ParseUuidOrBadRequest(c, c.FormValue("manufacturer_id"))
|
||||
if err != nil || mid == nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Invalid Manufacturer ID.")
|
||||
}
|
||||
|
||||
p.ManufacturerID = *mid
|
||||
@@ -112,13 +117,14 @@ func Post(c echo.Context) error {
|
||||
if metaString == "" {
|
||||
metaString = "{}"
|
||||
}
|
||||
var meta json.RawMessage = []byte(metaString)
|
||||
|
||||
meta := []byte(metaString)
|
||||
|
||||
newUuid := uuid.New().String()
|
||||
uid, _ := handlers.ParseUuid(newUuid)
|
||||
mid := handlers.ParseUuidOrBadRequest(c, manufacturerId)
|
||||
if mid == nil {
|
||||
return nil
|
||||
mid, err := handlers.ParseUuidOrBadRequest(c, manufacturerId)
|
||||
if err != nil || mid == nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Invalid Manufacturer ID.")
|
||||
}
|
||||
|
||||
err = db.Primer.InsertPrimer(context.Background(), primers.InsertPrimerParams{
|
||||
@@ -145,12 +151,14 @@ func Post(c echo.Context) error {
|
||||
|
||||
func Photo(c echo.Context) error {
|
||||
db := c.(*database.CustomContext).Db
|
||||
defer db.Db.Close(context.Background())
|
||||
defer func() {
|
||||
_ = db.Db.Close(context.Background())
|
||||
}()
|
||||
|
||||
id := c.Param("id")
|
||||
uid := handlers.ParseUuidOrBadRequest(c, id)
|
||||
if uid == nil {
|
||||
return nil
|
||||
uid, err := handlers.ParseUuidOrBadRequest(c, id)
|
||||
if err != nil || uid == nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Invalid UUID.")
|
||||
}
|
||||
|
||||
byId, err := db.Primer.GetPrimerById(context.Background(), *uid)
|
||||
@@ -172,18 +180,20 @@ func Photo(c echo.Context) error {
|
||||
|
||||
func Get(c echo.Context) error {
|
||||
db := c.(*database.CustomContext).Db
|
||||
defer db.Db.Close(context.Background())
|
||||
defer func() {
|
||||
_ = db.Db.Close(context.Background())
|
||||
}()
|
||||
|
||||
if c.Param("id") != "" {
|
||||
id := c.Param("id")
|
||||
uid := handlers.ParseUuidOrBadRequest(c, id)
|
||||
if uid == nil {
|
||||
return nil
|
||||
uid, err := handlers.ParseUuidOrBadRequest(c, id)
|
||||
if err != nil || uid == nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Invalid UUID.")
|
||||
}
|
||||
|
||||
row, err := db.Primer.GetPrimerById(context.Background(), *uid)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("primer not found: %v", err)
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, handlers.Response[PrimerResponse]{
|
||||
|
||||
@@ -11,7 +11,6 @@ type TestContext struct {
|
||||
}
|
||||
|
||||
func (t TestContext) JSON(code int, i interface{}) error {
|
||||
|
||||
if code != 400 {
|
||||
t.t.Fatal("expected 400")
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
"github.com/labstack/echo/v4"
|
||||
@@ -23,7 +24,7 @@ func ParseUuidOrEmpty(s string) *pgtype.UUID {
|
||||
func ParseUuid(s string) (*pgtype.UUID, error) {
|
||||
uid, err := uuid.Parse(s)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("invalid UUID format: %v", err)
|
||||
}
|
||||
|
||||
return &pgtype.UUID{
|
||||
@@ -32,14 +33,11 @@ func ParseUuid(s string) (*pgtype.UUID, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
func ParseUuidOrBadRequest(c echo.Context, s string) *pgtype.UUID {
|
||||
func ParseUuidOrBadRequest(c echo.Context, s string) (*pgtype.UUID, error) {
|
||||
uid, err := ParseUuid(s)
|
||||
if err != nil {
|
||||
_ = BadRequest(c, "Invalid UUID.")
|
||||
|
||||
return nil
|
||||
return nil, BadRequest(c, fmt.Sprintf("invalid UUID. %v", err))
|
||||
}
|
||||
|
||||
return uid
|
||||
|
||||
return uid, nil
|
||||
}
|
||||
|
||||
@@ -114,7 +114,9 @@ func migrate(e *echo.Echo) {
|
||||
|
||||
db := database.GetNewDatabase()
|
||||
|
||||
defer db.Db.Close(context.Background())
|
||||
defer func() {
|
||||
_ = db.Db.Close(context.Background())
|
||||
}()
|
||||
|
||||
db.Migrate()
|
||||
e.Logger.Info("✅ Complete!")
|
||||
|
||||
Reference in New Issue
Block a user