public IActionResult Create([FromBody] CampaignInputModel inputModel)
        {
            if (inputModel == null)
            {
                return(BadRequest());
            }


            var campaign = inputModel.ToEntity();

            if (!_productStore.Exists(campaign.ProductId))
            {
                ModelState.AddModelError("Product", $"Product '{campaign.ProductId}' does not exists.");
                return(BadRequest(ModelState));
            }

            campaign = _service.Create(campaign);
            campaign = _service.GetById(campaign.Id);
            if (null == campaign)
            {
                return(NoContent());
            }

            var outputModel = CampaignOutputModel.FromEntity(campaign);

            return(CreatedAtRoute("ViewCampaign",
                                  new { id = outputModel.Id }, outputModel));
        }
        public IActionResult Update(int id, [FromBody] CampaignInputModel inputModel)
        {
            if (inputModel == null)
            {
                return(BadRequest());
            }

            var campaign = _service.GetById(id);

            if (null == campaign)
            {
                return(NotFound());
            }

            if (!_productStore.Exists(inputModel.ProductId))
            {
                ModelState.AddModelError("Product", $"Product '{inputModel.ProductId}' does not exists.");
                return(BadRequest(ModelState));
            }

            campaign.Name      = inputModel.Name;
            campaign.ProductId = inputModel.ProductId;
            campaign.Start     = inputModel.Start;
            campaign.End       = inputModel.End;


            _service.Update(campaign);

            return(NoContent());
        }
        public IActionResult Edit(int id, [FromForm] CampaignInputModel inputModel)
        {
            if (inputModel == null)
            {
                return(NotFound());
            }

            if (!ModelState.IsValid)
            {
                ViewData["ProductId"] = new SelectList(_productStore.List(), "Id", "Name", inputModel.ProductId);
                return(View(inputModel.ToEntity()));
            }

            int campaignId = id;

            if (!_productStore.Exists(inputModel.ProductId))
            {
                ModelState.AddModelError("Product", $"Product '{inputModel.ProductId}' does not exists.");
                return(BadRequest(ModelState));
            }

            try
            {
                var campaign = _service.GetById(campaignId);
                if (null == campaign)
                {
                    return(NotFound());
                }

                campaign.Name        = inputModel.Name;
                campaign.Description = inputModel.Description;
                campaign.End         = inputModel.End;
                campaign.Start       = inputModel.Start;

                campaign.ProductId = inputModel.ProductId;

                _service.Update(campaign);
                campaignId = campaign.Id;
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!_service.Exists(campaignId))
                {
                    return(NotFound());
                }

                throw;
            }

            return(RedirectToAction(nameof(Index)));
        }
        public IActionResult Create([FromForm] CampaignInputModel inputModel)
        {
            if (!ModelState.IsValid)
            {
                ViewData["ProductId"] = new SelectList(_productStore.List(), "Id", "Name");
                return(View(inputModel));
            }

            var campaign = inputModel.ToEntity();

            if (!_productStore.Exists(campaign.ProductId))
            {
                ModelState.AddModelError("Product", $"Product '{campaign.ProductId}' does not exists.");
                return(BadRequest(ModelState));
            }

            campaign = _service.Create(campaign);

            var outputModel = CampaignOutputModel.FromEntity(campaign);

            return(RedirectToAction(nameof(Index)));
        }
Esempio n. 5
0
        public async Task FullTest()
        {
            // POST Create a product for later test purposes
            var pim = new ProductInputModel()
            {
                Name  = "CreatedWithTest",
                Price = 25
            };
            var content = new StringContent(JsonConvert.SerializeObject(pim), Encoding.Default, "application/json");

            var createResponse = await _client.PostAsync("/api/v1/products", content);

            Assert.Equal(HttpStatusCode.Created, createResponse.StatusCode);

            var responseString = await createResponse.Content.ReadAsStringAsync();

            var createdProduct = JsonConvert.DeserializeObject <ProductOutputModel>(responseString);


            // Add a campaign
            var cim = new CampaignInputModel()
            {
                Name      = "CreatedCampaignWithTest",
                ProductId = createdProduct.Id,
                Start     = DateTime.Now,
                End       = DateTime.Now.AddDays(5)
            };

            content        = new StringContent(JsonConvert.SerializeObject(cim), Encoding.Default, "application/json");
            createResponse = await _client.PostAsync(BaseUrl, content);

            responseString = await createResponse.Content.ReadAsStringAsync();

            var createdCampaign = JsonConvert.DeserializeObject <CampaignOutputModel>(responseString);

            Assert.NotNull(createdCampaign);
            Assert.Equal(cim.Name, createdCampaign.Name);



            // List Campaigns
            var listResult = await ListCampaigns();

            Assert.True(listResult.RecordCount > 0);



            // PUT Update
            cim = new CampaignInputModel()
            {
                Name      = "ChangedNameInTest",
                ProductId = createdProduct.Id,
                Start     = DateTime.Now.AddDays(1),
                End       = DateTime.Now.AddDays(5)
            };
            var updateContent = new StringContent(JsonConvert.SerializeObject(cim), Encoding.Default, "application/json");

            var updateResponse = await _client.PutAsync(BaseUrl + $"/{createdCampaign.Id}", updateContent);

            var updateBody = await updateResponse.Content.ReadAsStringAsync();

            Assert.Equal(HttpStatusCode.NoContent, updateResponse.StatusCode);



            // GET with id
            var getResponse = await _client.GetAsync($"{BaseUrl}/{createdCampaign.Id}");

            Assert.Equal(HttpStatusCode.OK, getResponse.StatusCode);

            var getBody = await getResponse.Content.ReadAsStringAsync();

            var getModel = JsonConvert.DeserializeObject <CampaignOutputModel>(getBody);

            Assert.Equal("ChangedNameInTest", getModel.Name);



            // DELETE with id
            var deleteResponse = await _client.DeleteAsync($"{BaseUrl}/{getModel.Id}");

            Assert.Equal(HttpStatusCode.NoContent, deleteResponse.StatusCode);


            // GET with id
            var getNoResponse = await _client.GetAsync($"{BaseUrl}/{getModel.Id}");

            Assert.Equal(HttpStatusCode.NoContent, getNoResponse.StatusCode);
        }