mirror of
https://github.com/lyx0/nourybot.git
synced 2024-11-13 19:49:55 +01:00
remove announce and check if the channel is already in the database
This commit is contained in:
parent
3abbeba46f
commit
58b02f178e
3 changed files with 36 additions and 17 deletions
|
@ -19,16 +19,19 @@ func AddChannel(login string, message twitch.PrivateMessage, app *Application) {
|
||||||
channel := &data.Channel{
|
channel := &data.Channel{
|
||||||
Login: login,
|
Login: login,
|
||||||
TwitchID: userId,
|
TwitchID: userId,
|
||||||
Announce: false,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err = app.Models.Channels.Insert(channel)
|
err = app.Models.Channels.Insert(channel)
|
||||||
if err != nil {
|
|
||||||
app.Logger.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
reply := fmt.Sprintf("Joined channel %s", login)
|
if err != nil {
|
||||||
common.Send(message.Channel, reply, app.TwitchClient)
|
reply := fmt.Sprintf("Something went wrong FeelsBadMan %s", err)
|
||||||
|
common.Send(message.Channel, reply, app.TwitchClient)
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
reply := fmt.Sprintf("Added channel %s", login)
|
||||||
|
common.Send(message.Channel, reply, app.TwitchClient)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func DeleteChannel(login string, message twitch.PrivateMessage, app *Application) {
|
func DeleteChannel(login string, message twitch.PrivateMessage, app *Application) {
|
||||||
|
|
|
@ -11,7 +11,6 @@ type Channel struct {
|
||||||
AddedAt time.Time `json:"-"`
|
AddedAt time.Time `json:"-"`
|
||||||
Login string `json:"login"`
|
Login string `json:"login"`
|
||||||
TwitchID string `json:"twitchid"`
|
TwitchID string `json:"twitchid"`
|
||||||
Announce bool `json:"announce"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type ChannelModel struct {
|
type ChannelModel struct {
|
||||||
|
@ -20,18 +19,37 @@ type ChannelModel struct {
|
||||||
|
|
||||||
func (c ChannelModel) Insert(channel *Channel) error {
|
func (c ChannelModel) Insert(channel *Channel) error {
|
||||||
query := `
|
query := `
|
||||||
INSERT INTO channels (login, twitchid, announce)
|
INSERT INTO channels(login, twitchid)
|
||||||
VALUES ($1, $2, $3)
|
VALUES ($1, $2)
|
||||||
RETURNING id, added_at`
|
ON CONFLICT (login)
|
||||||
|
DO NOTHING
|
||||||
|
RETURNING id, added_at;
|
||||||
|
`
|
||||||
|
|
||||||
args := []interface{}{channel.Login, channel.TwitchID, channel.Announce}
|
args := []interface{}{channel.Login, channel.TwitchID}
|
||||||
|
|
||||||
return c.DB.QueryRow(query, args...).Scan(&channel.ID, &channel.AddedAt)
|
// Execute the query returning the number of affected rows.
|
||||||
|
result, err := c.DB.Exec(query, args...)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check how many rows were affected.
|
||||||
|
rowsAffected, err := result.RowsAffected()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if rowsAffected == 0 {
|
||||||
|
return ErrRecordAlreadyExists
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c ChannelModel) Get(login string) (*Channel, error) {
|
func (c ChannelModel) Get(login string) (*Channel, error) {
|
||||||
query := `
|
query := `
|
||||||
SELECT id, added_at, login, twitchid, announce
|
SELECT id, added_at, login, twitchid
|
||||||
FROM channels
|
FROM channels
|
||||||
WHERE login = $1`
|
WHERE login = $1`
|
||||||
|
|
||||||
|
@ -42,7 +60,6 @@ func (c ChannelModel) Get(login string) (*Channel, error) {
|
||||||
&channel.AddedAt,
|
&channel.AddedAt,
|
||||||
&channel.Login,
|
&channel.Login,
|
||||||
&channel.TwitchID,
|
&channel.TwitchID,
|
||||||
&channel.Announce,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
CREATE TABLE IF NOT EXISTS channels (
|
CREATE TABLE IF NOT EXISTS channels (
|
||||||
id bigserial PRIMARY KEY,
|
id bigserial PRIMARY KEY,
|
||||||
added_at timestamp(0) with time zone NOT NULL DEFAULT NOW(),
|
added_at timestamp(0) with time zone NOT NULL DEFAULT NOW(),
|
||||||
login text NOT NULL,
|
login text UNIQUE NOT NULL,
|
||||||
twitchid text NOT NULL,
|
twitchid text NOT NULL
|
||||||
announce BOOLEAN NOT NULL
|
|
||||||
);
|
);
|
Loading…
Reference in a new issue