public async Task <ServiceResult> Add([FromBody] InfoObjectAddModel apiEntity) { var type = Enum.Parse <InfoObjectType>(apiEntity.Type, ignoreCase: true); if (type == InfoObjectType.Image) { // validate file exist var fileExistValidationAttribute = new FileExistValidationAttribute(Constants.WEB_CONTENT_ROOT_PATH, Constants.DEFAULT_PATH_TO_IMAGE); var validationResult = fileExistValidationAttribute.IsValid(apiEntity.Content); if (validationResult != ValidationResult.Success) { return(ServiceResultFactory.BadRequestResult(nameof(apiEntity.Content), validationResult.ErrorMessage)); } } var entity = InfoObjectAddModel.Map(apiEntity); var result = await _infoObjectService.AddAsync(entity); if (result.TryCastModel(out InfoObject infoObject)) { result.ViewModel = InfoObjectViewModel.Map(infoObject); } return(result); }
public async Task <ServiceResult> Edit(Guid id, [FromBody] InfoObjectEditModel apiEntity) { if (string.Equals(apiEntity.Type, InfoObjectType.Image.ToString(), StringComparison.OrdinalIgnoreCase)) { // validate file exist var fileExistValidationAttribute = new FileExistValidationAttribute(Constants.WEB_CONTENT_ROOT_PATH, Constants.DEFAULT_PATH_TO_IMAGE); var validationResult = fileExistValidationAttribute.IsValid(apiEntity.Content); if (validationResult != ValidationResult.Success) { return(ServiceResultFactory.BadRequestResult(nameof(apiEntity.Content), validationResult.ErrorMessage)); } } var entity = InfoObjectEditModel.Map(apiEntity, id); var result = await _infoObjectService.UpdateAsync(entity); if (result.TryCastModel(out InfoObject infoObject)) { result.ViewModel = InfoObjectViewModel.Map(infoObject); } return(result); }