public async Task ReportErrorsWhenEndDateIsInvalid()
        {
            // arrage
            var mediator = new Mock<IMediator>();
            var validator = new ActivityDetailModelValidator(mediator.Object);
            var campaign = new CampaignSummaryModel();
            var model = new ActivityDetailModel
            {
                StartDateTime = new DateTimeOffset(new DateTime(2000, 1, 1)),
                EndDateTime = new DateTimeOffset(new DateTime(1999, 1, 1))
            };

            // act
            var errors = await validator.Validate(model, campaign);

            // assert
            Assert.True(errors.Exists(x => x.Key.Equals("EndDateTime")));
        }
        public async Task ReportErrorsWhenPostalCodeInvalid()
        {
            // arrage
            var mediator = new Mock<IMediator>();
            var validator = new ActivityDetailModelValidator(mediator.Object);
            var campaign = new CampaignSummaryModel();
            var model = new ActivityDetailModel
            {
                Location = new LocationEditModel()
                {
                    PostalCode = "12345",
                    State = "WA",
                    City = "Seattle"
                }
            };

            // act
            var errors = await validator.Validate(model, campaign);

            // assert
            Assert.True(errors.Exists(x => x.Key.Equals("PostalCode")));
        }
Ejemplo n.º 3
0
        public async Task<IActionResult> Create(int campaignId, ActivityDetailModel activity, IFormFile fileUpload)
        {
            CampaignSummaryModel campaign = await _mediator.SendAsync(new CampaignSummaryQuery { CampaignId = campaignId });
            if (campaign == null ||
                !User.IsOrganizationAdmin(campaign.OrganizationId))
                {
                    return HttpUnauthorized();
                }

            var validator = new ActivityDetailModelValidator(_mediator);
            var errors = await validator.Validate(activity, campaign);
            errors.ToList().ForEach(e => ModelState.AddModelError(e.Key, e.Value));

            //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(activity))
            {
                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", activity);
                    }
                }

                activity.OrganizationId = campaign.OrganizationId;
                var id = _mediator.Send(new EditActivityCommand { Activity = activity });

                if (fileUpload != null)
                {
                    // resave now that activity has the ImageUrl
                    activity.Id = id;
                    activity.ImageUrl = await _imageService.UploadActivityImageAsync(campaign.OrganizationId, id, fileUpload);
                    _mediator.Send(new EditActivityCommand { Activity = activity });
                }

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

            return View("Edit", activity);
        }
Ejemplo n.º 4
0
        public async Task<IActionResult> Edit(ActivityDetailModel activity, IFormFile fileUpload)
        {
            if (activity == null)
            {
                return HttpBadRequest();
            }
            
            //TODO: Use the query pattern here
            var organizationId = _dataAccess.GetManagingOrganizationId(activity.Id);
            if (!User.IsOrganizationAdmin(organizationId))
            {
                return HttpUnauthorized();
            }

            CampaignSummaryModel campaign = await _mediator.SendAsync(new CampaignSummaryQuery { CampaignId = activity.CampaignId });

            var validator = new ActivityDetailModelValidator(_mediator);
            var errors = await validator.Validate(activity, campaign);
            errors.ForEach(e => ModelState.AddModelError(e.Key, e.Value));

            if (ModelState.IsValid)
            {
                if (fileUpload != null)
                {
                    if (fileUpload.IsAcceptableImageContentType())
                    {
                        activity.ImageUrl = await _imageService.UploadActivityImageAsync(campaign.OrganizationId, activity.Id, fileUpload);
                    }
                    else
                    {
                        ModelState.AddModelError("ImageUrl", "You must upload a valid image file for the logo (.jpg, .png, .gif)");
                        return View(activity);
                    }
                }
                
                var id = _mediator.Send(new EditActivityCommand { Activity = activity });
                return RedirectToAction("Details", "Activity", new { area = "Admin", id = id });
            }
            return View(activity);
        }