public RequestResultBase Remove(int id, string itemTypeName) { var res = new RequestResultBase() { msg = "Объект удален!" }; var removingMethod = GetRepositoryAction(itemTypeName, "Delete"); if (removingMethod == null) { return(MarkRequestAsIncorrect(res, "Эти объекты удалять запрещено!")); } var collection = GetObjCollectionOfSelectedItemType(ref itemTypeName); var item = FindByID(collection, id); if (item == null) { return(MarkRequestAsIncorrect(res, "Объект не найден!")); } var hasBeenRemoved = (bool)removingMethod.Invoke(db, new object[] { item }); if (!hasBeenRemoved) { MarkRequestAsIncorrect(res, "Не удалось удалить объект!"); } return(res); }
private RequestResultBase MarkRequestAsIncorrect(RequestResultBase res, string msg = null) { msg = msg ?? "Запрос некорректный!"; res.result = false; res.msg = msg; return(res); }
/// <summary> /// Handles the specified context. /// </summary> /// <param name="context">The context.</param> private static void Handle(ExceptionHandlerContext context) { ExceptionContext exceptionContext = context.ExceptionContext; if (exceptionContext.CatchBlock == ExceptionCatchBlocks.IExceptionFilter) { // The exception filter stage propagates unhandled exceptions by default (when no filter handles the // exception). return; } RequestResultBase result = null; var exception = context.Exception as HttpException; if (exception == null) { result = new ErrorResult(context.Request, HttpStatusCode.InternalServerError); } else { result = new ErrorResult(context.Request, exception.StatusCode); } context.Result = result; }
private string GetContractDocTypeName(RequestResultBase requestResult, DocflowRepositoryBase db, int id) { var docTypeModel = db.GetDocTypes().FirstOrDefault(type => type.id == id); if (docTypeModel == null) { MarkValidationError(requestResult, "Указан несуществующий тип документа!"); return(null); } return(docTypeModel.name); }
public RequestResultBase Validate(IRepository db) { var res = new RequestResultBase() { result = true }; foreach (var property in GetType().GetProperties()) { if (property.Name == "id") { continue; } var propValue = property.GetValue(this); if (propValue is DataModelBase nestedProperty) { if (ToSkipOnRecurseValidation(property)) { continue; } recurseStep++; var intermediateRes = nestedProperty.Validate(db); recurseStep--; if (!intermediateRes.result) { MarkValidationError(res, intermediateRes.msg); } } else if (!IsRequiredValueSet(property, propValue)) { var propertyName = GetPropertyNameInGenitive(property); MarkValidationError(res, $"Значение {propertyName} не задано!"); } else { CheckStringLength(res, property, propValue); } if (_customValidationProcedures.ContainsKey(property)) { var intermediateRes = _customValidationProcedures[property](propValue, db); if (!intermediateRes.result) { MarkValidationError(res, intermediateRes.msg); } } } return(res); }
protected RequestResultBase ValidateDocStatus(object propertyValue, IRepository db) { var docflowRepository = db as DocflowRepositoryBase; if (docflowRepository == null) { throw new ArgumentException("Incorrect repository type!"); } int statusID = int.Parse(propertyValue.ToString()); var res = new RequestResultBase(); var docTypeID = (int)GetType().GetProperty("typeID")?.GetValue(this); var docTypeName = string.Empty; if (GetType().Name == "as_shipments") { docTypeName = "Отправление"; } else if (GetType().Name == "as_contractDocs") { docTypeName = GetContractDocTypeName(res, docflowRepository, docTypeID); if (docTypeName == null) { return(res); } } var docStatusModel = docflowRepository.GetDocStatuses() .FirstOrDefault(status => status.id == statusID); if (docStatusModel == null) { MarkValidationError(res, "Указанного статуса не существует!"); return(res); } var docStatusApplicableNamesArray = (docStatusModel.docTypeNames ?? docTypeName) .Split(','); string restrictedDocTypeName = docStatusApplicableNamesArray .FirstOrDefault(name => name.Contains(docTypeName)); if (restrictedDocTypeName != null ^ docStatusModel.allowForListedTypes) { MarkValidationError(res, "Данный тип документа не может иметь указанный статус!"); } return(res); }
private void CheckStringLength(RequestResultBase res, PropertyInfo property, object value) { var lengthAttribute = property.GetCustomAttribute(typeof(StringLengthAttribute), true); int?maxLength = (lengthAttribute as StringLengthAttribute)?.MaximumLength; if (maxLength == null) { return; } else if (value?.ToString().Length > maxLength) { var propertyName = GetPropertyNameInGenitive(property); var declinedCharsWord = DataHelper.NounDeclensor.HowManyOf("символ", maxLength.Value); string msg = $"Длина {propertyName} превышает {maxLength} {declinedCharsWord}!"; MarkValidationError(res, msg); } }
public RequestResultBase CheckValue(string propertyName, object value) { var res = new RequestResultBase() { result = true }; if (!modelValidatorsDict.ContainsKey(propertyName)) { return(res); } var validationMethod = modelValidatorsDict[propertyName]; var errorText = validationMethod(propertyName); if (errorText != null) { res.result = false; res.msg = errorText; } return(res); }
protected RequestResultBase ValidateDocTypeBasedProps(object value, IRepository db) { var res = new RequestResultBase(); var docflowRepository = db as DocflowRepositoryBase; as_docType = docflowRepository.GetDocTypes() .FirstOrDefault(type => type.id == typeID); if (as_docType == null) { MarkValidationError(res, "Указан несуществующий тип!"); return(res); } if (as_docType.name != "Договор") { as_contractFile = new as_contractFiles(); } else if (as_docType.name != "Счет") { total = null; } return(res); }
public RequestResultBase CreateOrModifyItem(Dictionary <string, object> values, string entityNameToCreateOrModify) { var res = new RequestResultBase(); string entityName = entityNameToCreateOrModify; var itemsCollec = GetObjCollectionOfSelectedItemType(ref entityName); if (!entityNameToCreateOrModify.ToLower().Contains(entityName)) { return(MarkRequestAsIncorrect(res)); } else if (!values.ContainsKey("id")) { values.Add("id", 0); } int.TryParse(values["id"].ToString(), out int id); var modifyingMethod = GetRepositoryAction(entityName, "Modify"); if (id > 0 && modifyingMethod == null) { return(MarkRequestAsIncorrect(res, "Создание и редактирование невозможно!")); } var item = id == 0 ? InstantiateItem(GetCollectionOfSelectedItemType(ref entityName)) : FindByID(itemsCollec, id); if (item == null) { return(MarkRequestAsIncorrect(res, "Объект не найден!")); } foreach (var keyValue in values) { if (keyValue.Key == "id") { continue; } DataHelper.TrySetValByRefl(item, keyValue.Key, keyValue.Value); } res = (item as DataModelBase).Validate(db); if (!res.result) { return(res); } id = (int)modifyingMethod.Invoke(db, new object[] { item }); if (id <= 0) { res = MarkRequestAsIncorrect(res, "Не удалось сохранить объект!"); } else { res.d = item; res = new RequestResultBase() { d = res }; } return(res); }
protected void MarkValidationError(RequestResultBase baseRes, string msg) { baseRes.result = false; baseRes.msg += "\r\n" + msg; }