Пример #1
0
        private static void AddNewGiftBox()
        {
            do
            {
                WriteLine("Corporate Name: ");


                WriteLine("Image Url: ");



                CursorVisible = true;

                SetCursorPosition(16, 0);
                string name = ReadLine();

                SetCursorPosition(11, 1);
                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");


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

                if (confirmation.Key == ConsoleKey.Y)
                {
                    // Instantiate new Corporate object based on retrieved values
                    var corporate = new Corporate(name, imageUrl);

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

                    // Serialize to JSON
                    var httpContent = JsonConvert.SerializeObject(corporate, 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("corporates?api-version=1", byteContent).Result;

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

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

                    // Go back to prompt menu
                }
            } while (true);
        }
Пример #2
0
        private static void EditGiftBox()
        {
            ListGiftBoxes();

            WriteLine("_______________________________________________________________________________");

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

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

            Clear();

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

                var localizedCorporate = JsonConvert.DeserializeObject <Corporate>(jsonString);

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

                    WriteLine("__________________________________________________________________________________________");

                    WriteLine("Corporate Name: ");

                    Write("Image Url: ");

                    // Set cursor to visible
                    CursorVisible = true;

                    SetCursorPosition(16, 3);
                    string name = ReadLine();

                    SetCursorPosition(11, 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");



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

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

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

                        // Serialize to JSON
                        var httpContent = JsonConvert.SerializeObject(corporate, 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($"corporates/{id.ToString()}?api-version=1", byteContent).Result;

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

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

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

            Thread.Sleep(2000);
            Clear();
        }