add new dao impl

This commit is contained in:
Jay 2019-09-17 10:08:41 +00:00
parent 9936519c70
commit f438be5771
9 changed files with 331 additions and 4 deletions

View File

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

View File

@ -1,7 +1,10 @@
package public package public
import ( import (
"database/sql"
dsmodel "dorisbot/database/models/discord"
pubmodel "dorisbot/database/models/public" pubmodel "dorisbot/database/models/public"
"errors"
) )
// FacebookImpl - // FacebookImpl -
@ -9,5 +12,101 @@ type FacebookImpl struct{}
// Create new Facebook Page // Create new Facebook Page
func (p FacebookImpl) Create(fb *pubmodel.FacebookPage) (err error) { 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 return
} }

View File

@ -0,0 +1,10 @@
package public
import "github.com/jmoiron/sqlx"
var x *sqlx.DB
// Init -
func Init(db *sqlx.DB) {
x = db
}

View File

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

View File

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

View File

@ -9,8 +9,8 @@ import (
type FacebookDAO interface { type FacebookDAO interface {
Create(fb *pubmodel.FacebookPage) error Create(fb *pubmodel.FacebookPage) error
UpdatePost(fb *pubmodel.FacebookPage, postID string) 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) GetAll() ([]pubmodel.FacebookPage, error)
Delete(id string) error Delete(id string) error
GetChannels() ([]dsmodel.Channel, error) GetChannels(id string) ([]dsmodel.Channel, error)
} }

View File

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

1
go.mod
View File

@ -5,6 +5,7 @@ go 1.12
require ( require (
git.trj.tw/golang/utils v0.0.0-20190225142552-b019626f0349 git.trj.tw/golang/utils v0.0.0-20190225142552-b019626f0349
github.com/bwmarrin/discordgo v0.19.0 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-contrib/sse v0.1.0 // indirect
github.com/gin-gonic/gin v1.4.0 github.com/gin-gonic/gin v1.4.0
github.com/go-sql-driver/mysql v1.4.1 // indirect github.com/go-sql-driver/mysql v1.4.1 // indirect

6
go.sum
View File

@ -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.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 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 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 h1:t8FVkw33L+wilf2QiWkw0UV77qRpcH/JHPKGpKa2E8g=
github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s= 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= 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 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 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/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 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/ugorji/go v1.1.4 h1:j4s+tAvLfL3bZyefP2SEWmhBzmuIlH/eqNuPdFPgngw= 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 h1:+EXw7AwNOKzPFXMZ1yNjO40aWCh3PIquJB2fYlv9wcs=
golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 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-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 h1:wYqz/tQaWUgGKyx+B/rssSE6wkIKdY5Ee6ryOmzarIg=
golang.org/x/sys v0.0.0-20190913121621-c3b328c6e5a7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 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= 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 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 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 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/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 h1:lFB4DoMU6B626w8ny76MV7VX6W2VHct2GVOI3xgiMrQ=
gopkg.in/go-playground/validator.v8 v8.18.2/go.mod h1:RX2a/7Ha8BgOhfk7j780h4/u/RRjR0eouCJSH80/M2Y= gopkg.in/go-playground/validator.v8 v8.18.2/go.mod h1:RX2a/7Ha8BgOhfk7j780h4/u/RRjR0eouCJSH80/M2Y=