コード例 #1
0
        static void MainMenu()
        {
            // create prompt
            Console.Title = "CHIPS QuickLog";
            Console.Clear();
            StringBuilder prompt = new StringBuilder();

            // notify user if api is empty (first run)
            if (apiKey == "")
            {
                WriteConsole("The file holding the API key (" + ConfigurationManager.AppSettings.Get("ApiPath") + ") is empty or was just created.");
                WriteConsole("Add the Chips Service Manager API key to this file and try running again.");
            }
            WriteConsole(CONSOLE_HEADER);
            WriteConsole($"(Employees: {employees.Count})\n");

            if (employees.Count > MAX_COUNT)
            {
                WriteConsole($"{employees.Count - MAX_COUNT} employee(s) will not be displayed.");
                WriteConsole($"Manually adding employees to {ConfigurationManager.AppSettings.Get("EmpPath")} is not recommended.");
                WriteConsole($"Please remove employees to clean up the list.\n");
            }
            // list employees
            if (employees.Count > 0)
            {
                WriteConsole("Press the number next to your name to log a consultation:\n");
                for (int i = 0; i < Math.Min(employees.Count, MAX_COUNT); i++)
                {
                    WriteConsole($"[{i + 1}] {employees[i].EmployeeName}");
                }
                WriteConsole();
            }
            // list additional commands
            if (CanAddEmployee())
            {
                WriteConsole($"Press [{KEY_EMP_ADD}] to add an employee.");
            }
            if (CanRemoveEmployee())
            {
                WriteConsole($"Press [{KEY_EMP_REMOVE}] to remove an employee.");
            }
            WriteConsole($"Press [Esc] to quit.\n");

            // accept input
            do
            {
                // Wait for key and store
                userInput = Console.ReadKey(true);

                // check add/removes
                if (CanAddEmployee() && userInput.Key == KEY_EMP_ADD)
                {
                    AddEmployee();
                    // empty userInput to avoid Esc cascading and quitting app
                    userInput = new ConsoleKeyInfo();
                    break;
                }
                if (CanRemoveEmployee() && userInput.Key == KEY_EMP_REMOVE)
                {
                    RemoveEmployee();
                    // empty userInput to avoid Esc cascading and quitting app
                    userInput = new ConsoleKeyInfo();
                    break;
                }

                // check through available number keys
                for (int i = 1; i <= employees.Count; i++)
                {
                    // submit consultation log if keycode matches
                    if (userInput.Key.Equals(Enum.Parse(typeof(ConsoleKey), "D" + i.ToString())))
                    {
                        // submit consultation and get copy of consultation retrieved from api reponse
                        Consultation consultation = SubmitConsultation(employees[i - 1]).Result;
                        WriteConsole("Press any key to exit or wait five seconds.");
                        int waitCounter = 0;
                        while (!Console.KeyAvailable && waitCounter < 10) // counter increments every half second
                        {
                            Thread.Sleep(500);
                            waitCounter++;
                        }
                        Environment.Exit(0);
                    }
                }
            } while (userInput.Key != ConsoleKey.Escape);
        }
コード例 #2
0
        static async Task <Consultation> SubmitConsultation(Employee emp)
        {
            // save consultation locally to csv
            FileStream    logFs = null;
            StreamWriter  logSw = null;
            StringBuilder output;

            try
            {
                string logPath = ConfigurationManager.AppSettings.Get("LogPath");
                logFs  = new FileStream(logPath, FileMode.Append, FileAccess.Write);
                logSw  = new StreamWriter(logFs);
                output = new StringBuilder();

                // initialize csv if log is empty
                if (logFs.Length == 0)
                {
                    output.Append("datetime,employeename,username\n");
                }

                string logDateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

                // append datetime and employee name
                output.Append($"{logDateTime},{emp.EmployeeName},{emp.Username}\n");
                // write to file
                logSw.Write(output);
                // show confirmation
                WriteConsole($"Consultation logged locally.");
            }
            catch (IOException ioe)
            {
                WriteConsole("ERROR: Consultation failed to log locally.");
                WriteConsole(ioe.Message);
            }
            finally
            {
                if (logSw != null)
                {
                    logSw.Close();
                }
                if (logFs != null)
                {
                    logFs.Close();
                }
            }

            WriteConsole($"Connecting to database...");

            // upload consultation to db using api
            Consultation con = new Consultation(DateTime.Now, emp.Username);

            try
            {
                HttpResponseMessage response = await client.PostAsJsonAsync($"api/consultation", con);

                response.EnsureSuccessStatusCode();
                // return consultation from response
                con = await response.Content.ReadAsAsync <Consultation>();

                WriteConsole($"Consultation logged to database ({con.UserName} at {con.Time})");
            }
            catch (HttpRequestException)
            {
                WriteConsole("ERROR: Could not connect to database. Please check connection.");
            }

            return(con);
        }