From 58b02f178e5894c06f6202cb83f110f750982077 Mon Sep 17 00:00:00 2001 From: lyx0 <66651385+lyx0@users.noreply.github.com> Date: Tue, 9 Aug 2022 21:24:46 +0200 Subject: [PATCH] remove announce and check if the channel is already in the database --- cmd/bot/channel.go | 15 +++++---- internal/data/channel.go | 33 ++++++++++++++----- .../000001_create_channels_table.up.sql | 5 ++- 3 files changed, 36 insertions(+), 17 deletions(-) diff --git a/cmd/bot/channel.go b/cmd/bot/channel.go index 93ad5c8..2b38b24 100644 --- a/cmd/bot/channel.go +++ b/cmd/bot/channel.go @@ -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) { diff --git a/internal/data/channel.go b/internal/data/channel.go index 5ecbd79..c8280fd 100644 --- a/internal/data/channel.go +++ b/internal/data/channel.go @@ -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 { diff --git a/migrations/000001_create_channels_table.up.sql b/migrations/000001_create_channels_table.up.sql index 9a76bef..67a6621 100644 --- a/migrations/000001_create_channels_table.up.sql +++ b/migrations/000001_create_channels_table.up.sql @@ -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 ); \ No newline at end of file