public async Task <IActionResult> RefreshProjects() { IEnumerable <ProjectSAP> Projects = new List <ProjectSAP>(); List <String> ExistingProjects = new List <String>(); ExistingProjects = (from p in _context.Project select p.ProjectSapId).ToList(); using (HttpClient client = new HttpClient()) { client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var data = await client.GetAsync(string.Concat("http://api.dev.gbl/v3/", "projects")); data.EnsureSuccessStatusCode(); var stringResult = await data.Content.ReadAsStringAsync(); Projects = JsonConvert.DeserializeObject <IEnumerable <ProjectSAP> >(stringResult); foreach (ProjectSAP projectSAP in Projects) { if (projectSAP.projectStatus == "1-En attente" || projectSAP.projectStatus == "2-En cours" || projectSAP.projectStatus == "3-Terminé") { Budget budget = new Budget(); budget = CreateBudget(projectSAP.budget[0]); //foreach (ExpenseSAP exp in projectSAP.budget[0].expensesBudget) //{ // //ToDO verifier si expense exists déja ds la bd // CreateExpense(exp, budget); //} CreateProject(projectSAP, budget); ExistingProjects.Remove(RemoveUnusedDigitFromSAPProjectId(projectSAP.id_SAP)); } else { continue; } } if (ExistingProjects.Any()) { foreach (String projectSAPId in ExistingProjects) { Project projectToBeDeleted = (from p in _context.Project where p.ProjectSapId == projectSAPId select p).FirstOrDefault(); if (projectToBeDeleted != null) { _context.Project.Remove(projectToBeDeleted); } } } _context.SaveChanges(); return(new ObjectResult("Successfully added to data base...")); } }
public void RefreshNotifications() { IEnumerable <NotificationSAP> notifications = new List <NotificationSAP>(); notifications = GetNotificationsSAP().Result; List <String> ExistingNotificationsInDatabase = new List <String>(); ExistingNotificationsInDatabase = (from p in _context.Notification select p.NotificationSapId).ToList(); foreach (NotificationSAP notification in notifications) { Notification notificationEntitity = new Notification(); try { notificationEntitity = CreateNotification(notification); } catch (Exception ex) { Console.WriteLine(ex.Message); continue; } if (!VerifyIfNotificationAlreadyExistsInDataBase(notificationEntitity)) { _context.Notification.Add(notificationEntitity); if (notification.Partners.Any()) { foreach (Partner partner in notification.Partners) { try { NotificationPartner partnerEntity = CreatePartner(partner, notificationEntitity); _context.NotificationPartner.Add(partnerEntity); } catch (Exception ex) { Console.WriteLine(ex.Message); continue; } } } } else { if (VerifyIfNotificationAsBeenModified(notificationEntitity)) { updateNotificationInDatabase(notificationEntitity); } VerifyIfNotificationPartnerAsBeenModified(notification); } ExistingNotificationsInDatabase.Remove(notificationEntitity.NotificationSapId); if (notification.Tasks.Any()) { updateNotificationTasks(notification.Tasks, notificationEntitity); } } deleteUnexistingNotificationInSAP(ExistingNotificationsInDatabase); _context.SaveChanges(); }
public async Task <IActionResult> PostEmployees() { IEnumerable <EmployeeSAP> Employees = new List <EmployeeSAP>(); using (HttpClient client = new HttpClient()) { client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var data = await client.GetAsync(string.Concat("http://api.dev.gbl/v3/", "employees")); data.EnsureSuccessStatusCode(); var stringResult = await data.Content.ReadAsStringAsync(); Employees = JsonConvert.DeserializeObject <IEnumerable <EmployeeSAP> >(stringResult); foreach (EmployeeSAP employeeSAP in Employees) { Employe employee = new Employe(); employee.Department = employeeSAP.department; employee.Factory = employeeSAP.factory; employee.HiredDate = Convert.ToDateTime(employeeSAP.hiredDate); employee.IdSAP = employeeSAP.id_SAP.TrimStart('0'); employee.LeclercEmail = employeeSAP.leclercEmail; employee.Name = employeeSAP.name; employee.O365Id = employeeSAP.o365; employee.Picture = employeeSAP.picture; employee.Workload = Convert.ToInt32(double.Parse(employeeSAP.workload, System.Globalization.NumberStyles.AllowDecimalPoint, System.Globalization.NumberFormatInfo.InvariantInfo)); employee.Title = employeeSAP.title; //employee.SuperiorId = string.IsNullOrEmpty(employeeSAP.superior) ? (int?)null : int.Parse(employeeSAP.superior); employee.ProjectWorkRatio = 100; Employe employeeExists = _context.Employe.FirstOrDefault(x => x.IdSAP == employee.IdSAP); if (employeeExists != null) { if (employeeExists.Department == employee.Department && employeeExists.Factory == employee.Factory && employeeExists.HiredDate == employee.HiredDate && employeeExists.IdSAP == employee.IdSAP && employeeExists.LeclercEmail == employee.LeclercEmail && employeeExists.Name == employee.Name && employeeExists.O365Id == employee.O365Id && employeeExists.Picture == employee.Picture && employeeExists.Workload == employee.Workload && employeeExists.Title == employee.Title && //employeeExists.SuperiorId == employee.SuperiorId && employeeExists.ProjectWorkRatio == employee.ProjectWorkRatio) { continue; } else { employeeExists.Department = employee.Department; employeeExists.Factory = employee.Factory; employeeExists.HiredDate = employee.HiredDate; employeeExists.IdSAP = employee.IdSAP; employeeExists.LeclercEmail = employee.LeclercEmail; employeeExists.Name = employee.Name; employeeExists.O365Id = employee.O365Id; employeeExists.Picture = employee.Picture; employeeExists.Workload = employee.Workload; employeeExists.Title = employee.Title; //employeeExists.SuperiorId = employee.SuperiorId; employeeExists.ProjectWorkRatio = employee.ProjectWorkRatio; _context.Employe.Update(employeeExists); continue; } } else { _context.Add(employee); } } _context.SaveChanges(); return(new ObjectResult("Successfully added to data base...")); } }