commit
This commit is contained in:
@@ -1,27 +0,0 @@
|
||||
package Guns
|
||||
|
||||
import (
|
||||
"git.siteworxpro.com/gun-manager/sql"
|
||||
"github.com/gorilla/mux"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func Edit(w http.ResponseWriter, r *http.Request) {
|
||||
db := sql.GetDb()
|
||||
id, _ := strconv.ParseInt(mux.Vars(r)["id"], 10, 32)
|
||||
|
||||
gun, err := db.GunById(uint(id))
|
||||
if err != nil {
|
||||
w.WriteHeader(404)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
t, err := template.ParseFiles("templates/Index.gohtml")
|
||||
err = t.Execute(w, gun)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
@@ -1,37 +1,206 @@
|
||||
package Guns
|
||||
|
||||
import (
|
||||
"context"
|
||||
sql2 "database/sql"
|
||||
"git.siteworxpro.com/gun-manager/Handlers"
|
||||
"git.siteworxpro.com/gun-manager/sql"
|
||||
"html/template"
|
||||
"git.siteworxpro.com/gun-manager/sql/db"
|
||||
"github.com/labstack/echo/v4"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func Get(w http.ResponseWriter, r *http.Request) {
|
||||
db := sql.GetDb()
|
||||
|
||||
guns := db.AllGuns()
|
||||
|
||||
t, err := template.ParseFiles("templates/Index.gohtml")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
var total uint = 0
|
||||
for _, gun := range guns {
|
||||
total += gun.ValueAmount
|
||||
}
|
||||
|
||||
data := struct {
|
||||
Guns []sql.Gun
|
||||
Total uint
|
||||
}{
|
||||
Guns: guns,
|
||||
Total: total,
|
||||
}
|
||||
|
||||
err = t.Execute(w, data)
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
type gunResp struct {
|
||||
Status string `json:"status"`
|
||||
PayLoad []gunPayload `json:"payload"`
|
||||
}
|
||||
|
||||
type gunPayload struct {
|
||||
Id int64 `json:"id"`
|
||||
Make string `json:"make"`
|
||||
Model string `json:"model"`
|
||||
ValueAmount int64 `json:"value_amount"`
|
||||
}
|
||||
|
||||
type gunPost struct {
|
||||
Make string `json:"make" validate:"required"`
|
||||
Model string `json:"model" validate:"required"`
|
||||
SerialNumber string `json:"serial_number" validate:"required"`
|
||||
PurchaseAmount int64 `json:"purchase_amount" validate:"required"`
|
||||
ValueAmount int64 `json:"value_amount" validate:"required"`
|
||||
DatePurchased string `json:"date_purchased" validate:"required"`
|
||||
Notes string `json:"notes" validate:"required"`
|
||||
}
|
||||
|
||||
func Post(c echo.Context) error {
|
||||
gun := new(gunPost)
|
||||
err := c.Bind(gun)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
insertRow := db.InsertGunParams{
|
||||
Make: sql2.NullString{
|
||||
String: gun.Make,
|
||||
},
|
||||
Model: sql2.NullString{
|
||||
String: gun.Model,
|
||||
},
|
||||
SerialNumber: sql2.NullString{
|
||||
String: gun.SerialNumber,
|
||||
},
|
||||
PurchaseAmount: sql2.NullInt64{
|
||||
Int64: gun.PurchaseAmount,
|
||||
},
|
||||
ValueAmount: sql2.NullInt64{
|
||||
Int64: gun.ValueAmount,
|
||||
},
|
||||
DatePurchased: sql2.NullString{
|
||||
String: gun.DatePurchased,
|
||||
},
|
||||
Notes: sql2.NullString{
|
||||
String: gun.Notes,
|
||||
},
|
||||
}
|
||||
|
||||
getDb := sql.GetDb()
|
||||
id, err := getDb.Queries.InsertGun(context.Background(), insertRow)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = c.JSON(http.StatusAccepted, Handlers.Response[struct{ Id int64 }]{
|
||||
Status: http.StatusText(http.StatusOK),
|
||||
Payload: struct{ Id int64 }{Id: id},
|
||||
})
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
type photo struct {
|
||||
Id int64 `json:"id"`
|
||||
FileName string `json:"file_name"`
|
||||
}
|
||||
|
||||
type gunResponse struct {
|
||||
Id int64 `json:"id"`
|
||||
Make string `json:"make"`
|
||||
Model string `json:"model"`
|
||||
SerialNumber string `json:"serial_number"`
|
||||
PurchaseAmount int64 `json:"purchase_amount"`
|
||||
ValueAmount int64 `json:"value_amount"`
|
||||
DatePurchased string `json:"date_purchased"`
|
||||
Notes string `json:"notes"`
|
||||
Photos []photo `json:"photos"`
|
||||
}
|
||||
|
||||
getDb := sql.GetDb()
|
||||
byId, err := getDb.Queries.GetGunById(context.Background(), id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
photos, err := getDb.Queries.GetGunPhotos(context.Background(), sql2.NullInt64{Int64: byId.ID, Valid: true})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
respData := gunResponse{
|
||||
Id: id,
|
||||
Make: byId.Make.String,
|
||||
Model: byId.Model.String,
|
||||
SerialNumber: byId.SerialNumber.String,
|
||||
PurchaseAmount: byId.PurchaseAmount.Int64,
|
||||
ValueAmount: byId.ValueAmount.Int64,
|
||||
DatePurchased: byId.DatePurchased.String,
|
||||
Notes: byId.Notes.String,
|
||||
}
|
||||
|
||||
for _, photoData := range photos {
|
||||
respData.Photos = append(respData.Photos, photo{
|
||||
Id: photoData.ID,
|
||||
FileName: photoData.Filename.String,
|
||||
})
|
||||
}
|
||||
|
||||
resp := Handlers.Response[gunResponse]{
|
||||
Status: http.StatusText(http.StatusOK),
|
||||
Payload: respData,
|
||||
}
|
||||
|
||||
err = c.JSON(http.StatusOK, resp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func Get(c echo.Context) error {
|
||||
|
||||
getDb := sql.GetDb()
|
||||
guns, err := getDb.Queries.GetAllGuns(context.Background())
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var resp gunResp
|
||||
resp.Status = http.StatusText(http.StatusOK)
|
||||
for _, gun := range guns {
|
||||
resp.PayLoad = append(
|
||||
resp.PayLoad,
|
||||
gunPayload{
|
||||
Id: gun.ID,
|
||||
Make: gun.Make.String,
|
||||
Model: gun.Model.String,
|
||||
ValueAmount: gun.ValueAmount.Int64,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
err = c.JSON(http.StatusOK, resp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@@ -1,20 +1,8 @@
|
||||
package Photo
|
||||
|
||||
import (
|
||||
"git.siteworxpro.com/gun-manager/sql"
|
||||
"github.com/gorilla/mux"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func Get(w http.ResponseWriter, r *http.Request) {
|
||||
db := sql.GetDb()
|
||||
id, _ := strconv.ParseInt(mux.Vars(r)["id"], 10, 32)
|
||||
|
||||
photo := db.GetPhoto(uint(id))
|
||||
|
||||
_, err := w.Write([]byte(photo))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
6
Handlers/response.go
Normal file
6
Handlers/response.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package Handlers
|
||||
|
||||
type Response[T any] struct {
|
||||
Status string `json:"status"`
|
||||
Payload T `json:"payload"`
|
||||
}
|
18
go.mod
18
go.mod
@@ -3,6 +3,20 @@ module git.siteworxpro.com/gun-manager
|
||||
go 1.19
|
||||
|
||||
require (
|
||||
github.com/gorilla/mux v1.8.0 // indirect
|
||||
github.com/mattn/go-sqlite3 v1.14.16 // indirect
|
||||
github.com/labstack/echo/v4 v4.11.1
|
||||
github.com/mattn/go-sqlite3 v1.14.16
|
||||
)
|
||||
|
||||
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/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
|
||||
)
|
||||
|
47
go.sum
47
go.sum
@@ -1,4 +1,47 @@
|
||||
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
|
||||
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
|
||||
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/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/labstack/echo/v4 v4.11.1 h1:dEpLU2FLg4UVmvCGPuk/APjlH6GDpbEPti61srUUUs4=
|
||||
github.com/labstack/echo/v4 v4.11.1/go.mod h1:YuYRTSM3CHs2ybfrL8Px48bO6BAnYIN4l8wSTMP6BDQ=
|
||||
github.com/labstack/gommon v0.4.0 h1:y7cvthEAEbU0yHOf4axH8ZG2NH8knB9iNSoTO8dyIk8=
|
||||
github.com/labstack/gommon v0.4.0/go.mod h1:uW6kP17uPlLJsD3ijUYn3/M5bAxtlZhMI6m3MFxTMTM=
|
||||
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-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/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
|
||||
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=
|
||||
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/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4=
|
||||
golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||
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=
|
||||
|
30
main.go
30
main.go
@@ -3,13 +3,11 @@ package main
|
||||
import (
|
||||
"flag"
|
||||
"git.siteworxpro.com/gun-manager/Handlers/Guns"
|
||||
"git.siteworxpro.com/gun-manager/Handlers/Photo"
|
||||
"git.siteworxpro.com/gun-manager/sql"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v4/middleware"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -29,18 +27,18 @@ func main() {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
r := mux.NewRouter()
|
||||
r.HandleFunc("/", Guns.Get).Methods("GET")
|
||||
r.HandleFunc("/photo/{id}/{fileName}", Photo.Get).Methods("GET")
|
||||
e := echo.New()
|
||||
e.Use(middleware.Logger())
|
||||
e.Use(middleware.Recover())
|
||||
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
|
||||
AllowOrigins: []string{"localhost"},
|
||||
AllowMethods: nil,
|
||||
}))
|
||||
|
||||
srv := &http.Server{
|
||||
Handler: r,
|
||||
Addr: "0.0.0.0:8000",
|
||||
WriteTimeout: 15 * time.Second,
|
||||
ReadTimeout: 15 * time.Second,
|
||||
}
|
||||
e.GET("/gun", Guns.Get)
|
||||
e.GET("/gun/:id", Guns.GetById)
|
||||
e.POST("/gun", Guns.Post)
|
||||
e.GET("/gun/photo/:id/:filename", Guns.GetPhoto)
|
||||
|
||||
log.Println("Starting Server: 0.0.0.0:8000")
|
||||
|
||||
log.Fatal(srv.ListenAndServe())
|
||||
e.Logger.Fatal(e.Start(":8000"))
|
||||
}
|
||||
|
@@ -3,17 +3,19 @@ package sql
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
sqlc "git.siteworxpro.com/gun-manager/sql/db"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
type SqlDb struct {
|
||||
type Db struct {
|
||||
db *sql.DB
|
||||
created bool
|
||||
Queries *sqlc.Queries
|
||||
}
|
||||
|
||||
var dbConnection SqlDb
|
||||
var dbConnection Db
|
||||
|
||||
func NewDb(file string) (*SqlDb, error) {
|
||||
func NewDb(file string) (*Db, error) {
|
||||
if dbConnection.created {
|
||||
return &dbConnection, nil
|
||||
}
|
||||
@@ -31,9 +33,11 @@ func NewDb(file string) (*SqlDb, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
dbConnection.Queries = sqlc.New(db)
|
||||
|
||||
return &dbConnection, nil
|
||||
}
|
||||
|
||||
func GetDb() *SqlDb {
|
||||
func GetDb() *Db {
|
||||
return &dbConnection
|
||||
}
|
||||
|
31
sql/db/db.go
Normal file
31
sql/db/db.go
Normal file
@@ -0,0 +1,31 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.20.0
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
type DBTX interface {
|
||||
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
|
||||
PrepareContext(context.Context, string) (*sql.Stmt, error)
|
||||
QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
|
||||
QueryRowContext(context.Context, string, ...interface{}) *sql.Row
|
||||
}
|
||||
|
||||
func New(db DBTX) *Queries {
|
||||
return &Queries{db: db}
|
||||
}
|
||||
|
||||
type Queries struct {
|
||||
db DBTX
|
||||
}
|
||||
|
||||
func (q *Queries) WithTx(tx *sql.Tx) *Queries {
|
||||
return &Queries{
|
||||
db: tx,
|
||||
}
|
||||
}
|
27
sql/db/models.go
Normal file
27
sql/db/models.go
Normal file
@@ -0,0 +1,27 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.20.0
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
type Gun struct {
|
||||
ID int64 `json:"id"`
|
||||
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"`
|
||||
}
|
||||
|
||||
type Photo struct {
|
||||
ID int64 `json:"id"`
|
||||
GunID sql.NullInt64 `json:"gun_id"`
|
||||
Photo []byte `json:"photo"`
|
||||
Filename sql.NullString `json:"filename"`
|
||||
}
|
148
sql/db/query.sql.go
Normal file
148
sql/db/query.sql.go
Normal file
@@ -0,0 +1,148 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.20.0
|
||||
// source: query.sql
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
const getAllGuns = `-- name: GetAllGuns :many
|
||||
SELECT id, make, model, value_amount
|
||||
from guns
|
||||
order by id desc
|
||||
`
|
||||
|
||||
type GetAllGunsRow struct {
|
||||
ID int64 `json:"id"`
|
||||
Make sql.NullString `json:"make"`
|
||||
Model sql.NullString `json:"model"`
|
||||
ValueAmount sql.NullInt64 `json:"value_amount"`
|
||||
}
|
||||
|
||||
func (q *Queries) GetAllGuns(ctx context.Context) ([]GetAllGunsRow, error) {
|
||||
rows, err := q.db.QueryContext(ctx, getAllGuns)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []GetAllGunsRow
|
||||
for rows.Next() {
|
||||
var i GetAllGunsRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.Make,
|
||||
&i.Model,
|
||||
&i.ValueAmount,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const getGunById = `-- name: GetGunById :one
|
||||
SELECT guns.id as id, make, model, serial_number, purchase_amount, value_amount, date_purchased, notes
|
||||
from guns
|
||||
where guns.id = ?
|
||||
`
|
||||
|
||||
func (q *Queries) GetGunById(ctx context.Context, id int64) (Gun, error) {
|
||||
row := q.db.QueryRowContext(ctx, getGunById, id)
|
||||
var i Gun
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Make,
|
||||
&i.Model,
|
||||
&i.SerialNumber,
|
||||
&i.PurchaseAmount,
|
||||
&i.ValueAmount,
|
||||
&i.DatePurchased,
|
||||
&i.Notes,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getGunPhotoData = `-- name: GetGunPhotoData :one
|
||||
select photo from photos where id = ?
|
||||
`
|
||||
|
||||
func (q *Queries) GetGunPhotoData(ctx context.Context, id int64) ([]byte, error) {
|
||||
row := q.db.QueryRowContext(ctx, getGunPhotoData, id)
|
||||
var photo []byte
|
||||
err := row.Scan(&photo)
|
||||
return photo, err
|
||||
}
|
||||
|
||||
const getGunPhotos = `-- name: GetGunPhotos :many
|
||||
SELECT id, filename from photos where gun_id = ?
|
||||
`
|
||||
|
||||
type GetGunPhotosRow struct {
|
||||
ID int64 `json:"id"`
|
||||
Filename sql.NullString `json:"filename"`
|
||||
}
|
||||
|
||||
func (q *Queries) GetGunPhotos(ctx context.Context, gunID sql.NullInt64) ([]GetGunPhotosRow, error) {
|
||||
rows, err := q.db.QueryContext(ctx, getGunPhotos, gunID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []GetGunPhotosRow
|
||||
for rows.Next() {
|
||||
var i GetGunPhotosRow
|
||||
if err := rows.Scan(&i.ID, &i.Filename); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const insertGun = `-- name: InsertGun :one
|
||||
INSERT into guns
|
||||
(make, model, serial_number, purchase_amount, value_amount, date_purchased, notes) VALUES
|
||||
(?, ?, ?, ?, ?, ?, ?) returning id
|
||||
`
|
||||
|
||||
type InsertGunParams 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"`
|
||||
}
|
||||
|
||||
func (q *Queries) InsertGun(ctx context.Context, arg InsertGunParams) (int64, error) {
|
||||
row := q.db.QueryRowContext(ctx, insertGun,
|
||||
arg.Make,
|
||||
arg.Model,
|
||||
arg.SerialNumber,
|
||||
arg.PurchaseAmount,
|
||||
arg.ValueAmount,
|
||||
arg.DatePurchased,
|
||||
arg.Notes,
|
||||
)
|
||||
var id int64
|
||||
err := row.Scan(&id)
|
||||
return id, err
|
||||
}
|
70
sql/guns.go
70
sql/guns.go
@@ -1,70 +0,0 @@
|
||||
package sql
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"log"
|
||||
)
|
||||
|
||||
func (dbClient *SqlDb) AllGuns() []Gun {
|
||||
|
||||
var guns []Gun
|
||||
|
||||
rows, _ := dbClient.db.Query("select guns.*, p.id, p.gun_id, p.filename from guns join photos p on guns.id = p.gun_id order by guns.id")
|
||||
defer func(rows *sql.Rows) {
|
||||
_ = rows.Close()
|
||||
}(rows)
|
||||
|
||||
for rows.Next() {
|
||||
i := Gun{}
|
||||
p := Photo{}
|
||||
err := rows.Scan(
|
||||
&i.Id,
|
||||
&i.Make,
|
||||
&i.Model,
|
||||
&i.SerialNumber,
|
||||
&i.PurchaseAmount,
|
||||
&i.ValueAmount,
|
||||
&i.DatePurchased,
|
||||
&i.Notes,
|
||||
&p.Id,
|
||||
&p.GunId,
|
||||
&p.FileName,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
index := -1
|
||||
for gunIndex, gun := range guns {
|
||||
if gun.Id == i.Id {
|
||||
index = gunIndex
|
||||
continue
|
||||
}
|
||||
|
||||
index = -1
|
||||
}
|
||||
|
||||
if index == -1 {
|
||||
guns = append(guns, i)
|
||||
index = len(guns) - 1
|
||||
}
|
||||
|
||||
guns[index].Photos = append(guns[index].Photos, p)
|
||||
}
|
||||
|
||||
return guns
|
||||
}
|
||||
|
||||
func (dbClient *SqlDb) GunById(id uint) (*Gun, error) {
|
||||
var gun Gun
|
||||
|
||||
row, _ := dbClient.db.Query("select guns.*, p.id, p.gun_id, p.filename from guns join photos p on guns.id = p.gun_id where guns.id = ?", id)
|
||||
|
||||
err := row.Scan(gun)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &gun, nil
|
||||
}
|
@@ -1,14 +0,0 @@
|
||||
package sql
|
||||
|
||||
func (dbClient *SqlDb) GetPhoto(id uint) string {
|
||||
var photo string
|
||||
|
||||
row := dbClient.db.QueryRow("SELECT photo from photos where id = ?", id)
|
||||
|
||||
err := row.Scan(&photo)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return photo
|
||||
}
|
21
sql/query.sql
Normal file
21
sql/query.sql
Normal file
@@ -0,0 +1,21 @@
|
||||
-- name: GetGunById :one
|
||||
SELECT guns.id as id, make, model, serial_number, purchase_amount, value_amount, date_purchased, notes
|
||||
from guns
|
||||
where guns.id = ?;
|
||||
|
||||
-- name: GetGunPhotos :many
|
||||
SELECT id, filename from photos where gun_id = ?;
|
||||
|
||||
-- name: GetGunPhotoData :one
|
||||
select photo from photos where id = ?;
|
||||
|
||||
-- name: GetAllGuns :many
|
||||
SELECT id, make, model, value_amount
|
||||
from guns
|
||||
order by id desc;
|
||||
|
||||
-- name: InsertGun :one
|
||||
INSERT into guns
|
||||
(make, model, serial_number, purchase_amount, value_amount, date_purchased, notes) VALUES
|
||||
(?, ?, ?, ?, ?, ?, ?) returning id
|
||||
|
24
sql/schema.sql
Normal file
24
sql/schema.sql
Normal file
@@ -0,0 +1,24 @@
|
||||
create table 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 photos
|
||||
(
|
||||
id integer
|
||||
constraint id
|
||||
primary key autoincrement,
|
||||
gun_id integer,
|
||||
photo blob,
|
||||
filename text
|
||||
);
|
||||
|
10
sql/sqlc.yaml
Normal file
10
sql/sqlc.yaml
Normal file
@@ -0,0 +1,10 @@
|
||||
version: 2
|
||||
sql:
|
||||
- engine: "sqlite"
|
||||
schema: "schema.sql"
|
||||
queries: "query.sql"
|
||||
gen:
|
||||
go:
|
||||
package: "db"
|
||||
out: "db"
|
||||
emit_json_tags: true
|
22
sql/types.go
22
sql/types.go
@@ -1,22 +0,0 @@
|
||||
package sql
|
||||
|
||||
import "database/sql"
|
||||
|
||||
type Gun struct {
|
||||
Id uint `json:"id"`
|
||||
Make string `json:"make"`
|
||||
Model string `json:"model"`
|
||||
SerialNumber string `json:"serial_number"`
|
||||
PurchaseAmount uint `json:"purchase_amount"`
|
||||
ValueAmount uint `json:"value_amount"`
|
||||
DatePurchased sql.NullString `json:"date_purchased"`
|
||||
Notes sql.NullString `json:"notes"`
|
||||
Photos []Photo `json:"photos"`
|
||||
}
|
||||
|
||||
type Photo struct {
|
||||
Id uint `json:"id"`
|
||||
GunId uint `json:"gun_id"`
|
||||
Photo string `json:"photo"`
|
||||
FileName string `json:"file_name"`
|
||||
}
|
@@ -1,78 +0,0 @@
|
||||
<h1>Guns</h1>
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
<td>
|
||||
ID
|
||||
</td>
|
||||
<td>
|
||||
Make
|
||||
</td>
|
||||
<td>
|
||||
Model
|
||||
</td>
|
||||
<td>
|
||||
Serial Number
|
||||
</td>
|
||||
<td>
|
||||
Purchase Amount
|
||||
</td>
|
||||
<td>
|
||||
Value Amount
|
||||
</td>
|
||||
<td>
|
||||
Date Purchased
|
||||
</td>
|
||||
<td>
|
||||
Notes
|
||||
</td>
|
||||
<td>
|
||||
Photos
|
||||
</td>
|
||||
<td></td>
|
||||
</tr>
|
||||
{{ range .Guns }}
|
||||
<tr>
|
||||
<td>
|
||||
{{ .Id }}
|
||||
</td>
|
||||
<td>
|
||||
{{ .Make }}
|
||||
</td>
|
||||
<td>
|
||||
{{ .Model }}
|
||||
</td>
|
||||
<td>
|
||||
{{ .SerialNumber }}
|
||||
</td>
|
||||
<td>
|
||||
{{ .PurchaseAmount }}
|
||||
</td>
|
||||
<td>
|
||||
{{ .ValueAmount }}
|
||||
</td>
|
||||
<td>
|
||||
{{ .DatePurchased.Value }}
|
||||
</td>
|
||||
<td>
|
||||
{{ .Notes.Value }}
|
||||
</td>
|
||||
<td>
|
||||
{{ range .Photos}}
|
||||
<a target="_blank" href="/photo/{{ .Id }}/{{ .FileName }}">Photo</a>
|
||||
{{ end }}
|
||||
</td>
|
||||
<td>
|
||||
<a href="/edit/{{ .Id }}">Edit</a>
|
||||
</td>
|
||||
</tr>
|
||||
{{ end }}
|
||||
<tr>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td>Total Value</td>
|
||||
<td>{{ .Total }}</td>
|
||||
</tr>
|
||||
</table>
|
Reference in New Issue
Block a user