finish world initialization, set up init on startup and on shutdown, move all models to main directory
This commit is contained in:
55
internal/foundry/models/db/authors.go
Normal file
55
internal/foundry/models/db/authors.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
||||
type Author struct {
|
||||
ID uint
|
||||
|
||||
Name string
|
||||
URL string
|
||||
Email string
|
||||
Discord string
|
||||
// SystemAuthorsFlags SystemAuthorsFlags `json:"flags"`
|
||||
}
|
||||
|
||||
func (a *Author) Query(data *InsertId[string]) {
|
||||
data.query = fmt.Sprintf(`
|
||||
INSERT INTO author (%s, name, url, email, discord)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
RETURNING id`, data.fieldName)
|
||||
}
|
||||
|
||||
func (a *Author) Insert(tx *sqlx.Tx, data *InsertId[string]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id, a.Name, a.URL, a.Email, a.Discord}
|
||||
|
||||
err := tx.QueryRowx(data.query, args...).Scan(&a.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *Author) InsertCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[string]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id, a.Name, a.URL, a.Email, a.Discord}
|
||||
|
||||
err := tx.QueryRowxContext(ctx, data.query, args...).Scan(&a.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user