예제 #1
0
        public async Task Post_ShouldReturnOk()
        {
            // Arrange
            var model = new StrategyCreateViewModel
            {
                CampaignUuid = Guid.NewGuid(),
                StrategyName = "WJsHome",
                SupplySource = "PublicMarket",
                MediaType    = "Display",
            };
            var adGroup = new AdGroup {
                AdGroupUuid = Guid.NewGuid(), AdGroupName = "My Strategy 1"
            };

            Mock.Mock <IStrategyService>().Setup(x => x.GetStrategy(It.IsAny <Guid>())).Returns(Task.FromResult(adGroup));
            Mock.Mock <ICampaignService>().Setup(x => x.GetCampaign(model.CampaignUuid.Value)).Returns(Task.FromResult(new Campaign()));

            // Act
            var retVal = await Controller.Post(model);

            // Assert
            Assert.That(retVal, Is.Not.Null);
            Assert.That(retVal, Is.TypeOf <CreatedAtRouteNegotiatedContentResult <StrategyViewModel> >());
            Mock.Mock <IStrategyService>().Verify(x => x.CreateStrategy(It.IsAny <AdGroup>()), Times.Once);
        }
        public async Task <IHttpActionResult> Post(StrategyCreateViewModel model)
        {
            if (model == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var campaign = await _campaignService.GetCampaign(model.CampaignUuid.GetValueOrDefault(Guid.Empty)).ConfigureAwait(false);

            if (campaign == null)
            {
                return(BadRequest("The specified campaign was not found."));
            }

            var strategy = _mapping.Map <AdGroup>(model);
            await _strategyService.CreateStrategy(strategy).ConfigureAwait(false);

            strategy = await _strategyService.GetStrategy(strategy.AdGroupUuid).ConfigureAwait(false);

            var strategyViewModel = _mapping.Map <StrategyViewModel>(strategy);

            return(CreatedAtRoute("Strategies.GetById", new { Id = strategy.AdGroupUuid }, strategyViewModel));
        }