Example #1
0
        private async void OnSave(object obj)
        {
            IsBusy = true;

            try
            {
                // Thuc hien cong viec tai day
                if (IsPercentage)
                {
                    DiscountBindProp.Value        = PercentBindProp;
                    DiscountBindProp.IsPercentage = true;
                }
                else
                {
                    DiscountBindProp.Value        = ValueBindProp;
                    DiscountBindProp.IsPercentage = false;
                }
                var discountToCreate = new DiscountForCreateDto(DiscountBindProp);
                var json             = JsonConvert.SerializeObject(discountToCreate);
                var content          = new StringContent(json, Encoding.UTF8, "application/json");
                // Thuc hien cong viec tai day
                using (var client = new HttpClient())
                {
                    HttpResponseMessage response = new HttpResponseMessage();
                    if (DiscountBindProp.Id == Guid.Empty)
                    {
                        response = await client.PostAsync(Properties.Resources.BaseUrl + "discounts/", content);
                    }
                    else
                    {
                        response = await client.PutAsync(Properties.Resources.BaseUrl + "discounts/", content);
                    }
                    switch (response.StatusCode)
                    {
                    case HttpStatusCode.NoContent:
                        TempDiscount.Name         = DiscountBindProp.Name;
                        TempDiscount.Value        = DiscountBindProp.Value;
                        TempDiscount.MaxValue     = DiscountBindProp.MaxValue;
                        TempDiscount.IsPercentage = DiscountBindProp.IsPercentage;
                        TempDiscount = null;
                        break;

                    case HttpStatusCode.Created:
                        var discount = JsonConvert.DeserializeObject <DiscountDto>(await response.Content.ReadAsStringAsync());
                        ListDiscountBindProp.Add(discount);
                        DiscountBindProp = new DiscountDto();
                        break;
                    }
                };
                IsOpen = false;
            }
            catch (Exception e)
            {
                await ShowErrorAsync(e);
            }
            finally
            {
                IsBusy = false;
            }
        }
        public async Task <IActionResult> CreateDiscount(DiscountForCreateDto discountDto)
        {
            var discount = await _mediator.Send(new AddDiscountCommand(discountDto));

            if (discount != null)
            {
                return(CreatedAtRoute(nameof(GetDiscount), new { id = discount.Id }, discount));
            }
            else
            {
                return(BadRequest("Không thể tạo mới!"));
            }
        }
        public async Task <IActionResult> UpdateDiscount(DiscountForCreateDto discountDto)
        {
            var ok = await _mediator.Send(new UpdateDiscountCommand(discountDto));

            if (ok)
            {
                return(NoContent());
            }
            else
            {
                return(BadRequest("Không thể cập nhật danh mục!"));
            }
        }
 public UpdateDiscountCommand(DiscountForCreateDto discount)
 {
     Discount = discount;
 }