Exemplo n.º 1
0
        public async Task <IActionResult> Create(int campaignId, EventEditViewModel eventEditViewModel, IFormFile fileUpload)
        {
            var campaign = await _mediator.SendAsync(new CampaignSummaryQuery { CampaignId = campaignId });

            var authorizableCampaign = await _mediator.SendAsync(new AuthorizableCampaignQuery(campaign.Id, campaign.OrganizationId));

            if (!await authorizableCampaign.UserCanManageChildObjects())
            {
                return(new ForbidResult());
            }

            var errors = _eventEditViewModelValidator.Validate(eventEditViewModel, campaign);

            errors.ToList().ForEach(e => ModelState.AddModelError(e.Key, e.Value));

            ModelState.Remove("NewItinerary");

            //TryValidateModel is called explictly because of MVC 6 behavior that supresses model state validation during model binding when binding to an IFormFile.
            //See #619.
            if (ModelState.IsValid && TryValidateModel(eventEditViewModel))
            {
                if (fileUpload != null)
                {
                    if (!fileUpload.IsAcceptableImageContentType())
                    {
                        ModelState.AddModelError(nameof(eventEditViewModel.ImageUrl), "You must upload a valid image file for the logo (.jpg, .png, .gif)");
                        return(View("Edit", eventEditViewModel));
                    }


                    if (_imageSizeValidator != null && fileUpload.Length > _imageSizeValidator.FileSizeInBytes)
                    {
                        ModelState.AddModelError(nameof(eventEditViewModel.ImageUrl), $"File size must be less than {_imageSizeValidator.BytesToMb():#,##0.00}MB!");
                        return(View("Edit", eventEditViewModel));
                    }
                }

                eventEditViewModel.OrganizationId = campaign.OrganizationId;
                var id = await _mediator.SendAsync(new EditEventCommand { Event = eventEditViewModel });

                if (fileUpload != null)
                {
                    // resave now that event has the ImageUrl
                    eventEditViewModel.Id       = id;
                    eventEditViewModel.ImageUrl = await _imageService.UploadEventImageAsync(campaign.OrganizationId, id, fileUpload);

                    await _mediator.SendAsync(new EditEventCommand { Event = eventEditViewModel });
                }

                return(RedirectToAction(nameof(Details), new { area = AreaNames.Admin, id = id }));
            }

            return(View("Edit", eventEditViewModel));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Create(int campaignId, EventEditViewModel eventEditViewModel, IFormFile fileUpload)
        {
            var campaign = await _mediator.SendAsync(new CampaignSummaryQuery { CampaignId = campaignId });

            if (campaign == null || !User.IsOrganizationAdmin(campaign.OrganizationId))
            {
                return(Unauthorized());
            }

            var errors = _eventEditViewModelValidator.Validate(eventEditViewModel, campaign);

            errors.ToList().ForEach(e => ModelState.AddModelError(e.Key, e.Value));

            ModelState.Remove("NewItinerary");

            //TryValidateModel is called explictly because of MVC 6 behavior that supresses model state validation during model binding when binding to an IFormFile.
            //See #619.
            if (ModelState.IsValid && TryValidateModel(eventEditViewModel))
            {
                if (fileUpload != null)
                {
                    if (!fileUpload.IsAcceptableImageContentType())
                    {
                        ModelState.AddModelError("ImageUrl", "You must upload a valid image file for the logo (.jpg, .png, .gif)");
                        return(View("Edit", eventEditViewModel));
                    }
                }

                eventEditViewModel.OrganizationId = campaign.OrganizationId;
                var id = await _mediator.SendAsync(new EditEventCommand { Event = eventEditViewModel });

                if (fileUpload != null)
                {
                    // resave now that event has the ImageUrl
                    eventEditViewModel.Id       = id;
                    eventEditViewModel.ImageUrl = await _imageService.UploadEventImageAsync(campaign.OrganizationId, id, fileUpload);

                    await _mediator.SendAsync(new EditEventCommand { Event = eventEditViewModel });
                }

                return(RedirectToAction(nameof(Details), new { area = "Admin", id = id }));
            }

            return(View("Edit", eventEditViewModel));
        }