public BaseResponse DeleteImage(Guid id, string dataFolderPath) { return Execute(_repository, r => { var res = new BaseResponse(); var image = r.GetImage(id); if (image == null) { res.Success = false; res.Messages.Add("DataNotFound"); //res key return res; } res = r.DeleteImage(id); if (res.Success) { DeleteFile(dataFolderPath, image.ImagePath); } _cacheHelper.ClearGetProducts(); return res; }); }
public BaseResponse DeleteRole(Guid id) { var response = new BaseResponse(); using (var db = DbContext) { var entity = db.Roles.FirstOrDefault(e => e.Id == id); if (entity != null) { db.Entry(entity).State = EntityState.Deleted; response.Success = db.SaveChanges() > 0; if (response.Success) { db.Database.ExecuteSqlCommand("DELETE RolePermissions WHERE RoleId = {0}", id); } } else { response.Success = false; } } return response; }
public BaseResponse SaveAppSetting(SaveRequest request) { return Execute(_repository, r => { var response = new BaseResponse(); if (request.Entity.IsNew && r.CheckExist(request.Entity.SettingType, request.Entity.Name)) { response.Success = false; response.Messages.Add("NameIsExisted"); //resource key return response; } if (!request.Entity.IsNew && r.CheckExist(request.Entity.SettingType, request.Entity.Name, request.Entity.Id)) { response.Success = false; response.Messages.Add("NameIsExisted"); //resource key return response; } if (request.Entity.IsNew) { request.Entity.InitId(); } r.SaveAppSetting(request); return response; }); }
/// <summary> /// Hàm này gọi từ trang tôi quên mật khẩu. /// </summary> /// <param name="request"></param> /// <returns></returns> public BaseResponse ChangePwd(ChangePwdResquest request) { return Execute(_repository, r => { var response = new BaseResponse(); var requestData = _requestPasswordRepository.GetByRefKey(request.KeyRef); if (requestData == null) { response.Success = false; response.Messages.Add("BadRequest");//Res key return response; } var beUser = _cacheHelper.GetAllBeUsers().FirstOrDefault(i => i.Email == requestData.Email.ToStr().Trim().ToLower()); if (beUser == null) { response.Success = false; response.Messages.Add("UnableFindUser");//Res key return response; } request.UserId = beUser.Id; request.NewPassword = SimpleHash.ComputeHash(request.NewPassword, SiteSettings.HashAlgorithm, null); r.ChangePwd(request); _requestPasswordRepository.Delete(requestData.Id); _cacheHelper.ClearGetBeUser(beUser.Id); return response; }); }
public BaseResponse Insert(SaveRequest request) { var response = new BaseResponse(); using (var db = DbContext) { var entityDb = request.Entity.Map<DataLog, Entity.DataLog>(); db.DataLogs.Add(entityDb); response.Success = db.SaveChanges() > 0; } return response; }
public BaseResponse DeleteImage(Guid id) { var response = new BaseResponse(); using (var db = DbContext) { var entity = db.ProductImages.FirstOrDefault(e => e.Id == id); if (entity != null) { db.Entry(entity).State = EntityState.Deleted; response.Success = db.SaveChanges() > 0; } } return response; }
public BaseResponse SendEmail(ContactUs request) { return Execute(_repository, r => { var response = new BaseResponse(); var emailTemplate = _cacheHelper.GetSystemEmailTemplates() .FirstOrDefault(i => i.Type == SystemEmailTemplateType.StoreFrontContactUs && i.Language == SiteUtils.GetDefaultLanguageIfNullOrEmpty(SiteUtils.GetAdminNotifyLanguage())) ?? _cacheHelper.GetSystemEmailTemplates() .FirstOrDefault(i => i.Type == SystemEmailTemplateType.StoreFrontContactUs && i.Language == SiteUtils.GetDefaultLanguageIfNullOrEmpty()); if (emailTemplate == null) { response.Success = false; response.Messages.Add("EmailTemplateDataNotFound"); //Resource key } else { emailTemplate.Subject = emailTemplate.Subject.ToStr() + " - " + request.Subject; var mapKeys = new Dictionary<string, string>(); mapKeys.Add(ConstKeys.Email, request.Email); mapKeys.Add(ConstKeys.Name, request.Name); mapKeys.Add(ConstKeys.Phone, request.Phone); mapKeys.Add(ConstKeys.Subject, request.Subject); mapKeys.Add(ConstKeys.Message, request.Message); var emailNotification = SiteUtils.GetAdminEmailNotification(); if (string.IsNullOrWhiteSpace(emailNotification) || !std.IsEmail(emailNotification)) { response.Success = false; response.Messages.Add("EmailNotificationIsNotSetup"); //Resource key } else { EmailUtils.Instance.SendEmail(emailNotification, emailTemplate, mapKeys); } } return response; }); }
public BaseResponse DeleteRole(Guid id) { return Execute(_repository, r => { var res = new BaseResponse(); if (_repositoryBeUser.RoleIsUsed(id)) { res.Success = false; res.Messages.Add("RoleIsUsed");//res key return res; } res = r.DeleteRole(id); _cacheHelper.ClearGetRole(id); return res; }); }
public BaseResponse Delete(string id) { var response = new BaseResponse(); using (var db = DbContext) { var entity = db.RefreshTokens.FirstOrDefault(e => e.Id == id); if (entity != null) { db.Entry(entity).State = EntityState.Deleted; response.Success = db.SaveChanges() > 0; } else { response.Success = false; } } return response; }
public BaseResponse SaveArtice(SaveRequest request) { var response = new BaseResponse(); using (var db = DbContext) { var entityDb = request.Entity.Map<Artice, Entity.Artice>(); if (!db.Artices.Any(e => e.Id == entityDb.Id)) { db.Artices.Add(entityDb); } else { db.Entry(entityDb).State = EntityState.Modified; } response.Success = db.SaveChanges() > 0; } return response; }
public BaseResponse DeleteImages(List<Guid> ids) { var response = new BaseResponse(); if (ids == null || !ids.Any()) return response; using (var db = DbContext) { ids.ForEach(id => { var entity = db.ProductImages.FirstOrDefault(e => e.Id == id); if (entity != null) { db.Entry(entity).State = EntityState.Deleted; } }); response.Success = db.SaveChanges() > 0; return response; } }
public BaseResponse DeleteProduct(Guid id) { var response = new BaseResponse(); using (var db = DbContext) { var entity = db.Products.FirstOrDefault(e => e.Id == id); if (entity != null) { db.Entry(entity).State = EntityState.Deleted; response.Success = db.SaveChanges() > 0; if (response.Success) { db.Database.ExecuteSqlCommand("DELETE ProductImages WHERE ProductId = {0}", id); db.Database.ExecuteSqlCommand("DELETE ProductVideos WHERE ProductId = {0}", id); } } } return response; }
public BaseResponse SaveProducts(List<Product> products) { return Execute(_repository, r => { var res = new BaseResponse(); var rowIndex = 2; products.ForEach(product => { if (r.ProductCodeExists(product.ProductCode)) { res.Messages.Add( string.Format(ProductRes.ExcelProductCodeExisted, rowIndex, product.ProductCode)); } ++rowIndex; }); if (res.Messages.Any()) { res.Success = false; return res; } res = r.SaveProducts(products); return res; }); }
public BaseResponse SaveProduct(SaveRequest request) { return Execute(_repository, r => { var res = new BaseResponse(); var entity = request.Entity; #region check var isNew = !r.ProductIsExisted(entity.Id); if (isNew) { if (r.ProductCodeExists(entity.ProductCode)) { res.Success = false; res.Messages.Add("CodeExisted"); //res key return res; } } else if (r.ProductCodeExists(entity.ProductCode, entity.Id)) { res.Success = false; res.Messages.Add("CodeExisted");//res key return res; } #endregion #region check category has no child if (entity.CategoryId == Guid.Empty) { res.Success = false; res.Messages.Add("DataIsInvalid");//res key return res; } var allCates = _cacheHelper.GetCategories(); var cate = allCates.FirstOrDefault(i => i.Id == entity.CategoryId); if (cate == null) { res.Success = false; res.Messages.Add("DataIsInvalid");//res key return res; } if (allCates.Any(i => i.ParentId == cate.Id))//cate has childs { res.Success = false; res.Messages.Add("CannotAddNewIntoThisGroup");//res key return res; } #endregion res = r.SaveProduct(request); _cacheHelper.ClearGetProducts(); return res; }); }
/// <summary> /// Hàm này gọi từ trang change password khi user đã login /// </summary> /// <param name="request"></param> /// <returns></returns> public BaseResponse SaveChangePassword(ChangePwdResquest request) { return Execute(_repository, r => { var response = new BaseResponse(); var beUser = _cacheHelper.GetBeUser(request.UserId); if (beUser == null) { response.Success = false; response.Messages.Add("UnableFindUser");//Res key return response; } if (!SimpleHash.VerifyHash(request.Password, SiteSettings.HashAlgorithm, beUser.HashPassword)) { response.Messages.Add("PwdIncorrect"); response.Success = false; return response; } request.NewPassword = SimpleHash.ComputeHash(request.NewPassword, SiteSettings.HashAlgorithm, null); r.ChangePwd(request); _cacheHelper.ClearGetBeUser(beUser.Id); return response; }); }
public BaseResponse UpdateProductInfo(ProductUpdateInfo info) { var response = new BaseResponse(); using (var db = DbContext) { var entityDb = new Entity.Product { Id = info.Id }; db.Products.Attach(entityDb); var changed = false; if (changed) { response.Success = db.SaveChanges() > 0; } } return response; }
public BaseResponse SaveListImage(List<ProductImage> listImage) { var response = new BaseResponse(); using (var db = DbContext) { if (listImage.Any()) { listImage.ForEach(productImage => { var entityDb = productImage.Map<ProductImage, Entity.ProductImage>(); db.ProductImages.Add(entityDb); }); response.Success = db.SaveChanges() > 0; } } return response; }
public BaseResponse SaveProducts(List<Product> products) { var response = new BaseResponse(); using (var db = DbContext) { if (products == null || !products.Any()) return response; products.ForEach(customer => db.Products.Add(customer.Map<Product, Entity.Product>())); response.Success = db.SaveChanges() > 0; } return response; }
public BaseResponse UpdateInfo(UpdateInfo info) { var response = new BaseResponse(); using (var db = DbContext) { var entityDb = new Entity.Artice { Id = info.Id }; db.Artices.Attach(entityDb); var changed = false; if (info.Status.HasValue) { entityDb.Status = info.Status.Value; db.Entry(entityDb).Property(o => o.Status).IsModified = true; changed = true; } if (changed) { response.Success = db.SaveChanges() > 0; } } return response; }
public BaseResponse SaveImage(ProductImage productImage) { var response = new BaseResponse(); using (var db = DbContext) { var entityDb = productImage.Map<ProductImage, Entity.ProductImage>(); if (!db.ProductImages.Any(e => e.Id == entityDb.Id)) { db.ProductImages.Add(entityDb); } else { db.Entry(entityDb).State = EntityState.Modified; } response.Success = db.SaveChanges() > 0; } return response; }
public BaseResponse SubmitRequestPassword(SubmitPasswordRequest request) { return Execute(_repository, r => { var response = new BaseResponse(); var keyRef = (new RandomStringGenerator()).Generate(50, true, true, false, false); request.Email = request.Email.ToStr().Trim().ToLower(); var preferLanguage = string.Empty; var fullName = string.Empty; if (request.IsBeUser) { var beUser = _cacheHelper.GetAllBeUsers().FirstOrDefault(i => i.Email == request.Email); if (beUser == null) { response.Success = false; response.Messages.Add("UnableFindUser"); //Resource key return response; } preferLanguage = beUser.PreferLanguage; fullName = beUser.FullName; } else { var customer = _userRepository.GetCustomerByEmail(request.Email); if (customer == null) { response.Success = false; response.Messages.Add("UnableFindUser"); //Resource key return response; } fullName = customer.ClinicName; } var oldRequest = r.GetByEmail(request.Email); if (oldRequest == null) { var saveRequest = new SaveRequest { Entity = new RequestPassword { CreatedDate = DateTime.UtcNow, Email = request.Email, KeyRef = keyRef } }; saveRequest.Entity.InitId(); response.Success = r.Save(saveRequest).Success; } else { keyRef = oldRequest.KeyRef; } if (response.Success) { #region send mail var emailTemplate = _cacheHelper.GetSystemEmailTemplates() .FirstOrDefault(i => i.Type == SystemEmailTemplateType.BeForgotPassword && i.Language == SiteUtils.GetDefaultLanguageIfNullOrEmpty(preferLanguage)) ?? _cacheHelper.GetSystemEmailTemplates() .FirstOrDefault(i => i.Type == SystemEmailTemplateType.BeForgotPassword && i.Language == SiteUtils.GetDefaultLanguageIfNullOrEmpty()); if (emailTemplate == null) { response.Success = false; response.Messages.Add("EmailTemplateDataNotFound"); //Resource key } else { var mapKeys = new Dictionary<string, string>(); mapKeys.Add(ConstKeys.Email, request.Email); mapKeys.Add(ConstKeys.FullName, fullName); var link = request.Link.ToStr(); if (link.EndsWith("/")) { link += keyRef; } else { link += "/" + keyRef; } mapKeys.Add(ConstKeys.LinkRecoverPassword, link); EmailUtils.Instance.SendEmail(request.Email, emailTemplate, mapKeys); } #endregion } return response; }); }
public BaseResponse UpdateUserLogin(UpdateUserLoginRequest request) { var res = new BaseResponse(); using (var db = DbContext) { var entityDb = new Entity.BeUser { Id = request.UserId, LastLoginDate = DateTime.UtcNow }; db.BeUsers.Attach(entityDb); db.Entry(entityDb).Property(o => o.LastLoginDate).IsModified = true; res.Success = db.SaveChanges() > 0; } return res; }
public BaseResponse SaveRole(SaveRequest request) { return Execute(_repository, r => { var response = new BaseResponse(); var isNew = !r.IsExists(request.Entity.Id); if (isNew && r.NameIsExist(request.Entity.Name.ToStr().Trim())) { response.Success = false; response.Messages.Add("NameIsExisted"); //resource key return response; } if(r.NameIsExist(request.Entity.Name.ToStr().Trim(), request.Entity.Id)) { response.Success = false; response.Messages.Add("NameIsExisted"); //resource key return response; } request.Entity.Permissions.ForEach(m => { if (m.IsNew) m.InitId(); m.RoleId = request.Entity.Id; }); var pages = typeof(BePage).EnumToList(); if (request.Entity.Permissions.Count > pages.Count) { response.Success = false; response.Messages.Add("DataIsInvalid"); //resource key return response; } var res = r.SaveRole(request); _cacheHelper.ClearGetRole(request.Entity.Id); return res; }); }
public BaseResponse DeleteProduct(Guid id, string dataFolderPath) { return Execute(_repository, r => { var product = r.GetProduct(id); var res = new BaseResponse(); if (product == null) return res; res = r.DeleteProduct(id); if (res.Success) { product.ProductImages.ForEach(image => DeleteFile(dataFolderPath, image.ImagePath)); _cacheHelper.ClearGetProducts(); } return res; }); }
public BaseResponse UpdateImageInfo(ProductImageUpdateInfo info) { var response = new BaseResponse(); using (var db = DbContext) { if (!db.ProductImages.Any(i => i.Id == info.Id)) { return response; } var entityDb = new Entity.ProductImage { Id = info.Id }; db.ProductImages.Attach(entityDb); var changed = false; if (info.Visible.HasValue) { entityDb.Visible = info.Visible.Value; db.Entry(entityDb).Property(o => o.Visible).IsModified = true; changed = true; } if (info.Represent.HasValue) { entityDb.Represent = info.Represent.Value; db.Entry(entityDb).Property(o => o.Represent).IsModified = true; changed = true; } if (changed) { response.Success = db.SaveChanges() > 0; } } return response; }
public BaseResponse SaveRole(SaveRequest request) { var response = new BaseResponse(); using (var db = DbContext) { var entityDb = request.Entity.Map<Role, Entity.Role>(); var permissions = entityDb.Permissions; entityDb.Permissions = null; if (!db.Roles.Any(e => e.Id == entityDb.Id)) { db.Roles.Add(entityDb); } else { db.Entry(entityDb).State = EntityState.Modified; } response.Success = db.SaveChanges() > 0; if (response.Success) { var roleId = (permissions.FirstOrDefault() ?? new Entity.RolePermission()).RoleId; db.Database.ExecuteSqlCommand("DELETE RolePermissions WHERE RoleId = {0}", roleId); foreach (var permission in permissions) { db.RolePermissions.Add(permission); } response.Success = db.SaveChanges() > 0; } } return response; }
public BaseResponse SaveBeUser(SaveRequest request) { return Execute(_repository, r => { var response = new BaseResponse(); var beUser = request.Entity; beUser.Email = beUser.Email.ToStr().Trim().ToLower(); #region check if (beUser.IsNew) { if (r.EmailExists(beUser.Email)) { response.Success = false; response.Messages.Add("EmailExisted"); //res key return response; } } else if (r.EmailExists(beUser.Email, beUser.Id)) { response.Success = false; response.Messages.Add("EmailExisted");//res key return response; } if (beUser.IsNew) { if (r.CodeExists(beUser.Code)) { response.Success = false; response.Messages.Add("CodeExisted"); //res key return response; } } else if (r.CodeExists(beUser.Code, beUser.Id)) { response.Success = false; response.Messages.Add("CodeExisted");//res key return response; } #endregion if (beUser.IsNew) { beUser.InitId(); beUser.CreatedDate = DateTime.UtcNow; beUser.HashPassword = SimpleHash.ComputeHash(request.Password, SiteSettings.HashAlgorithm, null); } else { var oldData = r.GetBeUser(beUser.Id); if (oldData == null) { response.Success = false; response.Messages.Add("CannotLoadData");//res key return response; } beUser.CreatedDate = oldData.CreatedDate; beUser.LastLoginDate = oldData.LastLoginDate; beUser.HashPassword = !string.IsNullOrWhiteSpace(request.Password) ? SimpleHash.ComputeHash(request.Password, SiteSettings.HashAlgorithm, null) : oldData.HashPassword; if (request.IsUpdateProfile) { beUser.RoleId = oldData.RoleId; beUser.Type = oldData.Type; beUser.Code = oldData.Code; beUser.Status = oldData.Status; } } response = r.SaveBeUser(request); response.EntityId = beUser.Id; _cacheHelper.ClearGetBeUser(beUser.Id); return response; }); }