/// <summary> /// Get queue email for send email to revovery user passowrd /// </summary> /// <param name="emailTemplate">Template email</param> /// <param name="userEmail">active email user</param> /// <param name="webSiteName">system name</param> /// <param name="userDisplayName">User full name</param> /// <param name="recoveryLink">recovery link</param> public virtual QueuedEmail UserPasswordRecovery(MessageTemplate emailTemplate, string userEmail, string webSiteName, string userDisplayName, string recoveryLink) { GuardClausesParameter.Null(emailTemplate, nameof(emailTemplate)); GuardClausesParameter.NullOrEmpty(userEmail, nameof(userEmail)); GuardClausesParameter.NullOrEmpty(recoveryLink, nameof(recoveryLink)); Dictionary <string, string> replaceValue = new Dictionary <string, string> { { "[systemname]", webSiteName }, { "[userfullname]", userDisplayName }, { "[recoverylink]", recoveryLink } }; string title = CoreUtility.ReplaceContentHelper(emailTemplate.Title, replaceValue); string content = CoreUtility.ReplaceContentHelper(emailTemplate.Body, replaceValue) + "<br />" + _systemSettings.Common.EmailSignature; return(new QueuedEmail() { To = userEmail, Title = title, EmailBody = content, Cc = null, Bcc = null, ToName = userDisplayName, EmailAccountId = null, SendTime = null, Priority = 5, TrySend = 0 }); }
public SqlCacheService(IDistributedCache distributedCache, IOptions <SqlServerCacheOptions> option) { GuardClausesParameter.Null(option, nameof(option)); _distributedCache = distributedCache; _sqlServerCacheOptions = option.Value; }
public async Task <bool> ImportFromJsonAsync(string json) { GuardClausesParameter.NullOrEmpty(json, nameof(json)); List <CmsMenu> listMenu = JsonSerializer.Deserialize <List <CmsMenu> >(json); IEnumerable <TreeItem <CmsMenu> > treeMenu = listMenu.GenerateTree(q => q.Id, q => q.ParentId, q => q.DisplayOrder, string.Empty); await CreateMenuAsync(treeMenu, string.Empty); return(true); async Task CreateMenuAsync(IEnumerable <TreeItem <CmsMenu> > listTreeMenu, string parentId) { foreach (TreeItem <CmsMenu> cMenu in listTreeMenu) { CmsMenu menuInsert = new CmsMenu() { Active = cMenu.Item.Active, DisplayOrder = cMenu.Item.DisplayOrder, CssClass = cMenu.Item.CssClass, Link = cMenu.Item.Link, Name = cMenu.Item.Name, ParentId = parentId, Position = cMenu.Item.Position, RolesString = cMenu.Item.RolesString, TargetType = cMenu.Item.TargetType }; bool checkInsert = await InsertAsync(menuInsert); if (checkInsert) { await CreateMenuAsync(cMenu.Children, menuInsert.Id); } } } }
/// <summary> /// Get queue email for send email wellcome user /// </summary> /// <param name="emailTemplate">Template email</param> /// <param name="userEmail">Email of user</param> /// <param name="userPassowrd">Password of user optional</param> /// <param name="userDisplayName">full name of user</param> /// <param name="linkCallback">link if have</param> /// <returns></returns> public virtual QueuedEmail GetQueuedEmailForNewUser(MessageTemplate emailTemplate, string userEmail, string userPassowrd, string userDisplayName, string linkCallback = "") { GuardClausesParameter.Null(emailTemplate, nameof(emailTemplate)); GuardClausesParameter.NullOrEmpty(userEmail, nameof(userEmail)); Dictionary <string, string> replaceValue = new Dictionary <string, string> { { "[useremail]", userEmail }, { "[userpassword]", userPassowrd }, { "[userfullname]", userDisplayName }, { "[callbacklink]", linkCallback } }; string title = CoreUtility.ReplaceContentHelper(emailTemplate.Title, replaceValue); string content = CoreUtility.ReplaceContentHelper(emailTemplate.Body, replaceValue) + "<br />" + _systemSettings.Common.EmailSignature; return(new QueuedEmail() { To = userEmail, Title = title, EmailBody = content, Cc = null, Bcc = null, ToName = userDisplayName, EmailAccountId = null, SendTime = null, Priority = 5, TrySend = 0 }); }
public virtual async Task <bool> InsertAsync(IEnumerable <T> entities) { GuardClausesParameter.NullOrEmpty(entities, nameof(entities)); await DbContext.Set <T>().AddRangeAsync(entities); return(await DbContext.SaveChangesAsync() > 0); }
public virtual string GetFilePath(ResourceType fileType, string fileName) { GuardClausesParameter.NullOrEmpty(fileName, nameof(fileName)); string filePath = string.Empty; switch (fileType) { case ResourceType.Image: filePath = _systemSettings.Upload.UploadImgPath; break; case ResourceType.Movie: filePath = _systemSettings.Upload.UploadVideoPath; break; case ResourceType.Audio: filePath = _systemSettings.Upload.UploadAudioPath; break; case ResourceType.Avatar: filePath = _systemSettings.User.UploadAvatarPath; break; case ResourceType.None: break; default: filePath = _systemSettings.Upload.UploadFilePath; break; } return(string.IsNullOrEmpty(fileName) ? filePath : Path.Combine(filePath, fileName)); }
public virtual async Task <bool> InsertAsync(T entity) { GuardClausesParameter.Null(entity, nameof(entity)); await DbContext.Set <T>().AddAsync(entity); return(await DbContext.SaveChangesAsync() > 0); }
public virtual ResourceType GetMediaType(string resourcePath) { GuardClausesParameter.NullOrEmpty(resourcePath, nameof(resourcePath)); string fileExtentson = CoreUtility.GetFileExtension(resourcePath, false); if (!string.IsNullOrEmpty(fileExtentson)) { if (!string.IsNullOrEmpty(_systemSettings.Upload.ImageAllowedExtensions) && _systemSettings.Upload.ImageAllowedExtensions.Split(',', ';').Contains(fileExtentson, StringComparer.OrdinalIgnoreCase)) { return(ResourceType.Image); } if (!string.IsNullOrEmpty(_systemSettings.Upload.VideoAllowedExtensions) && _systemSettings.Upload.VideoAllowedExtensions.Split(',', ';').Contains(fileExtentson, StringComparer.OrdinalIgnoreCase)) { return(ResourceType.Movie); } if (!string.IsNullOrEmpty(_systemSettings.Upload.AudioAllowedExtensions) && _systemSettings.Upload.AudioAllowedExtensions.Split(',', ';').Contains(fileExtentson, StringComparer.OrdinalIgnoreCase)) { return(ResourceType.Audio); } if (!string.IsNullOrEmpty(_systemSettings.Upload.FileAllowedExtensions) && _systemSettings.Upload.FileAllowedExtensions.Split(',', ';').Contains(fileExtentson, StringComparer.OrdinalIgnoreCase)) { return(ResourceType.None); } } return(ResourceType.None); }
public virtual async Task <User> CacheGetUserById(string userId) { GuardClausesParameter.NullOrEmpty(userId, nameof(userId)); string cacheKey = $"{CoreConstants.UserCacheKey}gcua_{userId}uid"; return(await _asyncCacheService.GetOrCreateAsync(cacheKey, async() => await _userData.FindAsync(userId), CoreConstants.DefaultCacheTime)); }
/// <summary> /// Hàm khởi tạo /// </summary> /// <param name="pageSize">Số bản ghi trên trang</param> /// <param name="currentPage">Trang hiện tại</param> public Paging(int pageSize = 10, int currentPage = 1) { GuardClausesParameter.OutOfRange(pageSize, nameof(pageSize), 1, int.MaxValue); PageSize = pageSize; CurrentPage = currentPage; _rowsCount = 0; }
public async Task SetLocalizedModelLocalAsync <T>(IEnumerable <T> listLocalizedData, string objectId, ObjectTypeEnum objectType) { GuardClausesParameter.NullOrEmpty(listLocalizedData, nameof(listLocalizedData)); foreach (var istem in listLocalizedData) { await SetLocalizedStringAsync(istem, objectId, CoreUtility.GetProperty <string>(istem, "LanguageCulture"), objectType); } }
public virtual async Task <bool> UpdateAsync(IEnumerable <T> entities) { GuardClausesParameter.NullOrEmpty(entities, nameof(entities)); foreach (T entity in entities) { DbContext.Entry(entity).State = EntityState.Modified; } return(await DbContext.SaveChangesAsync() > 0); }
public virtual async Task <bool> ExecuteSqlCommandAsync(string queryCommand, IsolationLevel isolationLevel = IsolationLevel.ReadCommitted, params object[] parameters) { GuardClausesParameter.NullOrEmpty(queryCommand, nameof(queryCommand)); using IDbContextTransaction dbContextTransaction = await DbContext.Database.BeginTransactionAsync(isolationLevel); int dataQuery = await DbContext.Database.ExecuteSqlRawAsync(queryCommand, default(CancellationToken), parameters); dbContextTransaction.Commit(); return(dataQuery > 0); }
public virtual async Task <IReadOnlyList <T> > SqlQueryAsync(string queryCommand, IsolationLevel isolationLevel = IsolationLevel.ReadCommitted, params object[] parameters) { GuardClausesParameter.NullOrEmpty(queryCommand, nameof(queryCommand)); using IDbContextTransaction dbContextTransaction = await DbContext.Database.BeginTransactionAsync(isolationLevel); IQueryable <T> dataQuery = DbContext.Set <T>().FromSqlRaw(queryCommand, parameters); dbContextTransaction.Commit(); return(await dataQuery.ToListAsync()); }
/// <summary> /// Lấy danh sách ngôn ngữ và .net hỗ trợ /// </summary> /// <param name="helper">Html helper</param> /// <returns>List SelectListItem</returns> public static List <SelectListItem> GetSpecificCulturesSelectItem(this IHtmlHelper helper) { GuardClausesParameter.Null(helper, nameof(helper)); return(CultureInfo.GetCultures(CultureTypes.SpecificCultures).OrderBy(x => x.EnglishName).Select(x => new SelectListItem() { Value = x.IetfLanguageTag, Text = string.Format("{0}. {1}", x.EnglishName, x.IetfLanguageTag) }).ToList()); }
public virtual async Task SendEmailAync(EmailAccount emailAccount, QueuedEmail queuedEmail) { GuardClausesParameter.Null(emailAccount, nameof(emailAccount)); GuardClausesParameter.Null(queuedEmail, nameof(queuedEmail)); try { List <string> toList = null; if (!string.IsNullOrWhiteSpace(queuedEmail.To)) { toList = queuedEmail.To.Split(new char[] { ',', ';' }).ToList(); } List <string> bccList = null; if (!string.IsNullOrWhiteSpace(queuedEmail.Bcc)) { bccList = queuedEmail.Bcc.Split(new char[] { ',', ';' }).ToList(); } List <string> ccList = null; if (!string.IsNullOrWhiteSpace(queuedEmail.Cc)) { ccList = queuedEmail.Cc.Split(new char[] { ',', ';' }).ToList(); } await CoreUtility.SendEmail(emailAccount.UserName, emailAccount.Password, queuedEmail.Title, queuedEmail.EmailBody, emailAccount.Email, emailAccount.Name, toList, emailAccount.Host, emailAccount.Port, emailAccount.EnableSsl, emailAccount.UseDefaultCredentials, bccList, ccList); emailAccount.SendCountByDay++; if (emailAccount.SendCountByDay >= emailAccount.DaySendLimit) { emailAccount.DaySendLimited = DateTime.Now.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture); emailAccount.SendCountByDay = 0; } queuedEmail.EmailAccountId = emailAccount.Id; queuedEmail.SendTime = DateTime.Now; } catch (Exception ex) { _logger.LogError(new EventId(100, "SendEmail"), ex, ex.Message); } finally { queuedEmail.TrySend++; } }
public async Task <T> GetLocalizedStringAsync <T>(T entity, string languageCulture, ObjectTypeEnum objectType) where T : IBaseEntity { GuardClausesParameter.Null(entity, nameof(entity)); var listLocalizedString = await FindAllNoTrackingAsync(q => q.LanguageCulture == languageCulture && q.ObjectType == objectType && q.ObjectId == entity.Id); foreach (ObjectLocalized item in listLocalizedString) { CoreUtility.SetProperty(entity, item.PropertyName, item.LocalizedValue); } return(entity); }
/// <summary> /// Lấy đối tượng từ khóa cache nếu chưa có cache sẽ gọi hàm lấy đối tượng, hàm bất đồng bộ /// </summary> /// <typeparam name="TItem">Loại đối tượng</typeparam> /// <param name="key">Khóa cache</param> /// <param name="factory">Hàm lấy đối tượng nếu cache null</param> /// <param name="time">Thời gian tồn tại cache tính bắng giây</param> /// <returns>TItem object</returns> public virtual async Task <TItem> GetOrCreateAsync <TItem>(string key, Func <Task <TItem> > factory, int time) { GuardClausesParameter.Null(factory, nameof(factory)); if (!_memoryCache.TryGetValue(key, out object result)) { result = await factory(); _memoryCache.Set(AddKey(key), result, GetMemoryCacheEntryOptions(time)); } return((TItem)result); }
/// <summary> /// Hàm tạo icon hướng dẫn /// </summary> /// <param name="helper">HTML helper</param> /// <param name="value">Nội dung hướng dẫn</param> /// <returns>IHtmlContent</returns> public static IHtmlContent Hint(this IHtmlHelper helper, string value) { GuardClausesParameter.Null(helper, nameof(helper)); //Hàm này đồng bộ với file QiLabelTagHelper TagBuilder builder = new TagBuilder("i"); builder.MergeAttribute("title", value); builder.MergeAttribute("class", "fa fa-question-circle info hellp-hit"); builder.MergeAttribute("data-toggle", "tooltip"); builder.MergeAttribute("data-placement", "top"); return(new HtmlString(builder.ToHtmlString())); }
public async Task <bool> ImportFromJsonAsync(string culture, string json) { GuardClausesParameter.Null(culture, nameof(culture)); GuardClausesParameter.Null(json, nameof(json)); List <LanguageResource> listInsert = JsonSerializer.Deserialize <List <LanguageResource> >(json); listInsert = listInsert.Where(q => !string.IsNullOrEmpty(q.Key) && !string.IsNullOrEmpty(q.Culture) && !string.IsNullOrEmpty(q.Type) && !AnyAsync(c => c.Key == q.Key && c.Culture == q.Culture && c.Type == q.Type).Result).ToList(); return(await InsertAsync(listInsert)); }
public virtual async Task <bool> UpdateRateAsync(string currencyCode, decimal rate) { GuardClausesParameter.Null(currencyCode, nameof(currencyCode)); Currency updateEntity = await FindAsync(q => q.CurrencyCode == currencyCode); if (updateEntity != null) { updateEntity.Rate = rate; return(await UpdateAsync(updateEntity)); } return(false); }
public async Task <bool> SetLocalizedStringAsync <T>(T entity, string objectId, string languageCulture, ObjectTypeEnum objectType) { GuardClausesParameter.Null(entity, nameof(entity)); foreach (PropertyInfo item in CoreUtility.GetPropertyList(entity)) { if (item.Name != "LanguageId") { string value = CoreUtility.GetProperty <string>(entity, item.Name); await SaveLocalizedAsync(languageCulture, objectId, objectType, item.Name, value); } } return(true); }
public async Task <string> ExportToJsonAsync(string culture) { GuardClausesParameter.Null(culture, nameof(culture)); return(JsonSerializer.Serialize((await FindAllNoTrackingAsync(q => q.Culture == culture)).Select(q => new { q.Culture, q.Key, q.Type, q.Value }), new JsonSerializerOptions() { WriteIndented = true })); }
public async Task <IEnumerable <T> > GetLocalizedStringAsync <T>(IEnumerable <T> entitys, string languageCulture, ObjectTypeEnum objectType) where T : IBaseEntity { GuardClausesParameter.NullOrEmpty(entitys, nameof(entitys)); var ids = string.Join(",", entitys.Select(q => $"'{q.Id}'")); var localizedStrings = await SqlQueryAsync($"SELECT * FROM {TableName} WHERE LanguageCulture = '{languageCulture}' AND ObjectType = {objectType} AND ObjectId IN ({ids})"); foreach (var entity in entitys) { var itemLocalizedSting = localizedStrings.Where(q => q.ObjectId == entity.Id); foreach (ObjectLocalized item in itemLocalizedSting) { CoreUtility.SetProperty(entity, item.PropertyName, item.LocalizedValue); } } return(entitys); }
/// <summary> /// Set một giá trị theo key và mô tả /// </summary> /// <param name="key">Khóa</param> /// <param name="value">Giá trị</param> /// <returns>Nếu đã tồn tại thì sẽ update</returns> public async Task <bool> SetStringByKeyAsync(string key, string value) { GuardClausesParameter.Null(key, nameof(key)); GuardClausesParameter.Null(value, nameof(value)); SystemValue systemKeyValue = await FindAsync(q => q.Key == key); if (systemKeyValue != null) { systemKeyValue.Value = value; return(await UpdateAsync(systemKeyValue)); } systemKeyValue = new SystemValue() { Key = key, Value = value }; return(await InsertAsync(systemKeyValue)); }
/// <summary> /// Lấy người dùng hiện tại hoặc lấy tất cả /// Hàm này chỉ gọi được khi đã có người login /// </summary> /// <param name="claimsPrincipal">User claims</param> /// <returns>List SelectListItem</returns> public List <SelectListItem> GetAllUserOrOnlyMeSelectedItems(ClaimsPrincipal claimsPrincipal) { GuardClausesParameter.Null(claimsPrincipal, nameof(claimsPrincipal)); string userId = claimsPrincipal.FindFirstValue(ClaimTypes.NameIdentifier); return(new List <SelectListItem>() { new SelectListItem() { Text = _localizer["Tất cả mọi người"], Value = "" }, new SelectListItem() { Text = _localizer["Chỉ mình tôi"], Value = userId, Selected = true }, }); }
/// <summary> /// get IDataSourceResult from iquery /// </summary> /// <typeparam name="T">Type of entity</typeparam> /// <param name="inputQuery">IQueryable of T</param> /// <param name="specification">a specification</param> /// <returns>Task IDataSourceResult T</returns> public static async Task <IDataSourceResult <T> > ToDataSourceResultAsync <T>(this IQueryable <T> inputQuery, ISpecification <T> specification) where T : class { GuardClausesParameter.Null(specification, nameof(specification)); DataSourceResult <T> dataSourceResult = new DataSourceResult <T>(); IQueryable <T> query = inputQuery; if (specification.Criteria != null) { query = query.Where(specification.Criteria); } query = specification.Includes.Aggregate(query, (current, include) => current.Include(include)); query = specification.IncludeStrings.Aggregate(query, (current, include) => current.Include(include)); if (specification.OrderBy != null) { query = query.OrderBy(specification.OrderBy); } else if (specification.OrderByDescending != null) { query = query.OrderByDescending(specification.OrderByDescending); } if (specification.Selector != null) { query = query.Select(specification.Selector); } if (specification.GroupBy != null) { query = query.GroupBy(specification.GroupBy).SelectMany(x => x); } if (specification.IsPagingEnabled) { dataSourceResult.Total = query.Count(); query = query.Skip(specification.Skip).Take(specification.Take); } dataSourceResult.Data = await query.ToListAsync(); return(dataSourceResult); }
/// <summary> /// Get data protecter by file system key manager. if your aplication run on multi server you need use redis of azure key manager /// </summary> /// <param name="basePath">Base folder to save key /// ex. in asp.net core set WebHostEnvironment.ContentRootPath /// </param> /// <param name="applicationName">The application name /// ex. in asp.net core set WebHostEnvironment.ApplicationName /// </param> /// <param name="applicationKey">system key to identity system to read and write protected data /// ex. in asp.net core set WebHostEnvironment.EnvironmentName /// </param> public static IDataProtector InitializeFileSystemProtecter(string basePath, string applicationName, string applicationKey) { if (_dataProtector != null) { return(_dataProtector); } GuardClausesParameter.NullOrEmpty(basePath, nameof(basePath)); GuardClausesParameter.NullOrEmpty(applicationName, nameof(applicationName)); GuardClausesParameter.NullOrEmpty(applicationKey, nameof(applicationKey)); var destFolder = Path.Combine(basePath, "datakey"); var dataProtectionProvider = DataProtectionProvider.Create(new DirectoryInfo(destFolder), options => { options.SetApplicationName(applicationName); }); _dataProtector = dataProtectionProvider.CreateProtector(applicationKey); return(_dataProtector); }
public virtual string CurrencyToString(decimal amount, Currency targetCurrency) { GuardClausesParameter.Null(targetCurrency, nameof(targetCurrency)); if (!string.IsNullOrEmpty(targetCurrency.CustomFormatting)) { return(amount.ToString(targetCurrency.CustomFormatting, CultureInfo.InvariantCulture)); } else { if (!string.IsNullOrEmpty(targetCurrency.Culture)) { CultureInfo format = new CultureInfo(targetCurrency.Culture); return(amount.ToString("C", format)); } else { return($"{amount.ToString("N", CultureInfo.InvariantCulture)} ({targetCurrency.CurrencyCode})"); } } }
/// <summary> /// Ap dụng điều kiện lọc vào cấu query /// </summary> /// <param name="inputQuery">Dữ liệu đầu vào</param> /// <param name="specification">Láy chi tiết dữ liệu</param> /// <returns>IQueryable T</returns> public static IQueryable <T> GetSpecificationQuery <T>(this IQueryable <T> inputQuery, ISpecification <T> specification) where T : class { GuardClausesParameter.Null(specification, nameof(specification)); IQueryable <T> query = inputQuery; if (specification.Criteria != null) { query = query.Where(specification.Criteria); } query = specification.Includes.Aggregate(query, (current, include) => current.Include(include)); query = specification.IncludeStrings.Aggregate(query, (current, include) => current.Include(include)); if (specification.OrderBy != null) { query = query.OrderBy(specification.OrderBy); } else if (specification.OrderByDescending != null) { query = query.OrderByDescending(specification.OrderByDescending); } if (specification.Selector != null) { query = query.Select(specification.Selector); } if (specification.GroupBy != null) { query = query.GroupBy(specification.GroupBy).SelectMany(x => x); } if (specification.IsPagingEnabled) { query = query.Skip(specification.Skip).Take(specification.Take); } return(query); }