You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
104 lines
2.5 KiB
104 lines
2.5 KiB
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"git.aiterp.net/rpdata/api/database/postgres/psqlcore"
|
|
"git.aiterp.net/rpdata/api/models"
|
|
)
|
|
|
|
type channelRepository struct {
|
|
insertWithIDs bool
|
|
db *sql.DB
|
|
}
|
|
|
|
func (r *channelRepository) Find(ctx context.Context, name string) (*models.Channel, error) {
|
|
row, err := psqlcore.New(r.db).SelectChannelByName(ctx, name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return r.channel(row), nil
|
|
}
|
|
|
|
func (r *channelRepository) List(ctx context.Context, filter models.ChannelFilter) ([]*models.Channel, error) {
|
|
params := psqlcore.SelectChannelsParams{
|
|
LimitSize: 0,
|
|
}
|
|
|
|
if filter.Names != nil {
|
|
params.FilterName = true
|
|
params.Names = filter.Names
|
|
}
|
|
if filter.LocationName != nil {
|
|
params.FilterLocationName = true
|
|
params.LocationName = *filter.LocationName
|
|
}
|
|
if filter.EventName != nil {
|
|
params.FilterEventName = true
|
|
params.EventName = *filter.EventName
|
|
}
|
|
if filter.Logged != nil {
|
|
params.FilterLogged = true
|
|
params.Logged = *filter.Logged
|
|
}
|
|
if filter.Limit > 0 {
|
|
params.LimitSize = int32(filter.Limit)
|
|
}
|
|
|
|
rows, err := psqlcore.New(r.db).SelectChannels(ctx, params)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return r.channels(rows), nil
|
|
}
|
|
|
|
func (r *channelRepository) Insert(ctx context.Context, channel models.Channel) (*models.Channel, error) {
|
|
err := psqlcore.New(r.db).InsertChannel(ctx, psqlcore.InsertChannelParams(channel))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &channel, nil
|
|
}
|
|
|
|
func (r *channelRepository) Update(ctx context.Context, channel models.Channel, update models.ChannelUpdate) (*models.Channel, error) {
|
|
channel.ApplyUpdate(update)
|
|
|
|
err := psqlcore.New(r.db).UpdateChannel(ctx, psqlcore.UpdateChannelParams{
|
|
Name: channel.Name,
|
|
Logged: channel.Logged,
|
|
Hub: channel.Hub,
|
|
EventName: channel.EventName,
|
|
LocationName: channel.LocationName,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &channel, nil
|
|
}
|
|
|
|
func (r *channelRepository) Remove(ctx context.Context, channel models.Channel) error {
|
|
return psqlcore.New(r.db).DeleteChannel(ctx, channel.Name)
|
|
}
|
|
|
|
func (r *channelRepository) channel(row psqlcore.DataChannel) *models.Channel {
|
|
return &models.Channel{
|
|
Name: row.Name,
|
|
Logged: row.Logged,
|
|
Hub: row.Hub,
|
|
EventName: row.EventName,
|
|
LocationName: row.LocationName,
|
|
}
|
|
}
|
|
|
|
func (r *channelRepository) channels(rows []psqlcore.DataChannel) []*models.Channel {
|
|
results := make([]*models.Channel, 0, len(rows))
|
|
for _, row := range rows {
|
|
results = append(results, r.channel(row))
|
|
}
|
|
|
|
return results
|
|
}
|