Exemple #1
0
        static async Task <Uri> CreateCropGroupAsync(CropGroup newCropGroup)
        {
            HttpResponseMessage response = await client.PostAsJsonAsync("api/CropGroups", newCropGroup);

            response.EnsureSuccessStatusCode();

            // return URI of the created resource.
            return(response.Headers.Location);
        }
Exemple #2
0
        static async Task RunAsync()
        {
            // Update port # in the following line.
            client.BaseAddress = new Uri("https://localhost:44317");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));

            try
            {
                // Create a new product
                CropGroup cropGroup = new CropGroup
                {
                    CropGroupName = "Gizmo"
                };

                // Get the product
                cropGroup = await GetCropGroupAsync("https://localhost:44317/api/CropGroups/3");

                ShowCropGroup(cropGroup);

                CropGroup newCropGroup = new CropGroup {
                    CropGroupName = "new222"
                };
                var url = await CreateCropGroupAsync(newCropGroup);

                Console.WriteLine($"Created at {url}");

                // Get the product
                cropGroup = await GetCropGroupAsync(url.PathAndQuery);

                ShowCropGroup(cropGroup);

                // Update the product
                Console.WriteLine("Updating price...");
                cropGroup.CropGroupName = "new222 обн.";
                await UpdateProductAsync(cropGroup);

                // Get the updated product
                cropGroup = await GetCropGroupAsync(url.PathAndQuery);

                ShowCropGroup(cropGroup);

                // Delete the product
                var statusCode = await DeleteProductAsync(cropGroup.CropGroupId);

                Console.WriteLine($"Deleted (HTTP Status = {(int)statusCode})");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            Console.ReadLine();
        }
Exemple #3
0
        static async Task <CropGroup> GetCropGroupAsync(string path)
        {
            CropGroup           product  = null;
            HttpResponseMessage response = await client.GetAsync(path);

            if (response.IsSuccessStatusCode)
            {
                product = await response.Content.ReadAsAsync <CropGroup>();
            }
            return(product);
        }
Exemple #4
0
        static async Task <CropGroup> UpdateProductAsync(CropGroup updatingCropGroup)
        {
            HttpResponseMessage response = await client.PutAsJsonAsync(
                $"api/CropGroups/{updatingCropGroup.CropGroupId}", updatingCropGroup);

            response.EnsureSuccessStatusCode();

            // Deserialize the updated product from the response body.
            updatingCropGroup = await response.Content.ReadAsAsync <CropGroup>();

            return(updatingCropGroup);
        }
Exemple #5
0
 static void ShowCropGroup(CropGroup cropGroup)
 {
     Console.WriteLine($"Id: {cropGroup.CropGroupId} Name: {cropGroup.CropGroupName}");
 }