add server dao
This commit is contained in:
parent
290f5ce85b
commit
4da803d57c
10
database/impl/discord/init.go
Normal file
10
database/impl/discord/init.go
Normal file
@ -0,0 +1,10 @@
|
||||
package discord
|
||||
|
||||
import "github.com/jmoiron/sqlx"
|
||||
|
||||
var x *sqlx.DB
|
||||
|
||||
// Init -
|
||||
func Init(db *sqlx.DB) {
|
||||
x = db
|
||||
}
|
43
database/impl/discord/server.go
Normal file
43
database/impl/discord/server.go
Normal 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
|
||||
}
|
@ -43,7 +43,7 @@ func (p FacebookImpl) UpdatePost(fb *pubmodel.FacebookPage, postID string) (err
|
||||
// GetByID -
|
||||
func (p FacebookImpl) GetByID(id string) (fb pubmodel.FacebookPage, exists bool, err error) {
|
||||
exists = true
|
||||
fb := pubmodel.FacebookPage{}
|
||||
fb = pubmodel.FacebookPage{}
|
||||
query := `
|
||||
select "id", "last_post", "ctime", "mtime"
|
||||
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)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
return
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"database/sql"
|
||||
dsmodel "dorisbot/database/models/discord"
|
||||
pubmodel "dorisbot/database/models/public"
|
||||
"errors"
|
||||
|
||||
sq "github.com/elgris/sqrl"
|
||||
)
|
||||
@ -29,7 +30,7 @@ func (i InstagramImpl) Create(ig *pubmodel.Instagram) (err error) {
|
||||
|
||||
// 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()
|
||||
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
|
||||
}
|
||||
@ -53,7 +54,7 @@ func (i *InstagramImpl) GetByID(id string) (pubmodel.Instagram, bool, error) {
|
||||
|
||||
err = x.Get(&ig, query, args...)
|
||||
if err != nil {
|
||||
exists = true
|
||||
exists := true
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
exists = false
|
||||
err = nil
|
||||
|
11
database/interfaces/discord/server.go
Normal file
11
database/interfaces/discord/server.go
Normal 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user