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 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))); }