1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-28 13:48:49 +08:00
Ivan Milošević effade00aa MF-325 - Add SPDX license and copyright headers (#362)
* MF-325 - Add SPDX license and copyright headers

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* MF-325 - Add SPDX license and copyright headers

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* MF-325 - Add SPDX license and copyright headers

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* MF-325 - Add SPDX license and copyright headers

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* MF-325 - Change mainflux version from 0.4.0 to 0.5.0

Signed-off-by: Ivan Milošević <iva@blokovi.com>
2018-08-26 13:15:48 +02:00

53 lines
1.2 KiB
Go

//
// Copyright (c) 2018
// Mainflux
//
// SPDX-License-Identifier: Apache-2.0
//
package mongodb
import (
"context"
"github.com/mainflux/mainflux"
"github.com/mainflux/mainflux/readers"
"github.com/mongodb/mongo-go-driver/bson"
"github.com/mongodb/mongo-go-driver/mongo"
"github.com/mongodb/mongo-go-driver/mongo/findopt"
)
const collection = "mainflux"
var _ readers.MessageRepository = (*mongoRepository)(nil)
type mongoRepository struct {
db *mongo.Database
}
// New returns new MongoDB reader.
func New(db *mongo.Database) readers.MessageRepository {
return mongoRepository{db: db}
}
func (repo mongoRepository) ReadAll(chanID, offset, limit uint64) []mainflux.Message {
col := repo.db.Collection(collection)
cursor, err := col.Find(context.Background(), bson.NewDocument(bson.EC.Int64("channel", int64(chanID))), findopt.Limit(int64(limit)), findopt.Skip(int64(offset)))
if err != nil {
return []mainflux.Message{}
}
defer cursor.Close(context.Background())
messages := []mainflux.Message{}
for cursor.Next(context.Background()) {
var msg mainflux.Message
if err := cursor.Decode(&msg); err != nil {
return []mainflux.Message{}
}
messages = append(messages, msg)
}
return messages
}