implement dbhandler package and new test file

This commit is contained in:
Adrian Zürcher
2025-10-31 08:11:07 +01:00
parent 1568ee2482
commit 80675ed328
17 changed files with 674 additions and 572 deletions

View File

@@ -1,7 +1,29 @@
package models
import (
"database/sql/driver"
"encoding/json"
"fmt"
)
type Persons []Person
type Person struct {
Id int `json:"id,omitEmpty"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
Id int `gorm:"primaryKey" json:"id"`
FirstName string `gorm:"column:firstName" json:"firstName"`
LastName string `gorm:"column:lastName" json:"lastName"`
}
// --- Implement driver.Valuer (for saving to DB)
func (r Persons) Value() (driver.Value, error) {
return json.Marshal(r)
}
// --- Implement sql.Scanner (for reading from DB)
func (r *Persons) Scan(value any) error {
bytes, ok := value.([]byte)
if !ok {
return fmt.Errorf("failed to unmarshal Settings: %v", value)
}
return json.Unmarshal(bytes, r)
}