record contact requests to database
This commit is contained in:
parent
062b5dd9cf
commit
ea6a8615bb
3 changed files with 30 additions and 4 deletions
|
@ -9,6 +9,7 @@ import (
|
|||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/CyCoreSystems/cycore-web/db"
|
||||
"github.com/CyCoreSystems/sendinblue"
|
||||
"github.com/labstack/echo"
|
||||
)
|
||||
|
@ -37,6 +38,9 @@ func contactRequest(c echo.Context) (err error) {
|
|||
}
|
||||
|
||||
cc.Log.Debugf(`received contact request from "%s" <%s> (%s)`, req.Name, req.Email, c.RealIP())
|
||||
if err = db.LogContact(req.Name, req.Email); err != nil {
|
||||
cc.Log.Warn("failed to write contact record to database")
|
||||
}
|
||||
|
||||
if req.Name == "" {
|
||||
cc.Log.Warn("empty name")
|
||||
|
|
23
db/db.go
23
db/db.go
|
@ -32,14 +32,29 @@ func Connect() error {
|
|||
}
|
||||
|
||||
db, err = sqlx.Open("postgres", dsn)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
return db.Ping()
|
||||
}
|
||||
|
||||
func ensureDatabase() {
|
||||
if err := Connect(); err != nil {
|
||||
panic("no database connection: " + err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// Get returns a database connection handle for the database
|
||||
func Get() *sqlx.DB {
|
||||
if err := Connect(); err != nil {
|
||||
panic("no database connection: " + err.Error())
|
||||
}
|
||||
ensureDatabase()
|
||||
return db
|
||||
}
|
||||
|
||||
// LogContact adds a log entry to the database recording a contact request
|
||||
func LogContact(name, email string) error {
|
||||
ensureDatabase()
|
||||
|
||||
_, err := db.Exec(`INSERT INTO contact_requests (name,email) VALUES ($1, $2)`, name, email)
|
||||
return err
|
||||
}
|
||||
|
|
7
schema.sql
Normal file
7
schema.sql
Normal file
|
@ -0,0 +1,7 @@
|
|||
CREATE TABLE contact_requests (
|
||||
id UUID PRIMARY KEY NOT NULL DEFAULT uuid_v4()::UUID,
|
||||
name STRING(128) NOT NULL,
|
||||
email STRING(128) NOT NULL,
|
||||
received TIMESTAMPTZ DEFAULT now() NOT NULL
|
||||
);
|
||||
|
Loading…
Reference in a new issue