From f438be5771cabc053358e001891192b41e98f0fe Mon Sep 17 00:00:00 2001 From: Jay Date: Tue, 17 Sep 2019 10:08:41 +0000 Subject: [PATCH] add new dao impl --- database/impl/public/command.go | 65 +++++++++++++ database/impl/public/facebook.go | 99 ++++++++++++++++++++ database/impl/public/init.go | 10 ++ database/impl/public/instagram.go | 118 ++++++++++++++++++++++++ database/interfaces/public/command.go | 16 ++++ database/interfaces/public/facebook.go | 4 +- database/interfaces/public/instagram.go | 16 ++++ go.mod | 1 + go.sum | 6 +- 9 files changed, 331 insertions(+), 4 deletions(-) create mode 100644 database/impl/public/command.go create mode 100644 database/impl/public/init.go create mode 100644 database/impl/public/instagram.go create mode 100644 database/interfaces/public/command.go create mode 100644 database/interfaces/public/instagram.go diff --git a/database/impl/public/command.go b/database/impl/public/command.go new file mode 100644 index 0000000..18a2f8a --- /dev/null +++ b/database/impl/public/command.go @@ -0,0 +1,65 @@ +package public + +import ( + pubmodel "dorisbot/database/models/public" + + sq "github.com/elgris/sqrl" +) + +// CommandImpl - +type CommandImpl struct{} + +// Create - +func (c *CommandImpl) Create(cmd *pubmodel.Command) error { + query, args, err := sq.Insert(`"public"."command"`). + Columns("key", "platform", "binding", "require_manage", "require_init", "value", "ctime", "mtime"). + Values(cmd.Key, cmd.Platform, cmd.Binding, cmd.RequireManage, cmd.RequireInit, cmd.Value, sq.Expr("now()"), sq.Expr("now()")). + ToSql() + if err != nil { + return err + } + query = x.Rebind(query) + err = x.Get(cmd, query, args...) + if err != nil { + return err + } + return nil +} + +// Delete - +func (c *CommandImpl) Delete(key string, platform string, binding string) error { + query, args, err := sq.Delete(`"public"."command"`).Where(sq.And{ + sq.Eq{"key": key}, + sq.Eq{"platform": platform}, + sq.Eq{"binding": binding}, + }) + if err != nil { + return err + } + query = x.Rebind(query) + _, err = x.Exec(query, args...) + if err != nil { + return err + } + return nil +} + +func (c *CommandImpl) GetByKey(key string) ([]pubmodel.Command, error) { + panic("not implemented") +} + +func (c *CommandImpl) GetByKeyPlatform(key string, platform string) ([]pubmodel.Command, error) { + panic("not implemented") +} + +func (c *CommandImpl) GetByKeyPlatformBinding(key string, platform string, binding string) ([]pubmodel.Command, error) { + panic("not implemented") +} + +func (c *CommandImpl) GetByPlatformBinding(platform string, binding string) ([]pubmodel.Command, error) { + panic("not implemented") +} + +func (c *CommandImpl) GetByKeyBinding(key string, binding string) ([]pubmodel.Command, error) { + panic("not implemented") +} diff --git a/database/impl/public/facebook.go b/database/impl/public/facebook.go index 99a8414..47533cf 100644 --- a/database/impl/public/facebook.go +++ b/database/impl/public/facebook.go @@ -1,7 +1,10 @@ package public import ( + "database/sql" + dsmodel "dorisbot/database/models/discord" pubmodel "dorisbot/database/models/public" + "errors" ) // FacebookImpl - @@ -9,5 +12,101 @@ type FacebookImpl struct{} // Create new Facebook Page func (p FacebookImpl) Create(fb *pubmodel.FacebookPage) (err error) { + query := ` + insert into "public"."facebook_page" ("id") values ($1) returnint * + ` + + err = x.Get(fb, query, fb.ID) + if err != nil { + return err + } + return nil +} + +// UpdatePost - +func (p FacebookImpl) UpdatePost(fb *pubmodel.FacebookPage, postID string) (err error) { + query := ` + update "public"."facebook_page" set + "last_post" = $1, + "mtime" = now() + where + "id" = $2 + ` + + err = x.Get(fb, query, postID, fb.ID) + if err != nil { + return err + } + return +} + +// GetByID - +func (p FacebookImpl) GetByID(id string) (fb pubmodel.FacebookPage, exists bool, err error) { + exists = true + fb := pubmodel.FacebookPage{} + query := ` + select "id", "last_post", "ctime", "mtime" + from "public"."facebook_page" + where + "id" = $1 + limit 1 + ` + + err = x.Get(&fb, query, id) + if err != nil { + if errors.Is(err, sql.ErrNoRows) { + exists = false + err = nil + } + return fb, exists, err + } + return +} + +// GetAll - +func (p FacebookImpl) GetAll() (list []pubmodel.FacebookPage, err error) { + query := ` + select "id", "last_post", "ctime", "mtime" + from "public"."facebook_page" + order by "ctime" asc + ` + err = x.Select(&list, query) + if err != nil { + return nil, err + } + return +} + +// Delete - +func (p FacebookImpl) Delete(id string) (err error) { + query := ` + delete from "public"."facebook_page" + where "id" = $1 + ` + + _, err = x.Exec(query, id) + if err != nil { + return err + } + return +} + +// GetChannels - +func (p FacebookImpl) GetChannels(id string) (list []dsmodel.Channel, err error) { + query := ` + select + ch."id", ch."server", ch."enable_cmd", ch."enable_notify", ch."ctime", ch."mtime" + from "discord"."channel" ch + left join "discord"."channel_facebook_rt" rt + on rt.channel = ch.id + left join "public"."facebook_page" fb + on fb.id = rt.facebook + where + fb.id = $1 + ` + err = x.Select(&list, query, id) + if err != nil { + return err + } return } diff --git a/database/impl/public/init.go b/database/impl/public/init.go new file mode 100644 index 0000000..9d63bf2 --- /dev/null +++ b/database/impl/public/init.go @@ -0,0 +1,10 @@ +package public + +import "github.com/jmoiron/sqlx" + +var x *sqlx.DB + +// Init - +func Init(db *sqlx.DB) { + x = db +} diff --git a/database/impl/public/instagram.go b/database/impl/public/instagram.go new file mode 100644 index 0000000..19e7423 --- /dev/null +++ b/database/impl/public/instagram.go @@ -0,0 +1,118 @@ +package public + +import ( + "database/sql" + dsmodel "dorisbot/database/models/discord" + pubmodel "dorisbot/database/models/public" + + sq "github.com/elgris/sqrl" +) + +// InstagramImpl - +type InstagramImpl struct{} + +// Create - +func (i InstagramImpl) Create(ig *pubmodel.Instagram) (err error) { + query, args, err := sq.Insert(`"public"."instagram"`).Columns("id").Values(ig.ID).Suffix("returning *").ToSql() + if err != nil { + return err + } + query = x.Rebind(query) + + err = x.Get(ig, query, args...) + if err != nil { + return err + } + + return +} + +// UpdatePost - +func (i InstagramImpl) UpdatePost(ig *pubmodel.Instagram, postID string) (err error) { + query, args, err := sq.Update(`"public"."instagram"`).Set("last_post", postID).Set("mtime", sq.Expr("now()")).Where(sq.Eq{"id", ig.ID}).ToSql() + if err != nil { + return err + } + query = x.Rebind(query) + + err = x.Get(ig, query, args...) + if err != nil { + return err + } + return +} + +// GetByID - +func (i *InstagramImpl) GetByID(id string) (pubmodel.Instagram, bool, error) { + ig := pubmodel.Instagram{} + query, args, err := sq.Select("id", "last_post", "ctime", "mtime").From(`"public"."instagram"`).Where(sq.Eq{"id": id}).ToSql() + if err != nil { + return ig, false, err + } + query = x.Rebind(query) + + err = x.Get(&ig, query, args...) + if err != nil { + exists = true + if errors.Is(err, sql.ErrNoRows) { + exists = false + err = nil + } + return ig, exists, err + } + + return ig, true, nil +} + +// GetAll - +func (i *InstagramImpl) GetAll() ([]pubmodel.Instagram, error) { + query, args, err := sq.Select("id", "last_post", "ctime", "mtime").From(`"public"."instagram"`).OrderBy("ctime").ToSql() + if err != nil { + return nil, err + } + query = x.Rebind(query) + + list := make([]pubmodel.Instagram, 0) + err = x.Select(&list, query, args...) + if err != nil { + return nil, err + } + + return list, nil +} + +// Delete - +func (i *InstagramImpl) Delete(id string) error { + query, args, err := sq.Delete(`"public"."instagram"`).Where(sq.Eq{"id": id}).ToSql() + if err != nil { + return err + } + query = x.Rebind(query) + + _, err = x.Exec(query, args...) + if err != nil { + return err + } + + return nil +} + +// GetChannels - +func (i *InstagramImpl) GetChannels(id string) ([]dsmodel.Channel, error) { + query, args, err := sq.Select("ch.id", "ch.server", "ch.enable_cmd", "ch.enable_notify", "ch.ctime", "ch.mtime"). + From(`"discord"."channel" ch`). + LeftJoin(`"discord"."channel_instagram_rt" rt on rt.channel = ch.id`). + LeftJoin(`"public"."instagram" ig on ig.id = rt.instagram`). + Where(sq.Eq{"ig.id": id}).OrderBy("ch.ctime").ToSql() + if err != nil { + return nil, err + } + query = x.Rebind(query) + + list := make([]dsmodel.Channel, 0) + err = x.Select(&list, query, args...) + if err != nil { + return nil, err + } + return list, nil +} diff --git a/database/interfaces/public/command.go b/database/interfaces/public/command.go new file mode 100644 index 0000000..2c01c5c --- /dev/null +++ b/database/interfaces/public/command.go @@ -0,0 +1,16 @@ +package public + +import ( + pubmodel "dorisbot/database/models/public" +) + +// CommandDAO - +type CommandDAO interface { + Create(c *pubmodel.Command) error + Delete(key, platform, binding string) error + GetByKey(key string) ([]pubmodel.Command, error) + GetByKeyPlatform(key, platform string) ([]pubmodel.Command, error) + GetByKeyPlatformBinding(key, platform, binding string) ([]pubmodel.Command, error) + GetByPlatformBinding(platform, binding string) ([]pubmodel.Command, error) + GetByKeyBinding(key, binding string) ([]pubmodel.Command, error) +} diff --git a/database/interfaces/public/facebook.go b/database/interfaces/public/facebook.go index 8bc778f..511e10e 100644 --- a/database/interfaces/public/facebook.go +++ b/database/interfaces/public/facebook.go @@ -9,8 +9,8 @@ import ( type FacebookDAO interface { Create(fb *pubmodel.FacebookPage) error UpdatePost(fb *pubmodel.FacebookPage, postID string) error - GetByID(id string) (pubmodel.FacebookPage, error) + GetByID(id string) (pubmodel.FacebookPage, bool, error) GetAll() ([]pubmodel.FacebookPage, error) Delete(id string) error - GetChannels() ([]dsmodel.Channel, error) + GetChannels(id string) ([]dsmodel.Channel, error) } diff --git a/database/interfaces/public/instagram.go b/database/interfaces/public/instagram.go new file mode 100644 index 0000000..2b94b28 --- /dev/null +++ b/database/interfaces/public/instagram.go @@ -0,0 +1,16 @@ +package public + +import ( + dsmodel "dorisbot/database/models/discord" + pubmodel "dorisbot/database/models/public" +) + +// InstagramDAO - +type InstagramDAO interface { + Create(ig *pubmodel.Instagram) error + UpdatePost(ig *pubmodel.Instagram, postID string) error + GetByID(id string) (pubmodel.Instagram, bool, error) + GetAll() ([]pubmodel.Instagram, error) + Delete(id string) error + GetChannels(id string) ([]dsmodel.Channel, error) +} diff --git a/go.mod b/go.mod index 254c424..a8ec101 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.12 require ( git.trj.tw/golang/utils v0.0.0-20190225142552-b019626f0349 github.com/bwmarrin/discordgo v0.19.0 + github.com/elgris/sqrl v0.0.0-20190909141434-5a439265eeec github.com/gin-contrib/sse v0.1.0 // indirect github.com/gin-gonic/gin v1.4.0 github.com/go-sql-driver/mysql v1.4.1 // indirect diff --git a/go.sum b/go.sum index 9bc455e..6268d3e 100644 --- a/go.sum +++ b/go.sum @@ -5,6 +5,8 @@ github.com/bwmarrin/discordgo v0.19.0/go.mod h1:O9S4p+ofTFwB02em7jkpkV8M3R0/PUVO github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/elgris/sqrl v0.0.0-20190909141434-5a439265eeec h1:rHZeRq/c2NNprSLS3Ug0uKJvB8jKP1NuuyMSgKOjz+U= +github.com/elgris/sqrl v0.0.0-20190909141434-5a439265eeec/go.mod h1:hQPgqeM4LmbfKCaBkcedRq5y1yfb8Qb8iYdbuNjE4FU= github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3 h1:t8FVkw33L+wilf2QiWkw0UV77qRpcH/JHPKGpKa2E8g= github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= @@ -55,6 +57,7 @@ github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3Rllmb github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/ugorji/go v1.1.4 h1:j4s+tAvLfL3bZyefP2SEWmhBzmuIlH/eqNuPdFPgngw= @@ -82,8 +85,6 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20190606165138-5da285871e9c h1:+EXw7AwNOKzPFXMZ1yNjO40aWCh3PIquJB2fYlv9wcs= golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190825160603-fb81701db80f h1:LCxigP8q3fPRGNVYndYsyHnF0zRrvcoVwZMfb8iQZe4= -golang.org/x/sys v0.0.0-20190825160603-fb81701db80f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190913121621-c3b328c6e5a7 h1:wYqz/tQaWUgGKyx+B/rssSE6wkIKdY5Ee6ryOmzarIg= golang.org/x/sys v0.0.0-20190913121621-c3b328c6e5a7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -97,6 +98,7 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM= gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE= gopkg.in/go-playground/validator.v8 v8.18.2 h1:lFB4DoMU6B626w8ny76MV7VX6W2VHct2GVOI3xgiMrQ= gopkg.in/go-playground/validator.v8 v8.18.2/go.mod h1:RX2a/7Ha8BgOhfk7j780h4/u/RRjR0eouCJSH80/M2Y=