|
@ -0,0 +1,80 @@ |
|
|
|
|
|
package mongodb |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
|
"context" |
|
|
|
|
|
"git.aiterp.net/rpdata/api/models" |
|
|
|
|
|
"github.com/globalsign/mgo" |
|
|
|
|
|
"github.com/globalsign/mgo/bson" |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
type channelRepository struct { |
|
|
|
|
|
channels *mgo.Collection |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (r *channelRepository) Find(ctx context.Context, name string) (*models.Channel, error) { |
|
|
|
|
|
channel := new(models.Channel) |
|
|
|
|
|
err := r.channels.FindId(name).One(channel) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return nil, err |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return channel, nil |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (r *channelRepository) List(ctx context.Context, filter models.ChannelFilter) ([]*models.Channel, error) { |
|
|
|
|
|
query := bson.M{} |
|
|
|
|
|
if filter.Logged != nil { |
|
|
|
|
|
query["logged"] = *filter.Logged |
|
|
|
|
|
} |
|
|
|
|
|
if filter.EventName != nil { |
|
|
|
|
|
query["eventName"] = *filter.EventName |
|
|
|
|
|
} |
|
|
|
|
|
if filter.LocationName != nil { |
|
|
|
|
|
query["locationName"] = *filter.LocationName |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
channels := make([]*models.Channel, 0, 32) |
|
|
|
|
|
err := r.channels.Find(query).Limit(filter.Limit).Sort("_id").All(&channels) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
if err == mgo.ErrNotFound { |
|
|
|
|
|
return channels, nil |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return nil, err |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return channels, nil |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (r *channelRepository) Insert(ctx context.Context, channel models.Channel) (*models.Channel, error) { |
|
|
|
|
|
err := r.channels.Insert(&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) { |
|
|
|
|
|
updateBson := bson.M{} |
|
|
|
|
|
if update.Logged != nil { |
|
|
|
|
|
updateBson["logged"] = *update.Logged |
|
|
|
|
|
} |
|
|
|
|
|
if update.LocationName != nil { |
|
|
|
|
|
updateBson["locationName"] = *update.LocationName |
|
|
|
|
|
} |
|
|
|
|
|
if update.EventName != nil { |
|
|
|
|
|
updateBson["eventName"] = *update.EventName |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
err := r.channels.UpdateId(channel.Name, bson.M{"$set": updateBson}) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return nil, err |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return &channel, nil |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (r *channelRepository) Remove(ctx context.Context, channel models.Channel) error { |
|
|
|
|
|
return r.channels.RemoveId(channel.Name) |
|
|
|
|
|
} |