tercul-backend/internal/models/base.go
Damir Mukimov fa336cacf3
wip
2025-09-01 00:43:59 +02:00

79 lines
1.7 KiB
Go

package models
import (
"database/sql/driver"
"encoding/json"
"fmt"
"time"
)
// JSONB is a custom type for JSONB columns.
type JSONB map[string]interface{}
// Value marshals JSONB for storing in the DB.
func (j JSONB) Value() (driver.Value, error) {
if j == nil {
return "{}", nil
}
return json.Marshal(j)
}
// Scan unmarshals a JSONB value.
func (j *JSONB) Scan(value interface{}) error {
if value == nil {
*j = JSONB{}
return nil
}
switch v := value.(type) {
case []byte:
if len(v) == 0 {
*j = JSONB{}
return nil
}
return json.Unmarshal(v, j)
case string:
if v == "" {
*j = JSONB{}
return nil
}
return json.Unmarshal([]byte(v), j)
default:
return fmt.Errorf("failed to unmarshal JSONB value of type %T: %v", value, value)
}
}
// BaseModel contains common fields for all models
type BaseModel struct {
ID uint `gorm:"primaryKey"`
CreatedAt time.Time
UpdatedAt time.Time
}
// TranslatableModel extends BaseModel with language support
type TranslatableModel struct {
BaseModel
Language string `gorm:"size:50;default:'multi'"`
Slug string `gorm:"size:255;index"`
}
// Translation status enum
type TranslationStatus string
const (
TranslationStatusDraft TranslationStatus = "draft"
TranslationStatusPublished TranslationStatus = "published"
TranslationStatusReviewing TranslationStatus = "reviewing"
TranslationStatusRejected TranslationStatus = "rejected"
)
// UserRole enum
type UserRole string
const (
UserRoleReader UserRole = "reader"
UserRoleContributor UserRole = "contributor"
UserRoleReviewer UserRole = "reviewer"
UserRoleEditor UserRole = "editor"
UserRoleAdmin UserRole = "admin"
)