2024-11-05 16:18:04 +00:00
|
|
|
package frm
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2024-12-17 21:15:26 +00:00
|
|
|
"errors"
|
2024-11-05 16:18:04 +00:00
|
|
|
|
|
|
|
|
"github.com/acaloiaro/frm/internal"
|
2024-12-17 17:25:33 +00:00
|
|
|
"github.com/google/uuid"
|
2024-11-05 16:18:04 +00:00
|
|
|
)
|
|
|
|
|
|
2024-12-20 17:16:57 +00:00
|
|
|
const (
|
|
|
|
|
EventDraftCreated = "frmDraftCreated" // htmx event sent when new drafts are created
|
|
|
|
|
)
|
|
|
|
|
|
2024-12-17 21:15:26 +00:00
|
|
|
var ErrCannotDetermineWorkspace = errors.New("workspace cannot be determine without WorkspaceID or WorkspaceIDUrlParam")
|
|
|
|
|
|
|
|
|
|
// Frm is the primary API into frm
|
2024-11-05 16:18:04 +00:00
|
|
|
type Frm struct {
|
2024-12-17 21:15:26 +00:00
|
|
|
PostgresURL string // the database URL where forms are stored
|
2024-12-19 14:56:19 +00:00
|
|
|
WorkspaceID uuid.UUID // the ID of the workspace that the frm acts on behalf of
|
2024-12-17 21:15:26 +00:00
|
|
|
WorkspaceIDUrlParam string // the name of the URL parameter that provides your workspace ID
|
2024-11-05 16:18:04 +00:00
|
|
|
}
|
|
|
|
|
|
2024-12-19 14:56:19 +00:00
|
|
|
// Args are arguments passed to Frm
|
2024-11-05 16:18:04 +00:00
|
|
|
type Args struct {
|
2024-12-17 21:15:26 +00:00
|
|
|
PostgresURL string
|
|
|
|
|
WorkspaceID uuid.UUID
|
|
|
|
|
WorkspaceIDUrlParam string
|
2024-11-05 16:18:04 +00:00
|
|
|
}
|
|
|
|
|
|
2024-12-20 17:16:57 +00:00
|
|
|
type FormStatus internal.FormStatus
|
|
|
|
|
|
2024-11-05 16:18:04 +00:00
|
|
|
// New initializes a new frm instance
|
|
|
|
|
//
|
|
|
|
|
// If the frm database hasn't been initiailized, the database is initialized
|
2024-12-17 21:15:26 +00:00
|
|
|
func New(args Args) (f *Frm, err error) {
|
|
|
|
|
if args.WorkspaceID == uuid.Nil && args.WorkspaceIDUrlParam == "" {
|
|
|
|
|
return nil, ErrCannotDetermineWorkspace
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
f = &Frm{
|
|
|
|
|
PostgresURL: args.PostgresURL,
|
|
|
|
|
WorkspaceID: args.WorkspaceID,
|
|
|
|
|
WorkspaceIDUrlParam: args.WorkspaceIDUrlParam,
|
2024-11-05 16:18:04 +00:00
|
|
|
}
|
2024-12-17 21:15:26 +00:00
|
|
|
return
|
2024-11-05 16:18:04 +00:00
|
|
|
}
|
|
|
|
|
|
2024-12-17 21:15:26 +00:00
|
|
|
// Init initializes the frm database if it hasn't been initialized
|
2024-11-05 16:18:04 +00:00
|
|
|
func (f *Frm) Init(ctx context.Context) (err error) {
|
|
|
|
|
err = internal.InitializeDB(ctx, f.PostgresURL)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-17 21:15:26 +00:00
|
|
|
// GetForm retrieves forms by ID
|
2024-11-05 16:18:04 +00:00
|
|
|
func (f *Frm) GetForm(ctx context.Context, id int64) (form Form, err error) {
|
|
|
|
|
var frm internal.Form
|
2024-12-17 17:25:33 +00:00
|
|
|
frm, err = internal.Q(ctx, f.PostgresURL).GetForm(ctx, internal.GetFormParams{
|
|
|
|
|
WorkspaceID: f.WorkspaceID,
|
|
|
|
|
ID: id,
|
|
|
|
|
})
|
2024-11-05 16:18:04 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
form = (Form)(frm)
|
|
|
|
|
return
|
|
|
|
|
}
|
2024-12-17 21:15:26 +00:00
|
|
|
|
2024-12-20 17:16:57 +00:00
|
|
|
type ListFormsArgs struct {
|
|
|
|
|
Statuses []FormStatus
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-19 14:56:19 +00:00
|
|
|
// ListForms lists all forms for the current workspace
|
2024-12-20 17:16:57 +00:00
|
|
|
func (f *Frm) ListForms(ctx context.Context, args ListFormsArgs) (forms []Form, err error) {
|
2024-12-19 14:56:19 +00:00
|
|
|
var fs Forms
|
2024-12-20 17:16:57 +00:00
|
|
|
statuses := []internal.FormStatus{}
|
|
|
|
|
for _, s := range args.Statuses {
|
|
|
|
|
statuses = append(statuses, (internal.FormStatus)(s))
|
|
|
|
|
}
|
|
|
|
|
fs, err = internal.Q(ctx, f.PostgresURL).ListForms(ctx, internal.ListFormsParams{
|
|
|
|
|
WorkspaceID: f.WorkspaceID,
|
|
|
|
|
Statuses: statuses,
|
|
|
|
|
})
|
2024-12-17 21:15:26 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-19 14:56:19 +00:00
|
|
|
for _, f := range fs {
|
|
|
|
|
forms = append(forms, (Form)(f))
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-17 21:15:26 +00:00
|
|
|
return
|
|
|
|
|
}
|