public async Task <T> SaveReport <T>(T report) where T : ReportBase { //Get the dictionaries of the daily reports and the individual reports var reports = await this.StateManager.GetOrAddAsync <IReliableDictionary <long, T> >(CollectionNames.GetReportDictionaryName <T>()); var dailies = await this.StateManager.GetOrAddAsync <IReliableDictionary <DateTime, List <long> > >(CollectionNames.GetDailyReportDictionaryName <T>()); //The key is the hash of the date and poster var key = BuildReportKey(report); using (var tx = this.StateManager.CreateTransaction()) { //The behaviour expected is that if the report already exists we return null var exists = await reports.ContainsKeyAsync(tx, key); if (exists) { return(null); //should return more info } //The report does not exist yet. First, add it to the collection of daily reports. The update function is to append the new id to the list of ids. The update //function will usually run, since we usually already have a list of ids for each date await dailies.AddOrUpdateAsync(tx, report.Date, new List <long>() { BuildReportKey(report) }, (k, v) => { v.Add(BuildReportKey(report)); return(v); }); //Now that we've added the mapped id to the daily reports, let's add the actual report to the de-facto collection var success = await reports.AddOrUpdateAsync(tx, key, report, (k, v) => report); await tx.CommitAsync(); return(success); } }
public async Task <IEnumerable <T> > GetDailyReports <T>(DateTime day) where T : ReportBase { day = day.Date; List <T> rtn = new List <T>(); var dailyreports = await this.StateManager.GetOrAddAsync <IReliableDictionary <DateTime, List <long> > >(CollectionNames.GetDailyReportDictionaryName <T>()); using (var tx = this.StateManager.CreateTransaction()) { //get a list of the report keys for the day var reportIdsConditional = await dailyreports.TryGetValueAsync(tx, day); //Send back a count of 0, indicating nothing was retrieved (duh) if (!reportIdsConditional.HasValue) { return(rtn); } var reportIds = reportIdsConditional.Value; var reports = await this.StateManager.GetOrAddAsync <IReliableDictionary <long, T> >(CollectionNames.GetReportDictionaryName <T>()); using (var txx = this.StateManager.CreateTransaction()) { foreach (var id in reportIds) { var report = await reports.TryGetValueAsync(txx, id); if (report.HasValue) { rtn.Add(report.Value); } } } } return(rtn); }
public async Task <T> GetReport <T>(long id) where T : ReportBase { var reports = await this.StateManager.GetOrAddAsync <IReliableDictionary <long, T> >(CollectionNames.GetReportDictionaryName <T>()); using (var tx = this.StateManager.CreateTransaction()) { var report = await reports.TryGetValueAsync(tx, id); if (report.HasValue) { return(report.Value); } } return(null); }