You've already forked gun-manager-backend
Compare commits
16 Commits
ca56ca7b29
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
6e6bdc5709
|
|||
|
65af98bf03
|
|||
|
42bd95d804
|
|||
|
3c6750ab13
|
|||
|
61a31a6977
|
|||
|
5ac3032a16
|
|||
|
f5742527f6
|
|||
|
e292442e2c
|
|||
|
9c7d3ad787
|
|||
|
dbbeaa05f3
|
|||
|
fdc22b8896
|
|||
|
4c9380c548
|
|||
|
f6528d558f
|
|||
|
345134773f
|
|||
|
7a119a8b60
|
|||
|
04ebe9e855
|
5
.gitignore
vendored
5
.gitignore
vendored
@@ -1,4 +1,7 @@
|
||||
.DS_Store
|
||||
.idea/
|
||||
gun_inventory.sqlite
|
||||
*.sqlite
|
||||
gun-manager
|
||||
dist/
|
||||
|
||||
*.gpg
|
||||
@@ -1,15 +1,12 @@
|
||||
package Guns
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
sql2 "database/sql"
|
||||
"git.siteworxpro.com/gun-manager/Handlers"
|
||||
"git.siteworxpro.com/gun-manager/sql"
|
||||
"git.siteworxpro.com/gun-manager/sql/db"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/nfnt/resize"
|
||||
"image/jpeg"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
@@ -27,6 +24,7 @@ type gunPayload struct {
|
||||
}
|
||||
|
||||
type gunPost struct {
|
||||
Id int64 `json:"id"`
|
||||
Make string `json:"make" validate:"required"`
|
||||
Model string `json:"model" validate:"required"`
|
||||
SerialNumber string `json:"serial_number" validate:"required"`
|
||||
@@ -46,24 +44,31 @@ func Post(c echo.Context) error {
|
||||
insertRow := db.InsertGunParams{
|
||||
Make: sql2.NullString{
|
||||
String: gun.Make,
|
||||
Valid: true,
|
||||
},
|
||||
Model: sql2.NullString{
|
||||
String: gun.Model,
|
||||
Valid: true,
|
||||
},
|
||||
SerialNumber: sql2.NullString{
|
||||
String: gun.SerialNumber,
|
||||
Valid: true,
|
||||
},
|
||||
PurchaseAmount: sql2.NullInt64{
|
||||
Int64: gun.PurchaseAmount,
|
||||
Valid: true,
|
||||
},
|
||||
ValueAmount: sql2.NullInt64{
|
||||
Int64: gun.ValueAmount,
|
||||
Valid: true,
|
||||
},
|
||||
DatePurchased: sql2.NullString{
|
||||
String: gun.DatePurchased,
|
||||
Valid: true,
|
||||
},
|
||||
Notes: sql2.NullString{
|
||||
String: gun.Notes,
|
||||
Valid: true,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -73,9 +78,13 @@ func Post(c echo.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
err = c.JSON(http.StatusAccepted, Handlers.Response[struct{ Id int64 }]{
|
||||
err = c.JSON(http.StatusAccepted, Handlers.Response[struct {
|
||||
Id int64 `json:"id"`
|
||||
}]{
|
||||
Status: http.StatusText(http.StatusOK),
|
||||
Payload: struct{ Id int64 }{Id: id},
|
||||
Payload: struct {
|
||||
Id int64 `json:"id"`
|
||||
}{Id: id},
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -84,68 +93,76 @@ func Post(c echo.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetPhotoResize(c echo.Context) error {
|
||||
func DeleteById(c echo.Context) error {
|
||||
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
err := c.JSON(http.StatusNotFound, "")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
getDb := sql.GetDb()
|
||||
err = getDb.Queries.DeleteGunPhotosByGunId(context.Background(), sql2.NullInt64{
|
||||
Int64: id,
|
||||
Valid: true,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = getDb.Queries.DeleteGun(context.Background(), id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = c.JSON(http.StatusNoContent, "")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
size, err := strconv.ParseInt(c.Param("size"), 10, 64)
|
||||
if err != nil {
|
||||
err := c.JSON(http.StatusNotFound, "")
|
||||
func UpdateGun(c echo.Context) error {
|
||||
gun := new(gunPost)
|
||||
err := c.Bind(gun)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
getDb := sql.GetDb()
|
||||
photo, err := getDb.Queries.GetGunPhotoData(context.Background(), id)
|
||||
err = getDb.Queries.UpdateGun(context.Background(), db.UpdateGunParams{
|
||||
Make: sql2.NullString{
|
||||
String: gun.Make,
|
||||
Valid: true,
|
||||
},
|
||||
Model: sql2.NullString{
|
||||
String: gun.Model,
|
||||
Valid: true,
|
||||
},
|
||||
SerialNumber: sql2.NullString{
|
||||
String: gun.SerialNumber,
|
||||
Valid: true,
|
||||
},
|
||||
PurchaseAmount: sql2.NullInt64{
|
||||
Int64: gun.PurchaseAmount,
|
||||
Valid: true,
|
||||
},
|
||||
ValueAmount: sql2.NullInt64{
|
||||
Int64: gun.ValueAmount,
|
||||
Valid: true,
|
||||
},
|
||||
DatePurchased: sql2.NullString{
|
||||
String: gun.DatePurchased,
|
||||
Valid: true,
|
||||
},
|
||||
Notes: sql2.NullString{
|
||||
String: gun.Notes,
|
||||
Valid: true,
|
||||
},
|
||||
ID: gun.Id,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
img, err := jpeg.Decode(bytes.NewBuffer(photo))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
m := resize.Resize(uint(size), 0, img, resize.Bicubic)
|
||||
image := bytes.NewBuffer(make([]byte, 0))
|
||||
err = jpeg.Encode(image, m, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = c.String(http.StatusOK, image.String())
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetPhoto(c echo.Context) error {
|
||||
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
err := c.JSON(http.StatusNotFound, "")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
getDb := sql.GetDb()
|
||||
photo, err := getDb.Queries.GetGunPhotoData(context.Background(), id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = c.String(http.StatusOK, string(photo))
|
||||
err = c.JSON(http.StatusOK, "")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -156,10 +173,6 @@ func GetPhoto(c echo.Context) error {
|
||||
func GetById(c echo.Context) error {
|
||||
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
err := c.JSON(http.StatusNotFound, "")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -183,6 +196,10 @@ func GetById(c echo.Context) error {
|
||||
getDb := sql.GetDb()
|
||||
byId, err := getDb.Queries.GetGunById(context.Background(), id)
|
||||
if err != nil {
|
||||
err := c.JSON(http.StatusNotFound, "")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
156
Handlers/Guns/photos.go
Normal file
156
Handlers/Guns/photos.go
Normal file
@@ -0,0 +1,156 @@
|
||||
package Guns
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
sql2 "database/sql"
|
||||
"image/jpeg"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"git.siteworxpro.com/gun-manager/sql"
|
||||
"git.siteworxpro.com/gun-manager/sql/db"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/nfnt/resize"
|
||||
)
|
||||
|
||||
func GetPhotoResize(c echo.Context) error {
|
||||
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
err := c.JSON(http.StatusNotFound, "")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
size, err := strconv.ParseInt(c.Param("size"), 10, 64)
|
||||
if err != nil {
|
||||
err := c.JSON(http.StatusNotFound, "")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
getDb := sql.GetDb()
|
||||
photo, err := getDb.Queries.GetGunPhotoData(context.Background(), id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
img, err := jpeg.Decode(bytes.NewBuffer(photo))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
m := resize.Resize(uint(size), 0, img, resize.Bicubic)
|
||||
image := bytes.NewBuffer(make([]byte, 0))
|
||||
err = jpeg.Encode(image, m, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = c.String(http.StatusOK, image.String())
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func DeletePhoto(c echo.Context) error {
|
||||
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
getDb := sql.GetDb()
|
||||
err = getDb.Queries.DeleteGunPhotosById(context.Background(), id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = c.JSON(http.StatusNoContent, "")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func PostPhoto(c echo.Context) error {
|
||||
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
err := c.JSON(http.StatusNotFound, "")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
files, err := c.MultipartForm()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
getDb := sql.GetDb()
|
||||
byId, err := getDb.Queries.GetGunById(context.Background(), id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, handler := range files.File {
|
||||
for _, file := range handler {
|
||||
open, err := file.Open()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fileData := make([]byte, file.Size)
|
||||
_, err = open.Read(fileData)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = getDb.Queries.InsertGunPhoto(context.Background(), db.InsertGunPhotoParams{
|
||||
GunID: sql2.NullInt64{
|
||||
Int64: byId.ID,
|
||||
Valid: true,
|
||||
},
|
||||
Filename: sql2.NullString{
|
||||
String: file.Filename,
|
||||
Valid: true,
|
||||
},
|
||||
Photo: fileData,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetPhoto(c echo.Context) error {
|
||||
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
err := c.JSON(http.StatusNotFound, "")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
getDb := sql.GetDb()
|
||||
photo, err := getDb.Queries.GetGunPhotoData(context.Background(), id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = c.String(http.StatusOK, string(photo))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
package Photo
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func Get(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
38
go.mod
38
go.mod
@@ -1,23 +1,35 @@
|
||||
module git.siteworxpro.com/gun-manager
|
||||
|
||||
go 1.19
|
||||
go 1.24.4
|
||||
|
||||
require (
|
||||
github.com/labstack/echo/v4 v4.11.1
|
||||
github.com/mattn/go-sqlite3 v1.14.16
|
||||
github.com/labstack/echo/v4 v4.13.4
|
||||
github.com/mattn/go-sqlite3 v1.14.29
|
||||
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
|
||||
github.com/labstack/gommon v0.4.0 // indirect
|
||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||
github.com/mattn/go-isatty v0.0.19 // indirect
|
||||
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 // indirect
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
github.com/golang-migrate/migrate/v4 v4.18.3 // indirect
|
||||
github.com/google/uuid v1.6.0 // indirect
|
||||
github.com/hashicorp/errwrap v1.1.0 // indirect
|
||||
github.com/hashicorp/go-multierror v1.1.1 // indirect
|
||||
github.com/labstack/gommon v0.4.2 // indirect
|
||||
github.com/mattn/go-colorable v0.1.14 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/ncruces/go-strftime v0.1.9 // indirect
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||
github.com/valyala/bytebufferpool v1.0.0 // indirect
|
||||
github.com/valyala/fasttemplate v1.2.2 // indirect
|
||||
golang.org/x/crypto v0.11.0 // indirect
|
||||
golang.org/x/net v0.12.0 // indirect
|
||||
golang.org/x/sys v0.10.0 // indirect
|
||||
golang.org/x/text v0.11.0 // indirect
|
||||
golang.org/x/time v0.3.0 // indirect
|
||||
go.uber.org/atomic v1.7.0 // indirect
|
||||
golang.org/x/crypto v0.40.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b // indirect
|
||||
golang.org/x/net v0.42.0 // indirect
|
||||
golang.org/x/sys v0.34.0 // indirect
|
||||
golang.org/x/text v0.27.0 // indirect
|
||||
golang.org/x/time v0.12.0 // indirect
|
||||
modernc.org/libc v1.66.3 // indirect
|
||||
modernc.org/mathutil v1.7.1 // indirect
|
||||
modernc.org/memory v1.11.0 // indirect
|
||||
modernc.org/sqlite v1.38.1 // indirect
|
||||
)
|
||||
|
||||
70
go.sum
70
go.sum
@@ -1,49 +1,99 @@
|
||||
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/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
||||
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
|
||||
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
|
||||
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
|
||||
github.com/golang-migrate/migrate/v4 v4.18.3 h1:EYGkoOsvgHHfm5U/naS1RP/6PL/Xv3S4B/swMiAmDLs=
|
||||
github.com/golang-migrate/migrate/v4 v4.18.3/go.mod h1:99BKpIi6ruaaXRM1A77eqZ+FWPQ3cfRa+ZVy5bmWMaY=
|
||||
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/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=
|
||||
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
|
||||
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
|
||||
github.com/labstack/echo/v4 v4.11.1 h1:dEpLU2FLg4UVmvCGPuk/APjlH6GDpbEPti61srUUUs4=
|
||||
github.com/labstack/echo/v4 v4.11.1/go.mod h1:YuYRTSM3CHs2ybfrL8Px48bO6BAnYIN4l8wSTMP6BDQ=
|
||||
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.0 h1:y7cvthEAEbU0yHOf4axH8ZG2NH8knB9iNSoTO8dyIk8=
|
||||
github.com/labstack/gommon v0.4.0/go.mod h1:uW6kP17uPlLJsD3ijUYn3/M5bAxtlZhMI6m3MFxTMTM=
|
||||
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/mattn/go-colorable v0.1.11/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
|
||||
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-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
|
||||
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
|
||||
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
|
||||
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/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y=
|
||||
github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
|
||||
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/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM=
|
||||
github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
|
||||
github.com/mattn/go-sqlite3 v1.14.29 h1:1O6nRLJKvsi1H2Sj0Hzdfojwt8GiGKm+LOfLaBFaouQ=
|
||||
github.com/mattn/go-sqlite3 v1.14.29/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
|
||||
github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4=
|
||||
github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
|
||||
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ=
|
||||
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
|
||||
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/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
||||
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.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
|
||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
|
||||
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
|
||||
github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
|
||||
github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=
|
||||
github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
|
||||
golang.org/x/crypto v0.11.0 h1:6Ewdq3tDic1mg5xRO4milcWCfMVQhI4NkqWWvqejpuA=
|
||||
golang.org/x/crypto v0.11.0/go.mod h1:xgJhtzW8F9jGdVFWZESrid1U1bjeNy4zgy5cRr/CIio=
|
||||
golang.org/x/net v0.12.0 h1:cfawfvKITfUsFCeJIHJrbSxpeu/E81khclypR0GVT50=
|
||||
golang.org/x/net v0.12.0/go.mod h1:zEVYFnQC7m/vmpQFELhcD1EWkZlX69l4oqgmer6hfKA=
|
||||
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
|
||||
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
|
||||
golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk=
|
||||
golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw=
|
||||
golang.org/x/crypto v0.40.0 h1:r4x+VvoG5Fm+eJcxMaY8CQM7Lb0l1lsmjGBQ6s8BfKM=
|
||||
golang.org/x/crypto v0.40.0/go.mod h1:Qr1vMER5WyS2dfPHAlsOj01wgLbsyWtFn/aY+5+ZdxY=
|
||||
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b h1:M2rDM6z3Fhozi9O7NWsxAkg/yqS/lQJ6PmkyIV3YP+o=
|
||||
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b/go.mod h1:3//PLf8L/X+8b4vuAfHzxeRUl04Adcb341+IGKfnqS8=
|
||||
golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14=
|
||||
golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI=
|
||||
golang.org/x/net v0.42.0 h1:jzkYrhi3YQWD6MLBJcsklgQsoAcw89EcZbJw8Z614hs=
|
||||
golang.org/x/net v0.42.0/go.mod h1:FF1RA5d3u7nAYA4z2TkclSCKh68eSXtiFwcWQpPXdt8=
|
||||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
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.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
|
||||
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/text v0.11.0 h1:LAntKIrcmeSKERyiOh0XMV39LXS8IE9UL2yP7+f5ij4=
|
||||
golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
|
||||
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
|
||||
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.34.0 h1:H5Y5sJ2L2JRdyv7ROF1he/lPdvFsd0mJHFw2ThKHxLA=
|
||||
golang.org/x/sys v0.34.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
||||
golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
|
||||
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
|
||||
golang.org/x/text v0.27.0 h1:4fGWRpyh641NLlecmyl4LOe6yDdfaYNrGb2zdfo4JV4=
|
||||
golang.org/x/text v0.27.0/go.mod h1:1D28KMCvyooCX9hBiosv5Tz/+YLxj0j7XhWjpSUF7CU=
|
||||
golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4=
|
||||
golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||
golang.org/x/time v0.12.0 h1:ScB/8o8olJvc+CQPWrK3fPZNfh7qgwCrY0zJmoEQLSE=
|
||||
golang.org/x/time v0.12.0/go.mod h1:CDIdPxbZBQxdj6cxyCIdrNogrJKMJ7pr37NYpMcMDSg=
|
||||
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.0-20210107192922-496545a6307b/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=
|
||||
modernc.org/libc v1.66.3 h1:cfCbjTUcdsKyyZZfEUKfoHcP3S0Wkvz3jgSzByEWVCQ=
|
||||
modernc.org/libc v1.66.3/go.mod h1:XD9zO8kt59cANKvHPXpx7yS2ELPheAey0vjIuZOhOU8=
|
||||
modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU=
|
||||
modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg=
|
||||
modernc.org/memory v1.11.0 h1:o4QC8aMQzmcwCK3t3Ux/ZHmwFPzE6hf2Y5LbkRs+hbI=
|
||||
modernc.org/memory v1.11.0/go.mod h1:/JP4VbVC+K5sU2wZi9bHoq2MAkCnrt2r98UGeSK7Mjw=
|
||||
modernc.org/sqlite v1.38.1 h1:jNnIjleVta+DKSAr3TnkKK87EEhjPhBLzi6hvIX9Bas=
|
||||
modernc.org/sqlite v1.38.1/go.mod h1:cPTJYSlgg3Sfg046yBShXENNtPrWrDX8bsbAQBzgQ5E=
|
||||
|
||||
69
main.go
69
main.go
@@ -2,44 +2,91 @@ package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"git.siteworxpro.com/gun-manager/Handlers/Guns"
|
||||
"git.siteworxpro.com/gun-manager/sql"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v4/middleware"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"git.siteworxpro.com/gun-manager/Handlers/Guns"
|
||||
"git.siteworxpro.com/gun-manager/sql"
|
||||
_ "github.com/golang-migrate/migrate/v4/source/file"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v4/middleware"
|
||||
log2 "github.com/labstack/gommon/log"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var dbfile string
|
||||
var dbFile string
|
||||
|
||||
flag.StringVar(&dbfile, "database", "", "the database file to load")
|
||||
flag.StringVar(&dbFile, "database", "", "the database file to load")
|
||||
flag.Parse()
|
||||
|
||||
if dbfile == "" {
|
||||
if dbFile == "" {
|
||||
flag.Usage()
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
_, err := sql.NewDb(dbfile)
|
||||
e := echo.New()
|
||||
e.HideBanner = true
|
||||
e.HidePort = true
|
||||
|
||||
e.Logger.SetOutput(os.Stdout)
|
||||
e.Logger.SetLevel(log2.INFO)
|
||||
|
||||
db, err := sql.NewDb(dbFile)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
e := echo.New()
|
||||
e.Logger.Infof("Using database file: %s", dbFile)
|
||||
e.Logger.Infof("Running migrations for database file: %s", dbFile)
|
||||
err = db.Migrate()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
e.Logger.Infof("Database file %s is ready", dbFile)
|
||||
|
||||
var corsOriginsList []string
|
||||
corsOrigins := os.Getenv("CORS_ORIGINS")
|
||||
if corsOrigins != "" {
|
||||
corsOriginsList = strings.Split(corsOrigins, ",")
|
||||
} else {
|
||||
corsOriginsList = []string{
|
||||
"https://localhost",
|
||||
"https://127.0.0.1",
|
||||
"http://127.0.0.1:5173",
|
||||
"http://localhost:5173",
|
||||
"http://127.0.0.1:4173",
|
||||
"http://127.0.0.1:8000",
|
||||
}
|
||||
}
|
||||
|
||||
e.Use(middleware.Logger())
|
||||
e.Use(middleware.Recover())
|
||||
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
|
||||
AllowOrigins: []string{"localhost"},
|
||||
AllowMethods: nil,
|
||||
AllowOrigins: corsOriginsList,
|
||||
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
|
||||
AllowHeaders: []string{"Origin", "Content-Type", "Accept"},
|
||||
}))
|
||||
|
||||
// Serve static files from the "dist" directory
|
||||
e.Static("/", "dist")
|
||||
|
||||
// Gun management routes
|
||||
e.GET("/gun", Guns.Get)
|
||||
e.PUT("/gun/:id", Guns.UpdateGun)
|
||||
e.GET("/gun/:id", Guns.GetById)
|
||||
e.DELETE("/gun/:id", Guns.DeleteById)
|
||||
e.POST("/gun", Guns.Post)
|
||||
|
||||
// Photo download routes
|
||||
e.GET("/gun/photo/:id/:filename", Guns.GetPhoto)
|
||||
e.GET("/gun/photo/:id/:size/:filename", Guns.GetPhotoResize)
|
||||
|
||||
// Photo management routes
|
||||
e.POST("/gun/photo/:id", Guns.PostPhoto)
|
||||
e.DELETE("/gun/photo/:id", Guns.DeletePhoto)
|
||||
|
||||
e.Logger.Info("Starting server on port 8000")
|
||||
e.Logger.Fatal(e.Start(":8000"))
|
||||
}
|
||||
|
||||
2
migrations/000001_initial_db.down.sql
Normal file
2
migrations/000001_initial_db.down.sql
Normal file
@@ -0,0 +1,2 @@
|
||||
drop table photos;
|
||||
drop table guns;
|
||||
23
migrations/000001_initial_db.up.sql
Normal file
23
migrations/000001_initial_db.up.sql
Normal file
@@ -0,0 +1,23 @@
|
||||
create table if not exists guns
|
||||
(
|
||||
id integer
|
||||
constraint id
|
||||
primary key autoincrement,
|
||||
make text,
|
||||
model text,
|
||||
serial_number text,
|
||||
purchase_amount integer,
|
||||
value_amount integer,
|
||||
date_purchased text,
|
||||
notes text
|
||||
);
|
||||
|
||||
create table if not exists photos
|
||||
(
|
||||
id integer
|
||||
constraint id
|
||||
primary key autoincrement,
|
||||
gun_id integer,
|
||||
photo blob,
|
||||
filename text
|
||||
);
|
||||
350
openapi.yml
Normal file
350
openapi.yml
Normal file
@@ -0,0 +1,350 @@
|
||||
openapi: 3.1.0
|
||||
info:
|
||||
title: Gun API
|
||||
version: 1.0.0
|
||||
description: API for managing guns in a system.
|
||||
contact:
|
||||
name: Siteworx Pro
|
||||
url: https://siteworxpro.com
|
||||
email: websites@siteworxpro.com
|
||||
|
||||
servers:
|
||||
- url: http://localhost:8000
|
||||
description: Local Server
|
||||
|
||||
tags:
|
||||
- name: Gun Management
|
||||
description: Operations related to gun management
|
||||
- name: Photo Management
|
||||
description: Operations related to photo management
|
||||
|
||||
paths:
|
||||
/gun/{id}:
|
||||
delete:
|
||||
tags:
|
||||
- Gun Management
|
||||
operationId: deleteGunById
|
||||
description: Deletes a gun by its unique identifier.
|
||||
summary: Delete Gun by ID
|
||||
parameters:
|
||||
- name: id
|
||||
in: path
|
||||
required: true
|
||||
description: Unique identifier for the gun
|
||||
schema:
|
||||
type: string
|
||||
format: uuid
|
||||
responses:
|
||||
'404':
|
||||
description: Gun not found
|
||||
'400':
|
||||
description: Invalid ID supplied
|
||||
'204':
|
||||
description: Successfully deleted gun
|
||||
put:
|
||||
tags:
|
||||
- Gun Management
|
||||
operationId: updateGunById
|
||||
description: Updates a gun by its unique identifier.
|
||||
summary: Update Gun by ID
|
||||
parameters:
|
||||
- name: id
|
||||
in: path
|
||||
required: true
|
||||
description: Unique identifier for the gun
|
||||
schema:
|
||||
type: string
|
||||
format: uuid
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/Gun'
|
||||
responses:
|
||||
'404':
|
||||
description: Gun not found
|
||||
'400':
|
||||
description: Invalid ID supplied or invalid input data
|
||||
'200':
|
||||
description: Successfully updated gun'
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/Response'
|
||||
|
||||
get:
|
||||
tags:
|
||||
- Gun Management
|
||||
operationId: getGunById
|
||||
description: Retrieves a gun by its unique identifier.
|
||||
summary: Get Gun by ID
|
||||
parameters:
|
||||
- name: id
|
||||
in: path
|
||||
required: true
|
||||
description: Unique identifier for the gun
|
||||
schema:
|
||||
type: string
|
||||
format: uuid
|
||||
responses:
|
||||
'404':
|
||||
description: Gun not found
|
||||
'400':
|
||||
description: Invalid ID supplied
|
||||
'200':
|
||||
description: A single gun object'
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/Response'
|
||||
|
||||
/gun:
|
||||
post:
|
||||
tags:
|
||||
- Gun Management
|
||||
operationId: createGun
|
||||
description: Creates a new gun in the system.
|
||||
summary: Create a new Gun
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/Gun'
|
||||
responses:
|
||||
'201':
|
||||
description: Successfully created gun'
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/Response'
|
||||
'400':
|
||||
description: Invalid input data
|
||||
get:
|
||||
tags:
|
||||
- Gun Management
|
||||
operationId: getAllGuns
|
||||
description: Retrieves all guns in the system.
|
||||
summary: Get all Guns
|
||||
responses:
|
||||
'200':
|
||||
description: A list of guns'
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: array
|
||||
items:
|
||||
$ref: '#/components/schemas/Response'
|
||||
|
||||
/gun/photo/{id}/{filename}:
|
||||
get:
|
||||
tags:
|
||||
- Photo Management
|
||||
operationId: getGunPhotoById
|
||||
description: Retrieves a photo of a gun by its unique identifier.
|
||||
summary: Get Gun Photo by ID
|
||||
parameters:
|
||||
- name: id
|
||||
in: path
|
||||
required: true
|
||||
description: Unique identifier for the photo
|
||||
schema:
|
||||
type: string
|
||||
format: uuid
|
||||
responses:
|
||||
'404':
|
||||
description: Photo not found
|
||||
'400':
|
||||
description: Invalid ID supplied
|
||||
'200':
|
||||
description: A single photo object
|
||||
content:
|
||||
application/octet-stream:
|
||||
schema:
|
||||
type: string
|
||||
format: binary
|
||||
|
||||
/gun/photo/{id}/{size}/{filename}:
|
||||
get:
|
||||
tags:
|
||||
- Photo Management
|
||||
operationId: getGunPhotoByIdAndSize
|
||||
description: Retrieves a photo of a gun by its unique identifier and size.
|
||||
summary: Get Gun Photo by ID and Size
|
||||
parameters:
|
||||
- name: id
|
||||
in: path
|
||||
required: true
|
||||
description: Unique identifier for the photo
|
||||
schema:
|
||||
type: string
|
||||
format: uuid
|
||||
example: "uuid-1234-5678"
|
||||
- name: size
|
||||
in: path
|
||||
required: true
|
||||
description: Size of the photo
|
||||
schema:
|
||||
type: number
|
||||
example: 1024
|
||||
- name: filename
|
||||
in: path
|
||||
required: true
|
||||
description: Name of the photo file
|
||||
schema:
|
||||
type: string
|
||||
example: "gun_photo.jpg"
|
||||
responses:
|
||||
'404':
|
||||
description: Photo not found
|
||||
'400':
|
||||
description: Invalid ID or size supplied
|
||||
'200':
|
||||
description: A single photo object with specified size
|
||||
content:
|
||||
application/octet-stream:
|
||||
schema:
|
||||
type: string
|
||||
format: binary
|
||||
|
||||
/gun/photo/{id}:
|
||||
delete:
|
||||
tags:
|
||||
- Photo Management
|
||||
operationId: deleteGunPhotoById
|
||||
description: Deletes a photo of a gun by its unique identifier.
|
||||
summary: Delete Gun Photo by ID
|
||||
parameters:
|
||||
- name: id
|
||||
in: path
|
||||
required: true
|
||||
description: Unique identifier for the photo
|
||||
schema:
|
||||
type: string
|
||||
format: uuid
|
||||
responses:
|
||||
'404':
|
||||
description: Photo not found
|
||||
'400':
|
||||
description: Invalid ID supplied
|
||||
'204':
|
||||
description: Successfully deleted photo
|
||||
post:
|
||||
tags:
|
||||
- Photo Management
|
||||
operationId: uploadGunPhoto
|
||||
description: Uploads a photo for a gun.
|
||||
summary: Upload Gun Photo
|
||||
parameters:
|
||||
- name: id
|
||||
in: path
|
||||
required: true
|
||||
description: Unique identifier for the gun
|
||||
schema:
|
||||
type: string
|
||||
format: uuid
|
||||
example: "uuid-1234-5678"
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
multipart/form-data:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
file:
|
||||
type: string
|
||||
format: binary
|
||||
description: The photo file to upload
|
||||
responses:
|
||||
'201':
|
||||
description: Successfully uploaded photo'
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/Response'
|
||||
'400':
|
||||
description: Invalid input data or missing file
|
||||
'404':
|
||||
description: Gun not found
|
||||
|
||||
components:
|
||||
schemas:
|
||||
Photo:
|
||||
type: object
|
||||
properties:
|
||||
Id:
|
||||
readOnly: true
|
||||
type: string
|
||||
description: Unique identifier for the photo
|
||||
example: "uuid-1234-5678"
|
||||
format: uuid
|
||||
FileName:
|
||||
required: true
|
||||
type: string
|
||||
description: The name of the photo file
|
||||
example: "gun_photo.jpg"
|
||||
Gun:
|
||||
type: object
|
||||
properties:
|
||||
id:
|
||||
readOnly: true
|
||||
type: string
|
||||
description: Unique identifier for the gun
|
||||
example: "uuid-1234-5678"
|
||||
format: uuid
|
||||
Make:
|
||||
required: true
|
||||
type: string
|
||||
description: The make of the gun
|
||||
example: "Smith & Wesson"
|
||||
Model:
|
||||
required: true
|
||||
type: string
|
||||
description: The model of the gun
|
||||
example: "M&P Shield"
|
||||
PurchaseAmount:
|
||||
required: true
|
||||
type: string
|
||||
description: The purchase amount of the gun
|
||||
example: "500.00"
|
||||
ValueAmount:
|
||||
required: true
|
||||
type: string
|
||||
description: The value of the gun
|
||||
example: "10.00"
|
||||
DatePurchased:
|
||||
required: true
|
||||
type: string
|
||||
format: date
|
||||
description: The date the gun was purchased
|
||||
example: "2023-01-01"
|
||||
SerialNumber:
|
||||
required: true
|
||||
type: string
|
||||
description: The serial number of the gun
|
||||
example: "SN123456789"
|
||||
Notes:
|
||||
required: true
|
||||
type: string
|
||||
description: Additional notes about the gun
|
||||
example: "This is a test gun"
|
||||
Photos:
|
||||
readOnly: true
|
||||
type: array
|
||||
items:
|
||||
$ref: '#/components/schemas/Photo'
|
||||
|
||||
Response:
|
||||
type: object
|
||||
properties:
|
||||
status:
|
||||
type: string
|
||||
description: The status of the response
|
||||
data:
|
||||
type: object
|
||||
oneOf:
|
||||
- $ref: '#/components/schemas/Gun'
|
||||
- type: array
|
||||
items:
|
||||
$ref: '#/components/schemas/Gun'
|
||||
@@ -3,8 +3,12 @@ package sql
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
sqlc "git.siteworxpro.com/gun-manager/sql/db"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"github.com/golang-migrate/migrate/v4"
|
||||
"github.com/golang-migrate/migrate/v4/database/sqlite3"
|
||||
|
||||
_ "modernc.org/sqlite"
|
||||
)
|
||||
|
||||
type Db struct {
|
||||
@@ -20,7 +24,7 @@ func NewDb(file string) (*Db, error) {
|
||||
return &dbConnection, nil
|
||||
}
|
||||
|
||||
db, err := sql.Open("sqlite3", file)
|
||||
db, err := sql.Open("sqlite", file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -41,3 +45,22 @@ func NewDb(file string) (*Db, error) {
|
||||
func GetDb() *Db {
|
||||
return &dbConnection
|
||||
}
|
||||
|
||||
func (d *Db) Migrate() error {
|
||||
i, err := sqlite3.WithInstance(dbConnection.db, &sqlite3.Config{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
m, err := migrate.NewWithDatabaseInstance("file://migrations", "sqlite3", i)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = m.Up()
|
||||
if err != nil && !errors.Is(err, migrate.ErrNoChange) {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -10,6 +10,33 @@ import (
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
const deleteGun = `-- name: DeleteGun :exec
|
||||
DELETE from guns where id = ?
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteGun(ctx context.Context, id int64) error {
|
||||
_, err := q.db.ExecContext(ctx, deleteGun, id)
|
||||
return err
|
||||
}
|
||||
|
||||
const deleteGunPhotosByGunId = `-- name: DeleteGunPhotosByGunId :exec
|
||||
DELETE FROM photos where gun_id = ?
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteGunPhotosByGunId(ctx context.Context, gunID sql.NullInt64) error {
|
||||
_, err := q.db.ExecContext(ctx, deleteGunPhotosByGunId, gunID)
|
||||
return err
|
||||
}
|
||||
|
||||
const deleteGunPhotosById = `-- name: DeleteGunPhotosById :exec
|
||||
DELETE FROM photos where id = ?
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteGunPhotosById(ctx context.Context, id int64) error {
|
||||
_, err := q.db.ExecContext(ctx, deleteGunPhotosById, id)
|
||||
return err
|
||||
}
|
||||
|
||||
const getAllGuns = `-- name: GetAllGuns :many
|
||||
SELECT id, make, model, value_amount
|
||||
from guns
|
||||
@@ -146,3 +173,49 @@ func (q *Queries) InsertGun(ctx context.Context, arg InsertGunParams) (int64, er
|
||||
err := row.Scan(&id)
|
||||
return id, err
|
||||
}
|
||||
|
||||
const insertGunPhoto = `-- name: InsertGunPhoto :exec
|
||||
INSERT into photos (gun_id, photo, filename) values (?, ?, ?)
|
||||
`
|
||||
|
||||
type InsertGunPhotoParams struct {
|
||||
GunID sql.NullInt64 `json:"gun_id"`
|
||||
Photo []byte `json:"photo"`
|
||||
Filename sql.NullString `json:"filename"`
|
||||
}
|
||||
|
||||
func (q *Queries) InsertGunPhoto(ctx context.Context, arg InsertGunPhotoParams) error {
|
||||
_, err := q.db.ExecContext(ctx, insertGunPhoto, arg.GunID, arg.Photo, arg.Filename)
|
||||
return err
|
||||
}
|
||||
|
||||
const updateGun = `-- name: UpdateGun :exec
|
||||
UPDATE guns
|
||||
set make = ?, model = ?, serial_number = ?, purchase_amount = ?, value_amount = ?, date_purchased = ?, notes = ?
|
||||
where id = ?
|
||||
`
|
||||
|
||||
type UpdateGunParams struct {
|
||||
Make sql.NullString `json:"make"`
|
||||
Model sql.NullString `json:"model"`
|
||||
SerialNumber sql.NullString `json:"serial_number"`
|
||||
PurchaseAmount sql.NullInt64 `json:"purchase_amount"`
|
||||
ValueAmount sql.NullInt64 `json:"value_amount"`
|
||||
DatePurchased sql.NullString `json:"date_purchased"`
|
||||
Notes sql.NullString `json:"notes"`
|
||||
ID int64 `json:"id"`
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateGun(ctx context.Context, arg UpdateGunParams) error {
|
||||
_, err := q.db.ExecContext(ctx, updateGun,
|
||||
arg.Make,
|
||||
arg.Model,
|
||||
arg.SerialNumber,
|
||||
arg.PurchaseAmount,
|
||||
arg.ValueAmount,
|
||||
arg.DatePurchased,
|
||||
arg.Notes,
|
||||
arg.ID,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -17,5 +17,21 @@ order by id desc;
|
||||
-- name: InsertGun :one
|
||||
INSERT into guns
|
||||
(make, model, serial_number, purchase_amount, value_amount, date_purchased, notes) VALUES
|
||||
(?, ?, ?, ?, ?, ?, ?) returning id
|
||||
(?, ?, ?, ?, ?, ?, ?) returning id;
|
||||
|
||||
-- name: UpdateGun :exec
|
||||
UPDATE guns
|
||||
set make = ?, model = ?, serial_number = ?, purchase_amount = ?, value_amount = ?, date_purchased = ?, notes = ?
|
||||
where id = ?;
|
||||
|
||||
-- name: DeleteGun :exec
|
||||
DELETE from guns where id = ?;
|
||||
|
||||
-- name: DeleteGunPhotosByGunId :exec
|
||||
DELETE FROM photos where gun_id = ?;
|
||||
|
||||
-- name: DeleteGunPhotosById :exec
|
||||
DELETE FROM photos where id = ?;
|
||||
|
||||
-- name: InsertGunPhoto :exec
|
||||
INSERT into photos (gun_id, photo, filename) values (?, ?, ?);
|
||||
Reference in New Issue
Block a user