Repository navigation
Expand file tree
/
Copy pathform.go
More file actions
118 lines (101 loc) · 3.82 KB
/
Copy pathform.go
File metadata and controls
118 lines (101 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package models
import (
"database/sql"
"time"
)
// Form represents a contact form configuration
type Form struct {
ID int64 `json:"id"`
UserID int64 `json:"user_id"`
Name string `json:"name"`
Domain string `json:"domain"`
TurnstileSecret string `json:"turnstile_secret"` // Private key for validation
ForwardEmail string `json:"forward_email"`
CallbackURL string `json:"callback_url,omitempty"`
FormKey string `json:"form_key"` // Generated unique key
SubmissionCount int `json:"submission_count"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// CreateForm creates a new form in the database
func CreateForm(db *sql.DB, userID int64, name, domain, turnstileSecret, forwardEmail, callbackURL, formKey string) (*Form, error) {
result, err := db.Exec(
"INSERT INTO forms (user_id, name, domain, turnstile_secret, forward_email, callback_url, form_key) VALUES (?, ?, ?, ?, ?, ?, ?)",
userID, name, domain, turnstileSecret, forwardEmail, callbackURL, formKey,
)
if err != nil {
return nil, err
}
id, err := result.LastInsertId()
if err != nil {
return nil, err
}
return GetFormByID(db, id)
}
// GetFormByID retrieves a form by its ID
func GetFormByID(db *sql.DB, id int64) (*Form, error) {
var form Form
err := db.QueryRow(
"SELECT id, user_id, name, domain, turnstile_secret, forward_email, COALESCE(callback_url, ''), form_key, created_at, updated_at FROM forms WHERE id = ?",
id,
).Scan(&form.ID, &form.UserID, &form.Name, &form.Domain, &form.TurnstileSecret, &form.ForwardEmail, &form.CallbackURL, &form.FormKey, &form.CreatedAt, &form.UpdatedAt)
if err != nil {
if err == sql.ErrNoRows {
return nil, nil
}
return nil, err
}
return &form, nil
}
// GetFormsByUserID retrieves all forms for a specific user
func GetFormsByUserID(db *sql.DB, userID int64) ([]Form, error) {
rows, err := db.Query(
"SELECT id, user_id, name, domain, turnstile_secret, forward_email, COALESCE(callback_url, ''), form_key, created_at, updated_at FROM forms WHERE user_id = ? ORDER BY created_at DESC",
userID,
)
if err != nil {
return nil, err
}
defer rows.Close()
var forms []Form
for rows.Next() {
var form Form
if err := rows.Scan(&form.ID, &form.UserID, &form.Name, &form.Domain, &form.TurnstileSecret, &form.ForwardEmail, &form.CallbackURL, &form.FormKey, &form.CreatedAt, &form.UpdatedAt); err != nil {
return nil, err
}
forms = append(forms, form)
}
return forms, nil
}
// FormExists checks if a form with the given name already exists for a user
func FormExists(db *sql.DB, userID int64, name string) (bool, error) {
var exists bool
err := db.QueryRow(
"SELECT EXISTS(SELECT 1 FROM forms WHERE user_id = ? AND name = ?)",
userID, name,
).Scan(&exists)
return exists, err
}
// GetFormByKey retrieves a form by its form_key
func GetFormByKey(db *sql.DB, formKey string) (*Form, error) {
var form Form
err := db.QueryRow(
"SELECT id, user_id, name, domain, turnstile_secret, forward_email, COALESCE(callback_url, ''), form_key, created_at, updated_at FROM forms WHERE form_key = ?",
formKey,
).Scan(&form.ID, &form.UserID, &form.Name, &form.Domain, &form.TurnstileSecret, &form.ForwardEmail, &form.CallbackURL, &form.FormKey, &form.CreatedAt, &form.UpdatedAt)
if err != nil {
if err == sql.ErrNoRows {
return nil, nil
}
return nil, err
}
return &form, nil
}
// UpdateForm updates a form in the database
func UpdateForm(db *sql.DB, formID int64, name, domain, turnstileSecret, forwardEmail, callbackURL string) error {
_, err := db.Exec(
"UPDATE forms SET name = ?, domain = ?, turnstile_secret = ?, forward_email = ?, callback_url = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?",
name, domain, turnstileSecret, forwardEmail, callbackURL, formID,
)
return err
}