All checks were successful
continuous-integration/drone/push Build is passing
40 lines
1.0 KiB
Go
40 lines
1.0 KiB
Go
package models
|
|
|
|
import (
|
|
"log"
|
|
"time"
|
|
)
|
|
|
|
// Define audit functions
|
|
type Audit struct {
|
|
AuditId int `db:"AuditId" json:"auditId"`
|
|
UserId int `db:"UserId" json:"userId"`
|
|
SecretId int `db:"SecretId" json:"secretId"`
|
|
EventText string `db:"EventText" json:"eventText"`
|
|
EventTime time.Time `db:"EventTime" json:"eventTime"`
|
|
}
|
|
|
|
// AuditAdd adds a new audit record to the database
|
|
func (a *Audit) AuditAdd() (*Audit, error) {
|
|
var err error
|
|
|
|
// Populate timestamp field if not already set
|
|
if a.EventTime.IsZero() {
|
|
a.EventTime = time.Now().UTC()
|
|
}
|
|
|
|
result, err := db.NamedExec(("INSERT INTO audit (UserId, SecretId, EventText, EventTime) VALUES (:UserId, :SecretId, :EventText, :EventTime);"), a)
|
|
|
|
if err != nil {
|
|
log.Printf("AuditAdd error executing sql record : '%s'\n", err)
|
|
return &Audit{}, err
|
|
} else {
|
|
affected, _ := result.RowsAffected()
|
|
id, _ := result.LastInsertId()
|
|
a.AuditId = int(id)
|
|
log.Printf("AuditAdd insert returned result id '%d' affecting %d row(s).\n", id, affected)
|
|
}
|
|
|
|
return a, nil
|
|
}
|