You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
126 lines
3.1 KiB
126 lines
3.1 KiB
package common
|
|
|
|
import (
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
const (
|
|
CLIUserID = "01F5D7K0754ZDRPSKYDHE5CE0H"
|
|
CLIUsername = "cli"
|
|
)
|
|
|
|
type BasicFields struct {
|
|
CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
|
|
CreatedByDB GUID `gorm:"column:created_by" json:"-"`
|
|
CreatedByJSON string `gorm:"-" json:"created_by"`
|
|
UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
|
|
UpdatedByDB GUID `gorm:"column:updated_by" json:"-"`
|
|
UpdatedByJSON string `gorm:"-" json:"updated_by"`
|
|
DeletedAtDB gorm.DeletedAt `gorm:"column:deleted_at" json:"-"`
|
|
DeletedAtJSON *time.Time `gorm:"-" json:"deleted_at,omitempty"`
|
|
DeletedBy string `gorm:"column:deleted_by" json:"deleted_by,omitempty"`
|
|
}
|
|
|
|
func (basicFields *BasicFields) BeforeCreate(tx *gorm.DB) error {
|
|
if userID, ok := tx.Get("userID"); ok {
|
|
if guidString, ok := userID.(string); ok {
|
|
guid, err := StringToGUID(guidString)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
basicFields.CreatedByDB = guid
|
|
basicFields.UpdatedByDB = guid
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (basicFields *BasicFields) AfterCreate(tx *gorm.DB) error {
|
|
if username, ok := tx.Get("username"); ok {
|
|
if name, ok := username.(string); ok {
|
|
basicFields.CreatedByJSON = name
|
|
basicFields.UpdatedByJSON = name
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type ModelIntegerPK struct {
|
|
ID uint64 `gorm:"primaryKey;column:id" json:"id"`
|
|
BasicFields
|
|
}
|
|
|
|
type ModelGUIDPK struct {
|
|
ID GUID `gorm:"primaryKey;column:id" json:"id"`
|
|
BasicFields
|
|
}
|
|
|
|
func (guidPK *ModelGUIDPK) BeforeCreate(tx *gorm.DB) error {
|
|
id, err := NewGUID()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
guidPK.ID = GUID(id)
|
|
if userID, ok := tx.Get("userID"); ok {
|
|
if guidString, ok := userID.(string); ok {
|
|
guid, err := StringToGUID(guidString)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
guidPK.CreatedByDB = guid
|
|
guidPK.UpdatedByDB = guid
|
|
}
|
|
}
|
|
return err
|
|
}
|
|
|
|
type ModelHiddenGUIDPK struct {
|
|
ID GUID `gorm:"primaryKey;column:id" json:"-"`
|
|
BasicFields
|
|
}
|
|
|
|
func (guidPK *ModelHiddenGUIDPK) BeforeCreate(tx *gorm.DB) error {
|
|
id, err := NewGUID()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
guidPK.ID = GUID(id)
|
|
if userID, ok := tx.Get("userID"); ok {
|
|
if guidString, ok := userID.(string); ok {
|
|
guid, err := StringToGUID(guidString)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
guidPK.CreatedByDB = guid
|
|
guidPK.UpdatedByDB = guid
|
|
}
|
|
}
|
|
return err
|
|
}
|
|
|
|
// Envelope for response objects
|
|
type Envelope struct {
|
|
Success bool `json:"success"`
|
|
RequestID string `json:"request_id,omitempty"`
|
|
Warnings []string `json:"warnings,omitempty"`
|
|
Errors []string `json:"errors,omitempty"`
|
|
// PaginationInfo *pagination.Pagination `json:"pagination_info,omitempty"`
|
|
Result interface{} `json:"result,omitempty"`
|
|
}
|
|
|
|
// AppendError to envelope
|
|
func (envelope *Envelope) AppendError(err error) *Envelope {
|
|
envelope.Success = false
|
|
envelope.Result = nil
|
|
envelope.Errors = append(envelope.Errors, err.Error())
|
|
return envelope
|
|
}
|
|
|
|
// SetSuccess to envelope
|
|
func (envelope *Envelope) SetSuccess(result interface{}) *Envelope {
|
|
envelope.Success = true
|
|
envelope.Result = result
|
|
return envelope
|
|
}
|