/// <inheritdoc />
        public int AddSubscriber(string authKey, DataTransferObject.SubscriberDto s)
        {
            if (!IsAuthenticatedKey(authKey)) {
                return 0;
            }

            using (var context = new NewsletterEntities()) {
                var subscriber = new Subscriber();
                var sessions = new SessionManager(context);
                subscriber.name = s.Name;
                subscriber.contact = s.Contact;
                subscriber.newsletter = sessions.GetSession(authKey).NewsletterId;

                context.Subscribers.AddObject(subscriber);
                context.SaveChanges();

                return subscriber.id;
            }
        }
        /// <inheritdoc />
        public void DeleteSubscriber(string authKey, int id)
        {
            if (!IsAuthenticatedKey(authKey)) {
                return;
            }

            // Create a message entity and store it
            using (var context = new NewsletterEntities()) {

                var sessions = new SessionManager(context);
                var newsletterId = sessions.GetSession(authKey).NewsletterId;
                var subscriberQuery = from s in context.Subscribers
                                      where s.newsletter == newsletterId && s.id == id
                                      select s;

                foreach (var row in subscriberQuery) {
                    context.Subscribers.DeleteObject(row);
                }

                // Persist changes
                context.SaveChanges();
            }
        }
        /// <inheritdoc />
        public List<NewsletterServer.DataTransferObject.MessageDto> GetMessageList(string authKey)
        {
            if (!IsAuthenticatedKey(authKey)) {
                return null;
            }

            // Fetch all subscribers for authed user
            using (var context = new NewsletterEntities()) {
                var sessions = new SessionManager(context);
                var newsletterId = sessions.GetSession(authKey).NewsletterId;
                var msgQuery = from m in context.Messages
                                      where m.newsletter == newsletterId
                                      select m;

                var msgs = new List<DataTransferObject.MessageDto>();
                CreateMappings();
                foreach (var msg in msgQuery) {
                    var mapped = AutoMapper.Mapper.Map<Message, DataTransferObject.MessageDto>(msg);
                    switch (msg.status) {
                        case 0:
                            mapped.Status = "Done";
                            break;
                        case 1:
                            mapped.Status = "Canceled";
                            break;
                        case 3:
                            mapped.Status = "Queued";
                            break;
                    }

                    var queueCountQuery = from s in msg.Subscribers select s;
                    mapped.WaitingToBeSent = queueCountQuery.Count();

                    msgs.Add(mapped);
                }

                return msgs;
            }
        }
        /// <inheritdoc />
        public void UpdateSubscriber(string authKey, DataTransferObject.SubscriberDto sub)
        {
            if (!IsAuthenticatedKey(authKey)) {
                return;
            }

            using (var context = new NewsletterEntities()) {
                var sessions = new SessionManager(context);
                var newsletterId = sessions.GetSession(authKey).NewsletterId;
                var subscriberQuery = from s in context.Subscribers
                                      where s.newsletter == newsletterId && s.id == sub.Id
                                      select s;

                foreach (var row in subscriberQuery) {
                    row.name = sub.Name;
                    row.contact = sub.Contact;
                }

                context.SaveChanges();
            }
        }
        /// <inheritdoc />
        public bool QueueMessage(string subject, string body, string clean_body, string authKey)
        {
            if (!IsAuthenticatedKey(authKey)) {
                return false;
            }

            // Create a message entity and store it
            using (var context = new NewsletterEntities()) {

                var sessions = new SessionManager(context);
                var message = new Message();
                message.status = 3; // TODO DeliveryServer.TransferAgent.Message.StatusWaiting;
                message.text = body;
                message.clean_text = clean_body;
                message.subject = subject;
                message.newsletter = sessions.GetSession(authKey).NewsletterId;
                message.date = DateTime.Now;

                context.Messages.AddObject(message);

                // Persist changes
                context.SaveChanges();
            }

            return true;
        }
        /// <inheritdoc />
        public List<DataTransferObject.SubscriberDto> GetSubscribers(string authKey)
        {
            if (!IsAuthenticatedKey(authKey)) {
                return null;
            }

            // Fetch all subscribers for authed user
            using (var context = new NewsletterEntities()) {
                var sessions = new SessionManager(context);
                var newsletterId = sessions.GetSession(authKey).NewsletterId;
                var subscriberQuery = from s in context.Subscribers
                                      where s.newsletter == newsletterId
                                      select s;

                var subscribers = new List<DataTransferObject.SubscriberDto>();
                CreateMappings();
                foreach (var subscriber in subscriberQuery) {
                    var mapped = AutoMapper.Mapper.Map<Subscriber, DataTransferObject.SubscriberDto>(subscriber);
                    subscribers.Add(mapped);
                }

                return subscribers;
            }
        }