diff --git a/src/EasyFrameWork/Modules/MutiLanguage/LanguageService.cs b/src/EasyFrameWork/Modules/MutiLanguage/LanguageService.cs index 632e9fc05..2e2e44324 100644 --- a/src/EasyFrameWork/Modules/MutiLanguage/LanguageService.cs +++ b/src/EasyFrameWork/Modules/MutiLanguage/LanguageService.cs @@ -117,7 +117,7 @@ private ServiceResult Save(LanguageEntity item) } catch (Exception ex) { - result.AddRuleViolation(ex.Message); + result.AddError(ex.Message); } return result; } diff --git a/src/EasyFrameWork/Modules/Role/RoleService.cs b/src/EasyFrameWork/Modules/Role/RoleService.cs index d697ec1f1..39e20362b 100644 --- a/src/EasyFrameWork/Modules/Role/RoleService.cs +++ b/src/EasyFrameWork/Modules/Role/RoleService.cs @@ -37,7 +37,7 @@ public ServiceResult Add(RoleEntity roleEntity, List { var result = Add(roleEntity); - if (result.HasViolation) return result; + if (result.HasError) return result; var permissions = new List(); permissionDescriptors.Where(m => m.Checked ?? false).Each(m => { diff --git a/src/EasyFrameWork/Modules/User/Service/UserService.cs b/src/EasyFrameWork/Modules/User/Service/UserService.cs index 953ae51eb..cdc207844 100644 --- a/src/EasyFrameWork/Modules/User/Service/UserService.cs +++ b/src/EasyFrameWork/Modules/User/Service/UserService.cs @@ -82,7 +82,7 @@ public override ServiceResult Add(UserEntity item) throw new Exception(_localize.Get("{0} is already exists").FormatWith(item.Email)); } var result = base.Add(item); - if (!result.HasViolation) + if (!result.HasError) { if (item.Roles != null) { diff --git a/src/EasyFrameWork/Mvc/Controllers/BasicController.cs b/src/EasyFrameWork/Mvc/Controllers/BasicController.cs index f0b802306..21793ac81 100644 --- a/src/EasyFrameWork/Mvc/Controllers/BasicController.cs +++ b/src/EasyFrameWork/Mvc/Controllers/BasicController.cs @@ -50,11 +50,11 @@ public virtual IActionResult Create(TEntity entity) if (ModelState.IsValid) { var result = Service.Add(entity); - if (result.HasViolation) + if (result.HasError) { foreach (var item in result.RuleViolations) { - ModelState.AddModelError(item.ParameterName, item.ErrorMessage); + ModelState.AddModelError(item.ParameterName, item.Message); } return View(entity); } @@ -92,11 +92,11 @@ public virtual IActionResult Edit(TEntity entity) if (ModelState.IsValid) { var result = Service.Update(entity); - if (result.HasViolation) + if (result.HasError) { foreach (var item in result.RuleViolations) { - ModelState.AddModelError(item.ParameterName, item.ErrorMessage); + ModelState.AddModelError(item.ParameterName, item.Message); } return View(entity); } diff --git a/src/EasyFrameWork/RepositoryPattern/Error.cs b/src/EasyFrameWork/RepositoryPattern/Error.cs new file mode 100644 index 000000000..0051d51ee --- /dev/null +++ b/src/EasyFrameWork/RepositoryPattern/Error.cs @@ -0,0 +1,23 @@ +/* http://www.zkea.net/ + * Copyright (c) ZKEASOFT. All rights reserved. + * http://www.zkea.net/licenses */ + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Easy.RepositoryPattern +{ + public class Error : Violation + { + public Error(string message) : base(string.Empty, message) + { + } + + public Error(string parameterName, string message) : base(parameterName, message) + { + } + } +} diff --git a/src/EasyFrameWork/RepositoryPattern/Info.cs b/src/EasyFrameWork/RepositoryPattern/Info.cs new file mode 100644 index 000000000..54d2fa38e --- /dev/null +++ b/src/EasyFrameWork/RepositoryPattern/Info.cs @@ -0,0 +1,22 @@ +/* http://www.zkea.net/ + * Copyright (c) ZKEASOFT. All rights reserved. + * http://www.zkea.net/licenses */ + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Easy.RepositoryPattern +{ + public class Info : Violation + { + public Info(string message) : base(string.Empty, message) + { + } + public Info(string parameterName, string message) : base(parameterName, message) + { + } + } +} diff --git a/src/EasyFrameWork/RepositoryPattern/ServiceBase.cs b/src/EasyFrameWork/RepositoryPattern/ServiceBase.cs index e00ac1584..ad6d6a703 100644 --- a/src/EasyFrameWork/RepositoryPattern/ServiceBase.cs +++ b/src/EasyFrameWork/RepositoryPattern/ServiceBase.cs @@ -72,7 +72,7 @@ public TEntity BeginTransaction(Func action) try { var result = action.Invoke(); - if (result is ServiceResult sResult && sResult.HasViolation) + if (result is ServiceResult sResult && sResult.HasError) { transaction.Rollback(); } @@ -112,7 +112,7 @@ protected virtual ServiceResult Validate(T item) { if (!v.Validate(p.GetValue(item))) { - serviceResult.RuleViolations.Add(new RuleViolation(p.Name, v.ErrorMessage)); + serviceResult.AddError(p.Name, v.ErrorMessage); } }); } @@ -126,7 +126,7 @@ protected virtual ServiceResult Validate(T item) public virtual ServiceResult Add(T item) { var result = Validate(item); - if (result.HasViolation) + if (result.HasError) { return result; } @@ -154,7 +154,7 @@ public virtual ServiceResult AddRange(params T[] items) foreach (var item in items) { var itemResult = Validate(item); - if (itemResult.HasViolation) + if (itemResult.HasError) { return itemResult; } @@ -287,7 +287,7 @@ public virtual async Task CountAsync(Expression> filter) public virtual ServiceResult Update(T item) { var result = Validate(item); - if (result.HasViolation) + if (result.HasError) { return result; } @@ -310,7 +310,7 @@ public virtual ServiceResult UpdateRange(params T[] items) foreach (var item in items) { var itemResult = Validate(item); - if (itemResult.HasViolation) + if (itemResult.HasError) { return itemResult; } diff --git a/src/EasyFrameWork/RepositoryPattern/ServiceResult.cs b/src/EasyFrameWork/RepositoryPattern/ServiceResult.cs index 155343d8d..970c49d7d 100644 --- a/src/EasyFrameWork/RepositoryPattern/ServiceResult.cs +++ b/src/EasyFrameWork/RepositoryPattern/ServiceResult.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Text; namespace Easy.RepositoryPattern @@ -12,43 +13,148 @@ public class ServiceResult { public ServiceResult() { - RuleViolations = new List(); + RuleViolations = new List(); } - public List RuleViolations + public List RuleViolations { get; private set; } - public void AddRuleViolation(string message) + public void AddError(string message) { - RuleViolations.Add(new RuleViolation(string.Empty, message)); + RuleViolations.Add(new Error(string.Empty, message)); } - public void AddRuleViolation(string name, string message) + public void AddError(string name, string message) { - RuleViolations.Add(new RuleViolation(name, message)); + RuleViolations.Add(new Error(name, message)); } - public bool HasViolation + public void AddWarning(string message) { - get { return RuleViolations != null && RuleViolations.Count > 0; } + RuleViolations.Add(new Warning(string.Empty, message)); + } + public void AddWarning(string name, string message) + { + RuleViolations.Add(new Warning(name, message)); + } + public void AddInfo(string message) + { + RuleViolations.Add(new Info(string.Empty, message)); + } + public void AddInfo(string name, string message) + { + RuleViolations.Add(new Info(name, message)); + } + public bool HasError + { + get { return RuleViolations.Any(m => m is Error); } + } + + public IEnumerable Errors + { + get + { + foreach (var item in RuleViolations) + { + if (item is Error error) + { + yield return error; + } + } + } } public string ErrorMessage { get { - string msg = string.Empty; - if (RuleViolations != null && RuleViolations.Count > 0) + var msg = new StringBuilder(); + + foreach (Violation item in Errors) + { + if (item is Error) + { + msg.AppendLine(item.Message); + } + } + return msg.ToString(); + } + } + + public IEnumerable Warnings + { + get + { + foreach (var item in RuleViolations) + { + if (item is Warning warning) + { + yield return warning; + } + } + } + } + public string WarningMessage + { + get + { + var msg = new StringBuilder(); + foreach (Violation item in RuleViolations) + { + if (item is Warning) + { + msg.AppendLine(item.Message); + } + } + return msg.ToString(); + } + } + public IEnumerable Infos + { + get + { + foreach (var item in RuleViolations) + { + if (item is Info info) + { + yield return info; + } + } + } + } + public string InfoMessage + { + get + { + var msg = new StringBuilder(); + foreach (Violation item in RuleViolations) { - foreach (RuleViolation item in RuleViolations) + if (item is Info) { - if (msg != string.Empty) - msg += "\r\n"; - msg += item.ErrorMessage; + msg.AppendLine(item.Message); } } - return msg; + return msg.ToString(); } } + + public static implicit operator ServiceResult(Error error) + { + var result = new ServiceResult(); + result.RuleViolations.Add(error); + return result; + } + public static implicit operator ServiceResult(Warning warning) + { + var result = new ServiceResult(); + result.RuleViolations.Add(warning); + return result; + } + public static implicit operator ServiceResult(Info info) + { + var result = new ServiceResult(); + result.RuleViolations.Add(info); + return result; + } } public class ServiceResult : ServiceResult { @@ -57,10 +163,29 @@ public ServiceResult(T result) { Result = result; } + public T Result { get; set; } + public static implicit operator ServiceResult(T result) { return new ServiceResult(result); } - public T Result { get; set; } + public static implicit operator ServiceResult(Error error) + { + var result = new ServiceResult(); + result.RuleViolations.Add(error); + return result; + } + public static implicit operator ServiceResult(Warning warning) + { + var result = new ServiceResult(); + result.RuleViolations.Add(warning); + return result; + } + public static implicit operator ServiceResult(Info info) + { + var result = new ServiceResult(); + result.RuleViolations.Add(info); + return result; + } } } diff --git a/src/EasyFrameWork/RepositoryPattern/RuleViolation.cs b/src/EasyFrameWork/RepositoryPattern/Violation.cs similarity index 71% rename from src/EasyFrameWork/RepositoryPattern/RuleViolation.cs rename to src/EasyFrameWork/RepositoryPattern/Violation.cs index ddfa418c5..a911386d2 100644 --- a/src/EasyFrameWork/RepositoryPattern/RuleViolation.cs +++ b/src/EasyFrameWork/RepositoryPattern/Violation.cs @@ -8,19 +8,19 @@ namespace Easy.RepositoryPattern { - public class RuleViolation + public abstract class Violation { - public RuleViolation(string parameterName, string errorMessage) + public Violation(string parameterName, string message) { ParameterName = parameterName; - ErrorMessage = errorMessage; + Message = message; } public string ParameterName { get; private set; } - public string ErrorMessage + public string Message { get; private set; diff --git a/src/EasyFrameWork/RepositoryPattern/Warning.cs b/src/EasyFrameWork/RepositoryPattern/Warning.cs new file mode 100644 index 000000000..4114da8c5 --- /dev/null +++ b/src/EasyFrameWork/RepositoryPattern/Warning.cs @@ -0,0 +1,22 @@ +/* http://www.zkea.net/ + * Copyright (c) ZKEASOFT. All rights reserved. + * http://www.zkea.net/licenses */ + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Easy.RepositoryPattern +{ + public class Warning : Violation + { + public Warning(string message) : base(string.Empty, message) + { + } + public Warning(string parameterName, string message) : base(parameterName, message) + { + } + } +} diff --git a/src/ZKEACMS.Article/Service/ArticleApiService.cs b/src/ZKEACMS.Article/Service/ArticleApiService.cs index 73b1b1906..e80406eaf 100644 --- a/src/ZKEACMS.Article/Service/ArticleApiService.cs +++ b/src/ZKEACMS.Article/Service/ArticleApiService.cs @@ -58,7 +58,7 @@ public ArticleEntity GetByName(string name) public ServiceResult Create(ArticleEntity article) { var validResult = ValidArticleType(article); - if (validResult.HasViolation) return validResult; + if (validResult.HasError) return validResult; article.ArticleContent = _htmlSanitizer.Sanitize(article.ArticleContent); return _articleService.Add(article); @@ -67,7 +67,7 @@ public ServiceResult Create(ArticleEntity article) public ServiceResult Update(ArticleEntity article) { var validResult = ValidArticleType(article); - if (validResult.HasViolation) return validResult; + if (validResult.HasError) return validResult; article.ArticleContent = _htmlSanitizer.Sanitize(article.ArticleContent); return _articleService.Update(article); @@ -79,7 +79,7 @@ private ServiceResult ValidArticleType(ArticleEntity article) ArticleType articleType = _articleTypeService.Get(article.ArticleTypeID ?? 0); if (articleType == null) { - serviceResult.AddRuleViolation("ArticleTypeID", "Article type is not exist."); + serviceResult.AddError("ArticleTypeID", "Article type is not exist."); return serviceResult; } return serviceResult; diff --git a/src/ZKEACMS.Article/Service/ArticleService.cs b/src/ZKEACMS.Article/Service/ArticleService.cs index f5d678363..400fc1f88 100644 --- a/src/ZKEACMS.Article/Service/ArticleService.cs +++ b/src/ZKEACMS.Article/Service/ArticleService.cs @@ -31,18 +31,13 @@ public ArticleService(IApplicationContext applicationContext, public override ServiceResult Add(ArticleEntity item) { ServiceResult result; - if (item.Url.IsNotNullAndWhiteSpace()) + if (item.Url.IsNotNullAndWhiteSpace() && GetByUrl(item.Url) != null) { - if (GetByUrl(item.Url) != null) - { - result = new ServiceResult(); - result.RuleViolations.Add(new RuleViolation("Url", _localize.Get("URL already exists"))); - return result; - } + return new Error("Url", _localize.Get("URL already exists")); } _eventManager.Trigger(Events.OnArticleAdding, item); result = base.Add(item); - if (!result.HasViolation) + if (!result.HasError) { _eventManager.Trigger(Events.OnArticleAdded, item); } @@ -51,18 +46,13 @@ public override ServiceResult Add(ArticleEntity item) public override ServiceResult Update(ArticleEntity item) { ServiceResult result; - if (item.Url.IsNotNullAndWhiteSpace()) + if (item.Url.IsNotNullAndWhiteSpace() && Count(m => m.Url == item.Url && m.ID != item.ID) > 0) { - if (Count(m => m.Url == item.Url && m.ID != item.ID) > 0) - { - result = new ServiceResult(); - result.RuleViolations.Add(new RuleViolation("Url", _localize.Get("URL already exists"))); - return result; - } + return new Error("Url", _localize.Get("URL already exists")); } _eventManager.Trigger(Events.OnArticleUpdating, item); result = base.Update(item); - if (!result.HasViolation) + if (!result.HasError) { _eventManager.Trigger(Events.OnArticleUpdated, item); } diff --git a/src/ZKEACMS.Article/Service/ArticleTypeApiService.cs b/src/ZKEACMS.Article/Service/ArticleTypeApiService.cs index 8b5353908..18925520a 100644 --- a/src/ZKEACMS.Article/Service/ArticleTypeApiService.cs +++ b/src/ZKEACMS.Article/Service/ArticleTypeApiService.cs @@ -24,7 +24,7 @@ public ArticleTypeApiService(IArticleTypeService articleTypeService) public ServiceResult Create(ArticleType articleType) { var validResult= ValidParentId(articleType); - if (validResult.HasViolation) return validResult; + if (validResult.HasError) return validResult; return _articleTypeService.Add(articleType); } @@ -36,7 +36,7 @@ private ServiceResult ValidParentId(ArticleType articleType) var parentArticleType = _articleTypeService.Get(articleType.ParentID.Value); if (parentArticleType == null) { - result.AddRuleViolation("ParentID", "Parent is not exist"); + result.AddError("ParentID", "Parent is not exist"); } } return result; @@ -59,7 +59,7 @@ public ArticleType GetByName(string name) public ServiceResult Update(ArticleType articleType) { var validResult = ValidParentId(articleType); - if (validResult.HasViolation) return validResult; + if (validResult.HasError) return validResult; return _articleTypeService.Update(articleType); } diff --git a/src/ZKEACMS.Article/Service/ArticleTypeService.cs b/src/ZKEACMS.Article/Service/ArticleTypeService.cs index 8063f0d1a..47ac698c7 100644 --- a/src/ZKEACMS.Article/Service/ArticleTypeService.cs +++ b/src/ZKEACMS.Article/Service/ArticleTypeService.cs @@ -27,27 +27,17 @@ public ArticleTypeService(IApplicationContext applicationContext, IArticleServic public override ServiceResult Add(ArticleType item) { item.ParentID = item.ParentID ?? 0; - if (item.Url.IsNotNullAndWhiteSpace()) + if (item.Url.IsNotNullAndWhiteSpace()&& GetByUrl(item.Url) != null) { - if (GetByUrl(item.Url) != null) - { - var result = new ServiceResult(); - result.RuleViolations.Add(new RuleViolation("Url", _localize.Get("URL already exists"))); - return result; - } + return new Error("Url", _localize.Get("URL already exists")); } return base.Add(item); } public override ServiceResult Update(ArticleType item) { - if (item.Url.IsNotNullAndWhiteSpace()) + if (item.Url.IsNotNullAndWhiteSpace() && Count(m => m.Url == item.Url && m.ID != item.ID) > 0) { - if (Count(m => m.Url == item.Url && m.ID != item.ID) > 0) - { - var result = new ServiceResult(); - result.RuleViolations.Add(new RuleViolation("Url", _localize.Get("URL already exists"))); - return result; - } + return new Error("Url", _localize.Get("URL already exists")); } return base.Update(item); } diff --git a/src/ZKEACMS.EventAction/ActionExecutor/ActionContent.cs b/src/ZKEACMS.EventAction/ActionExecutor/ActionContent.cs index 34bda493e..e69e29400 100644 --- a/src/ZKEACMS.EventAction/ActionExecutor/ActionContent.cs +++ b/src/ZKEACMS.EventAction/ActionExecutor/ActionContent.cs @@ -62,7 +62,7 @@ public ServiceResult ParseTemplate(string key, string template) } catch (Exception ex) { - result.AddRuleViolation("With", ex.Message); + result.AddError("With", ex.Message); } return result; } @@ -74,9 +74,9 @@ public ServiceResult ValidateWith() foreach (var item in With) { var template = ParseTemplate(item.Key, item.Value); - if (!template.HasViolation) continue; + if (!template.HasError) continue; - result.AddRuleViolation(template.ErrorMessage); + result.AddError(template.ErrorMessage); break; } return result; diff --git a/src/ZKEACMS.EventAction/Service/ActionBodyService.cs b/src/ZKEACMS.EventAction/Service/ActionBodyService.cs index 732f75595..5da23749b 100644 --- a/src/ZKEACMS.EventAction/Service/ActionBodyService.cs +++ b/src/ZKEACMS.EventAction/Service/ActionBodyService.cs @@ -35,7 +35,7 @@ public override ServiceResult Add(ActionBody item) if (item.Body.IsNotNullAndWhiteSpace() && !FluidTemplateHelper.TryParse(item.Body, out templateResult, out var error)) { var result = new ServiceResult(); - result.AddRuleViolation("Body", error); + result.AddError("Body", error); return result; } var addResult = base.Add(item); @@ -52,7 +52,7 @@ public override ServiceResult Update(ActionBody item) if (item.Body.IsNotNullAndWhiteSpace() && !FluidTemplateHelper.TryParse(item.Body, out templateResult, out var error)) { var result = new ServiceResult(); - result.AddRuleViolation("Body", error); + result.AddError("Body", error); return result; } if (templateResult != null) @@ -87,7 +87,7 @@ private ServiceResult ParseTemplate(int ID) catch (RecordNotFoundException) { } catch (Exception ex) { - result.AddRuleViolation(ex.Message); + result.AddError(ex.Message); } return result; diff --git a/src/ZKEACMS.EventAction/Service/EventActionService.cs b/src/ZKEACMS.EventAction/Service/EventActionService.cs index c31c6e0f3..71a50725f 100644 --- a/src/ZKEACMS.EventAction/Service/EventActionService.cs +++ b/src/ZKEACMS.EventAction/Service/EventActionService.cs @@ -47,7 +47,7 @@ public EventActionService(IApplicationContext applicationContext, CMSDbContext d public override ServiceResult Update(Models.EventAction item) { var result = ValidateActions(item); - if (result.HasViolation) return result; + if (result.HasError) return result; _signals.Trigger(EventActionChanged); return base.Update(item); @@ -55,7 +55,7 @@ public EventActionService(IApplicationContext applicationContext, CMSDbContext d public override ServiceResult Add(Models.EventAction item) { var result = ValidateActions(item); - if (result.HasViolation) return result; + if (result.HasError) return result; _signals.Trigger(EventActionChanged); return base.Add(item); @@ -106,20 +106,20 @@ public Dictionary> GetAllActivedActinosFromCach { if (!ExecutorManager.IsExecutorRegisted(action.Uses)) { - result.AddRuleViolation("Actions", _localize.Get("{0} is not available").FormatWith(action.Uses)); + result.AddError("Actions", _localize.Get("{0} is not available").FormatWith(action.Uses)); break; } var validateResult = action.ValidateWith(); - if (validateResult.HasViolation) + if (validateResult.HasError) { - result.AddRuleViolation("Actions", validateResult.ErrorMessage); + result.AddError("Actions", validateResult.ErrorMessage); break; } } } catch (Exception ex) { - result.AddRuleViolation("Actions", ex.Message); + result.AddError("Actions", ex.Message); } return result; } diff --git a/src/ZKEACMS.FormGenerator/Controllers/FormDataController.cs b/src/ZKEACMS.FormGenerator/Controllers/FormDataController.cs index 3f54aa4de..9151bf173 100644 --- a/src/ZKEACMS.FormGenerator/Controllers/FormDataController.cs +++ b/src/ZKEACMS.FormGenerator/Controllers/FormDataController.cs @@ -38,7 +38,7 @@ public IActionResult Submit(string FormId) var result = Service.SaveForm(Request.Form, FormId); ModelState.Merge(result); - if (!result.HasViolation) + if (!result.HasError) { TempData["Message"] = _localize.Get("Form have submited"); } diff --git a/src/ZKEACMS.FormGenerator/Service/FormDataService.cs b/src/ZKEACMS.FormGenerator/Service/FormDataService.cs index f81b9ab11..a83fd9c05 100644 --- a/src/ZKEACMS.FormGenerator/Service/FormDataService.cs +++ b/src/ZKEACMS.FormGenerator/Service/FormDataService.cs @@ -43,7 +43,7 @@ public FormDataService(IApplicationContext applicationContext, public override ServiceResult Add(FormData item) { var result = base.Add(item); - if (result.HasViolation) + if (result.HasError) { return result; } @@ -107,8 +107,7 @@ public ServiceResult SaveForm(IFormCollection formCollection, string f var form = _formService.Get(formId); if (form == null) { - result.RuleViolations.Add(new RuleViolation("Form", "Form not found!")); - return result; + return new Error("Form", "Form not found!"); } var formData = new FormData { FormId = formId, Datas = new List(), Form = form }; @@ -136,7 +135,7 @@ public ServiceResult SaveForm(IFormCollection formCollection, string f { if (!validator.Validate(field, dataitem, out string message)) { - result.RuleViolations.Add(new RuleViolation(item, message)); + result.AddError(item, message); } } formData.Datas.Add(dataitem); @@ -144,7 +143,7 @@ public ServiceResult SaveForm(IFormCollection formCollection, string f } MergeDataToForm(formData); result.Result = formData; - if (result.HasViolation) + if (result.HasError) { return result; } @@ -153,7 +152,7 @@ public ServiceResult SaveForm(IFormCollection formCollection, string f formData.Title = formData.Datas.FirstOrDefault().FieldValue; } result = Add(formData); - if (!result.HasViolation) + if (!result.HasError) { FormData data = Get(formData.ID); _eventManager.Trigger(Events.OnFormDataSubmitted, data); diff --git a/src/ZKEACMS.Message/Controllers/MessageHandleController.cs b/src/ZKEACMS.Message/Controllers/MessageHandleController.cs index 2aaf92b4c..59cf3e5dc 100644 --- a/src/ZKEACMS.Message/Controllers/MessageHandleController.cs +++ b/src/ZKEACMS.Message/Controllers/MessageHandleController.cs @@ -46,7 +46,7 @@ public IActionResult PostMessage(MessageEntity entity) entity.ActionType = ActionType.Continue;//Use continue to send notification. var result = _messageService.Add(entity); ModelState.Merge(result); - if (!result.HasViolation) + if (!result.HasError) { TempData["Message"] = _localize.Get("Thank You for your submit!"); } diff --git a/src/ZKEACMS.Message/Service/CommentsService.cs b/src/ZKEACMS.Message/Service/CommentsService.cs index 1e9c27f9f..ebcbbce43 100644 --- a/src/ZKEACMS.Message/Service/CommentsService.cs +++ b/src/ZKEACMS.Message/Service/CommentsService.cs @@ -24,7 +24,7 @@ public CommentsService(IApplicationContext applicationContext, CMSDbContext dbCo public override ServiceResult Add(Comments item) { ServiceResult result = base.Add(item); - if (!result.HasViolation) + if (!result.HasError) { _eventManager.Trigger(Events.OnCommentsSubmitted, item); } diff --git a/src/ZKEACMS.Message/Service/MessageService.cs b/src/ZKEACMS.Message/Service/MessageService.cs index a8c1d9498..1adaae001 100644 --- a/src/ZKEACMS.Message/Service/MessageService.cs +++ b/src/ZKEACMS.Message/Service/MessageService.cs @@ -25,7 +25,7 @@ public MessageService(IApplicationContext applicationContext, CMSDbContext dbCon public override ServiceResult Add(MessageEntity item) { ServiceResult result = base.Add(item); - if (!result.HasViolation && item.ActionType == ActionType.Continue) + if (!result.HasError && item.ActionType == ActionType.Continue) { _eventManager.Trigger(Events.OnMessageSubmitted, item); } diff --git a/src/ZKEACMS.Product/Service/ProductApiService.cs b/src/ZKEACMS.Product/Service/ProductApiService.cs index a477cc705..84f829492 100644 --- a/src/ZKEACMS.Product/Service/ProductApiService.cs +++ b/src/ZKEACMS.Product/Service/ProductApiService.cs @@ -60,7 +60,7 @@ public ProductEntity GetByName(string name) public ServiceResult Create(ProductEntity product) { var validResult = ValidProductType(product); - if (validResult.HasViolation) return validResult; + if (validResult.HasError) return validResult; if (product.ProductImages != null) { @@ -76,7 +76,7 @@ public ServiceResult Create(ProductEntity product) public ServiceResult Update(ProductEntity product) { var validResult = ValidProductType(product); - if (validResult.HasViolation) return validResult; + if (validResult.HasError) return validResult; if (product.ProductImages != null) { @@ -98,7 +98,7 @@ private ServiceResult ValidProductType(ProductEntity product) ProductCategory productType = _productCategoryService.Get(product.ProductCategoryID); if (productType == null) { - serviceResult.AddRuleViolation("ProductTypeID", "Product type is not exist."); + serviceResult.AddError("ProductTypeID", "Product type is not exist."); return serviceResult; } return serviceResult; diff --git a/src/ZKEACMS.Product/Service/ProductCategoryApiService.cs b/src/ZKEACMS.Product/Service/ProductCategoryApiService.cs index 248c70b35..51d20e4cd 100644 --- a/src/ZKEACMS.Product/Service/ProductCategoryApiService.cs +++ b/src/ZKEACMS.Product/Service/ProductCategoryApiService.cs @@ -24,7 +24,7 @@ public ProductCategoryApiService(IProductCategoryService productCategoryService) public ServiceResult Create(ProductCategory productCategory) { var validResult = ValidParentId(productCategory); - if (validResult.HasViolation) return validResult; + if (validResult.HasError) return validResult; return _productCategoryService.Add(productCategory); } @@ -36,7 +36,7 @@ private ServiceResult ValidParentId(ProductCategory productCate var parentProductCategory = _productCategoryService.Get(productCategory.ParentID); if (parentProductCategory == null) { - result.AddRuleViolation("ParentID", "Parent is not exist"); + result.AddError("ParentID", "Parent is not exist"); } } return result; @@ -59,7 +59,7 @@ public ProductCategory GetByName(string name) public ServiceResult Update(ProductCategory productCategory) { var validResult = ValidParentId(productCategory); - if (validResult.HasViolation) return validResult; + if (validResult.HasError) return validResult; return _productCategoryService.Update(productCategory); } diff --git a/src/ZKEACMS.Product/Service/ProductCategoryService.cs b/src/ZKEACMS.Product/Service/ProductCategoryService.cs index 51a66926c..41ff1319a 100644 --- a/src/ZKEACMS.Product/Service/ProductCategoryService.cs +++ b/src/ZKEACMS.Product/Service/ProductCategoryService.cs @@ -18,9 +18,9 @@ public class ProductCategoryService : ServiceBase private readonly IProductService _productService; private readonly IProductCategoryTagService _productCategoryTagService; private readonly ILocalize _localize; - public ProductCategoryService(IProductService productService, + public ProductCategoryService(IProductService productService, IApplicationContext applicationContext, - IProductCategoryTagService productCategoryTagService, + IProductCategoryTagService productCategoryTagService, ILocalize localize, CMSDbContext dbContext) : base(applicationContext, dbContext) @@ -31,27 +31,17 @@ public ProductCategoryService(IProductService productService, } public override ServiceResult Add(ProductCategory item) { - if (item.Url.IsNotNullAndWhiteSpace()) + if (item.Url.IsNotNullAndWhiteSpace() && GetByUrl(item.Url) != null) { - if (GetByUrl(item.Url) != null) - { - var result = new ServiceResult(); - result.RuleViolations.Add(new RuleViolation("Url", _localize.Get("URL already exists"))); - return result; - } + return new Error("Url", _localize.Get("URL already exists")); } return base.Add(item); } public override ServiceResult Update(ProductCategory item) { - if (item.Url.IsNotNullAndWhiteSpace()) + if (item.Url.IsNotNullAndWhiteSpace() && Count(m => m.Url == item.Url && m.ID != item.ID) > 0) { - if (Count(m => m.Url == item.Url && m.ID != item.ID) > 0) - { - var result = new ServiceResult(); - result.RuleViolations.Add(new RuleViolation("Url", _localize.Get("URL already exists"))); - return result; - } + return new Error("Url", _localize.Get("URL already exists")); } return base.Update(item); } diff --git a/src/ZKEACMS.Product/Service/ProductService.cs b/src/ZKEACMS.Product/Service/ProductService.cs index 946859965..62a18388b 100644 --- a/src/ZKEACMS.Product/Service/ProductService.cs +++ b/src/ZKEACMS.Product/Service/ProductService.cs @@ -45,19 +45,15 @@ public void Publish(int ID) public override ServiceResult Add(ProductEntity item) { ServiceResult result = new ServiceResult(); - if (item.Url.IsNotNullAndWhiteSpace()) + if (item.Url.IsNotNullAndWhiteSpace() && GetByUrl(item.Url) != null) { - if (GetByUrl(item.Url) != null) - { - result.RuleViolations.Add(new RuleViolation("Url", _localize.Get("URL already exists"))); - return result; - } + return new Error("Url", _localize.Get("URL already exists")); } _eventManager.Trigger(Events.OnProductAdding, item); BeginTransaction(() => { result = base.Add(item); - if (!result.HasViolation) + if (!result.HasError) { if (item.ProductTags != null) { @@ -106,19 +102,15 @@ private void SaveImages(ProductImage item) public override ServiceResult Update(ProductEntity item) { ServiceResult result = new ServiceResult(); - if (item.Url.IsNotNullAndWhiteSpace()) + if (item.Url.IsNotNullAndWhiteSpace() && Count(m => m.Url == item.Url && m.ID != item.ID) > 0) { - if (Count(m => m.Url == item.Url && m.ID != item.ID) > 0) - { - result.RuleViolations.Add(new RuleViolation("Url", _localize.Get("URL already exists"))); - return result; - } + return new Error("Url", _localize.Get("URL already exists")); } _eventManager.Trigger(Events.OnProductUpdating, item); BeginTransaction(() => { result = base.Update(item); - if (!result.HasViolation) + if (!result.HasError) { if (item.ProductTags != null) { diff --git a/src/ZKEACMS.Redirection/Service/UrlRedirectService.cs b/src/ZKEACMS.Redirection/Service/UrlRedirectService.cs index 809e94a35..a0091932e 100644 --- a/src/ZKEACMS.Redirection/Service/UrlRedirectService.cs +++ b/src/ZKEACMS.Redirection/Service/UrlRedirectService.cs @@ -38,7 +38,7 @@ private ServiceResult PatternTest(UrlRedirect item) } catch (Exception ex) { - exceptionResult.AddRuleViolation("InComingUrl", ex.Message); + exceptionResult.AddError("InComingUrl", ex.Message); } } @@ -65,7 +65,7 @@ private ServiceResult LoopTest(UrlRedirect item) if (direction.Contains(url)) { direction.Add(url); - result.AddRuleViolation("DestinationURL", string.Join(" > ", direction)); + result.AddError("DestinationURL", string.Join(" > ", direction)); break; } else @@ -79,7 +79,7 @@ private ServiceResult LoopTest(UrlRedirect item) private ServiceResult Valid(UrlRedirect item) { ServiceResult result = PatternTest(item); - if (result.HasViolation) + if (result.HasError) { return result; } @@ -92,13 +92,13 @@ public override IQueryable Get() public override ServiceResult Add(UrlRedirect item) { ServiceResult validResult = Valid(item); - if (validResult.HasViolation) + if (validResult.HasError) { return validResult; } ServiceResult result = base.Add(item); - if (!result.HasViolation) + if (!result.HasError) { RemoveCache(); } @@ -107,7 +107,7 @@ public override ServiceResult Add(UrlRedirect item) public override ServiceResult AddRange(params UrlRedirect[] items) { ServiceResult result = base.AddRange(items); - if (!result.HasViolation) + if (!result.HasError) { RemoveCache(); } @@ -117,12 +117,12 @@ public override ServiceResult AddRange(params UrlRedirect[] items) public override ServiceResult Update(UrlRedirect item) { ServiceResult validResult = Valid(item); - if (validResult.HasViolation) + if (validResult.HasError) { return validResult; } ServiceResult result = base.Update(item); - if (!result.HasViolation) + if (!result.HasError) { RemoveCache(); } @@ -131,7 +131,7 @@ public override ServiceResult Update(UrlRedirect item) public override ServiceResult UpdateRange(params UrlRedirect[] items) { ServiceResult result = base.UpdateRange(items); - if (!result.HasViolation) + if (!result.HasError) { RemoveCache(); } diff --git a/src/ZKEACMS.SectionWidget/Service/SectionGroupService.cs b/src/ZKEACMS.SectionWidget/Service/SectionGroupService.cs index 0af696851..954e505bc 100644 --- a/src/ZKEACMS.SectionWidget/Service/SectionGroupService.cs +++ b/src/ZKEACMS.SectionWidget/Service/SectionGroupService.cs @@ -81,7 +81,7 @@ public override ServiceResult Add(SectionGroup item) { item.ID = Guid.NewGuid().ToString("N"); var result = base.Add(item); - if (result.HasViolation) + if (result.HasError) { return result; } diff --git a/src/ZKEACMS.SectionWidget/Service/SectionWidgetService.cs b/src/ZKEACMS.SectionWidget/Service/SectionWidgetService.cs index d4899b13d..5c884e809 100644 --- a/src/ZKEACMS.SectionWidget/Service/SectionWidgetService.cs +++ b/src/ZKEACMS.SectionWidget/Service/SectionWidgetService.cs @@ -87,7 +87,7 @@ public override void Remove(Models.SectionWidget item) public override ServiceResult Add(Models.SectionWidget item) { var result = base.Add(item); - if (result.HasViolation) + if (result.HasError) { return result; } diff --git a/src/ZKEACMS.Shop/Service/AliPaymentService.cs b/src/ZKEACMS.Shop/Service/AliPaymentService.cs index 737c617ba..860b87435 100644 --- a/src/ZKEACMS.Shop/Service/AliPaymentService.cs +++ b/src/ZKEACMS.Shop/Service/AliPaymentService.cs @@ -69,7 +69,7 @@ public ServiceResult CloseOrder(Order order) }; if (!status) { - result.RuleViolations.Add(new RuleViolation("Error", responseEntry.msg + (responseEntry.sub_msg ?? ""))); + result.AddError("Error", responseEntry.msg + (responseEntry.sub_msg ?? "")); } return result; } @@ -138,7 +138,7 @@ public ServiceResult Refund(Order order) }; if (!status) { - result.RuleViolations.Add(new RuleViolation("Error", responseEntry.msg + (responseEntry.sub_msg ?? ""))); + result.AddError("Error", responseEntry.msg + (responseEntry.sub_msg ?? "")); } return result; } diff --git a/src/ZKEACMS.Shop/Service/OrderService.cs b/src/ZKEACMS.Shop/Service/OrderService.cs index aa4ecfd81..a54f01cc1 100644 --- a/src/ZKEACMS.Shop/Service/OrderService.cs +++ b/src/ZKEACMS.Shop/Service/OrderService.cs @@ -46,7 +46,7 @@ public override ServiceResult Add(Order item) item.ID = Guid.NewGuid().ToString("N"); item.OrderStatus = (int)OrderStatus.UnPaid; ServiceResult result = base.Add(item); - if (!result.HasViolation) + if (!result.HasError) { foreach (var orderItem in item.OrderItems) { @@ -83,7 +83,7 @@ public ServiceResult CloseOrder(string orderId) { Result = false }; - result.RuleViolations.Add(new RuleViolation("Error", _localize.Get("Only unpaid order can be closed!"))); + result.AddError("Error", _localize.Get("Only unpaid order can be closed!")); return result; } @@ -139,15 +139,15 @@ public ServiceResult Refund(string orderId, decimal amount, string reason) }; if (order.PaymentID.IsNullOrEmpty()) { - failed.RuleViolations.Add(new RuleViolation("Error", _localize.Get("Unpaid order"))); + failed.AddError("Error", _localize.Get("Unpaid order")); } if (order.RefundID.IsNotNullAndWhiteSpace()) { - failed.RuleViolations.Add(new RuleViolation("Error", _localize.Get("Refunded"))); + failed.AddError("Error", _localize.Get("Refunded")); } if (amount > order.Total) { - failed.RuleViolations.Add(new RuleViolation("Error", _localize.Get("Refund amount exceeds the amount of the order"))); + failed.AddError("Error", _localize.Get("Refund amount exceeds the amount of the order")); } return failed; } diff --git a/src/ZKEACMS.TemplateImporter/Service/TemplateImporterService.cs b/src/ZKEACMS.TemplateImporter/Service/TemplateImporterService.cs index 02278356e..7b077fd6c 100644 --- a/src/ZKEACMS.TemplateImporter/Service/TemplateImporterService.cs +++ b/src/ZKEACMS.TemplateImporter/Service/TemplateImporterService.cs @@ -158,7 +158,7 @@ public ThemeEntity Import(string zipFile) page.DisplayOrder = ++index; } var addPageResult = _pageService.Add(page); - if (addPageResult.HasViolation) + if (addPageResult.HasError) { throw new Exception(addPageResult.ErrorMessage); } diff --git a/src/ZKEACMS/Common/Service/CarouselService.cs b/src/ZKEACMS/Common/Service/CarouselService.cs index d58037276..82a855073 100644 --- a/src/ZKEACMS/Common/Service/CarouselService.cs +++ b/src/ZKEACMS/Common/Service/CarouselService.cs @@ -37,7 +37,7 @@ public override CarouselEntity Get(params object[] primaryKey) public override ServiceResult Add(CarouselEntity item) { var result = base.Add(item); - if (result.HasViolation) + if (result.HasError) { return result; } @@ -50,7 +50,7 @@ public override ServiceResult Add(CarouselEntity item) if (m.ActionType.HasFlag(ActionType.Create)) { var itemResult = _carouselItemService.Add(m); - if (itemResult.HasViolation) + if (itemResult.HasError) { result.RuleViolations.AddRange(itemResult.RuleViolations); } @@ -83,7 +83,7 @@ private void SaveCarouselItems(CarouselItemEntity item) public override ServiceResult Update(CarouselEntity item) { var result = base.Update(item); - if (result.HasViolation) + if (result.HasError) { return result; } @@ -102,7 +102,7 @@ public override ServiceResult Update(CarouselEntity item) public override ServiceResult UpdateRange(params CarouselEntity[] items) { var result = base.UpdateRange(items); - if (result.HasViolation) + if (result.HasError) { return result; } diff --git a/src/ZKEACMS/Common/Service/TemplateService.cs b/src/ZKEACMS/Common/Service/TemplateService.cs index 7b90d9a6c..2d9d545e5 100644 --- a/src/ZKEACMS/Common/Service/TemplateService.cs +++ b/src/ZKEACMS/Common/Service/TemplateService.cs @@ -132,7 +132,6 @@ public virtual ServiceResult CreateOrUpdate(TemplateFile model) { string name = model.Name; string relativePath = model.RelativePath; - ServiceResult result = new ServiceResult(); string ext = Path.GetExtension(name); if (GetSupportFileExtensions().Contains(ext)) { @@ -149,13 +148,12 @@ public virtual ServiceResult CreateOrUpdate(TemplateFile model) ExtFile.WriteFile(model.Path, model.Content); EnsureHasViewImports(model.Path); _cacheMgr.Remove(_templateFilesCacheKey); - result.Result = GetTemplateFiles().First(m => m.RelativePath == model.RelativePath); + return new ServiceResult(GetTemplateFiles().First(m => m.RelativePath == model.RelativePath)); } else { - result.RuleViolations.Add(new RuleViolation("Name", "File not support")); + return new Error("Name", "File not support"); } - return result; } public void Delete(int id) diff --git a/src/ZKEACMS/Controllers/PageController.cs b/src/ZKEACMS/Controllers/PageController.cs index 62706842a..fe3b67eea 100644 --- a/src/ZKEACMS/Controllers/PageController.cs +++ b/src/ZKEACMS/Controllers/PageController.cs @@ -149,7 +149,7 @@ public override IActionResult Edit(PageEntity entity) else if (entity.ActionType.HasFlag(ActionType.Publish)) { var result = Service.Publish(entity); - if (result.HasViolation) + if (result.HasError) { ModelState.AddUnknownError(result.ErrorMessage); return View(entity); @@ -262,7 +262,7 @@ public JsonResult Publish(string id) public IActionResult PublishPage(string ID, string ReturnUrl) { var result = Service.Publish(Service.Get(ID)); - if (result.HasViolation) + if (result.HasError) { ModelState.AddUnknownError(result.ErrorMessage); return Edit(ID); diff --git a/src/ZKEACMS/Controllers/RolesController.cs b/src/ZKEACMS/Controllers/RolesController.cs index 017e272c8..c170cfe69 100644 --- a/src/ZKEACMS/Controllers/RolesController.cs +++ b/src/ZKEACMS/Controllers/RolesController.cs @@ -30,11 +30,11 @@ public override IActionResult Create(RoleEntity entity) public IActionResult Create(RoleEntity entity, List PermissionSet) { var result = Service.Add(entity, PermissionSet); - if (result.HasViolation) + if (result.HasError) { foreach (var item in result.RuleViolations) { - ModelState.AddModelError(item.ParameterName, item.ErrorMessage); + ModelState.AddModelError(item.ParameterName, item.Message); } return View(entity); } @@ -60,12 +60,12 @@ public override IActionResult Edit(RoleEntity entity) public IActionResult Edit(RoleEntity entity, List PermissionSet) { var result = Service.Update(entity, PermissionSet); - if (result.HasViolation) + if (result.HasError) { ViewBag.Permissions = Service.GetPermission(entity.ID); foreach (var item in result.RuleViolations) { - ModelState.AddModelError(item.ParameterName, item.ErrorMessage); + ModelState.AddModelError(item.ParameterName, item.Message); } return View(entity); } diff --git a/src/ZKEACMS/Controllers/TemplateController.cs b/src/ZKEACMS/Controllers/TemplateController.cs index d13e09aee..c76aff868 100644 --- a/src/ZKEACMS/Controllers/TemplateController.cs +++ b/src/ZKEACMS/Controllers/TemplateController.cs @@ -40,7 +40,7 @@ public IActionResult Create(TemplateFile model, ActionType ActionType) if (ModelState.IsValid) { var result = _templateService.CreateOrUpdate(model); - if (result.HasViolation) + if (result.HasError) { ModelState.AddModelError("Name", result.ErrorMessage); return View(model); @@ -79,7 +79,7 @@ public IActionResult Edit(TemplateFile model, ActionType ActionType) if (ModelState.IsValid) { var result = _templateService.CreateOrUpdate(model); - if (result.HasViolation) + if (result.HasError) { ModelState.AddModelError("Name", result.ErrorMessage); return View(model); diff --git a/src/ZKEACMS/Extend/ModelStateDictionaryExtend.cs b/src/ZKEACMS/Extend/ModelStateDictionaryExtend.cs index 30f30f07e..d317cd371 100644 --- a/src/ZKEACMS/Extend/ModelStateDictionaryExtend.cs +++ b/src/ZKEACMS/Extend/ModelStateDictionaryExtend.cs @@ -18,7 +18,7 @@ public static void Merge(this ModelStateDictionary modelState, ServiceResult< { foreach (var item in serviceResult.RuleViolations) { - modelState.AddModelError(item.ParameterName, item.ErrorMessage); + modelState.AddModelError(item.ParameterName, item.Message); } } public static void AddUnknownError(this ModelStateDictionary modelState, string message) diff --git a/src/ZKEACMS/Layout/LayoutService.cs b/src/ZKEACMS/Layout/LayoutService.cs index 302230fb0..a86ed74fb 100644 --- a/src/ZKEACMS/Layout/LayoutService.cs +++ b/src/ZKEACMS/Layout/LayoutService.cs @@ -54,7 +54,7 @@ public override ServiceResult Add(LayoutEntity item) { item.ID = Guid.NewGuid().ToString("N"); var result = base.Add(item); - if (result.HasViolation) + if (result.HasError) { return result; } diff --git a/src/ZKEACMS/Page/PageService.cs b/src/ZKEACMS/Page/PageService.cs index e88726c1d..431076c6e 100644 --- a/src/ZKEACMS/Page/PageService.cs +++ b/src/ZKEACMS/Page/PageService.cs @@ -176,7 +176,7 @@ public override ServiceResult Add(PageEntity item) } SerializeAssets(item); var result = base.Add(item); - if (!result.HasViolation) + if (!result.HasError) { _eventManager.Trigger(Events.OnPageAdded, item); } @@ -193,7 +193,7 @@ public override ServiceResult Update(PageEntity item) item.IsPublish = false; SerializeAssets(item); var result = base.Update(item); - if (!result.HasViolation) + if (!result.HasError) { _eventManager.Trigger(Events.OnPageUpdated, item); } @@ -230,7 +230,7 @@ public ServiceResult Publish(PageEntity item) } catch (Exception ex) { - result.AddRuleViolation("Title", ex.Message); + result.AddError("Title", ex.Message); } return result; } diff --git a/src/ZKEACMS/PendingTask/PendingTaskExecutor.cs b/src/ZKEACMS/PendingTask/PendingTaskExecutor.cs index f659aad0e..6628bdc0f 100644 --- a/src/ZKEACMS/PendingTask/PendingTaskExecutor.cs +++ b/src/ZKEACMS/PendingTask/PendingTaskExecutor.cs @@ -46,7 +46,7 @@ public async Task ProcessAllPendingTaskAsync() { var context = taskHandler.Deserialize(task.Data); var result = await taskHandler.ExecuteAsync(context); - if (result.HasViolation) throw new Exception(result.ErrorMessage); + if (result.HasError) throw new Exception(result.ErrorMessage); task.LogMessage = result.Result; _pendingTaskService.Complete(task); diff --git a/src/ZKEACMS/Rule/RuleService.cs b/src/ZKEACMS/Rule/RuleService.cs index 94056a4e5..31f31605b 100644 --- a/src/ZKEACMS/Rule/RuleService.cs +++ b/src/ZKEACMS/Rule/RuleService.cs @@ -83,9 +83,7 @@ public override ServiceResult Add(Rule item) } catch { - var result = new ServiceResult(); - result.RuleViolations.Add(new RuleViolation("Title", _localize.Get("There is an error value in the condition, save failed!"))); - return result; + return new Error("Title", _localize.Get("There is an error value in the condition, save failed!")); } return base.Add(item); } @@ -98,9 +96,7 @@ public override ServiceResult Update(Rule item) } catch { - var result = new ServiceResult(); - result.RuleViolations.Add(new RuleViolation("Title", _localize.Get("There is an error value in the condition, save failed!"))); - return result; + return new Error("Title", _localize.Get("There is an error value in the condition, save failed!")); } return base.Update(item); } diff --git a/src/ZKEACMS/Setting/ApplicationSettingService.cs b/src/ZKEACMS/Setting/ApplicationSettingService.cs index 323616f73..5e5ce62d0 100644 --- a/src/ZKEACMS/Setting/ApplicationSettingService.cs +++ b/src/ZKEACMS/Setting/ApplicationSettingService.cs @@ -40,13 +40,9 @@ public override ServiceResult Add(ApplicationSetting item) { lock (ApplicationSetting) { - if (base.Get(item.SettingKey) == null) - { - return base.Add(item); - } - var result = new ServiceResult(); - result.RuleViolations.Add(new RuleViolation("SettingKey", ApplicationContext.GetService().Get("The setting key is already exists"))); - return result; + if (base.Get(item.SettingKey) != null) return new Error("SettingKey", ApplicationContext.GetService().Get("The setting key is already exists")); + + return base.Add(item); } } diff --git a/src/ZKEACMS/Widget/WidgetBasePartService.cs b/src/ZKEACMS/Widget/WidgetBasePartService.cs index 3127f9bc9..fdf02d4df 100644 --- a/src/ZKEACMS/Widget/WidgetBasePartService.cs +++ b/src/ZKEACMS/Widget/WidgetBasePartService.cs @@ -88,7 +88,7 @@ List getWidgets(int[] p) public override ServiceResult Update(WidgetBasePart item) { var result = base.Update(item); - if (!result.HasViolation) + if (!result.HasError) { EventManager.Trigger(Events.OnWidgetBasePartUpdated, item); } diff --git a/src/ZKEACMS/Widget/WidgetService.cs b/src/ZKEACMS/Widget/WidgetService.cs index 6cb820dc3..df894f4dd 100644 --- a/src/ZKEACMS/Widget/WidgetService.cs +++ b/src/ZKEACMS/Widget/WidgetService.cs @@ -45,12 +45,12 @@ public override ServiceResult Add(T item) var basePart = item.ToWidgetBasePart(); basePart.ID = id; var baseResult = WidgetBasePartService.Add(basePart); - if (baseResult.HasViolation) + if (baseResult.HasError) { var result = new ServiceResult(); foreach (var item in baseResult.RuleViolations) { - result.AddRuleViolation(item.ParameterName, item.ErrorMessage); + result.AddError(item.ParameterName, item.Message); } return result; } @@ -66,12 +66,12 @@ public override ServiceResult Update(T item) var basePart = WidgetBasePartService.Get(item.ID); item.CopyTo(basePart); var baseResult = WidgetBasePartService.Update(basePart); - if (baseResult.HasViolation) + if (baseResult.HasError) { var result = new ServiceResult(); foreach (var item in baseResult.RuleViolations) { - result.AddRuleViolation(item.ParameterName, item.ErrorMessage); + result.AddError(item.ParameterName, item.Message); } return result; } @@ -90,12 +90,12 @@ public override ServiceResult UpdateRange(params T[] items) return BeginTransaction(() => { var baseResult = WidgetBasePartService.UpdateRange(baseParts.ToArray()); - if (baseResult.HasViolation) + if (baseResult.HasError) { var result = new ServiceResult(); foreach (var item in baseResult.RuleViolations) { - result.AddRuleViolation(item.ParameterName, item.ErrorMessage); + result.AddError(item.ParameterName, item.Message); } return result; } @@ -236,7 +236,7 @@ public virtual void Publish(WidgetBase widget) widget.IsTemplate = false; widget.IsSystem = false; var publishResult = Add((T)widget); - if (publishResult.HasViolation) throw new Exception(widget.WidgetName + " " + publishResult.ErrorMessage); + if (publishResult.HasError) throw new Exception(widget.WidgetName + " " + publishResult.ErrorMessage); } #region PackWidget