All pastes #2123322 Raw Edit

Something

public text v1 · immutable
#2123322 ·published 2012-03-01 20:12 UTC
rendered paste body
namespace Dumball.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        private static IEnumerable<Dumball.Models.FundRaiser> fundraisers = new List<Dumball.Models.FundRaiser>();
        private static DateTime justGivingCacheExpiry = DateTime.Now;
        public static IEnumerable<Dumball.Models.FundRaiser> FundRaisers
        {
            get
            {
                try
                {
                    if (DateTime.Now.CompareTo(justGivingCacheExpiry) > 0)
                    {
                        fundraisers = new List<Dumball.Models.FundRaiser>();
                        int currentPage = 1;
                        int totalPages = 0;
                        do
                        {
                            XDocument fundraiserPages = XDocument.Load(string.Format("https://api.justgiving.com/59b1dd3d/v1/event/718649/pages?page={0}", currentPage));

                            totalPages = int.Parse(fundraiserPages.Root.Element("totalPages").Value);
                            currentPage++;

                            fundraisers = fundraisers.Concat(
                                from justGivingPage in fundraiserPages.Descendants("fundraisingPage")
                                where justGivingPage.Element("pageStatus").Value == "Active"
                                select new Dumball.Models.FundRaiser(
                                    justGivingPage.Element("pageTitle").Value,
                                    Decimal.Parse(justGivingPage.Element("raisedAmount").Value),
                                    int.Parse(justGivingPage.Element("pageId").Value),
                                    justGivingPage.Element("pageShortName").Value));
                        } while (currentPage <= totalPages);

                        justGivingCacheExpiry = DateTime.Now.AddMinutes(30);
                    }

                    return fundraisers;
                }
                catch (Exception)
                {
                    return null;
                }
            }
        }

        public ActionResult Totaliser()
        {
            return View(FundRaisers.OrderByDescending(i => i.Amount).AsEnumerable() ?? new List<Dumball.Models.FundRaiser>());
        }
    }
}