public bool Update(Member member)
        {
            var id = member.Id;
            IMongoQuery query = Query.EQ("_id", id);

            var movies = new BsonDocument(true);

              //  movies.Add(member.Movies.ToDictionary(movie => movie.id));

            IMongoUpdate update = MongoDB.Driver.Builders.Update
                .Set("Email", member.Email)
                .Set("LastModified", DateTime.UtcNow)
                .Set("Name", member.Name)
                .Set("MovieQueues", movies)
                .Set("Phone", member.Phone);
            var result = _members.Update(query, update);
            return result.UpdatedExisting;
        }
        public Member Add(Member member)
        {
            try
            {
                if (!member.Id.HasValue)
                {
                    member.Id = Guid.NewGuid();
                }

                if (member.Movies == null)
                {
                    member.Movies = new List<Movie>();
                }
                _members.Add(member);
            }
            catch (Exception e)
            {
                _logger.Error(e);
            }

            return member;
        }
        public MemberRepository(string connection)
        {
            if (string.IsNullOrWhiteSpace(connection))
            {
                connection = "mongodb://*****:*****@ds031627.mongolab.com:31627/movieman";
            }

            _server = MongoServer.Create(connection);
            _database = _server.GetDatabase("Members", SafeMode.True);
            _members = _database.GetCollection<Member>("Members");

            // Reset database and add some default entries
            for (int index = 1; index < 5; index++)
            {
                var user = new Member
                               {
                                   Email = string.Format("test{0}@example.com", index),
                                   Name = string.Format("test{0}", index),
                                   Phone = string.Format("{0}{0}{0} {0}{0}{0} {0}{0}{0}{0}", index),
                                   //Movies = new List<Movie> {new Movie() {Title = string.Format("My Flick - {0}", index)}}
                                };
                Add(user);
            }
        }
        public bool Update(Member member)
        {
            Member found = null;
            try
            {
                var movies = member.Movies.Distinct(new MovieKeyComparer());

                foreach (var movie in movies)
                {
                    if (movie.Key!=null && movie.Key.wasWatched)
                    {
                        movie.Key.WatchedDate = DateTime.Now.ToShortDateString();
                    }
                }

                found = _members.FirstOrDefault(m => m.Id == member.Id);
                found.Movies = movies.ToList();
                if (found != null)
                {

                    int index = _members.IndexOf(found);
                    _members[index] = member;
                }
            }
            catch (Exception e)
            {
                _logger.Error(e);
                return false;
            }
            return true;
        }
        public HttpResponseMessage<Member> PostMember(Member member)
        {
            _logger.InfoFormat("Creating member {0}", member!=null ? member.Name : "name not specified");
            member = _memberRepo.Add(member);

            var response = GetMememberResponse(member, HttpStatusCode.Created);
            _logger.InfoFormat("Member: {0} was created successfully!", member.Name);
            return response;
        }
        private List<Movie> GetMovies(Member member)
        {
            var helper = new SearchHelper();

            var movies = new List<Movie>();
            var memberMovies = member.Movies;
            foreach (var movie in memberMovies)
            {
                var criteria = new MovieSearchCriteria {Key = movie.Key};
                var movieDetail = helper.Search(criteria).FirstOrDefault();
                if (movieDetail!=null)
                {
                    //make sure we set the key to the one we already have....otherwise a new one will get created.
                    movieDetail.Key = movie.Key;
                    movies.Add(movieDetail);
                }
            }
            return movies;
        }
 private HttpResponseMessage<Member> GetMememberResponse(Member member, HttpStatusCode statusCode)
 {
     var response = new HttpResponseMessage<Member>(member, statusCode);
     return response;
 }
        public HttpResponseMessage<Member> PutMember(Member member)
        {
            var response = GetMememberResponse(member, HttpStatusCode.OK);

            _logger.InfoFormat("Updating member: {0}", member.Id);
            try
            {
                if (!_memberRepo.Update(member))
                {
                    _logger.ErrorFormat("Member {0} not found!", member.Id);
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }

                var m = _memberRepo.GetById(member.Id.Value);
                response = GetMememberResponse(m, HttpStatusCode.OK);
            }
            catch (Exception e)
            {
                _logger.Error(e);
                if (!member.Id.HasValue)
                {
                    response = GetMememberResponse(_defaultMembers.First(), HttpStatusCode.OK);
                }
            }
            return response;
        }
 public Member Add(Member member)
 {
     _members.Insert(member);
     return member;
 }