Example #1
0
        public MatchModule(IRepository<Match> matchRepository, IRepository<Country> countryRepository)
            : base("/matches")
        {
            Get["/"] = parameters =>
                           {
                               var matches = matchRepository.FindAll();

                               var resource = matches.ToArray().Select(
                                   m => new MatchResource
                                            {
                                                Id = m.Id,
                                                Date = m.Date,
                                                Team1CountryId = m.Team1.Country.Id,
                                                Team2CountryId = m.Team2.Country.Id
                                            });

                               return Response.AsJson(resource);
                           };

            Post["/"] = parameters =>
                {
                    var resource = this.Bind<MatchResource>();

                    var country1 = countryRepository.GetById(resource.Team1CountryId);
                    if (country1 == null)
                        return HttpStatusCode.BadRequest;

                    var country2 = countryRepository.GetById(resource.Team2CountryId);
                    if (country2 == null)
                        return HttpStatusCode.BadRequest;

                    var match = Match.Create(
                        resource.Date,
                        country1,
                        country2);

                    matchRepository.Add(match);

                    return Response.AsRedirect("/matches/" + match.Id);
                };

            Get["/{id}"] = parameters =>
                {
                    var match = matchRepository.GetById((int)parameters.Id);
                    if (match == null)
                        return HttpStatusCode.NotFound;

                    return Response.AsJson(new
                        {
                            Date = match.Date,
                            Team1 = match.Team1,
                            Team2 = match.Team2
                        });
                };
        }
        public void FindAll_Should_Return_All_Items_Which_Satisfy_Composite_Specification(IRepository<Contact, string> repository)
        {
            for (int i = 1; i <= 3; i++)
            {
                var contact = new Contact { Name = "Test User " + i };
                repository.Add(contact);
            }

            var result = repository.FindAll(new Specification<Contact>(p => p.Name == "Test User 1").OrElse(new Specification<Contact>(p => p.Name == "Test User 2")));
            result.Count().ShouldEqual(2);
        }
        public void FindAll_Should_Return_All_Items_Which_Satisfy_Composite_Specification_With_Paging_And_Sort_Descending(IRepository<Contact, string> repository)
        {
            const int resultingPage = 2;
            const int pageSize = 2;
            var queryOptions = new PagingOptions<Contact>(resultingPage, pageSize, "Name", true);

            for (int i = 1; i <= 10; i++)
            {
                var contact = new Contact { Name = "Test User " + i };
                repository.Add(contact);
            }

            IEnumerable<Contact> result = repository
                .FindAll(new Specification<Contact>(p => p.Name == "Test User 1")
                                .OrElse(new Specification<Contact>(p => p.Name == "Test User 5")
                                        .OrElse(new Specification<Contact>(p => p.Name == "Test User 8"))),
                            queryOptions);

            result.Count().ShouldEqual(1);
            queryOptions.TotalItems.ShouldEqual(3);
            result.First().Name.ShouldEqual("Test User 1");
        }
        public void GetFirstEmployee_noParams_employee()
        {
            Employee employee;

            using (var dbContext = new DatabaseContext(testUser, testPass))
            {
                employeeRepository = new EmployeesRepository(dbContext);
                employee = employeeRepository.FindAll().FirstOrDefault();
            }

            Assert.IsNotNull(employee);
        }
        public void FindAll_Should_Return_All_Items_Which_Satisfy_Specification_With_Paging_MagicString(IRepository<Contact, string> repository)
        {
            const int resultingPage = 2;
            const int pageSize = 2;
            const int totalItems = 10;

            var queryOptions = new PagingOptions<Contact>(resultingPage, pageSize, "Name");

            for (int i = 1; i <= totalItems; i++)
            {
                var contact = new Contact { Name = "Test User " + i, ContactTypeId = i };
                repository.Add(contact);
            }

            // this fails for RavenDb because the ContactId is an int but is being used as the key, so the check on ContactId <= 5 is doing a string comparison and including ContactId = 10 as well
            //  need to look into why this happens and how to get around it
            var result = repository.FindAll(new Specification<Contact>(p => p.ContactTypeId <= totalItems / 2), queryOptions);
            result.Count().ShouldEqual(pageSize);
            queryOptions.TotalItems.ShouldEqual(totalItems / 2);
            result.First().Name.ShouldEqual("Test User 3");
        }
        public void FindAll_Should_Return_All_Items_Which_Satisfy_Specification(IRepository<Contact, string> repository)
        {
            for (int i = 1; i <= 3; i++)
            {
                var contact = new Contact {Name = "Test User " + i};
                repository.Add(contact);
            }

            var result = repository.FindAll(new Specification<Contact>(p => p.Name == "Test User 1")); // Note: Raven doesn't like p.Name.Equals("...");
            result.Count().ShouldEqual(1);
        }
 public StackLoader(IRepository<Stack> stackRepository, IRepository<Instance> instanceRepository)
 {
     _stackRepository = stackRepository;
     _instanceRepository = instanceRepository;
     _repositoryInstances = new Lazy<List<Instance>>(() => _instanceRepository.FindAll().ToList());
 }
        public JsonResult GetPagedJsonBonuses(int take, int skip)
        {
            PagedResponse<BonusAggregate> bonuses;

            var filteredRequest = new FilteredRequest(Request.Params);

            using (var dbContext = new DatabaseContext())
            {
                BonusesRepository = new BonusesRepository(dbContext);
                bonuses = BonusesRepository.FindAll(skip,
                                                    take,
                                                    filteredRequest.SortingField,
                                                    filteredRequest.Direction,
                                                    filteredRequest.FilterField,
                                                    filteredRequest.FilterPattern);
            }

            return Json(bonuses, JsonRequestBehavior.AllowGet);
        }
        public void Save_BonusesList_2BonusesUpdated()
        {
            IList<BonusAggregate> bonusesToUpdate;
            IList<BonusAggregate> updatedBonuses = new List<BonusAggregate>();
            var bonusesIds = new int[2];

            string newComment = "comment on " + DateTime.Now;
            using (var dbContext = new DatabaseContext(TestUser, TestPass))
            {
                bonusRepository = new BonusesRepository(dbContext);
                bonusesToUpdate = bonusRepository.FindAll().Take(2).ToList();
                bonusesToUpdate[0].Comment = newComment;
                bonusesToUpdate[1].Comment = newComment;

                bonusesIds[0] = bonusesToUpdate[0].BonusId;
                bonusesIds[1] = bonusesToUpdate[1].BonusId;
                bonusRepository.Save(bonusesToUpdate);
            }

            using (var dbContext = new DatabaseContext(TestUser, TestPass))
            {
                bonusRepository = new BonusesRepository(dbContext);
                updatedBonuses.Add(bonusRepository.GetById(bonusesIds[0]));
                updatedBonuses.Add(bonusRepository.GetById(bonusesIds[1]));
            }

            Assert.AreEqual(updatedBonuses[0].Comment, newComment);
            Assert.AreEqual(updatedBonuses[1].Comment, newComment);
        }
        public void Save_BonusesList_2BonusesAdded()
        {
            int numberOfItemsBeforSave;

            int numberOfCurrentBonuses;
            using (var dbContext = new DatabaseContext(TestUser, TestPass))
            {
                bonusFactory = new BonusFactory(new EmployeesRepository(dbContext));
                var bonusesList = new List<BonusAggregate>
                                        {
                                          bonusFactory.Create(GetEmployeeById(4), DateTime.Now, 100),
                                          bonusFactory.Create(GetEmployeeById(5), DateTime.Now, 90)
                                        };

                bonusRepository = new BonusesRepository(dbContext);
                numberOfItemsBeforSave = bonusRepository.FindAll().Count();
                bonusRepository.Save(bonusesList);
                numberOfCurrentBonuses = bonusRepository.FindAll().Count();
            }

            Assert.AreEqual(numberOfCurrentBonuses - 2, numberOfItemsBeforSave);
        }
        public void FindAll_noParams_SomeBonuses()
        {
            IList<BonusAggregate> bonuses;

            using (var dbContext = new DatabaseContext(TestUser, TestPass))
            {
                bonusRepository = new BonusesRepository(dbContext);
                bonuses = bonusRepository.FindAll();
            }

            Assert.IsNotNull(bonuses);
            Assert.AreNotEqual(0, bonuses.Count);
        }
        public void FindAll_NoParams_EmployeeExists()
        {
            BonusAggregate bonusAggregate;

            using (var dbContext = new DatabaseContext(TestUser, TestPass))
            {
                bonusRepository = new BonusesRepository(dbContext);
                bonusAggregate = bonusRepository.FindAll().First();
            }

            Assert.IsNotNull(bonusAggregate.Employee);
        }
        public void FindAllWithPaging_Skip2Take3_CorrectTotalCount()
        {
            IList<BonusAggregate> notSkipedBonuses;
            PagedResponse<BonusAggregate> skipedBonuses;

            using (var dbContext = new DatabaseContext(TestUser, TestPass))
            {
                bonusRepository = new BonusesRepository(dbContext);
                notSkipedBonuses = bonusRepository.FindAll();
                skipedBonuses = bonusRepository.FindAllWithPaging(2, 3);
            }
            Assert.AreEqual(skipedBonuses.TotalCount, notSkipedBonuses.Count);
        }
Example #14
0
        // TODO: let's do content negotiation ourselves unless there is a nice
        // way to do it in Nancy like there seems to be for most things
        public CountryModule(IRepository<Domain.Country> countryRepository, CountryFactory countryFactory)
            : base("/countries")
        {
            Get["/"] = parameters =>
                {
                    var countryResources = countryRepository.FindAll()
                        .Select(c => new Country {id = c.Id.ToString(), Name = c.Name});

                    var resource = countryResources.ToArray();

                    return Response.AsJson(resource);
                };

            Get["/{id}"] = parameters =>
                {
                    var country = countryRepository.GetById((int)parameters.id);
                    if (country == null)
                        return HttpStatusCode.NotFound;

                    var resource = new Country {id = country.Id.ToString(), Name = country.Name};

                    return Response.AsJson(resource);
                };

            Post["/"] = parameters =>
                {
                    var countryResource = this.Bind<Country>(blacklistedProperties:"id");

                    var validationResult = this.Validate(countryResource);
                    if (! validationResult.IsValid)
                    {
                        var response = Response.AsJson(validationResult.Errors);
                        response.StatusCode = HttpStatusCode.BadRequest;
                        return response;
                    }

                    var country = countryFactory.CreateCountry(countryResource.Name);
                    countryRepository.Add(country);

                    return Response.AsRedirect("/countries/"+country.Id);
                };

            Put["/{id}"] = parameters =>
                {
                    var country = countryRepository.GetById((int) parameters.id);
                    if (country == null)
                        return HttpStatusCode.NotFound; // this correct for a put? should probably be some error

                    // we don't actually support updates to countries!
                    //country.Name = parameters.Name;

                    return Response.AsRedirect("/countries/" + country.Id);
                };

            Delete["/{id}"] = parameters =>
                {
                    var country = countryRepository.GetById((int)parameters.id);
                    if (country == null)
                        return HttpStatusCode.NotFound; // this correct for a put? should probably be some error

                    countryRepository.Remove(country);

                    return HttpStatusCode.OK;
                };
        }