Example #1
0
        public async Task <List <ExcelEntryResult> > AddExcelEntries(List <ExcelEntry> entries)
        {
            List <KeyValuePair <ExcelEntryResult, Task <XElement> > > storage = new List <KeyValuePair <ExcelEntryResult, Task <XElement> > >();
            List <Task> tasks = new List <Task>();

            foreach (ExcelEntry entry in entries)
            {
                TimeSpan?hours = entry.TimeTo - entry.TimeFrom;

                XElement content = new XElement("entry");
                content.Add(new XElement("type", "entry"));
                content.Add(new XElement("resource", entry.EmployeeCode));
                content.Add(new XElement("status", "Soft"));
                content.Add(new XElement("date", entry.Date?.ToString("yyyyMMdd")));
                content.Add(new XElement("timeFrom", entry.TimeFrom?.ToString(@"hh\:mm")));
                content.Add(new XElement("hours", hours?.TotalHours));
                content.Add(new XElement("project", entry.Project));
                content.Add(new XElement("breakdown", entry.Phase));

                Task <XElement> uploadTask = Task.Run(async() => await TimewaxApi.AddCalendarEntry(content));

                int.TryParse(entry.ID, out int id);

                storage.Add(new KeyValuePair <ExcelEntryResult, Task <XElement> >(
                                new ExcelEntryResult()
                {
                    Row = entry.Row, ID = id
                },
                                uploadTask));

                tasks.Add(uploadTask);
                Thread.Sleep(50);
            }
            await Task.WhenAll(tasks);

            List <ExcelEntryResult> list = new List <ExcelEntryResult>();

            foreach (KeyValuePair <ExcelEntryResult, Task <XElement> > pair in storage)
            {
                XElement responseContent = pair.Value.Result;

                ExcelEntryResult result = pair.Key;
                int.TryParse(responseContent.Element("id")?.Value, out int timeID);

                result.TimeID = timeID;
                result.Valid  = (responseContent.Element("valid").Value == "yes");
                list.Add(result);
            }
            return(list);
        }
Example #2
0
        public async Task <List <ExcelEntryResult> > RemoveExcelEntries(List <ExcelEntry> entries)
        {
            List <KeyValuePair <ExcelEntryResult, Task <XElement> > > storage = new List <KeyValuePair <ExcelEntryResult, Task <XElement> > >();
            List <Task> tasks = new List <Task>();

            foreach (ExcelEntry entry in entries)
            {
                Task <XElement> uploadTask = Task.Run(async() => await TimewaxApi.RemoveCalendarEntry(entry.TimeID));

                int.TryParse(entry.ID, out int id);
                int.TryParse(entry.TimeID, out int timeID);

                storage.Add(new KeyValuePair <ExcelEntryResult, Task <XElement> >(
                                new ExcelEntryResult()
                {
                    Row = entry.Row, ID = id, TimeID = timeID
                },
                                uploadTask));

                tasks.Add(uploadTask);
                Thread.Sleep(50);
            }
            await Task.WhenAll(tasks);


            List <ExcelEntryResult> list = new List <ExcelEntryResult>();

            foreach (KeyValuePair <ExcelEntryResult, Task <XElement> > pair in storage)
            {
                XElement responseContent = pair.Value.Result;

                ExcelEntryResult result = pair.Key;
                result.Valid = (responseContent.Element("valid").Value == "yes");
                list.Add(result);
            }
            return(list);
        }