implement GetAllChannels functionality

This commit is contained in:
lyx0 2022-08-09 23:19:08 +02:00
parent 364ea24ca0
commit 332de23f64
4 changed files with 65 additions and 0 deletions

View file

@ -34,6 +34,15 @@ func (app *Application) AddChannel(login string, message twitch.PrivateMessage)
} }
} }
func (app *Application) GetAllChannels() {
channel, err := app.Models.Channels.GetAll()
if err != nil {
app.Logger.Error(err)
}
app.Logger.Infow("All channels:",
"channel", channel)
}
func (app *Application) DeleteChannel(login string, message twitch.PrivateMessage) { func (app *Application) DeleteChannel(login string, message twitch.PrivateMessage) {
err := app.Models.Channels.Delete(login) err := app.Models.Channels.Delete(login)
if err != nil { if err != nil {

View file

@ -99,6 +99,7 @@ func main() {
common.StartTime() common.StartTime()
common.Send("nourylul", "xd", app.TwitchClient) common.Send("nourylul", "xd", app.TwitchClient)
app.GetAllChannels()
}) })
app.TwitchClient.Join("nourylul") app.TwitchClient.Join("nourylul")

View file

@ -1,6 +1,7 @@
package data package data
import ( import (
"context"
"database/sql" "database/sql"
"errors" "errors"
"time" "time"
@ -74,6 +75,59 @@ func (c ChannelModel) Get(login string) (*Channel, error) {
return &channel, nil return &channel, nil
} }
// GetAll() returns a slice of all channels in the database.
func (c ChannelModel) GetAll() ([]*Channel, error) {
query := `
SELECT id, added_at, login, twitchid
FROM channels
ORDER BY id`
// Create a context with 3 seconds timeout.
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
// Use QueryContext() the context and query. This returns a
// sql.Rows resultset containing our channels.
rows, err := c.DB.QueryContext(ctx, query)
if err != nil {
return nil, err
}
// Need to defer a call to rows.Close() to ensure the resultset
// is closed before GetAll() returns.
defer rows.Close()
// Initialize an empty slice to hold the data.
channels := []*Channel{}
// Iterate over the resultset.
for rows.Next() {
// Initialize an empty Channel struct where we put on
// a single channel value.
var channel Channel
// Scan the values onto the channel struct
err := rows.Scan(
&channel.ID,
&channel.AddedAt,
&channel.Login,
&channel.TwitchID,
)
if err != nil {
return nil, err
}
// Add the single movie struct onto the slice.
channels = append(channels, &channel)
}
// When rows.Next() finishes call rows.Err() to retrieve any errors.
if err = rows.Err(); err != nil {
return nil, err
}
return channels, nil
}
func (c ChannelModel) Delete(login string) error { func (c ChannelModel) Delete(login string) error {
// Prepare the statement. // Prepare the statement.
query := ` query := `

View file

@ -17,6 +17,7 @@ type Models struct {
Channels interface { Channels interface {
Insert(channel *Channel) error Insert(channel *Channel) error
Get(login string) (*Channel, error) Get(login string) (*Channel, error)
GetAll() ([]*Channel, error)
Delete(login string) error Delete(login string) error
} }
Users interface { Users interface {