making this thing actually usable.

This commit is contained in:
2023-08-07 21:26:14 -04:00
parent 7a119a8b60
commit 345134773f
2 changed files with 155 additions and 144 deletions

View File

@@ -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"
)
@@ -95,147 +92,6 @@ func Post(c echo.Context) error {
return nil
}
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
}
func DeleteById(c echo.Context) error {
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil {

155
Handlers/Guns/photos.go Normal file
View File

@@ -0,0 +1,155 @@
package Guns
import (
"bytes"
"context"
sql2 "database/sql"
"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"
)
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
}