add Get channel method

This commit is contained in:
lyx0 2022-08-11 01:18:23 +02:00
parent 76e7a4f755
commit b625d46258

View file

@ -18,6 +18,33 @@ type ChannelModel struct {
DB *sql.DB 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 { func (c ChannelModel) Insert(channel *Channel) error {
query := ` query := `
INSERT INTO channels(login, twitchid) INSERT INTO channels(login, twitchid)
@ -48,33 +75,6 @@ func (c ChannelModel) Insert(channel *Channel) error {
return nil 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. // GetAll() returns a slice of all channels in the database.
func (c ChannelModel) GetAll() ([]*Channel, error) { func (c ChannelModel) GetAll() ([]*Channel, error) {
query := ` query := `