add server dao

This commit is contained in:
Jay 2019-09-29 14:46:01 +00:00
parent 290f5ce85b
commit 4da803d57c
5 changed files with 69 additions and 4 deletions

View File

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

View File

@ -0,0 +1,43 @@
package discord
import (
dsmodel "dorisbot/database/models/discord"
sq "github.com/elgris/sqrl"
)
// ServerImpl -
type ServerImpl struct{}
// Create -
func (s ServerImpl) Create(server *dsmodel.Server) error {
query, args, err := sq.Insert(`"discord"."server"`).Columns("id", "name", "permission", "owner").
Values(server.ID, server.Name, server.Permission, server.Owner).
Suffix("returning *").
ToSql()
if err != nil {
return err
}
query = x.Rebind(query)
err = x.Get(server, query, args...)
if err != nil {
return err
}
return nil
}
// Delete -
func (s ServerImpl) Delete(id string) error {
query, args, err := sq.Delete(`"discord"."server"`).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
}

View File

@ -43,7 +43,7 @@ func (p FacebookImpl) UpdatePost(fb *pubmodel.FacebookPage, postID string) (err
// GetByID - // GetByID -
func (p FacebookImpl) GetByID(id string) (fb pubmodel.FacebookPage, exists bool, err error) { func (p FacebookImpl) GetByID(id string) (fb pubmodel.FacebookPage, exists bool, err error) {
exists = true exists = true
fb := pubmodel.FacebookPage{} fb = pubmodel.FacebookPage{}
query := ` query := `
select "id", "last_post", "ctime", "mtime" select "id", "last_post", "ctime", "mtime"
from "public"."facebook_page" from "public"."facebook_page"
@ -106,7 +106,7 @@ func (p FacebookImpl) GetChannels(id string) (list []dsmodel.Channel, err error)
` `
err = x.Select(&list, query, id) err = x.Select(&list, query, id)
if err != nil { if err != nil {
return err return nil, err
} }
return return
} }

View File

@ -4,6 +4,7 @@ import (
"database/sql" "database/sql"
dsmodel "dorisbot/database/models/discord" dsmodel "dorisbot/database/models/discord"
pubmodel "dorisbot/database/models/public" pubmodel "dorisbot/database/models/public"
"errors"
sq "github.com/elgris/sqrl" sq "github.com/elgris/sqrl"
) )
@ -29,7 +30,7 @@ func (i InstagramImpl) Create(ig *pubmodel.Instagram) (err error) {
// UpdatePost - // UpdatePost -
func (i InstagramImpl) UpdatePost(ig *pubmodel.Instagram, postID string) (err error) { 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() 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 { if err != nil {
return err return err
} }
@ -53,7 +54,7 @@ func (i *InstagramImpl) GetByID(id string) (pubmodel.Instagram, bool, error) {
err = x.Get(&ig, query, args...) err = x.Get(&ig, query, args...)
if err != nil { if err != nil {
exists = true exists := true
if errors.Is(err, sql.ErrNoRows) { if errors.Is(err, sql.ErrNoRows) {
exists = false exists = false
err = nil err = nil

View File

@ -0,0 +1,11 @@
package discord
import (
dsmodel "dorisbot/database/models/discord"
)
// ServerDAO -
type ServerDAO interface {
Create(s *dsmodel.Server) error
Delete(id string) error
}