public void BuildTable() { try { var devices = FoundDeviceCollection .collection .Select(x => x.Value) .OrderBy(x => x, new FoundDeviceCompare()); var cnt = 1; foreach (var device in devices) { device.Id = cnt; device.Key = device.GetHashCode(); cnt++; } #region Store-To-Db foreach (var device in devices) { try { var result = DbContext.Insert(device); //var result = DbContext.Merge(device); Console.WriteLine($"Database Result: {result}"); /* var found = liteDbContext.KeyExists(device); * if (found) { liteDbContext.Update(device); } * else { liteDbContext.Insert(device); } */ } catch (Exception ex) { //todo: reimplement key exits lookup. } } #endregion //var x = devices.Select(x => x).OrderBy(y => y.IpAddress).ToList(); var tempWrite = DisplayTable(devices.Select(x => x).OrderBy(y => y.IpAddress).ToList()); displayResult.Display(tempWrite); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
static async Task RunAsync() { try { IDisplayResult displayResult = null; FactoryDisplay factoryDisplay = new FactoryDisplay(); HttpResponseMessage response = new HttpResponseMessage(); string userChoice = ""; do { Console.WriteLine("1.All Products "); Console.WriteLine("2.Products By CategoryId "); Console.WriteLine("3.All Category "); Console.WriteLine("\n"); Console.WriteLine("Please enter your choice either 1,2 or 3 "); var userOption = Convert.ToInt32(Console.ReadLine()); ResultType resultType = (ResultType)Enum.ToObject(typeof(ResultType), userOption); using (var client = new HttpClient()) { client.BaseAddress = new Uri("https://localhost:44384/"); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); //Get DisplayResult(i.e.ShowAllProducts) object from factory class. displayResult = factoryDisplay.GetDisplayResultType(resultType); if (displayResult != null) { //Get response from api. response = await displayResult.GetResponse(client); //Display in console. displayResult.Display(response); } } do { Console.WriteLine("\n"); Console.WriteLine("Do you want to continue - yes or no?"); userChoice = Console.ReadLine(); if (userChoice != "yes" && userChoice != "no") { Console.WriteLine("Invalid choice, please say yes or no"); } } while (userChoice != "yes" && userChoice != "no"); } while (userChoice == "yes"); } catch (UriFormatException ex) { Console.WriteLine("Your Web Api Url is incorrect:: {0}", ex.Message); Console.ReadLine(); } catch (HttpRequestException ex) { Console.WriteLine("Check whether your Web Api is running or not:: {0}", ex.Message); Console.ReadLine(); } catch (Exception ex) { Console.WriteLine("There is some error/exception:: {0}", ex.Message); Console.ReadLine(); } }