public async Task AddSubreddit(Models.Subreddit newSub, string modname, string ip) { newSub.Active = true; newSub.Settings = new Models.SubredditSettings(); newSub.Settings.AccessMask = 64; var cache = MemoryCache.Default; var ucacheitem = cache.GetCacheItem(modname); var icacheitem = cache.GetCacheItem(ip); var ucache = ucacheitem == null ? new CacheObject(modname) : ucacheitem.Value as CacheObject; var icache = icacheitem == null ? new CacheObject(ip) : icacheitem.Value as CacheObject; int ureqs = ucache.Value; int ireqs = icache.Value; if (Math.Max(ureqs, ireqs) > 5) { throw new Exception("You are doing that too much! Limited to created 5 subreddits per 24 hours, sorry!"); } if ((await subDAL.GetActiveSubs()).Select(s => s.SubName.ToLower()).Contains(newSub.SubName.ToLower())) { throw new Exception("Subreddit already exists!"); } try { DAL.NoteTypesDAL ntDAL = new DAL.NoteTypesDAL(); //loads default note types, currently same types as Toolbox newSub.Settings.NoteTypes = Models.SubredditSettings.DefaultNoteTypes(newSub.SubName); await subDAL.AddSubreddit(newSub); await ntDAL.AddMultipleNoteTypes(newSub.Settings.NoteTypes, modname); ucache.Value += 1; icache.Value += 1; cache.Set(new CacheItem(ucache.Key, ucache), new CacheItemPolicy() { AbsoluteExpiration = ucache.ExpirationDate }); cache.Set(new CacheItem(icache.Key, icache), new CacheItemPolicy() { AbsoluteExpiration = icache.ExpirationDate }); } catch { throw; } }
public async Task <IEnumerable <NoteType> > AddMultipleNoteTypes(IEnumerable <NoteType> values, string name) { foreach (Models.NoteType nt in values) { if (!await ValidateNoteType(nt)) { throw new HttpResponseException(System.Net.HttpStatusCode.BadRequest); } } var ret = await noteTypesDAL.AddMultipleNoteTypes(values, name); Signalr.SnooNoteUpdates.Instance.RefreshNoteTypes(values.Select(nt => nt.SubName).Distinct()); return(ret); }
public async Task AddSubreddit(Models.Subreddit newSub, string modname, string ip) { newSub.Active = true; newSub.Settings = new Models.SubredditSettings(); newSub.Settings.AccessMask = 64; var cache = MemoryCache.Default; var ucacheitem = cache.GetCacheItem( modname ); var icacheitem = cache.GetCacheItem( ip ); var ucache = ucacheitem == null ? new CacheObject( modname ) : ucacheitem.Value as CacheObject; var icache = icacheitem == null ? new CacheObject( ip ) : icacheitem.Value as CacheObject; int ureqs = ucache.Value; int ireqs = icache.Value; if ( Math.Max( ureqs, ireqs ) > 5 ) { throw new Exception( "You are doing that too much! Limited to created 5 subreddits per 24 hours, sorry!" ); } if ( (await subDAL.GetActiveSubs()).Select( s => s.SubName.ToLower() ).Contains( newSub.SubName.ToLower() ) ) { throw new Exception( "Subreddit already exists!" ); } try { DAL.NoteTypesDAL ntDAL = new DAL.NoteTypesDAL(); //loads default note types, currently same types as Toolbox newSub.Settings.NoteTypes = Models.SubredditSettings.DefaultNoteTypes( newSub.SubName ); await subDAL.AddSubreddit( newSub ); await ntDAL.AddMultipleNoteTypes( newSub.Settings.NoteTypes, modname ); ucache.Value += 1; icache.Value += 1; cache.Set( new CacheItem( ucache.Key, ucache ), new CacheItemPolicy() { AbsoluteExpiration = ucache.ExpirationDate } ); cache.Set( new CacheItem( icache.Key, icache ), new CacheItemPolicy() { AbsoluteExpiration = icache.ExpirationDate } ); } catch { throw; } }