From b625d46258a591e224c1126d37115f88cc73f4a6 Mon Sep 17 00:00:00 2001 From: lyx0 <66651385+lyx0@users.noreply.github.com> Date: Thu, 11 Aug 2022 01:18:23 +0200 Subject: [PATCH] add Get channel method --- internal/data/channel.go | 54 ++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/internal/data/channel.go b/internal/data/channel.go index 7f85dc6..f66abbc 100644 --- a/internal/data/channel.go +++ b/internal/data/channel.go @@ -18,6 +18,33 @@ type ChannelModel struct { DB *sql.DB } +func (c ChannelModel) Get(login string) (*Channel, error) { + query := ` + SELECT id, added_at, login, twitchid + FROM channels + WHERE login = $1` + + var channel Channel + + err := c.DB.QueryRow(query, login).Scan( + &channel.ID, + &channel.AddedAt, + &channel.Login, + &channel.TwitchID, + ) + + if err != nil { + switch { + case errors.Is(err, sql.ErrNoRows): + return nil, ErrRecordNotFound + default: + return nil, err + } + } + + return &channel, nil +} + func (c ChannelModel) Insert(channel *Channel) error { query := ` INSERT INTO channels(login, twitchid) @@ -48,33 +75,6 @@ func (c ChannelModel) Insert(channel *Channel) error { return nil } -func (c ChannelModel) Get(login string) (*Channel, error) { - query := ` - SELECT id, added_at, login, twitchid - FROM channels - WHERE login = $1` - - var channel Channel - - err := c.DB.QueryRow(query, login).Scan( - &channel.ID, - &channel.AddedAt, - &channel.Login, - &channel.TwitchID, - ) - - if err != nil { - switch { - case errors.Is(err, sql.ErrNoRows): - return nil, ErrRecordNotFound - default: - return nil, err - } - } - - return &channel, nil -} - // GetAll() returns a slice of all channels in the database. func (c ChannelModel) GetAll() ([]*Channel, error) { query := `