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{
|
||||
Login: login,
|
||||
TwitchID: userId,
|
||||
Announce: false,
|
||||
}
|
||||
|
||||
err = app.Models.Channels.Insert(channel)
|
||||
if err != nil {
|
||||
app.Logger.Error(err)
|
||||
}
|
||||
|
||||
reply := fmt.Sprintf("Joined channel %s", login)
|
||||
common.Send(message.Channel, reply, app.TwitchClient)
|
||||
if err != nil {
|
||||
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) {
|
||||
|
|
|
@ -11,7 +11,6 @@ type Channel struct {
|
|||
AddedAt time.Time `json:"-"`
|
||||
Login string `json:"login"`
|
||||
TwitchID string `json:"twitchid"`
|
||||
Announce bool `json:"announce"`
|
||||
}
|
||||
|
||||
type ChannelModel struct {
|
||||
|
@ -20,18 +19,37 @@ type ChannelModel struct {
|
|||
|
||||
func (c ChannelModel) Insert(channel *Channel) error {
|
||||
query := `
|
||||
INSERT INTO channels (login, twitchid, announce)
|
||||
VALUES ($1, $2, $3)
|
||||
RETURNING id, added_at`
|
||||
INSERT INTO channels(login, twitchid)
|
||||
VALUES ($1, $2)
|
||||
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) {
|
||||
query := `
|
||||
SELECT id, added_at, login, twitchid, announce
|
||||
SELECT id, added_at, login, twitchid
|
||||
FROM channels
|
||||
WHERE login = $1`
|
||||
|
||||
|
@ -42,7 +60,6 @@ func (c ChannelModel) Get(login string) (*Channel, error) {
|
|||
&channel.AddedAt,
|
||||
&channel.Login,
|
||||
&channel.TwitchID,
|
||||
&channel.Announce,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
CREATE TABLE IF NOT EXISTS channels (
|
||||
id bigserial PRIMARY KEY,
|
||||
added_at timestamp(0) with time zone NOT NULL DEFAULT NOW(),
|
||||
login text NOT NULL,
|
||||
twitchid text NOT NULL,
|
||||
announce BOOLEAN NOT NULL
|
||||
login text UNIQUE NOT NULL,
|
||||
twitchid text NOT NULL
|
||||
);
|
Loading…
Reference in a new issue