public void Test_Delete_Users()
        {
            HomeController       controller = this.Resolve();
            List <Models.Person> persons    = this.Generate();
            List <Models.Person> used       = persons.Take(50).ToList();

            // Tworzymy 50 userów
            used.ForEach(person =>
            {
                HttpPostRequest_AddUser addUserRequest = new HttpPostRequest_AddUser
                {
                    Firstname  = person.FirstName,
                    Lastname   = person.LastName,
                    Birthdate  = person.BirthdateDisplay,
                    Height     = person.Height,
                    ClubMember = person.Member
                };

                // Tworzymy goscia na serwie
                JsonResult createUserResult = controller.PostRequestProcessor(JsonConvert.SerializeObject(addUserRequest));
                JObject jObject             = JObject.FromObject(createUserResult.Data);

                PostRequestProcessorResult resultJson = jObject.ToObject <PostRequestProcessorResult>();
                Assert.IsTrue(resultJson.Result, string.Format("Could not create user {0} {1} on web service side!", person.FirstName, person.LastName));
            });

            List <Models.Person> deleted = used.Take(25).ToList();

            deleted.ForEach(person =>
            {
                HttpPostRequest_DropUser dropUserRequest = new HttpPostRequest_DropUser
                {
                    UserID = person.PersonID
                };

                JsonResult createUserResult = controller.PostRequestProcessor(JsonConvert.SerializeObject(dropUserRequest));
                JObject jObject             = JObject.FromObject(createUserResult.Data);

                PostRequestProcessorResult resultJson = jObject.ToObject <PostRequestProcessorResult>();
                Assert.IsTrue(resultJson.Result, string.Format("Could not delete user {0} {1} on web service side!", person.FirstName, person.LastName));
            });

            HttpPostRequest_Users request = new HttpPostRequest_Users {
                ID = "HttpPostRequest_Users", Page = 1, TotalPerPage = 25
            };
            JsonResult requestResult = controller.PostRequestProcessor(JsonConvert.SerializeObject(request));

            JObject jObjectResult = JObject.FromObject(requestResult.Data);
            PostRequestProcessorResultUsers resultUsers = jObjectResult.ToObject <PostRequestProcessorResultUsers>();

            // Intersect powinien oddać nam used - deleted
            deleted.ForEach(person =>
            {
                used.Remove(person);
            });

            List <Models.Person> check = used.Intersect(resultUsers.Users, new UsersEqualityComparer()).ToList();

            Assert.AreEqual(used.Count, check.Count, "Got different users!");
        }
        public void Test_Filter_Users()
        {
            HomeController       controller = this.Resolve();
            List <Models.Person> persons    = this.Generate();
            List <Models.Person> used       = persons.Take(50).ToList();

            // Tworzymy 50 userów
            used.ForEach(person =>
            {
                HttpPostRequest_AddUser addUserRequest = new HttpPostRequest_AddUser
                {
                    Firstname  = person.FirstName,
                    Lastname   = person.LastName,
                    Birthdate  = person.BirthdateDisplay,
                    Height     = person.Height,
                    ClubMember = person.Member
                };

                // Tworzymy goscia na serwie
                JsonResult createUserResult = controller.PostRequestProcessor(JsonConvert.SerializeObject(addUserRequest));
                JObject jObject             = JObject.FromObject(createUserResult.Data);

                PostRequestProcessorResult resultJson = jObject.ToObject <PostRequestProcessorResult>();
                Assert.IsTrue(resultJson.Result, string.Format("Could not create user {0} {1} on web service side!", person.FirstName, person.LastName));
            });

            HttpPostRequest_Users request = new HttpPostRequest_Users {
                Page = 1, TotalPerPage = 10
            };
            JsonResult requestResult = controller.PostRequestProcessor(JsonConvert.SerializeObject(request));

            JObject jObjectResult = JObject.FromObject(requestResult.Data);
            PostRequestProcessorResultUsers resultUsers = jObjectResult.ToObject <PostRequestProcessorResultUsers>();

            // Sprawdzamy paging
            Assert.IsTrue(resultUsers.Result, "There was an error downloading user from web server!");
            Assert.AreEqual(resultUsers.TotalUsersCount, 50, "Wrong users number on remote web server!");
            Assert.AreEqual(resultUsers.Users.Count(), 10, "Wrong users count returned from web server!");

            // Sprawdzamy czy userzy odpowiednio sie sortują
            // Najpierw sortujemy swoich lokalnych żeby wiedziec co jest gdzie
            IEnumerable <Models.Person> sorted = used.OrderBy(x => x.FirstName);

            // Posortowalismy ascending, teraz wiemy ktory koles bedzie na koncu
            request = new HttpPostRequest_Users {
                Page = 1, TotalPerPage = 10, SortBy = "firstname", SortOrder = false
            };
            requestResult = controller.PostRequestProcessor(JsonConvert.SerializeObject(request));

            jObjectResult = JObject.FromObject(requestResult.Data);
            resultUsers   = jObjectResult.ToObject <PostRequestProcessorResultUsers>();

            Assert.IsTrue(resultUsers.Result, "There was an error downloading user from web server!");
            Assert.IsTrue(sorted.Last().PersonID == resultUsers.Users.First().PersonID, "Users are not sorted correctly!");

            // Sprawdzamy filtrowanie / szukanie
            request = new HttpPostRequest_Users {
                Page = 1, TotalPerPage = used.Count, Filter = sorted.First().FirstName
            };
            requestResult = controller.PostRequestProcessor(JsonConvert.SerializeObject(request));
            jObjectResult = JObject.FromObject(requestResult.Data);
            resultUsers   = jObjectResult.ToObject <PostRequestProcessorResultUsers>();

            Assert.IsTrue(resultUsers.Result, "There was an error downloading user from web server!");
            // Trzeba obczaic jaki powinien byc wynik
            IEnumerable <Models.Person> expectedResult =
                sorted.Where(x => x.FirstName.ToLower().StartsWith(sorted.First().FirstName.ToLower()) ||
                             x.LastName.ToLower().StartsWith(sorted.First().FirstName.ToLower()));

            Assert.AreEqual(expectedResult.Count(), resultUsers.Users.Count(), "Wrong number of filtered users!");
        }
        public void Test_Update_Users()
        {
            HomeController       controller = this.Resolve();
            List <Models.Person> persons    = this.Generate();
            List <Models.Person> used       = persons.Take(50).ToList();

            // Tworzymy 50 userów
            used.ForEach(person =>
            {
                HttpPostRequest_AddUser addUserRequest = new HttpPostRequest_AddUser
                {
                    Firstname  = person.FirstName,
                    Lastname   = person.LastName,
                    Birthdate  = person.BirthdateDisplay,
                    Height     = person.Height,
                    ClubMember = person.Member
                };

                // Tworzymy goscia na serwie
                JsonResult createUserResult = controller.PostRequestProcessor(JsonConvert.SerializeObject(addUserRequest));
                JObject jObject             = JObject.FromObject(createUserResult.Data);

                PostRequestProcessorResult resultJson = jObject.ToObject <PostRequestProcessorResult>();
                Assert.IsTrue(resultJson.Result, string.Format("Could not create user {0} {1} on web service side!", person.FirstName, person.LastName));
            });

            List <Models.Person> modified = used.Take(25).ToList();

            modified.ForEach(person =>
            {
                person.FirstName += " edited";
                person.LastName  += " edited";
                person.Height    += 10;
                person.Member     = !person.Member;
                person.BirthdateDisplay.AddDays(10);
                person.Birthdate = (person.BirthdateDisplay - new DateTime(1970, 1, 1, 0, 0, 0).ToUniversalTime()).TotalMilliseconds;

                HttpPostRequest_UpdateUser updateUserRequest = new HttpPostRequest_UpdateUser
                {
                    UserID     = person.PersonID,
                    Firstname  = person.FirstName,
                    Lastname   = person.LastName,
                    Birthdate  = person.BirthdateDisplay,
                    Height     = person.Height,
                    ClubMember = person.Member
                };

                JsonResult createUserResult = controller.PostRequestProcessor(JsonConvert.SerializeObject(updateUserRequest));
                JObject jObject             = JObject.FromObject(createUserResult.Data);

                PostRequestProcessorResult resultJson = jObject.ToObject <PostRequestProcessorResult>();
                Assert.IsTrue(resultJson.Result, string.Format("Could not modify user {0} {1} on web service side!", person.FirstName, person.LastName));
            });

            modified.ForEach(person =>
            {
                Models.Person p = used.Find(x => x.PersonID == person.PersonID);
                Assert.IsNotNull(p);

                p = person;
            });

            HttpPostRequest_Users request = new HttpPostRequest_Users {
                Page = 1, TotalPerPage = 50
            };
            JsonResult requestResult = controller.PostRequestProcessor(JsonConvert.SerializeObject(request));

            JObject jObjectResult = JObject.FromObject(requestResult.Data);
            PostRequestProcessorResultUsers resultUsers = jObjectResult.ToObject <PostRequestProcessorResultUsers>();

            List <Models.Person> check = used.Intersect(resultUsers.Users, new UsersEqualityComparer()).ToList();

            Assert.AreEqual(used.Count, check.Count, "Got different users!");
        }
        private PostRequestProcessorResultUsers Process(HttpPostRequest_Users request)
        {
            IEnumerable <Person>        users   = m_dataPrivider.GetUsers();
            Dictionary <Type, Delegate> locator = new Dictionary <Type, Delegate>
                                                              {
                                {
                                        typeof(string),
                                        new Func <string, IEnumerable <Person> >(s =>
                                                                                                     {
                        return(users.Where(x => x.FirstName.ToLower().StartsWith(s.ToLower()) ||
                                           x.LastName.ToLower().StartsWith(s.ToLower())).ToList());

                                           
                    })
                                   
                },
                                {
                                        typeof(DateTime),
                                        new Func <DateTime, IEnumerable <Person> >(s =>
                                                                                                       {
                        return(users.Where(x => x.BirthdateDisplay == s ||
                                           x.Birthdate == (s - new DateTime(1970, 1, 1, 0, 0, 0).ToUniversalTime()).TotalMilliseconds));

                                           
                    })
                                   
                },
                                {
                                        typeof(double),
                                        new Func <double, IEnumerable <Person> >(s =>
                                                                                                     {
                        return(users.Where(x => x.Height == s));

                                           
                    })
                                   
                },
                                {
                                        typeof(bool),
                                        new Func <bool, IEnumerable <Person> >(s =>
                                                                                                   {
                        return(users.Where(x => x.Member == s));

                                           
                    })
                                   
                }
                           
            };

            // Zakładam że filtrujemy po nazwach userów (name i lastname)
            List <Person> result = new List <Person>();

            string[]      fltrs   = !string.IsNullOrWhiteSpace(request.Filter) ? request.Filter.Split(null) : null;
            List <object> filters = fltrs != null ? new List <object>(fltrs) : new List <object>();

            if (filters != null && filters.Count > 0)
            {
                foreach (object obj in filters)
                {
                    List <Person> temp = (locator[obj.GetType()].DynamicInvoke(obj) as IEnumerable <Person>).ToList();
                    if (result.Count == 0)
                    {
                        // Nie mamy nic innego pozatym wiec chociaż cos - już dobrze
                        result = new List <Person>(temp);
                    }
                    else
                    {
                        // Mamy już jakieś poprzednie wyniki, sprawdzamy wspolne elementy
                        List <Person> unique = result.Intersect(temp).ToList();

                        // Jak w obydwu kolekcjach nie ma nic wspolnego, to wynik de facto jest pusty, bo user szuka cos czego nie ma.
                        result = new List <Person>(unique);
                    }
                }
            }
            else
            {
                result = new List <Person>(users);
            }

            IEnumerable <Person> sorted = null;

            if (!string.IsNullOrWhiteSpace(request.SortBy))
            {
                if (request.SortOrder)
                {
                    sorted = result.OrderBy(x => request.SortBy.ToLower().Equals("firstname") ? x.FirstName : x.LastName);
                }
                else
                {
                    sorted = result.OrderByDescending(x => request.SortBy.ToLower().Equals("firstname") ? x.FirstName : x.LastName);
                }
            }
            else
            {
                sorted = result;
            }

            // Teraz robimy taka paczke, o jaka zostaliśmy poproszeni
            IEnumerable <Person> batch = sorted.Skip((request.Page - 1) * request.TotalPerPage).Take(request.TotalPerPage);

            // Obliczamy ile zostało zakwalifikowanych ale nie oddanych
            int leftBehind = sorted.Count() - batch.Count();

            // Sprawdzamy ile potrzebujemy page'ów, czyli current batch + reszta po stronach + ewentualne niedobitki
            int reminder = 0;
            int pages    = Math.DivRem(leftBehind, request.TotalPerPage, out reminder) + 1;

            if (reminder > 0)
            {
                ++pages;
            }

            return(new PostRequestProcessorResultUsers {
                Result = true, Message = "", Users = batch, TotalUsersCount = sorted.Count()
            });
        }