コード例 #1
0
        public async Task AddProduct()
        {
            if (_createdProduct.Name == null || _createdProduct.Name == "" || _createdProduct.Stock <= 0)
            {
                ErrorString = (string)Application.Current.FindResource("InvalidProductError");
                return;
            }
            _createdProduct.NullNavigationProperties();

            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                ProductDto content = createProductDto(_createdProduct);

                var        json    = Newtonsoft.Json.JsonConvert.SerializeObject(content);
                URLBuilder url     = new URLBuilder(controler);
                var        request = new HttpRequestMessage()
                {
                    RequestUri = new Uri(url.URL),
                    Method     = HttpMethod.Post,
                    Content    = new StringContent(json,
                                                   Encoding.UTF8,
                                                   "application/json")
                };
                request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", _authenticationUser.UserId.ToString(), _authenticationUser.Password))));
                var response = await client.SendAsync(request);

                if (response.IsSuccessStatusCode)
                {
                    Products.Add(_createdProduct);
                    _createdProduct = ProductWrapper.CreateEmptyProduct(_user);
                }
                else
                {
                    ErrorString = (string)Application.Current.FindResource("InsertProductError");
                    return;
                }
            }
            ErrorString = null;
        }