2014-04-19 22:37:11 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"labix.org/v2/mgo"
|
|
|
|
"labix.org/v2/mgo/bson"
|
|
|
|
"github.com/ian-kent/MailHog/mailhog/data"
|
|
|
|
"github.com/ian-kent/MailHog/mailhog"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Store(c *mailhog.Config, m *data.SMTPMessage) (string, error) {
|
|
|
|
msg := data.ParseSMTPMessage(c, m)
|
2014-04-20 14:35:59 +00:00
|
|
|
session, err := mgo.Dial(c.MongoUri)
|
2014-04-19 22:37:11 +00:00
|
|
|
if(err != nil) {
|
|
|
|
log.Printf("Error connecting to MongoDB: %s", err)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer session.Close()
|
2014-04-20 14:35:59 +00:00
|
|
|
err = session.DB(c.MongoDb).C(c.MongoColl).Insert(msg)
|
2014-04-19 22:37:11 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error inserting message: %s", err)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return msg.Id, nil
|
|
|
|
}
|
|
|
|
|
2014-04-20 15:05:50 +00:00
|
|
|
func List(c *mailhog.Config, start int, limit int) (*data.Messages, error) {
|
|
|
|
session, err := mgo.Dial(c.MongoUri)
|
|
|
|
if(err != nil) {
|
|
|
|
log.Printf("Error connecting to MongoDB: %s", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer session.Close()
|
|
|
|
messages := &data.Messages{}
|
|
|
|
err = session.DB(c.MongoDb).C(c.MongoColl).Find(bson.M{}).Skip(start).Limit(limit).All(messages)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error loading messages: %s", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return messages, nil;
|
|
|
|
}
|
|
|
|
|
2014-04-20 16:09:06 +00:00
|
|
|
func DeleteOne(c *mailhog.Config, id string) error {
|
|
|
|
session, err := mgo.Dial(c.MongoUri)
|
|
|
|
if(err != nil) {
|
|
|
|
log.Printf("Error connecting to MongoDB: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer session.Close()
|
|
|
|
_, err = session.DB(c.MongoDb).C(c.MongoColl).RemoveAll(bson.M{"id": id})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteAll(c *mailhog.Config) error {
|
|
|
|
session, err := mgo.Dial(c.MongoUri)
|
|
|
|
if(err != nil) {
|
|
|
|
log.Printf("Error connecting to MongoDB: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer session.Close()
|
|
|
|
_, err = session.DB(c.MongoDb).C(c.MongoColl).RemoveAll(bson.M{})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-04-20 14:35:59 +00:00
|
|
|
func Load(c *mailhog.Config, id string) (*data.Message, error) {
|
|
|
|
session, err := mgo.Dial(c.MongoUri)
|
2014-04-19 22:37:11 +00:00
|
|
|
if(err != nil) {
|
|
|
|
log.Printf("Error connecting to MongoDB: %s", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer session.Close()
|
|
|
|
result := &data.Message{}
|
2014-04-20 14:35:59 +00:00
|
|
|
err = session.DB(c.MongoDb).C(c.MongoColl).Find(bson.M{"id": id}).One(&result)
|
2014-04-19 22:37:11 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error loading message: %s", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return result, nil;
|
|
|
|
}
|