Example #1
0
        private static void SearchByStatus()
        {
            Console.WriteLine("Enter status to seacrh(Pending/Approved/Rejected)");
            var statusString = Console.ReadLine();
            var status       = EnumStringMap.ToEnum(statusString);

            if (status == Status.Other)
            {
                Console.WriteLine("Enter valid status");
                return;
            }

            var leaves = Leave.GetByStatus(status);

            Console.WriteLine();
            if (leaves.Count < 1)
            {
                Console.WriteLine("Not Found any matching leave");
                return;
            }
            leaves.ForEach(leave =>
            {
                Console.WriteLine(leave);
            });
        }
Example #2
0
        public static List <Leave> ReadLeaves()
        {
            var leaves = new List <Leave>();

            if (!Directory.Exists(Constants.LeaveCsvPath))
            {
                Console.WriteLine("Please provide correct path");
                return(leaves);
            }

            var csvFiles = Directory.GetFiles(Constants.LeaveCsvPath, "*.csv", SearchOption.TopDirectoryOnly);

            if (csvFiles.Length < 1)
            {
                return(leaves);
            }
            var line = "";

            try
            {
                using (var sr = new StreamReader(csvFiles[0]))
                {
                    var header = sr.ReadLine(); // To ignore headers in csv File.
                    while ((line = sr.ReadLine()) != null)
                    {
                        var coloms = line.Split(",");

                        var leave = new Leave()
                        {
                            Id      = int.Parse(coloms[0]),
                            Creator = new UserBase {
                                Name = coloms[1]
                            },
                            Manager = new UserBase {
                                Name = coloms[2]
                            },
                            Title       = coloms[3],
                            Description = coloms[4],
                            StartDate   = DateTime.Parse(coloms[5]),
                            EndDate     = DateTime.Parse(coloms[6]),
                            Status      = EnumStringMap.ToEnum(coloms[7])
                        };
                        leaves.Add(leave);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }
            return(leaves);
        }
Example #3
0
        internal static bool WriteLeave(List <Leave> leaves)
        {
            leaves = leaves.OrderBy(lv => lv.Id).ToList();

            try
            {
                DeleteLeaveFiles();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
                return(false);
            }
            try
            {
                using (var writer = File.AppendText(Constants.LeaveCsvFile))  // For colum Headers
                {
                    writer.WriteLine(string.Concat("Id", ",", "Creator", ",", "Manager", ",",
                                                   "Title", ",", "Description", ",", "Start-Date", ",", "End-Date", ",", "Status"));
                }

                foreach (var lv in leaves)
                {
                    using (var writer = File.AppendText(Constants.LeaveCsvFile))
                    {
                        writer.WriteLine(lv.Id + "," + lv.Creator.Name + "," + lv.Manager.Name + ","
                                         + lv.Title + "," + lv.Description + "," +
                                         lv.StartDate.ToShortDateString() + "," +
                                         lv.EndDate.ToShortDateString() + "," +
                                         EnumStringMap.ToString(lv.Status));
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
                return(false);
            }

            return(true);
        }