test audit add
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2024-01-15 09:54:42 +11:00
parent 5920db48d8
commit 95125b691d
4 changed files with 56 additions and 13 deletions

View File

@@ -1,3 +1,39 @@
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"`
Timestamp time.Time `db:"Timestamp" json:"Timestamp"`
}
// 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.Timestamp.IsZero() {
a.Timestamp = time.Now()
}
result, err := db.NamedExec(("INSERT INTO audit (UserId, SecretId, EventText, Timestamp) VALUES (:UserId, :SecretId, :EventText, :Timestamp);"), 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
}

View File

@@ -20,16 +20,6 @@ const (
sqlFile = "smt.db"
)
/*
const createRoles string = `
CREATE TABLE IF NOT EXISTS roles (
RoleId INTEGER PRIMARY KEY ASC,
RoleName VARCHAR,
ReadOnly BOOLEAN
);
`
*/
const createUsers string = `
CREATE TABLE IF NOT EXISTS users (
UserId INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -90,10 +80,11 @@ const createSchema string = `
const createAudit string = `
CREATE TABLE IF NOT EXISTS audit (
EventId INTEGER PRIMARY KEY AUTOINCREMENT,
UserName VARCHAR,
AuditId INTEGER PRIMARY KEY AUTOINCREMENT,
UserId INTEGER DEFAULT 0,
SecretId INTEGER DEFAULT 0,
EventText VARCHAR,
EventTime INTEGER
Timestamp datetime
);
`

View File

@@ -62,6 +62,7 @@ func (u *User) SaveUser() (*User, error) {
} else {
affected, _ := result.RowsAffected()
id, _ := result.LastInsertId()
u.UserId = int(id)
log.Printf("SaveUser insert returned result id '%d' affecting %d row(s).\n", id, affected)
}
} else {