You've already forked gun-manager-backend
commit
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user