Пример #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;
            }
        }
Пример #2
0
        public async override void OnNavigatedTo(INavigationParameters parameters)
        {
            switch (parameters.GetNavigationMode())
            {
            case NavigationMode.Back:
                if (parameters.ContainsKey("item"))
                {
                    var item = parameters["item"] as ItemForInvoiceDto;
                    if (InvoiceBindProp == null)
                    {
                        InvoiceBindProp = new InvoiceDto();
                    }
                    InvoiceBindProp.Items.Add(item);
                    InvoiceBindProp.TotalPrice += item.Value;
                }
                if (parameters.ContainsKey(nameof(InvoiceBindProp)))
                {
                    var invoice = parameters[nameof(InvoiceBindProp)] as InvoiceDto;
                    await _connection.InvokeAsync("DeleteInvoice", InvoiceBindProp.Id);

                    InvoiceBindProp     = null;
                    TempInvoiceBindProp = null;
                }
                break;

            case NavigationMode.New:
                using (var client = new HttpClient())
                {
                    var categoryTask = client.GetAsync(Properties.Resources.BaseUrl + "categories/");
                    var discountTask = client.GetAsync(Properties.Resources.BaseUrl + "discounts/");
                    var invoiceTask  = client.GetAsync(Properties.Resources.BaseUrl + "invoices/");
                    var zoneTask     = client.GetAsync(Properties.Resources.BaseUrl + "zones/");

                    var allTasks = new List <Task> {
                        categoryTask, discountTask, invoiceTask, zoneTask
                    };
                    while (allTasks.Any())
                    {
                        Task finished = await Task.WhenAny(allTasks);

                        if (finished == categoryTask)
                        {
                            if (categoryTask.Result.IsSuccessStatusCode)
                            {
                                var categories = JsonConvert.DeserializeObject <IEnumerable <CategoryDto> >(await categoryTask.Result.Content.ReadAsStringAsync());
                                foreach (var category in categories)
                                {
                                    ListCategoryBindProp.Add(category);
                                }
                            }
                            else
                            {
                                await PageDialogService.DisplayAlertAsync("Lỗi", $"{await categoryTask.Result.Content.ReadAsStringAsync()}", "Đóng");
                            }
                        }
                        else if (finished == discountTask)
                        {
                            if (discountTask.Result.IsSuccessStatusCode)
                            {
                                var discounts = JsonConvert.DeserializeObject <IEnumerable <DiscountDto> >(await discountTask.Result.Content.ReadAsStringAsync());
                                foreach (var discount in discounts)
                                {
                                    ListDiscountBindProp.Add(discount);
                                }
                            }
                            else
                            {
                                await PageDialogService.DisplayAlertAsync("Lỗi", $"{await discountTask.Result.Content.ReadAsStringAsync()}", "Đóng");
                            }
                        }
                        else if (finished == invoiceTask)
                        {
                            if (invoiceTask.Result.IsSuccessStatusCode)
                            {
                                var invoices = JsonConvert.DeserializeObject <IEnumerable <InvoiceDto> >(await invoiceTask.Result.Content.ReadAsStringAsync());
                                foreach (var invoice in invoices)
                                {
                                    ListInvoiceBindProp.Add(invoice);
                                }
                            }
                            else
                            {
                                await PageDialogService.DisplayAlertAsync("Lỗi", $"{await invoiceTask.Result.Content.ReadAsStringAsync()}", "Đóng");
                            }
                        }
                        else if (finished == zoneTask)
                        {
                            if (zoneTask.Result.IsSuccessStatusCode)
                            {
                                var zones = JsonConvert.DeserializeObject <IEnumerable <ZoneDto> >(await zoneTask.Result.Content.ReadAsStringAsync());
                                foreach (var zone in zones)
                                {
                                    ListZoneBindProp.Add(zone);
                                }
                            }
                            else
                            {
                                await PageDialogService.DisplayAlertAsync("Lỗi", $"{await zoneTask.Result.Content.ReadAsStringAsync()}", "Đóng");
                            }
                        }
                        allTasks.Remove(finished);
                    }
                    var id       = Xamarin.Essentials.Preferences.Get("token", "token");
                    var response = await client.GetAsync(Properties.Resources.BaseUrl + "users/" + id);

                    if (response.IsSuccessStatusCode)
                    {
                        var user = JsonConvert.DeserializeObject <UserDto>(await response.Content.ReadAsStringAsync());
                        CurrentUserBindProp = user;
                    }
                }
                break;

            case NavigationMode.Forward:
                break;

            case NavigationMode.Refresh:
                break;

            default:
                break;
            }
        }