GraphQL API and utilities for the rpdata project
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

  1. package postgres
  2. import (
  3. "context"
  4. "database/sql"
  5. "git.aiterp.net/rpdata/api/database/postgres/psqlcore"
  6. "git.aiterp.net/rpdata/api/models"
  7. )
  8. type channelRepository struct {
  9. insertWithIDs bool
  10. db *sql.DB
  11. }
  12. func (r *channelRepository) Find(ctx context.Context, name string) (*models.Channel, error) {
  13. row, err := psqlcore.New(r.db).SelectChannelByName(ctx, name)
  14. if err != nil {
  15. return nil, err
  16. }
  17. return r.channel(row), nil
  18. }
  19. func (r *channelRepository) List(ctx context.Context, filter models.ChannelFilter) ([]*models.Channel, error) {
  20. params := psqlcore.SelectChannelsParams{
  21. LimitSize: 0,
  22. }
  23. if filter.Names != nil {
  24. params.FilterName = true
  25. params.Names = filter.Names
  26. }
  27. if filter.LocationName != nil {
  28. params.FilterLocationName = true
  29. params.LocationName = *filter.LocationName
  30. }
  31. if filter.EventName != nil {
  32. params.FilterEventName = true
  33. params.EventName = *filter.EventName
  34. }
  35. if filter.Logged != nil {
  36. params.FilterLogged = true
  37. params.Logged = *filter.Logged
  38. }
  39. if filter.Limit > 0 {
  40. params.LimitSize = int32(filter.Limit)
  41. }
  42. rows, err := psqlcore.New(r.db).SelectChannels(ctx, params)
  43. if err != nil {
  44. return nil, err
  45. }
  46. return r.channels(rows), nil
  47. }
  48. func (r *channelRepository) Insert(ctx context.Context, channel models.Channel) (*models.Channel, error) {
  49. err := psqlcore.New(r.db).InsertChannel(ctx, psqlcore.InsertChannelParams(channel))
  50. if err != nil {
  51. return nil, err
  52. }
  53. return &channel, nil
  54. }
  55. func (r *channelRepository) Update(ctx context.Context, channel models.Channel, update models.ChannelUpdate) (*models.Channel, error) {
  56. channel.ApplyUpdate(update)
  57. err := psqlcore.New(r.db).UpdateChannel(ctx, psqlcore.UpdateChannelParams{
  58. Name: channel.Name,
  59. Logged: channel.Logged,
  60. Hub: channel.Hub,
  61. EventName: channel.EventName,
  62. LocationName: channel.LocationName,
  63. })
  64. if err != nil {
  65. return nil, err
  66. }
  67. return &channel, nil
  68. }
  69. func (r *channelRepository) Remove(ctx context.Context, channel models.Channel) error {
  70. return psqlcore.New(r.db).DeleteChannel(ctx, channel.Name)
  71. }
  72. func (r *channelRepository) channel(row psqlcore.DataChannel) *models.Channel {
  73. return &models.Channel{
  74. Name: row.Name,
  75. Logged: row.Logged,
  76. Hub: row.Hub,
  77. EventName: row.EventName,
  78. LocationName: row.LocationName,
  79. }
  80. }
  81. func (r *channelRepository) channels(rows []psqlcore.DataChannel) []*models.Channel {
  82. results := make([]*models.Channel, 0, len(rows))
  83. for _, row := range rows {
  84. results = append(results, r.channel(row))
  85. }
  86. return results
  87. }