Started to work the shortcodes
This commit is contained in:
@@ -3,6 +3,7 @@ package core
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
@@ -137,14 +138,43 @@ type WebsiteSettings struct {
|
||||
}
|
||||
|
||||
type Media struct {
|
||||
ID uuid.UUID `bun:"type:uuid,pk,default:gen_random_uuid()" json:"id"`
|
||||
AuthorID uuid.UUID `bun:"author_id,type:uuid,notnull" json:"authorID"`
|
||||
Author *User `bun:"rel:belongs-to,join:author_id=user_id" json:"author,omitempty"`
|
||||
Type string `bun:"media_type" json:"type"` // Image, Video, GIF etc. Add support for PDFs?
|
||||
Alt string `bun:"media_alt" json:"alt"`
|
||||
Path string `bun:"media_path" json:"path"`
|
||||
Size int64 `bun:"media_size" json:"size"`
|
||||
URL string `bun:"-" json:"url"`
|
||||
bun.BaseModel `bun:"table:media"`
|
||||
ID uuid.UUID `bun:"type:uuid,pk,default:gen_random_uuid()" json:"id"`
|
||||
AuthorID uuid.UUID `bun:"author_id,type:uuid,notnull" json:"authorID"`
|
||||
Author *User `bun:"rel:belongs-to,join:author_id=user_id" json:"author,omitempty"`
|
||||
Type string `bun:"media_type" json:"type"` // Image, Video, GIF etc. Add support for PDFs?
|
||||
Alt string `bun:"media_alt" json:"alt"`
|
||||
Path string `bun:"media_path" json:"path"`
|
||||
Size int64 `bun:"media_size" json:"size"`
|
||||
URL string `bun:"-" json:"url"`
|
||||
}
|
||||
|
||||
type ShortcodeType string
|
||||
|
||||
const (
|
||||
ShortcodeMedia ShortcodeType = "media"
|
||||
ShortcodeValue ShortcodeType = "value"
|
||||
)
|
||||
|
||||
type Shortcode struct {
|
||||
bun.BaseModel `bun:"table:shortcodes,alias:sc"`
|
||||
|
||||
ID int64 `bun:"id,pk,autoincrement" json:"id"` // Primary key
|
||||
Code string `bun:"code,notnull,unique" json:"code"` // The shortcode value
|
||||
Type ShortcodeType `bun:"shortcode_type,notnull" json:"type"`
|
||||
Value *string `bun:"value" json:"value,omitempty"`
|
||||
MediaID *uuid.UUID `bun:"media_id,type:uuid" json:"media_id,omitempty"` // Nullable reference to another table's ID
|
||||
Media *Media `bun:"rel:belongs-to,join:media_id=id" json:"media,omitempty"` // Relation to Media
|
||||
}
|
||||
|
||||
func (s *Shortcode) Validate() error {
|
||||
if s.Value != nil && s.MediaID != nil {
|
||||
return errors.New("both value and media_id cannot be set at the same time")
|
||||
}
|
||||
if s.Value == nil && s.MediaID == nil {
|
||||
return errors.New("either value or media_id must be set")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func InitDatabase(dsn DSN) (*bun.DB, error) {
|
||||
@@ -164,6 +194,7 @@ func InitDatabase(dsn DSN) (*bun.DB, error) {
|
||||
_, err = db.NewCreateTable().Model((*Blog)(nil)).IfNotExists().Exec(ctx)
|
||||
_, err = db.NewCreateTable().Model((*WebsiteSettings)(nil)).IfNotExists().Exec(ctx)
|
||||
_, err = db.NewCreateTable().Model((*Media)(nil)).IfNotExists().Exec(ctx)
|
||||
_, err = db.NewCreateTable().Model((*Shortcode)(nil)).IfNotExists().Exec(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user