コード例 #1
0
        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);
        }
コード例 #2
0
        public static DeliveryViewModel Map(ContentGroup model)
        {
            if (model == null)
            {
                return(null);
            }

            if (model.Items == null)
            {
                model.Items = new List <InfoObject>();
            }

            return(new DeliveryViewModel
            {
                Id = model.Id,
                Order = model.Order,
                DateOfCreation = model.DateOfCreation,
                Items = model.Items.Select(x => InfoObjectViewModel.Map(x)).ToList()
            });
        }
コード例 #3
0
        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);
        }