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.
This commit is contained in:
parent
1785a8e4f5
commit
bac4472c3a
5 changed files with 127 additions and 93 deletions
40
data/crud.go
Normal file
40
data/crud.go
Normal file
|
@ -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
|
||||
}
|
||||
|
||||
|
19
data/models.go
Normal file
19
data/models.go
Normal file
|
@ -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"`
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue