public async Task PersistUserRepoDataAsync(RepositoryData repositoryData) { if (!string.IsNullOrEmpty(repositoryData.Username) && !string.IsNullOrEmpty(repositoryData.RepositoryName)) { try { var newOrExistingCollection = Database.GetCollection <RepositoryData>(CollectionName); try { await newOrExistingCollection.ReplaceOneAsync(repositoryDataForAUser => repositoryDataForAUser.Username != null && repositoryDataForAUser.Username.ToLower() == repositoryData.Username.ToLower() && repositoryDataForAUser.RepositoryName != null && repositoryDataForAUser.RepositoryName.ToLower() == repositoryData.RepositoryName.ToLower(), repositoryData, new UpdateOptions { IsUpsert = true } ); } catch (Exception ex) { Console.WriteLine("Cannot save records."); throw ex; } } catch (Exception ex) { Console.WriteLine("Cannot create/get collection to save data into."); throw ex; } } else { Console.WriteLine("Netiher username nor repository name can be null."); throw new ArgumentException(); } }
private async Task PersistNewRecords(RepositoryData repositoryData) { try { await MongoDbConnection.Value.PersistUserRepoDataAsync(repositoryData); } catch (Exception ex) { Console.WriteLine("Cannot create connection to the database."); throw ex; } }
public async Task RunAllSteps(string username, string repositoryName) { Console.WriteLine("Fetching user and repository information from github API...\n"); var githubUserRepoData = await GithubConnection.FetchRepositoryData(username, repositoryName); var fetchedRepositoryData = new RepositoryData(githubUserRepoData, repositoryName, username); Console.WriteLine("Trying to save user and repository information into the database...\n"); await PersistNewRecords(fetchedRepositoryData); Console.WriteLine("Data saved in the database:\n"); var fetchedCommitsFormattedToDisplay = fetchedRepositoryData.ToString(); Console.WriteLine(fetchedCommitsFormattedToDisplay); }