/// <summary> /// Get Receivable Account /// </summary> /// <param name="db"></param> /// <returns></returns> public static List <string> GetCashAccounts(NancyBlackDatabase db) { var siteSettings = AdminModule.ReadSiteSettings(); if (siteSettings.accounting == null) { return(db.QueryAsDynamic("SELECT DISTINCT IncreaseAccount AS Name FROM AccountingEntry", new { Name = "" }) .AsEnumerable() .Select(item => (string)item.Name) .ToList()); } var accountSettings = (from dynamic item in siteSettings.accounting.accounts as JArray select new { Name = (string)item.Name, Type = (string)item.Type }).ToLookup(i => i.Name, i => i.Type); return(db.QueryAsDynamic("SELECT DISTINCT IncreaseAccount AS Name FROM AccountingEntry", new { Name = "" }) .AsEnumerable() .Select(item => (string)item.Name) .Where(s => string.IsNullOrEmpty(s) == false && accountSettings[s].FirstOrDefault() == "Cash") .ToList()); }
/// <summary> /// Get child content of given url /// </summary> /// <param name="url"></param> /// <returns></returns> public static IEnumerable<dynamic> GetChildContent(NancyBlackDatabase db, string url) { if (url.StartsWith("/") == false) { url = "/" + url; } return db.QueryAsDynamic("Content", string.Format("startswith(Url, '{0}/')", url.ToLowerInvariant()), "DisplayOrder"); }
/// <summary> /// Get Page View Count for given item in table /// </summary> /// <param name="db"></param> /// <param name="table"></param> /// <param name="id"></param> /// <returns></returns> public static long GetPageViewCount(NancyBlackDatabase db, dynamic siteSettings, string table, int id) { var key = "PageViewCount-" + table + id; object cached = MemoryCache.Default.Get(key); if (cached != null) { return((long)cached); } if (siteSettings.analytics == null) { dynamic result = db.Query (string.Format("SELECT COUNT(Id) as Hit FROM PageView WHERE ContentId = {0} AND TableName = '{1}'", id, table), new { Hit = 0 }).FirstOrDefault(); if (result == null) { result = 0; } MemoryCache.Default.Add(key, result, DateTimeOffset.Now.AddMinutes(10)); return(result); } else { var content = db.QueryAsDynamic(table, "Id eq " + id).FirstOrDefault(); if (content == null) { MemoryCache.Default.Add(key, 0, DateTimeOffset.Now.AddMinutes(10)); return(0); // wrong content } string url = content.Url; if (url == null) { MemoryCache.Default.Add(key, 0, DateTimeOffset.Now.AddMinutes(10)); return(0); // cannot get Url } if (url.Contains("/archive/")) { url = url.Replace("/archive/", "/"); } url = url.Replace('/', '-'); long pageViews = 0; lock (BaseModule.GetLockObject("PageViewSummary-" + url)) // ensure only one thread is working on calculation { // if multiple threads is locked - they will arrive here when lock is released // so check the cache again cached = MemoryCache.Default.Get(key); if (cached != null) { return((int)cached); } CloudTable summaryTable = ContentModule.GetSummaryTable(siteSettings); var queryString = TableQuery.CombineFilters( TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, url), TableOperators.And, TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, url)); var query = new TableQuery <PageViewSummary>(); var result = summaryTable.ExecuteQuery <PageViewSummary>(query.Where(queryString)).FirstOrDefault(); if (result == null) { result = new PageViewSummary(); result.PageViews = 0; result.Path = url; result.PrepareForAzure(); result.Timestamp = DateTimeOffset.MinValue; summaryTable.Execute(TableOperation.InsertOrReplace(result)); } // find all pageview since the pageview summary timestamp var pvQueryString = TableQuery.CombineFilters( TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, url), TableOperators.And, TableQuery.GenerateFilterConditionForDate("Timestamp", QueryComparisons.GreaterThanOrEqual, result.Timestamp)); CloudTable rawTable = ContentModule.GetPageViewTable(siteSettings); var pageviewQuery = new TableQuery <PageView>(); var count = rawTable.ExecuteQuery <PageView>(pageviewQuery.Where(pvQueryString)).Count(); result.PageViews = result.PageViews + count; summaryTable.Execute(TableOperation.InsertOrReplace(result)); pageViews = result.PageViews; MemoryCache.Default.Add(key, pageViews, DateTimeOffset.Now.AddMinutes(10)); return(pageViews); } } }