예제 #1
0
        public static async Task <int> Count(ApplicationDbContext context, MailTemplateEntity entity)
        {
            if (!entity.iscache || Configs.GeneralSettings.cache_duration == 0 || entity.pagenumber > Configs.GeneralSettings.max_cache_pages)
            {
                return(await CountRecords(context, entity));
            }
            else
            {
                string key     = GenerateKey("cnt_mailtemplate", entity);
                int    records = 0;
                if (!SiteConfig.Cache.TryGetValue(key, out records))
                {
                    records = await CountRecords(context, entity);

                    var cacheEntryOptions = new MemoryCacheEntryOptions()
                                            // Keep in cache for this time, reset time if accessed.
                                            .SetSlidingExpiration(TimeSpan.FromSeconds(3600));

                    // Save data in cache.
                    SiteConfig.Cache.Set(key, records, cacheEntryOptions);
                }
                else
                {
                    records = (int)SiteConfig.Cache.Get(key);
                }
                return(records);
            }
        }
        /// <inheritdoc/>
        public async Task <MailTemplateEntity> GetMailTemplate(string applicationName, string templateName)
        {
            var traceProps = new Dictionary <string, string>();

            traceProps[AIConstants.Application]      = applicationName;
            traceProps[AIConstants.MailTemplateName] = templateName;

            this.logger.TraceInformation($"Started {nameof(this.GetMailTemplate)} method of {nameof(MailTemplateRepository)}.", traceProps);

            string blobName    = this.GetBlobName(applicationName, templateName);
            var    contentTask = this.cloudStorageClient.DownloadBlobAsync(blobName).ConfigureAwait(false);

            TableOperation retrieveOperation = TableOperation.Retrieve <MailTemplateEntity>(applicationName, templateName);

            TableResult retrievedResult = await this.cloudTable.ExecuteAsync(retrieveOperation).ConfigureAwait(false);

            MailTemplateEntity templateEntity = retrievedResult?.Result as MailTemplateEntity;

            if (templateEntity != null)
            {
                templateEntity.Content = await contentTask;
            }

            this.logger.TraceInformation($"Finished {nameof(this.GetMailTemplate)} method of {nameof(MailTemplateRepository)}.", traceProps);

            return(templateEntity);
        }
        /// <inheritdoc/>
        public async Task <bool> DeleteMailTemplate(string applicationName, string templateName)
        {
            var traceProps = new Dictionary <string, string>();

            traceProps[AIConstants.Application]      = applicationName;
            traceProps[AIConstants.MailTemplateName] = templateName;

            this.logger.TraceInformation($"Started {nameof(this.DeleteMailTemplate)} method of {nameof(MailTemplateRepository)}.", traceProps);
            bool   result   = false;
            string blobName = this.GetBlobName(applicationName, templateName);
            var    status   = await this.cloudStorageClient.DeleteBlobsAsync(blobName).ConfigureAwait(false);

            if (status)
            {
                // Retrieve the entity to be deleted.
                TableOperation retrieveOperation = TableOperation.Retrieve <MailTemplateEntity>(applicationName, templateName);
                TableResult    tableResult       = await this.cloudTable.ExecuteAsync(retrieveOperation).ConfigureAwait(false);

                MailTemplateEntity mailTemplateEntity = tableResult.Result as MailTemplateEntity;

                // Create the TableOperation object that Delets the entity.
                TableOperation deleteOperation = TableOperation.Delete(mailTemplateEntity);
                var            response        = await this.cloudTable.ExecuteAsync(deleteOperation).ConfigureAwait(false);

                result = true;
            }

            this.logger.TraceInformation($"Finished {nameof(this.DeleteMailTemplate)} method of {nameof(MailTemplateRepository)}.", traceProps);

            return(result);
        }
        /// <inheritdoc/>
        public async Task <bool> UpsertEmailTemplateEntities(MailTemplateEntity mailTemplateEntity)
        {
            bool result = false;

            this.logger.TraceInformation($"Started {nameof(this.UpsertEmailTemplateEntities)} method of {nameof(MailTemplateRepository)}.");

            if (mailTemplateEntity is null)
            {
                throw new ArgumentNullException(nameof(mailTemplateEntity));
            }

            string blobName = this.GetBlobName(mailTemplateEntity.Application, mailTemplateEntity.TemplateId);
            string blobUri  = await this.cloudStorageClient.UploadBlobAsync(
                blobName,
                mailTemplateEntity.Content)
                              .ConfigureAwait(false);

            // Making sure content is not stored in table storage
            mailTemplateEntity.Content = null;

            // Create the TableOperation object that inserts the entity.
            TableOperation insertOperation = TableOperation.InsertOrReplace(mailTemplateEntity);
            var            response        = await this.cloudTable.ExecuteAsync(insertOperation).ConfigureAwait(false);

            result = true;
            this.logger.TraceInformation($"Finished {nameof(this.UpsertEmailTemplateEntities)} method of {nameof(MailTemplateRepository)}.");

            return(result);
        }
예제 #5
0
        public async Task <ActionResult> Index()
        {
            var data = new MailTemplateEntity()
            {
                pagenumber = 1,
                type       = "-1",
                pagesize   = 20,
                order      = "id desc"
            };
            var _posts = await MailTemplateBLL.Load(_context, data);;

            return(Ok(new { posts = _posts, records = 434 }));
        }
        /// <summary>
        /// Converts <see cref="MailTemplateEntity"/> to a <see cref="MailTemplateInfo"/>.
        /// </summary>
        /// <param name="mailTemplateEntity">Email template item.</param>
        /// <returns><see cref="MailTemplateInfo"/>.</returns>
        public static MailTemplateInfo ToTemplateInfoContract(this MailTemplateEntity mailTemplateEntity)
        {
            if (mailTemplateEntity != null)
            {
                return(new MailTemplateInfo
                {
                    TemplateId = mailTemplateEntity.TemplateId,
                    Description = mailTemplateEntity.Description,
                    TemplateType = mailTemplateEntity.TemplateType,
                });
            }

            return(null);
        }
        /// <summary>
        /// Converts <see cref="MailTemplateEntity"/> to a <see cref="MailTemplate"/>.
        /// </summary>
        /// <param name="mailTemplateEntity">Email template item.</param>
        /// <param name="encryptionService">Instance of encryption service to protect the secure content before saving in datastore.</param>
        /// <returns><see cref="MailTemplate"/>.</returns>
        public static MailTemplate ToContract(this MailTemplateEntity mailTemplateEntity, IEncryptionService encryptionService)
        {
            if (encryptionService is null)
            {
                throw new ArgumentNullException(nameof(encryptionService));
            }

            if (mailTemplateEntity != null)
            {
                return(new MailTemplate
                {
                    TemplateId = mailTemplateEntity.TemplateId,
                    Description = mailTemplateEntity.Description,
                    TemplateType = mailTemplateEntity.TemplateType,
                    Content = encryptionService.Decrypt(mailTemplateEntity.Content),
                });
            }

            return(null);
        }
예제 #8
0
        private static System.Linq.Expressions.Expression <Func <JGN_MailTemplates, bool> > returnWhereClause(MailTemplateEntity entity)
        {
            var where_clause = PredicateBuilder.New <JGN_MailTemplates>(true);

            if (entity.id > 0)
            {
                where_clause = where_clause.And(p => p.id == entity.id);
            }

            if (entity.term != "")
            {
                where_clause = where_clause.And(p => p.subject.Contains(entity.term) || p.templatekey.Contains(entity.term) || p.description.Contains(entity.term) || p.contents.Contains(entity.term));
            }

            if (entity.templatekey != "")
            {
                where_clause = where_clause.And(p => p.templatekey == entity.templatekey);
            }

            if (entity.type != "-1" && entity.id == 0)
            {
                where_clause = where_clause.And(p => p.type == entity.type);
            }


            return(where_clause);
        }
예제 #9
0
        private static IQueryable <JGN_MailTemplates> processOptionalConditions(IQueryable <JGN_MailTemplates> collectionQuery, MailTemplateEntity query)
        {
            if (query.order != "")
            {
                collectionQuery = (IQueryable <JGN_MailTemplates>)collectionQuery.Sort(query.order);
            }

            if (query.id == 0)
            {
                // skip logic
                if (query.pagenumber > 1)
                {
                    collectionQuery = collectionQuery.Skip(query.pagesize * (query.pagenumber - 1));
                }
                // take logic
                if (!query.loadall)
                {
                    collectionQuery = collectionQuery.Take(query.pagesize);
                }
            }
            return(collectionQuery);
        }
예제 #10
0
 private static string GenerateKey(string key, MailTemplateEntity entity)
 {
     return(key + UtilityBLL.ReplaceSpaceWithHyphin(entity.order.ToLower()) + "" + entity.pagenumber);
 }
예제 #11
0
 private static Task <int> CountRecords(ApplicationDbContext context, MailTemplateEntity entity)
 {
     return(context.JGN_MailTemplates.Where(returnWhereClause(entity)).CountAsync());
 }
예제 #12
0
        public static Task <List <JGN_MailTemplates> > Load(ApplicationDbContext context, MailTemplateEntity entity)
        {
            var collectionQuery = context.JGN_MailTemplates.Where(returnWhereClause(entity));

            collectionQuery = processOptionalConditions(collectionQuery, entity);
            if (entity.id > 0 || !entity.issummary)
            {
                return(LoadCompleteList(collectionQuery));
            }
            else
            {
                return(LoadSummaryList(collectionQuery));
            }
        }