use dao pattern

This commit is contained in:
Jay 2019-09-16 14:06:23 +00:00
parent 4f8749a969
commit 9936519c70
9 changed files with 184 additions and 0 deletions

53
database/database.go Normal file
View File

@ -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
}

View File

@ -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
}

View File

@ -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)
}

View File

@ -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"`
}

View File

@ -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"`
}

View File

@ -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"`
}

View File

@ -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"`
}

View File

@ -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"`
}

View File

@ -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"`
}