Never gonna let you down

This commit is contained in:
2023-08-07 20:38:50 -04:00
parent ca56ca7b29
commit 04ebe9e855
4 changed files with 166 additions and 8 deletions

View File

@@ -73,9 +73,13 @@ func Post(c echo.Context) error {
return err
}
err = c.JSON(http.StatusAccepted, Handlers.Response[struct{ Id int64 }]{
Status: http.StatusText(http.StatusOK),
Payload: struct{ Id int64 }{Id: id},
err = c.JSON(http.StatusAccepted, Handlers.Response[struct {
Id int64 `json:"id"`
}]{
Status: http.StatusText(http.StatusOK),
Payload: struct {
Id int64 `json:"id"`
}{Id: id},
})
if err != nil {
return err
@@ -129,6 +133,78 @@ func GetPhotoResize(c echo.Context) error {
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 {
@@ -153,13 +229,35 @@ func GetPhoto(c echo.Context) error {
return nil
}
func DeleteById(c echo.Context) error {
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
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
}
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 +281,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
}

View File

@@ -37,8 +37,11 @@ func main() {
e.GET("/gun", Guns.Get)
e.GET("/gun/:id", Guns.GetById)
e.DELETE("/gun/:id", Guns.DeleteById)
e.POST("/gun", Guns.Post)
e.GET("/gun/photo/:id/:filename", Guns.GetPhoto)
e.POST("/gun/photo/:id", Guns.PostPhoto)
e.DELETE("/gun/photo/:id", Guns.DeletePhoto)
e.GET("/gun/photo/:id/:size/:filename", Guns.GetPhotoResize)
e.Logger.Fatal(e.Start(":8000"))

View File

@@ -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,18 @@ 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
}

View File

@@ -17,5 +17,16 @@ 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: 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 (?, ?, ?);