public async Task <ActionResult <List <CampaignDTO> > > GetAllCampaignsAsync() { var campaignList = new List <Campaign>(); if (!_settings.CachingEnabled) { campaignList = await _context.Campaigns.ToListAsync(); } else { var options = new CachingOptions { StoreAs = StoreAs.SeperateEntities }; options.SetAbsoluteExpiration(DateTime.Now.AddMinutes(_settings.MarketingCacheExpirationTimeInMinutes)); campaignList = (await _context.Campaigns.FromCacheAsync(options)).ToList(); } if (campaignList is null) { return(Ok()); } return(MapCampaignModelListToDtoList(campaignList)); }
public async Task <ActionResult <CatalogItem> > ItemByIdAsync(int id) { if (id <= 0) { return(BadRequest()); } CatalogItem item = null; if (!_settings.EFCoreCachingEnabled) { item = await _catalogContext.CatalogItems.SingleOrDefaultAsync(ci => ci.Id == id); } else { var options = new CachingOptions { }; options.SetAbsoluteExpiration(DateTime.Now.AddMinutes(_settings.NCacheAbsoluteExpirationTime)); item = await _catalogContext.CatalogItems.DeferredSingleOrDefault(ci => ci.Id == id).FromCacheAsync(options); } var baseUri = _settings.PicBaseUrl; item.FillProductUrl(baseUri); if (item != null) { return(item); } return(NotFound()); }
public ActionResult <UserLocationRuleDTO> GetLocationByCampaignAndLocationRuleId(int campaignId, int userLocationRuleId) { if (campaignId < 1 || userLocationRuleId < 1) { return(BadRequest()); } UserLocationRule location = null; if (!_settings.CachingEnabled) { location = _context.Rules .OfType <UserLocationRule>() .SingleOrDefault(c => c.CampaignId == campaignId && c.Id == userLocationRuleId); } else { var options = new CachingOptions(); options.SetAbsoluteExpiration(DateTime.Now.AddMinutes(_settings.MarketingCacheExpirationTimeInMinutes)); location = _context.Rules .OfType <UserLocationRule>() .DeferredSingleOrDefault(c => c.CampaignId == campaignId && c.Id == userLocationRuleId) .FromCache(options); } if (location is null) { return(NotFound()); } return(MapUserLocationRuleModelToDto(location)); }
public async Task <ActionResult> CreateLocationAsync(int campaignId, [FromBody] UserLocationRuleDTO locationRuleDto) { if (campaignId < 1 || locationRuleDto is null) { return(BadRequest()); } var locationRule = MapUserLocationRuleDtoToModel(locationRuleDto); locationRule.CampaignId = campaignId; await _context.Rules.AddAsync(locationRule); await _context.SaveChangesAsync(); if (_settings.CachingEnabled) { var cache = _context.GetCache(); var options = new CachingOptions(); options.SetAbsoluteExpiration(DateTime.Now.AddMinutes(_settings.MarketingCacheExpirationTimeInMinutes)); await Task.Run(() => cache.Insert(locationRule, out string cacheKey, options)); } return(CreatedAtAction(nameof(GetLocationByCampaignAndLocationRuleId), new { campaignId = campaignId, userLocationRuleId = locationRule.Id }, null)); }
public async Task <ActionResult <PaginatedItemsViewModel <CatalogItem> > > ItemsByTypeIdAndBrandIdAsync(int catalogTypeId, int?catalogBrandId, [FromQuery] int pageSize = 10, [FromQuery] int pageIndex = 0) { var root = (IQueryable <CatalogItem>)_catalogContext.CatalogItems; root = root.Where(ci => ci.CatalogTypeId == catalogTypeId); if (catalogBrandId.HasValue) { root = root.Where(ci => ci.CatalogBrandId == catalogBrandId); } long totalItems = -1L; var itemsOnPage = new List <CatalogItem>(); if (!_settings.EFCoreCachingEnabled) { totalItems = await root .LongCountAsync(); itemsOnPage = await root .Skip(pageSize *pageIndex) .Take(pageSize) .ToListAsync(); } else { var catalogBrandIDString = ""; if (catalogBrandId.HasValue) { catalogBrandIDString = $"{catalogBrandId }"; } var options = new CachingOptions { StoreAs = StoreAs.SeperateEntities }; options.SetAbsoluteExpiration(DateTime.Now.AddMinutes(_settings.NCacheAbsoluteExpirationTime)); totalItems = await root .DeferredLongCount() .FromCacheAsync(options); options.StoreAs = StoreAs.SeperateEntities; itemsOnPage = (await root .Skip(pageSize * pageIndex) .Take(pageSize) .FromCacheAsync(options)).ToList(); } itemsOnPage = ChangeUriPlaceholder(itemsOnPage); return(new PaginatedItemsViewModel <CatalogItem>(pageIndex, pageSize, totalItems, itemsOnPage)); }
public async Task <ActionResult> UpdateCampaignAsync(int id, [FromBody] CampaignDTO campaignDto) { if (id < 1 || campaignDto is null) { return(BadRequest()); } Campaign campaignToUpdate = null; if (!_settings.CachingEnabled) { campaignToUpdate = await _context.Campaigns.FindAsync(id); } else { var options = new CachingOptions { StoreAs = StoreAs.SeperateEntities }; options.SetAbsoluteExpiration(DateTime.Now.AddMinutes(_settings.MarketingCacheExpirationTimeInMinutes)); campaignToUpdate = await _context.Campaigns.DeferredSingleOrDefault(c => c.Id == id).FromCacheAsync(options); } if (campaignToUpdate is null) { return(NotFound()); } campaignToUpdate.Name = campaignDto.Name; campaignToUpdate.Description = campaignDto.Description; campaignToUpdate.From = campaignDto.From; campaignToUpdate.To = campaignDto.To; campaignToUpdate.PictureUri = campaignDto.PictureUri; await _context.SaveChangesAsync(); if (_settings.CachingEnabled) { var cache = _context.GetCache(); var options = new CachingOptions(); options.SetAbsoluteExpiration(DateTime.Now.AddMinutes(_settings.MarketingCacheExpirationTimeInMinutes)); await Task.Run(() => cache.Insert(campaignToUpdate, out string cacheKey, options)); } return(CreatedAtAction(nameof(GetCampaignByIdAsync), new { id = campaignToUpdate.Id }, null)); }
public async Task <ActionResult <PaginatedItemsViewModel <CatalogItem> > > ItemsWithNameAsync(string name, [FromQuery] int pageSize = 10, [FromQuery] int pageIndex = 0) { long totalItems = -1L; List <CatalogItem> itemsOnPage = new List <CatalogItem>(); if (!_settings.EFCoreCachingEnabled) { totalItems = await _catalogContext.CatalogItems .Where(c => c.Name.StartsWith(name)) .LongCountAsync(); itemsOnPage = await _catalogContext.CatalogItems .Where(c => c.Name.StartsWith(name)) .Skip(pageSize * pageIndex) .Take(pageSize) .ToListAsync(); } else { var options = new CachingOptions { }; options.SetAbsoluteExpiration(DateTime.Now.AddMinutes(_settings.NCacheAbsoluteExpirationTime)); totalItems = await _catalogContext.CatalogItems .Where(c => c.Name.StartsWith(name)) .DeferredLongCount() .FromCacheAsync(options); // options.QueryIdentifier = $"ItemsWithNameAsync-{name}-{pageSize}-{pageIndex}"; options.StoreAs = StoreAs.SeperateEntities; itemsOnPage = (await _catalogContext.CatalogItems .Where(c => c.Name.StartsWith(name)) .Skip(pageSize * pageIndex) .Take(pageSize) .FromCacheAsync(options)) .ToList(); } itemsOnPage = ChangeUriPlaceholder(itemsOnPage); return(new PaginatedItemsViewModel <CatalogItem>(pageIndex, pageSize, totalItems, itemsOnPage)); }
// GET: /<controller>/ public async Task <ActionResult> GetImageAsync(int catalogItemId) { if (catalogItemId <= 0) { return(BadRequest()); } CatalogItem item = null; if (!_settings.EFCoreCachingEnabled) { item = await _catalogContext.CatalogItems .SingleOrDefaultAsync(ci => ci.Id == catalogItemId); } else { var options = new CachingOptions { // QueryIdentifier = $"ItemByIdAsync-{catalogItemId}" }; options.SetAbsoluteExpiration(DateTime.Now.AddMinutes(_settings.NCacheAbsoluteExpirationTime)); item = await _catalogContext.CatalogItems.DeferredSingleOrDefault(ci => ci.Id == catalogItemId).FromCacheAsync(options); } if (item != null) { var webRoot = _env.WebRootPath; var path = Path.Combine(webRoot, item.PictureFileName); string imageFileExtension = Path.GetExtension(item.PictureFileName); string mimetype = GetImageMimeTypeFromImageFileExtension(imageFileExtension); var buffer = System.IO.File.ReadAllBytes(path); return(File(buffer, mimetype)); } return(NotFound()); }
public async Task <ActionResult> DeleteLocationByIdAsync(int campaignId, int userLocationRuleId) { if (campaignId < 1 || userLocationRuleId < 1) { return(BadRequest()); } UserLocationRule locationToDelete = null; if (!_settings.CachingEnabled) { locationToDelete = _context.Rules .OfType <UserLocationRule>() .SingleOrDefault(c => c.CampaignId == campaignId && c.Id == userLocationRuleId); } else { var options = new CachingOptions(); options.SetAbsoluteExpiration(DateTime.Now.AddMinutes(_settings.MarketingCacheExpirationTimeInMinutes)); locationToDelete = await _context.Rules .OfType <UserLocationRule>() .DeferredSingleOrDefault(c => c.CampaignId == campaignId && c.Id == userLocationRuleId) .FromCacheAsync(options); } if (locationToDelete is null) { return(NotFound()); } _context.Rules.Remove(locationToDelete); await _context.SaveChangesAsync(); if (_settings.CachingEnabled) { var cache = _context.GetCache(); await Task.Run(() => cache.Remove(locationToDelete)); } return(NoContent()); }
public async Task <ActionResult <List <CatalogBrand> > > CatalogBrandsAsync() { if (!_settings.EFCoreCachingEnabled) { return(await _catalogContext.CatalogBrands.ToListAsync()); } else { var options = new CachingOptions { StoreAs = StoreAs.SeperateEntities }; options.SetAbsoluteExpiration(DateTime.Now.AddMinutes(_settings.NCacheAbsoluteExpirationTime)); var result = await _catalogContext.CatalogBrands.FromCacheAsync(options); return(result.ToList()); } }
public ActionResult <List <UserLocationRuleDTO> > GetAllLocationsByCampaignId(int campaignId) { if (campaignId < 1) { return(BadRequest()); } List <UserLocationRule> locationList = null; if (!_settings.CachingEnabled) { locationList = _context.Rules .OfType <UserLocationRule>() .Where(c => c.CampaignId == campaignId) .ToList(); } else { var options = new CachingOptions { StoreAs = StoreAs.SeperateEntities }; options.SetAbsoluteExpiration(DateTime.Now.AddMinutes(_settings.MarketingCacheExpirationTimeInMinutes)); locationList = _context.Rules .OfType <UserLocationRule>() .Where(c => c.CampaignId == campaignId) .FromCache(options) .ToList(); } if (locationList is null) { return(Ok()); } return(MapUserLocationRuleModelListToDtoList(locationList)); }
public async Task <ActionResult> CreateCampaignAsync([FromBody] CampaignDTO campaignDto) { if (campaignDto is null) { return(BadRequest()); } var campaign = MapCampaignDtoToModel(campaignDto); await _context.Campaigns.AddAsync(campaign); await _context.SaveChangesAsync(); if (_settings.CachingEnabled) { var cache = _context.GetCache(); var cachingOptions = new CachingOptions(); cachingOptions.SetAbsoluteExpiration(DateTime.Now.AddMinutes(_settings.MarketingCacheExpirationTimeInMinutes)); await Task.Run(() => cache.Insert(campaign, out string cacheKey, cachingOptions)); } return(CreatedAtAction(nameof(GetCampaignByIdAsync), new { id = campaign.Id }, null)); }
private async Task <List <CatalogItem> > GetItemsByIdsAsync(string ids) { var numIds = ids.Split(',').Select(id => (Ok: int.TryParse(id, out int x), Value: x)); if (!numIds.All(nid => nid.Ok)) { return(new List <CatalogItem>()); } var idsToSelect = numIds .Select(id => id.Value); var items = new List <CatalogItem>(); if (_settings.EFCoreCachingEnabled) { var options = new CachingOptions { // QueryIdentifier = $"GetItemsByIdsAsync-{ids}", StoreAs = StoreAs.SeperateEntities }; options.SetAbsoluteExpiration(DateTime.Now.AddMinutes(_settings.NCacheAbsoluteExpirationTime)); items = (await _catalogContext.CatalogItems.Where(ci => idsToSelect.Contains(ci.Id)).FromCacheAsync(options)).ToList(); } else { items = await _catalogContext.CatalogItems.Where(ci => idsToSelect.Contains(ci.Id)).ToListAsync(); } items = ChangeUriPlaceholder(items); return(items); }
public async Task <ActionResult <CampaignDTO> > GetCampaignByIdAsync(int id) { Campaign campaign = null; if (!_settings.CachingEnabled) { campaign = await _context.Campaigns.SingleOrDefaultAsync(c => c.Id == id); } else { var options = new CachingOptions(); options.SetAbsoluteExpiration(DateTime.Now.AddMinutes(_settings.MarketingCacheExpirationTimeInMinutes)); campaign = await _context.Campaigns.DeferredSingleOrDefault(c => c.Id == id).FromCacheAsync(options); } if (campaign is null) { return(NotFound()); } return(MapCampaignModelToDto(campaign)); }
public async Task <IActionResult> ItemsAsync([FromQuery] int pageSize = 10, [FromQuery] int pageIndex = 0, string ids = null) { if (!string.IsNullOrEmpty(ids)) { var items = await GetItemsByIdsAsync(ids); if (!items.Any()) { return(BadRequest("ids value invalid. Must be comma-separated list of numbers")); } return(Ok(items)); } var totalItems = -1L; var itemsOnPage = new List <CatalogItem>(); if (!_settings.EFCoreCachingEnabled) { totalItems = await _catalogContext.CatalogItems .LongCountAsync(); itemsOnPage = await _catalogContext.CatalogItems .OrderBy(c => c.Name) .Skip(pageSize * pageIndex) .Take(pageSize) .ToListAsync(); } else { var options = new CachingOptions { // QueryIdentifier = $"ItemsAsyncCount", // StoreAs = StoreAs.SeperateEntities }; options.SetAbsoluteExpiration(DateTime.Now.AddMinutes(_settings.NCacheAbsoluteExpirationTime)); totalItems = await _catalogContext.CatalogItems.DeferredLongCount().FromCacheAsync(options); // options.QueryIdentifier = $"ItemsAsync-{pageSize}-{pageIndex}"; options.StoreAs = StoreAs.SeperateEntities; itemsOnPage = (await _catalogContext.CatalogItems .OrderBy(c => c.Name) .Skip(pageSize * pageIndex) .Take(pageSize) .FromCacheAsync(options)) .ToList(); } /* The "awesome" fix for testing Devspaces */ /* * foreach (var pr in itemsOnPage) { * pr.Name = "Awesome " + pr.Name; * } * */ itemsOnPage = ChangeUriPlaceholder(itemsOnPage); var model = new PaginatedItemsViewModel <CatalogItem>(pageIndex, pageSize, totalItems, itemsOnPage); return(Ok(model)); }