diff --git a/database/database.go b/database/database.go new file mode 100644 index 0000000..ecac3f5 --- /dev/null +++ b/database/database.go @@ -0,0 +1,53 @@ +package database + +import ( + "dorisbot/pkg/config" + "errors" + "fmt" + "log" + + "github.com/jmoiron/sqlx" + _ "github.com/lib/pq" +) + +// Errors +var ( + ErrDBNotInit = errors.New("database not init") +) + +var x *sqlx.DB + +// NewDB - +func NewDB(conf *config.Database) (*sqlx.DB, error) { + var err error + connStr := fmt.Sprintf("dbname=%s user=%s password=%s host=%s port=%d sslmode=disable", + conf.DBName, + conf.User, + conf.Pass, + conf.Host, + conf.Port, + ) + log.Println("DB Connstr :: ", connStr) + + x, err = sqlx.Connect("postgres", connStr) + if err != nil { + return nil, err + } + return x, nil +} + +// Ping database connection +func Ping() error { + if x == nil { + return ErrDBNotInit + } + return x.Ping() +} + +// GetConn database connection +func GetConn() *sqlx.DB { + if x == nil { + panic(errors.New("database object not init")) + } + return x +} diff --git a/database/impl/public/facebook.go b/database/impl/public/facebook.go new file mode 100644 index 0000000..99a8414 --- /dev/null +++ b/database/impl/public/facebook.go @@ -0,0 +1,13 @@ +package public + +import ( + pubmodel "dorisbot/database/models/public" +) + +// FacebookImpl - +type FacebookImpl struct{} + +// Create new Facebook Page +func (p FacebookImpl) Create(fb *pubmodel.FacebookPage) (err error) { + return +} diff --git a/database/interfaces/public/facebook.go b/database/interfaces/public/facebook.go new file mode 100644 index 0000000..8bc778f --- /dev/null +++ b/database/interfaces/public/facebook.go @@ -0,0 +1,16 @@ +package public + +import ( + dsmodel "dorisbot/database/models/discord" + pubmodel "dorisbot/database/models/public" +) + +// FacebookDAO - +type FacebookDAO interface { + Create(fb *pubmodel.FacebookPage) error + UpdatePost(fb *pubmodel.FacebookPage, postID string) error + GetByID(id string) (pubmodel.FacebookPage, error) + GetAll() ([]pubmodel.FacebookPage, error) + Delete(id string) error + GetChannels() ([]dsmodel.Channel, error) +} diff --git a/database/models/discord/channel.go b/database/models/discord/channel.go new file mode 100644 index 0000000..227f96d --- /dev/null +++ b/database/models/discord/channel.go @@ -0,0 +1,13 @@ +package discord + +import "time" + +// Channel - +type Channel struct { + ID string `db:"id"` + Server string `db:"server"` + EnableCMD bool `db:"enable_cmd"` + EnableNotify bool `db:"enable_notify"` + Ctime time.Time `db:"ctime"` + Mtime time.Time `db:"mtime"` +} diff --git a/database/models/discord/rt_table.go b/database/models/discord/rt_table.go new file mode 100644 index 0000000..cf19a95 --- /dev/null +++ b/database/models/discord/rt_table.go @@ -0,0 +1,39 @@ +package discord + +import "time" + +// ChannelFacebookRT - +type ChannelFacebookRT struct { + Channel string `db:"channel"` + Facebook string `db:"facebook"` + Tmpl string `db:"tmpl"` + Ctime time.Time `db:"ctime"` + Mtime time.Time `db:"mtime"` +} + +// ChannelInstagramRT - +type ChannelInstagramRT struct { + Channel string `db:"channel"` + Instagram string `db:"instagram"` + Tmpl string `db:"tmpl"` + Ctime time.Time `db:"ctime"` + Mtime time.Time `db:"mtime"` +} + +// ChannelYoutubeRT - +type ChannelYoutubeRT struct { + Channel string `db:"channel"` + Youtube string `db:"youtube"` + Tmpl string `db:"tmpl"` + Ctime time.Time `db:"ctime"` + Mtime time.Time `db:"mtime"` +} + +// ChannelTwitchRT - +type ChannelTwitchRT struct { + Channel string `db:"channel"` + Twitch string `db:"twitch"` + Tmpl string `db:"tmpl"` + Ctime time.Time `db:"ctime"` + Mtime time.Time `db:"mtime"` +} diff --git a/database/models/discord/server.go b/database/models/discord/server.go new file mode 100644 index 0000000..5933743 --- /dev/null +++ b/database/models/discord/server.go @@ -0,0 +1,13 @@ +package discord + +import "time" + +// Server - +type Server struct { + ID string `db:"id"` + Name string `db:"name"` + Permission int `db:"permission"` + Owner string `db:"owner"` + Ctime time.Time `db:"ctime"` + Mtime time.Time `db:"mtime"` +} diff --git a/database/models/public/command.go b/database/models/public/command.go new file mode 100644 index 0000000..09ce259 --- /dev/null +++ b/database/models/public/command.go @@ -0,0 +1,15 @@ +package public + +import "time" + +// Commands - +type Command struct { + Key string `db:"key"` + Platform string `db:"platform"` + Binding string `db:"binding"` + RequireManage bool `db:"require_manage"` + RequireInit bool `db:"require_init"` + Value string `db:"value"` + Ctime time.Time `db:"ctime"` + Mtime time.Time `db:"mtime"` +} diff --git a/database/models/public/facebook.go b/database/models/public/facebook.go new file mode 100644 index 0000000..93a924a --- /dev/null +++ b/database/models/public/facebook.go @@ -0,0 +1,11 @@ +package public + +import "time" + +// FacebookPage - +type FacebookPage struct { + ID string `db:"id"` + LastPost string `db:"lastpost"` + Ctime time.Time `db:"ctime"` + Mtime time.Time `db:"mtime"` +} diff --git a/database/models/public/instagram.go b/database/models/public/instagram.go new file mode 100644 index 0000000..6e07d42 --- /dev/null +++ b/database/models/public/instagram.go @@ -0,0 +1,11 @@ +package public + +import "time" + +// Instagram - +type Instagram struct { + ID string `db:"id"` + LastPost string `db:"lastpost"` + Ctime time.Time `db:"ctime"` + Mtime time.Time `db:"mtime"` +}