use dao pattern
This commit is contained in:
		
							parent
							
								
									4f8749a969
								
							
						
					
					
						commit
						9936519c70
					
				
							
								
								
									
										53
									
								
								database/database.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										53
									
								
								database/database.go
									
									
									
									
									
										Normal 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
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										13
									
								
								database/impl/public/facebook.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								database/impl/public/facebook.go
									
									
									
									
									
										Normal 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
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										16
									
								
								database/interfaces/public/facebook.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								database/interfaces/public/facebook.go
									
									
									
									
									
										Normal 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)
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										13
									
								
								database/models/discord/channel.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								database/models/discord/channel.go
									
									
									
									
									
										Normal 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"`
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										39
									
								
								database/models/discord/rt_table.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								database/models/discord/rt_table.go
									
									
									
									
									
										Normal 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"`
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										13
									
								
								database/models/discord/server.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								database/models/discord/server.go
									
									
									
									
									
										Normal 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"`
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										15
									
								
								database/models/public/command.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								database/models/public/command.go
									
									
									
									
									
										Normal 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"`
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										11
									
								
								database/models/public/facebook.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								database/models/public/facebook.go
									
									
									
									
									
										Normal 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"`
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										11
									
								
								database/models/public/instagram.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								database/models/public/instagram.go
									
									
									
									
									
										Normal 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"`
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user