Пример #1
0
        private static void EditCake()
        {
            ListCakes();

            Write("\n\tID: ");
            bool idValid = Int32.TryParse(ReadLine(), out int idParsed);
            int  id      = idValid ? idParsed : 0;

            var responseLocalizedCake = httpClient.GetAsync($"cakes/{id.ToString()}?api-version=1")
                                        .GetAwaiter()
                                        .GetResult();

            Clear();

            if (responseLocalizedCake.IsSuccessStatusCode)
            {
                var jsonString = responseLocalizedCake.Content.ReadAsStringAsync()
                                 .GetAwaiter()
                                 .GetResult();

                var localizedCake = JsonConvert.DeserializeObject <Cake>(jsonString);

                // Open prompt loop
                do
                {
                    WriteLine(localizedCake.ToString());

                    // Prompt
                    SetCursorPosition(2, 10);
                    Write("Name: ");

                    SetCursorPosition(2, 11);
                    Write("Description: ");

                    SetCursorPosition(2, 12);
                    Write("Image Url: ");

                    SetCursorPosition(2, 13);
                    Write("Price: ");


                    // Set cursor to visible
                    CursorVisible = true;

                    // Set cursor position to each prompt subsequently & retrieve respecitve information
                    int unifiedIdentation = "Description: ".Length + 3;

                    SetCursorPosition(unifiedIdentation, 10);
                    string name = ReadLine();

                    SetCursorPosition(unifiedIdentation, 11);
                    string description = ReadLine();

                    SetCursorPosition(unifiedIdentation, 12);
                    string imageUrlString = ReadLine();
                    bool   imageUrlOk     = Uri.TryCreate(imageUrlString, UriKind.Absolute, out Uri imageUrlVerified) &&
                                            (imageUrlVerified.Scheme == Uri.UriSchemeHttp || imageUrlVerified.Scheme == Uri.UriSchemeHttps);
                    Uri imageUrl = imageUrlOk ? imageUrlVerified : new Uri("https://image.shutterstock.com/image-vector/ui-image-placeholder-wireframes-apps-260nw-1037719204.jpg");

                    SetCursorPosition(unifiedIdentation, 13);
                    bool priceOk = Int32.TryParse(ReadLine(), out int priceParsed);
                    int  price   = priceOk ? priceParsed : 0;



                    // Further confirmation request
                    ConsoleKeyInfo confirmation = RequestConfirmation();

                    // Respond to confirmation choice: "Yes"
                    if (confirmation.Key == ConsoleKey.Y)
                    {
                        // Instantiate new Cake object based on retrieved values
                        var cake = new Cake(id, name, description, imageUrl, price);

                        // Set TypeNameHandling to auto
                        JsonSerializerSettings settings = new JsonSerializerSettings();
                        settings.TypeNameHandling = TypeNameHandling.Auto;

                        // Serialize to JSON
                        var httpContent = JsonConvert.SerializeObject(cake, settings);

                        // Construct a content object to send the data
                        var buffer      = System.Text.Encoding.UTF8.GetBytes(httpContent);
                        var byteContent = new ByteArrayContent(buffer);

                        // Set the content type to JSON
                        byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                        // Send put request
                        var response = httpClient.PutAsync($"cakes/{id.ToString()}?api-version=1", byteContent).Result;

                        if (response.IsSuccessStatusCode)
                        {
                            WriteLine("\n  Cake Edited");
                        }
                        else
                        {
                            WriteLine("\n  Something went wrong while updating the cake...");
                        }

                        Thread.Sleep(2000);
                        Clear();
                        break;
                    }
                    else
                    {
                        Thread.Sleep(1000);
                        Clear();

                        // Go back to prompt menu
                    }
                } while (true);
            }
            else
            {
                WriteLine("\n  Cake not found...");
            }

            Thread.Sleep(2000);
            Clear();
        }
Пример #2
0
        private static void RegisterNewCakeToCorporateView()
        {
            // Open prompt loop
            do
            {
                // Prompt
                SetCursorPosition(2, 2);
                Write("Name: ");

                SetCursorPosition(2, 3);
                Write("Description: ");

                SetCursorPosition(2, 4);
                Write("Image Url: ");

                SetCursorPosition(2, 5);
                Write("Price: ");

                SetCursorPosition(2, 7);
                Write("Corporate name: ");


                // Set cursor to visible
                CursorVisible = true;

                // Set cursor position to each prompt subsequently & retrieve respecitve information
                int unifiedIdentation = "Description: ".Length + 5;

                SetCursorPosition(unifiedIdentation, 2);
                string name = ReadLine();

                SetCursorPosition(unifiedIdentation, 3);
                string description = ReadLine();

                SetCursorPosition(unifiedIdentation, 4);
                string imageUrlString = ReadLine();
                bool   imageUrlOk     = Uri.TryCreate(imageUrlString, UriKind.Absolute, out Uri imageUrlVerified) &&
                                        (imageUrlVerified.Scheme == Uri.UriSchemeHttp || imageUrlVerified.Scheme == Uri.UriSchemeHttps);
                Uri imageUrl = imageUrlOk ? imageUrlVerified : new Uri("https://image.shutterstock.com/image-vector/ui-image-placeholder-wireframes-apps-260nw-1037719204.jpg");

                SetCursorPosition(unifiedIdentation, 5);
                bool priceOk = Int32.TryParse(ReadLine(), out int priceParsed);
                int  price   = priceOk ? priceParsed : 0;

                SetCursorPosition(unifiedIdentation, 7);
                string corporateName = ReadLine();


                // Further confirmation request
                ConsoleKeyInfo confirmation = RequestConfirmation();

                // Respond to confirmation choice: "Yes"
                if (confirmation.Key == ConsoleKey.Y)
                {
                    var corporateId = FetchCorporateIdByCorporateName(corporateName);

                    if (corporateId <= 0)
                    {
                        WriteLine("\n  Indicated Category was not found...");
                        Thread.Sleep(2000);
                        Clear();
                        break;
                    }

                    // Instantiate new Cake object based on retrieved values
                    var cake = new Cake(name, description, imageUrl, price, corporateId, corporateId);

                    // Set TypeNameHandling to auto
                    JsonSerializerSettings settings = new JsonSerializerSettings();
                    settings.TypeNameHandling = TypeNameHandling.Auto;

                    // Serialize to JSON
                    var httpContent = JsonConvert.SerializeObject(cake, settings);

                    // Construct a content object to send the data
                    var buffer      = System.Text.Encoding.UTF8.GetBytes(httpContent);
                    var byteContent = new ByteArrayContent(buffer);

                    // Set the content type to JSON
                    byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                    // Send post request
                    var response = httpClient.PostAsync("Cakes/PostCakeToCorporate?api-version=1", byteContent).Result;

                    if (response.IsSuccessStatusCode)
                    {
                        WriteLine("\n  Cake Added");
                    }
                    else
                    {
                        WriteLine("\n  Something went wrong...");
                    }

                    Thread.Sleep(2000);
                    Clear();
                    break;
                }
                else
                {
                    Thread.Sleep(1000);
                    Clear();

                    // Go back to prompt menu
                }
            } while (true);
        }