public UserModel AuthenticateAgainstDomain(string domainName, string password)
        {
            try
            {
                var directoryEntry = new DirectoryEntry(LDAP, domainName, password);
                var searchAdForUser = new DirectorySearcher(directoryEntry) { Filter = "(&(objectClass=user)(anr=" + domainName + "))" };
                var retrievedUser = searchAdForUser.FindOne();

                var ldapEmailAddress = retrievedUser.Properties["mail"][0].ToString();

                var existingUser = this.userRepository.Find(new UserSpecification().WithEmailAddress(ldapEmailAddress));

                if(existingUser == null)
                {
                    existingUser = new User { EmailAddress = ldapEmailAddress };
                }

                this.userRepository.Save(existingUser);

                var userModel = new UserModel()
                {
                    Title = retrievedUser.Properties["title"][0].ToString(),
                    Mobile = retrievedUser.Properties["mobile"][0].ToString(),
                    EmailAddress = ldapEmailAddress,
                    Name = retrievedUser.Properties["givenname"][0].ToString(),
                    Surname = retrievedUser.Properties["sn"][0].ToString()
                };

                return userModel;
            }
            catch (DirectoryServicesCOMException ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public FeedFineModel MapToFeedModelWithPayment(Fine fine, User issuer, User receiver, Payment payment)
        {
            FeedFineModel fineModel = new FeedFineModel {
                Id = fine.Id,
                IssuerId = fine.IssuerId,
                Reason = fine.Reason,
                SeconderId = fine.SeconderId,
                Pending = fine.Pending,
                AwardedDate = fine.AwardedDate,
                IssuerDisplayName = issuer.DisplayName,
                ReceiverDisplayName = receiver.DisplayName,
                ReceiverId = receiver.Id,
                ModifiedDate = fine.ModifiedDate,
                UserImage = receiver.Image,
                Platform = fine.Platform.GetDescription()
            };

            if(payment != null)
            {
                fineModel.PaidDate = payment.PaidDate;
                fineModel.PayerId = payment.PayerId;
                fineModel.PaymentImage = payment.PaymentImage.ImageBytes.ToString();
            }

            return fineModel;
        }
        public FeedFineModel MapPaymentToFeedModel(Payment payment, User issuer, User receiver, int totalPaid)
        {
            string paymentImage = payment.PaymentImage != null
                                  && payment.PaymentImage.ImageBytes != null
                                  && payment.PaymentImage.MimeType != null
                                  ? new StringBuilder("data:").Append(payment.PaymentImage.MimeType)
                                                     .Append(";base64,")
                                                     .Append(System.Convert.ToBase64String(payment.PaymentImage.ImageBytes))
                                                     .ToString()
                                  : null;

            return new FeedFineModel {
                Id = payment.Id,
                IssuerDisplayName = issuer.DisplayName,
                ReceiverDisplayName = receiver.DisplayName,
                PaidDate = payment.PaidDate,
                ModifiedDate = payment.PaidDate,
                PayerId = payment.PayerId,
                PaymentImage = paymentImage,
                AwardedDate = payment.PaidDate,
                UserImage = receiver.Image,
                TotalPaid = totalPaid,
                ReceiverId = receiver.Id,
                LikedBy = payment.LikedBy,
                DislikedBy = payment.DislikedBy,
                IssuerId = issuer.Id,
                Platform = payment.Platform.GetDescription()
            };
        }
        public UserModel MapToModel(User user)
        {
            var userModel = this.MapToModelShallow(user);

            userModel.Fines = user.Fines.Select(x => this.fineMapper.MapToModel(x)).ToList();

            return userModel;
        }
        public FeedFineModel MapToFeedModel(Fine fine, User issuer, User receiver, User seconder)
        {
            FeedFineModel model = this.MapToFeedModelWithPayment(fine, issuer, receiver, null);

            model.Seconder = seconder != null ? seconder.DisplayName : null;

            return model;
        }
        public UserModel MapToModelForThisYear(User user)
        {
            if (user == null) return null;

            return new UserModel
            {
                Id = user.Id,
                SlackId = user.SlackId,
                EmailAddress = user.EmailAddress,
                DisplayName = user.DisplayName,
                Image = user.Image,
                AwardedFineCount = user.Fines.Count(x => !x.Pending && x.AwardedDate >= DateTime.Now.StartOfYear())
            };
        }
        public UserModel MapToModelShallow(User user)
        {
            if(user == null) return null;

            return new UserModel
                   {
                       Id = user.Id,
                       SlackId = user.SlackId,
                       EmailAddress = user.EmailAddress,
                       DisplayName = user.DisplayName,
                       AwardedFineCount = user.Fines.Count(x => !x.Pending),
                       PendingFineCount = user.Fines.Count(x => x.Pending),
                       Image = user.Image
                   };
        }
        public void GivenALimitedListOfRecentPaidAndNewFines_When_RetrievingLatestFinesForFeed_Then_TheLatestOfBothShouldBeRetrieved()
        {
            IRepository<User, UserDataModel, Guid> userRepository = MockRepository.GenerateMock<IRepository<User, UserDataModel, Guid>>();
            IRepository<Payment, PaymentDataModel, Guid> paymentRepository = MockRepository.GenerateMock<IRepository<Payment, PaymentDataModel, Guid>>();
            IExcelExportService<FineExportModel> excelExportService =
            MockRepository.GenerateMock<IExcelExportService<FineExportModel>>();
            Guid userGuid1 = Guid.NewGuid();
            Guid userGuid2 = Guid.NewGuid();

            Guid fineId1 = Guid.NewGuid();
            Guid fineId2 = Guid.NewGuid();
            Guid fineId3 = Guid.NewGuid();

            Guid paymentId2 = Guid.NewGuid();

            User user = new User() { Id = userGuid2 };
            userRepository.Stub(x => x.Find(null)).IgnoreArguments().Return(user);

            paymentRepository.Stub(x => x.Find(null)).IgnoreArguments().Return(new Payment() {
                Id = paymentId2,
                PaidDate = new DateTime(2015, 09, 24),
                PayerId = userGuid2
            });

            paymentRepository.Stub(x => x.FindAll(null)).IgnoreArguments().Return(new List<Payment> {new Payment() {
                Id = paymentId2,
                PaidDate = new DateTime(2015, 09, 24),
                PayerId = userGuid2
            }});

            userRepository.Stub(x => x.GetAll()).Return(new List<User>
                                                        {
                                                            user,
                                                            new User()
                                                            {
                                                                Id = userGuid1,
                                                                Fines = new List<Fine>
                                                                        {
                                                                            new Fine()
                                                                            {
                                                                                Id = fineId1,
                                                                                AwardedDate = new DateTime(2015,09,20),
                                                                                ModifiedDate = new DateTime(2015,09,21),
                                                                                IssuerId = userGuid2
                                                                            },
                                                                            new Fine()
                                                                            {
                                                                                Id = fineId2,
                                                                                AwardedDate = new DateTime(2015,09,22),
                                                                                ModifiedDate = new DateTime(2015,09,23),
                                                                                IssuerId = userGuid2
                                                                            },
                                                                            new Fine()
                                                                            {
                                                                                Id = fineId3,
                                                                                AwardedDate = new DateTime(2015,09,10),
                                                                                ModifiedDate = new DateTime(2015,09,24),
                                                                                IssuerId = userGuid2,
                                                                                PaymentId = paymentId2
                                                                            }
                                                                        }
                                                            }
                                                        });

            FineApi fineApi = new FineApi(userRepository, paymentRepository, new FineMapper(), new UserMapper(new FineMapper()), MockRepository.GenerateMock<IPaymentMapper>(), excelExportService, null, null, null, null, null);

            List<FeedFineModel> finesList = fineApi.GetLatestSetOfFines(0, 3);

            Assert.That(finesList.Count == 3);
            Assert.AreEqual(0, finesList.Count(x => x.Id == fineId1));
            Assert.AreEqual(1, finesList.Count(x => x.Id == fineId2));
            Assert.AreEqual(1, finesList.Count(x => x.Id == fineId3));
            Assert.AreEqual(1, finesList.Count(x => x.Id == paymentId2));
            Assert.AreEqual(1, finesList.Count(x => x.IsPaid));
        }
        public void SecondOldestPendingFine_SecondsCorrectFine()
        {
            // Arrange:
            IRepository<User, UserDataModel, Guid> userRepository = MockRepository.GenerateMock<IRepository<User, UserDataModel, Guid>>();
            IRepository<Payment, PaymentDataModel, Guid> paymentRepository = MockRepository.GenerateMock<IRepository<Payment, PaymentDataModel, Guid>>();
            IFineMapper fineMapper = MockRepository.GenerateMock<IFineMapper>();
            IUserMapper userMapper = MockRepository.GenerateMock<IUserMapper>();
            IPaymentMapper paymentMapper = MockRepository.GenerateMock<IPaymentMapper>();
            IExcelExportService<FineExportModel> excelExportService =
                MockRepository.GenerateMock<IExcelExportService<FineExportModel>>();

            var fine = new Fine{AwardedDate = DateTime.Now};
            var user = new User{Fines = new List<Fine>{fine, new Fine{AwardedDate = DateTime.Now.AddMinutes(1)}}};

            userRepository.Stub(x => x.FindAll(null)).IgnoreArguments().Return(new List<User> { user });

            var userModel = new UserModel();
            userMapper.Stub(x => x.MapToModelShallow(user)).Return(userModel);

            FineApi fineApi = new FineApi(userRepository, paymentRepository, fineMapper, userMapper, paymentMapper, excelExportService, null, null, null, null, null);

            // Pre-Assert:
            fine.Pending.Should().Be.True();

            // Act:
            fineApi.SecondOldestPendingFine(Guid.NewGuid());

            // Assert:
            fine.Pending.Should().Be.False();

            userRepository.AssertWasCalled(x => x.Save(user));
            fineMapper.AssertWasCalled(x => x.MapToModelWithUser(fine, userModel));
        }
        public UserModel MapToModelSmall(User user)
        {
            if (user == null) return null;

            return new UserModel {
                Id = user.Id,
                DisplayName = user.DisplayName,
                Image = user.Image
            };
        }
        public UserModel MapToModelWithDate(User user)
        {
            if (user == null) return null;

            return new UserModel
            {
                Id = user.Id,
                SlackId = user.SlackId,
                EmailAddress = user.EmailAddress,
                DisplayName = user.DisplayName,
                Image = user.Image,
                AwardedFineCount = user.Fines.Count(x => !x.Pending && x.AwardedDate >= DateTime.Today)
            };
        }
        public UserModel RegisterUserBySlackId(string slackId)
        {
            var info = this.memberInfoApi.GetMemberInformation(slackId);

            var foundUser = this.userRepository.Find(new UserSpecification().WithEmailAddress(info.profile.email));

            if(foundUser != null)
            {
                foundUser.SlackId = slackId;
                this.userRepository.Save(foundUser);

                return this.userMapper.MapToModelShallow(foundUser);
            }

            User newUser = new User
                        {
                            EmailAddress = info.profile.email,
                            SlackId = slackId,
                            DisplayName = String.IsNullOrEmpty(info.name) ? String.Join(" ",this.GetUserNameAndSurnameFromEmail(info.profile.email)) : info.name
                        };

            var user = this.userRepository.Save(newUser);

            return this.userMapper.MapToModelShallow(user);
        }
        public UserModel RegisterUserByEmail(string email)
        {
            User newUser = new User
            {
                EmailAddress = email
            };

            var user = this.userRepository.Save(newUser);

            return this.userMapper.MapToModelShallow(user);
        }