private async Task CreateProject() { try { List <KeyValuePair <string, string> > values = new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("Title", Title_), new KeyValuePair <string, string>("Project Type", TypeText), new KeyValuePair <string, string>("Project Status", StatusText), new KeyValuePair <string, string>("Address", Address) }; if (FieldValidation.ValidateFields(values)) { CanSaveProject = false; ProjectModel projectData = new ProjectModel() { Title = Title_, ProjectTypeID = SelectedType?.ID, Type = SelectedType == null ? new TypeModel { Title = TypeText, CreatedBy = ProjectSelection.LoggedInUser.Name } : null, ProjectStatusID = SelectedStatus?.ID, Status = SelectedStatus == null ? new StatusModel { Title = StatusText, CreatedBy = ProjectSelection.LoggedInUser.Name } : null, StartDate = StartDate, DueDate = DueDate, Address = Address, CreatedBy = ProjectSelection.LoggedInUser.Name, CreatedOn = DateTime.Now }; HttpResponseMessage result = await apiHelper.PostProject(ProjectSelection.LoggedInUser.Token, projectData).ConfigureAwait(false); if (result.IsSuccessStatusCode) { MessageBox.Show($"Project Saved Successfully", "Success", MessageBoxButton.OK, MessageBoxImage.Information); Application.Current.Dispatcher.Invoke((Action) delegate { new Action(async() => await ProjectSelection.GetProjects(null))(); ClosePopupCommand.Execute(null); }); } else { MessageBox.Show("Error in saving Project", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } CanSaveProject = true; } } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); CanSaveProject = true; } }
private async Task CreateProject() { try { List <KeyValuePair <string, string> > values = new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("Title", Title), new KeyValuePair <string, string>("Project Type", TypeText), new KeyValuePair <string, string>("Project Status", StatusText), new KeyValuePair <string, string>("Firm", FirmText), new KeyValuePair <string, string>("Address", Address) }; if (FieldValidation.ValidateFields(values)) { if (Contractors.Where(c => c.IsChecked).Count() <= 0) { MessageBox.Show("Please add atleast 1 Contractor to the Project", "Add Contractor", MessageBoxButton.OK, MessageBoxImage.Warning); } else if (Suppliers.Where(c => c.IsChecked).Count() <= 0) { MessageBox.Show("Please add atleast 1 Supplier to the Project", "Add Supplier", MessageBoxButton.OK, MessageBoxImage.Warning); } else { CanSaveProject = false; List <ProjectContractorsModel> projectContractors = new List <ProjectContractorsModel>(); List <ProjectSuppliersModel> projectSuppliers = new List <ProjectSuppliersModel>(); Contractors.Where(s => s.IsChecked).ToList().ForEach(s => projectContractors.Add( new ProjectContractorsModel { ProjectID = ID, ContractorID = s.ID, })); Suppliers.Where(s => s.IsChecked).ToList().ForEach(s => projectSuppliers.Add( new ProjectSuppliersModel { ProjectID = ID, SupplierID = s.ID, })); ProjectModel projectData = new ProjectModel() { Title = Title, Description = Description, ProjectTypeID = SelectedType?.ID, Type = SelectedType == null ? new TypeModel { Title = TypeText, CreatedBy = ParentLayout.LoggedInUser.Name } : null, ProjectStatusID = SelectedStatus?.ID, Status = SelectedStatus == null ? new StatusModel { Title = StatusText, CreatedBy = ParentLayout.LoggedInUser.Name } : null, FirmID = SelectedFirm?.ID, Firm = SelectedFirm == null ? new FirmModel { Name = FirmText, CreatedBy = ParentLayout.LoggedInUser.Name } : null, StartDate = StartDate, DueDate = DueDate, Address = Address, TeamID = SelectedTeam?.ID, Contractors = projectContractors, Suppliers = projectSuppliers }; HttpResponseMessage result = null; if (isUpdate) { projectData.ID = ID; projectData.CreatedBy = SelectedProject.CreatedBy; projectData.CreatedOn = SelectedProject.CreatedOn; projectData.ModifiedBy = ParentLayout.LoggedInUser.Name; projectData.ModifiedOn = DateTime.Now; result = await apiHelper.PutProject(ParentLayout.LoggedInUser.Token, projectData).ConfigureAwait(false); } else { projectData.CreatedBy = ParentLayout.LoggedInUser.Name; projectData.CreatedOn = DateTime.Now; result = await apiHelper.PostProject(ParentLayout.LoggedInUser.Token, projectData).ConfigureAwait(false); } if (result.IsSuccessStatusCode) { MessageBox.Show($"Project Saved Successfully", "Success", MessageBoxButton.OK, MessageBoxImage.Information); await GetProjects(); await ParentLayout.GetProjects(); IsUpdate = false; ClearFields(); await GetContractors(); await GetSuppliers(); await GetTypes(); await GetStatuses(); await GetFirms(); } else { MessageBox.Show("Error in saving Project", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } CanSaveProject = true; } } } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); CanSaveProject = true; } }