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
+65
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")
}
+99
View File
@@ -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
}
+10
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
}
+118
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
}
+16
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)
}
+2 -2
View File
@@ -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)
}
+16
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)
}