Apparently for 2 weeks (yes 2 whole fucking weeks) I was using NamedExec wrong instead of MustExec so inserts haven't worked since then. Other than that, this is a more cleaned up MVC style setup than before.
parent
1785a8e4f5
commit
bac4472c3a
@ -0,0 +1,43 @@ |
||||
package api |
||||
|
||||
import ( |
||||
"log" |
||||
"github.com/gofiber/fiber/v2" |
||||
"github.com/go-playground/validator/v10" |
||||
) |
||||
|
||||
type Failure struct { |
||||
Message string `json:"message"` |
||||
} |
||||
|
||||
func IfErrNil(err error, c *fiber.Ctx) error { |
||||
if err != nil { |
||||
log.Output(10, err.Error()) |
||||
c.SendStatus(fiber.StatusInternalServerError) |
||||
return c.JSON(Failure{err.Error()}) |
||||
} |
||||
|
||||
return err |
||||
} |
||||
|
||||
func ReceivePost[T any](c *fiber.Ctx) (*T, error) { |
||||
var result *T |
||||
result = new(T) |
||||
|
||||
if err := c.BodyParser(result); err != nil { |
||||
log.Println(err); |
||||
return result, err |
||||
} |
||||
|
||||
var validate *validator.Validate |
||||
validate = validator.New(validator.WithRequiredStructEnabled()) |
||||
|
||||
if err := validate.Struct(result); err != nil { |
||||
validationErrors := err.(validator.ValidationErrors) |
||||
log.Println(validationErrors) |
||||
return result, err |
||||
} |
||||
|
||||
return result, nil |
||||
} |
||||
|
@ -0,0 +1,40 @@ |
||||
package data |
||||
|
||||
import ( |
||||
"github.com/gofiber/fiber/v2" |
||||
_ "github.com/mattn/go-sqlite3" |
||||
"github.com/jmoiron/sqlx" |
||||
) |
||||
|
||||
func SelectJson[T any](db *sqlx.DB, c *fiber.Ctx, err error, sql string, args ...interface{}) error { |
||||
var result []T |
||||
if err != nil { goto fail } |
||||
|
||||
err = db.Select(&result, sql, args...) |
||||
if err != nil { goto fail } |
||||
|
||||
return c.JSON(&result) |
||||
|
||||
fail: return err |
||||
} |
||||
|
||||
func GetJson[T any](db *sqlx.DB, c *fiber.Ctx, err error, sql string, args ...interface{}) error { |
||||
var result T |
||||
if err != nil { goto fail } |
||||
|
||||
err = db.Get(&result, sql, args...) |
||||
if err != nil { goto fail } |
||||
|
||||
return c.JSON(&result) |
||||
|
||||
fail: return err |
||||
} |
||||
|
||||
func Insert(db *sqlx.DB, err error, sql_query string, args ...interface{}) error { |
||||
if err != nil { return err } |
||||
|
||||
db.MustExec(sql_query, args...) |
||||
return err |
||||
} |
||||
|
||||
|
@ -0,0 +1,19 @@ |
||||
package data |
||||
|
||||
type Login struct { |
||||
Username string `db:"username" validate:"required"` |
||||
Password string `db:"password" validate:"required"` |
||||
} |
||||
|
||||
type Link struct { |
||||
Id int `db:"id" json:"id"` |
||||
StreamId int `db:"stream_id" json:"stream_id" form:"stream_id" validate:"required,numeric"` |
||||
Url string `db:"url" json:"url" form:"url" validate:"required,url"` |
||||
Description string `db:"description" json:"description" form:"description" validate:"required"` |
||||
} |
||||
|
||||
type Stream struct { |
||||
Id int `db:"id" json:"id"` |
||||
Title string `db:"title" json:"title"` |
||||
Description string `db:"description" json:"description"` |
||||
} |
@ -1,69 +0,0 @@ |
||||
package tools |
||||
|
||||
import ( |
||||
"log" |
||||
"github.com/gofiber/fiber/v2" |
||||
|
||||
_ "github.com/mattn/go-sqlite3" |
||||
"github.com/jmoiron/sqlx" |
||||
"github.com/go-playground/validator/v10" |
||||
) |
||||
|
||||
func SelectJson[T any](db *sqlx.DB, c *fiber.Ctx, err error, sql string, args ...interface{}) error { |
||||
if(err != nil) { |
||||
log.Fatalln(err) |
||||
} |
||||
var result []T |
||||
|
||||
err = db.Select(&result, sql, args...) |
||||
if(err != nil) { |
||||
log.Fatalln(err); |
||||
} |
||||
|
||||
return c.JSON(&result); |
||||
} |
||||
|
||||
func GetJson[T any](db *sqlx.DB, c *fiber.Ctx, err error, sql string, args ...interface{}) error { |
||||
if(err != nil) { |
||||
log.Fatalln(err) |
||||
} |
||||
var result T |
||||
|
||||
err = db.Get(&result, sql, args...) |
||||
if(err != nil) { |
||||
log.Fatalln(err); |
||||
} |
||||
|
||||
return c.JSON(&result); |
||||
} |
||||
|
||||
func ReceivePost[T any](c *fiber.Ctx) (*T, error) { |
||||
var result *T |
||||
result = new(T) |
||||
|
||||
if err := c.BodyParser(result); err != nil { |
||||
log.Println(err); |
||||
return result, err |
||||
} |
||||
|
||||
var validate *validator.Validate |
||||
validate = validator.New(validator.WithRequiredStructEnabled()) |
||||
|
||||
if err := validate.Struct(result); err != nil { |
||||
validationErrors := err.(validator.ValidationErrors) |
||||
log.Println(validationErrors) |
||||
return result, err |
||||
} |
||||
|
||||
return result, nil |
||||
} |
||||
|
||||
func Insert(db *sqlx.DB, err error, sql_query string, args ...interface{}) error { |
||||
if err != nil { |
||||
log.Println(err) |
||||
return err |
||||
} |
||||
|
||||
_, exec_err := db.NamedExec(sql_query, args) |
||||
return exec_err |
||||
} |
Loading…
Reference in new issue