public ActionResult Edit([Bind(Include = "Id,Name,Text,StartDate,EndDate,Discount")] EditPromotionBindingModel bindingModel)
        {
            if (ModelState.IsValid)
            {
                this.promotionService.EditPromotion(bindingModel);
                return(RedirectToAction("Details", "Promotions", new { id = bindingModel.Id }));
            }

            EditPromotionViewModel viewModel = this.promotionService.GetPromotionById(bindingModel.Id);

            return(View(viewModel));
        }
Ejemplo n.º 2
0
        public EditPromotionViewModel GetPromotionById(int?id)
        {
            Promotion promotion = this.Context.Promotions.Find(id);

            if (promotion == null)
            {
                return(null);
            }

            EditPromotionViewModel viewModel = Mapper.Map <Promotion, EditPromotionViewModel>(promotion);

            return(viewModel);
        }
        // GET: Admin/Promotions/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                throw new Exception("Invalid URL - promotion's id can not be null");
            }

            EditPromotionViewModel viewModel = this.promotionService.GetPromotionById(id);

            if (viewModel == null)
            {
                throw new Exception($"Invalid URL - there is no promotion with id {id}");
            }

            return(View(viewModel));
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> EditPromotion([FromRoute] int id)
        {
            var promotion = new EditPromotionViewModel();

            using (var client = _restClient.CreateClient(User))
            {
                using (
                    var response = await client.GetAsync("/api/promotion/" + id))
                {
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        promotion = JsonConvert.DeserializeObject <EditPromotionViewModel>(
                            await response.Content.ReadAsStringAsync());
                    }
                    else
                    {
                        return(RedirectToAction("Promotions"));
                    }
                }
            }

            return(View(promotion));
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> EditPromotion([FromRoute] int id, EditPromotionViewModel promotion)
        {
            if (ModelState.IsValid)
            {
                if (promotion.StartDate.Date > promotion.EndDate.Date)
                {
                    ModelState.AddModelError("StartGreaterEnd", "Start date must be smaller or equal to end date.");
                    return(View(promotion));
                }

                if (promotion.PercentOff > 1 || promotion.PercentOff <= 0)
                {
                    ModelState.AddModelError("OverPercent",
                                             "Percent discount must be greater than 0 and smaller than 1.");
                    return(View(promotion));
                }

                using (var client = _restClient.CreateClient(User))
                {
                    using (
                        var response = await client.PutAsync("/api/promotion",
                                                             new StringContent(JsonConvert.SerializeObject(promotion), Encoding.UTF8,
                                                                               "application/json")))
                    {
                        if (response.StatusCode == HttpStatusCode.OK)
                        {
                            return(RedirectToAction("Promotions"));
                        }
                    }
                }

                ModelState.AddModelError(String.Empty, "Update failed");
            }


            return(View(promotion));
        }
        public ActionResult Edit(int id)
        {
            var model = new EditPromotionViewModel(ControllersEnum.Home, id, this.promotionsDb);

            return(View(model));
        }