lots of stuff, don't truly remember
This commit is contained in:
@@ -3,6 +3,8 @@ package workout
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -26,11 +28,46 @@ type Workout struct {
|
||||
EquipmentID *uint `gorm:"index" json:"equipment_id"`
|
||||
FileURL string `gorm:"default:''" json:"file_url"`
|
||||
WorkoutData WorkoutDataJSON `gorm:"type:jsonb" json:"workout_data,omitempty"`
|
||||
RPE int `gorm:"default:0" json:"rpe"`
|
||||
Notes string `json:"notes"`
|
||||
Tags Tags `gorm:"type:jsonb;default:'[]'" json:"tags"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
// Tags is a JSONB array of string tags for workouts
|
||||
type Tags []string
|
||||
|
||||
// Scan implements the sql.Scanner interface for reading from the database
|
||||
func (t *Tags) Scan(value interface{}) error {
|
||||
if value == nil {
|
||||
*t = Tags{}
|
||||
return nil
|
||||
}
|
||||
|
||||
switch v := value.(type) {
|
||||
case []byte:
|
||||
return json.Unmarshal(v, t)
|
||||
case string:
|
||||
return json.Unmarshal([]byte(v), t)
|
||||
default:
|
||||
return fmt.Errorf("unsupported type for Tags: %T", value)
|
||||
}
|
||||
}
|
||||
|
||||
// Value implements the driver.Valuer interface for writing to the database
|
||||
func (t Tags) Value() (driver.Value, error) {
|
||||
if t == nil {
|
||||
return json.Marshal([]string{})
|
||||
}
|
||||
return json.Marshal(t)
|
||||
}
|
||||
|
||||
// String returns a comma-separated string representation
|
||||
func (t Tags) String() string {
|
||||
return strings.Join(t, ", ")
|
||||
}
|
||||
|
||||
type WorkoutDataJSON struct {
|
||||
Name string `json:"name"`
|
||||
Author string `json:"author"`
|
||||
@@ -64,4 +101,4 @@ func (w WorkoutDataJSON) Value() (driver.Value, error) {
|
||||
|
||||
func (Workout) TableName() string {
|
||||
return "workouts"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user