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.

107 lines
2.5 KiB

  1. package mongodb
  2. import (
  3. "context"
  4. "git.aiterp.net/rpdata/api/models"
  5. "github.com/globalsign/mgo"
  6. "github.com/globalsign/mgo/bson"
  7. )
  8. func newChannelRepository(db *mgo.Database) (*channelRepository, error) {
  9. collection := db.C("common.channels")
  10. err := collection.EnsureIndexKey("logged")
  11. if err != nil {
  12. return nil, err
  13. }
  14. err = collection.EnsureIndexKey("event")
  15. if err != nil {
  16. return nil, err
  17. }
  18. err = collection.EnsureIndexKey("location")
  19. if err != nil {
  20. return nil, err
  21. }
  22. return &channelRepository{
  23. channels: collection,
  24. }, nil
  25. }
  26. type channelRepository struct {
  27. channels *mgo.Collection
  28. }
  29. func (r *channelRepository) Find(ctx context.Context, name string) (*models.Channel, error) {
  30. channel := new(models.Channel)
  31. err := r.channels.FindId(name).One(channel)
  32. if err != nil {
  33. return nil, err
  34. }
  35. return channel, nil
  36. }
  37. func (r *channelRepository) List(ctx context.Context, filter models.ChannelFilter) ([]*models.Channel, error) {
  38. query := bson.M{}
  39. if filter.Logged != nil {
  40. query["logged"] = *filter.Logged
  41. }
  42. if filter.EventName != nil {
  43. query["eventName"] = *filter.EventName
  44. }
  45. if filter.LocationName != nil {
  46. query["locationName"] = *filter.LocationName
  47. }
  48. if len(filter.Names) > 0 {
  49. query["_id"] = bson.M{"$in": filter.Names}
  50. }
  51. channels := make([]*models.Channel, 0, 32)
  52. err := r.channels.Find(query).Limit(filter.Limit).Sort("_id").All(&channels)
  53. if err != nil {
  54. if err == mgo.ErrNotFound {
  55. return channels, nil
  56. }
  57. return nil, err
  58. }
  59. return channels, nil
  60. }
  61. func (r *channelRepository) Insert(ctx context.Context, channel models.Channel) (*models.Channel, error) {
  62. err := r.channels.Insert(channel)
  63. if err != nil {
  64. return nil, err
  65. }
  66. return &channel, nil
  67. }
  68. func (r *channelRepository) Update(ctx context.Context, channel models.Channel, update models.ChannelUpdate) (*models.Channel, error) {
  69. updateBson := bson.M{}
  70. if update.Logged != nil {
  71. updateBson["logged"] = *update.Logged
  72. channel.Logged = *update.Logged
  73. }
  74. if update.LocationName != nil {
  75. updateBson["locationName"] = *update.LocationName
  76. channel.LocationName = *update.LocationName
  77. }
  78. if update.EventName != nil {
  79. updateBson["eventName"] = *update.EventName
  80. channel.EventName = *update.EventName
  81. }
  82. err := r.channels.UpdateId(channel.Name, bson.M{"$set": updateBson})
  83. if err != nil {
  84. return nil, err
  85. }
  86. return &channel, nil
  87. }
  88. func (r *channelRepository) Remove(ctx context.Context, channel models.Channel) error {
  89. return r.channels.RemoveId(channel.Name)
  90. }