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.

100 lines
2.4 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: 1000,
  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.Limit > 0 {
  36. params.LimitSize = int32(filter.Limit)
  37. }
  38. rows, err := psqlcore.New(r.db).SelectChannels(ctx, params)
  39. if err != nil {
  40. return nil, err
  41. }
  42. return r.channels(rows), nil
  43. }
  44. func (r *channelRepository) Insert(ctx context.Context, channel models.Channel) (*models.Channel, error) {
  45. err := psqlcore.New(r.db).InsertChannel(ctx, psqlcore.InsertChannelParams(channel))
  46. if err != nil {
  47. return nil, err
  48. }
  49. return &channel, nil
  50. }
  51. func (r *channelRepository) Update(ctx context.Context, channel models.Channel, update models.ChannelUpdate) (*models.Channel, error) {
  52. channel.ApplyUpdate(update)
  53. err := psqlcore.New(r.db).UpdateChannel(ctx, psqlcore.UpdateChannelParams{
  54. Name: channel.Name,
  55. Logged: channel.Logged,
  56. Hub: channel.Hub,
  57. EventName: channel.EventName,
  58. LocationName: channel.LocationName,
  59. })
  60. if err != nil {
  61. return nil, err
  62. }
  63. return &channel, nil
  64. }
  65. func (r *channelRepository) Remove(ctx context.Context, channel models.Channel) error {
  66. return psqlcore.New(r.db).DeleteChannel(ctx, channel.Name)
  67. }
  68. func (r *channelRepository) channel(row psqlcore.DataChannel) *models.Channel {
  69. return &models.Channel{
  70. Name: row.Name,
  71. Logged: row.Logged,
  72. Hub: row.Hub,
  73. EventName: row.EventName,
  74. LocationName: row.LocationName,
  75. }
  76. }
  77. func (r *channelRepository) channels(rows []psqlcore.DataChannel) []*models.Channel {
  78. results := make([]*models.Channel, 0, len(rows))
  79. for _, row := range rows {
  80. results = append(results, r.channel(row))
  81. }
  82. return results
  83. }