Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add raw (UI needs) and implement searching for MongoDB storage #1

Merged
merged 1 commit into from
Mar 20, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions mongodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package storage

import (
"log"

"github.com/mailhog/data"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
Expand Down Expand Up @@ -52,8 +51,31 @@ func (mongo *MongoDB) Count() int {
// Search finds messages matching the query
func (mongo *MongoDB) Search(kind, query string, start, limit int) (*data.Messages, int, error) {
messages := &data.Messages{}
// FIXME
return messages, 0, nil
var count = 0
var field = "raw.data"
switch kind {
case "to":
field = "raw.to"
case "from":
field = "raw.from"
}
err := mongo.Collection.Find(bson.M{field: bson.RegEx{Pattern: query, Options: "i"}}).Skip(start).Limit(limit).Sort("-created").Select(bson.M{
"id": 1,
"_id": 1,
"from": 1,
"to": 1,
"content.headers": 1,
"content.size": 1,
"created": 1,
"raw": 1,
}).All(messages)
if err != nil {
log.Printf("Error loading messages: %s", err)
return nil, 0, err
}
count, _ = mongo.Collection.Find(bson.M{field: bson.RegEx{Pattern: query, Options: "i"}}).Count()

return messages, count, nil
}

// List returns a list of messages by index
Expand All @@ -67,6 +89,7 @@ func (mongo *MongoDB) List(start int, limit int) (*data.Messages, error) {
"content.headers": 1,
"content.size": 1,
"created": 1,
"raw": 1,
}).All(messages)
if err != nil {
log.Printf("Error loading messages: %s", err)
Expand Down