From 9b25671707c16bbaaf8eda1b53f651fdfed6363c Mon Sep 17 00:00:00 2001 From: Derek Holloway Date: Mon, 14 Jul 2025 16:55:20 -0700 Subject: [PATCH] split out functions for readability --- src/Server/Services/DatabaseService/Resume.cs | 358 +++++------------- 1 file changed, 85 insertions(+), 273 deletions(-) diff --git a/src/Server/Services/DatabaseService/Resume.cs b/src/Server/Services/DatabaseService/Resume.cs index 2a88464..f7155f3 100644 --- a/src/Server/Services/DatabaseService/Resume.cs +++ b/src/Server/Services/DatabaseService/Resume.cs @@ -1,5 +1,6 @@ using BoredCareers.Entities; using MySql.Data.MySqlClient; +using Stripe.Terminal; using System.Data; using System.Data.Common; @@ -57,8 +58,7 @@ namespace BoredCareers.Services.DatabaseService { } public async Task GetResume(int ResumeID) { - Resume? resume = null; - + // Open connections for multi-threaded request MySqlConnection resumeConnection = GetConnection(); MySqlConnection ResumeExperienceConnection = GetConnection(); MySqlConnection ResumeExperienceBulletConnection = GetConnection(); @@ -70,6 +70,7 @@ namespace BoredCareers.Services.DatabaseService { MySqlConnection ResumeCertificationConnection = GetConnection(); MySqlConnection ResumeProjectConnection = GetConnection(); + // Open the connections Task resumeopen = resumeConnection.OpenAsync(); Task resumeexperienceopen = ResumeExperienceConnection.OpenAsync(); Task resumeexperiencebulletopen = ResumeExperienceBulletConnection.OpenAsync(); @@ -81,9 +82,11 @@ namespace BoredCareers.Services.DatabaseService { Task resumecertifcationopen = ResumeCertificationConnection.OpenAsync(); Task resumeprojectopen = ResumeProjectConnection.OpenAsync(); + // Wait for all the connections to open await Task.WhenAll(resumeopen, resumeexperienceopen, resumeexperiencebulletopen, resumemilitaryopen, resumemilitarybulletopen, resumeeducationopen, resumeskillopen, resumelanguageopen, resumecertifcationopen, resumeprojectopen); + // Setup the commands for the connections MySqlCommand resumeCommand = new MySqlCommand("SELECT * FROM Resume WHERE ID = @ResumeID;", resumeConnection); MySqlCommand ResumeExperienceCommand = new MySqlCommand("SELECT * FROM ResumeExperience WHERE ResumeID = @ResumeID;", ResumeExperienceConnection); MySqlCommand ResumeExperienceBulletCommand = new MySqlCommand("SELECT * FROM ResumeExperienceBullet WHERE ResumeID = @ResumeID;", ResumeExperienceBulletConnection); @@ -95,6 +98,7 @@ namespace BoredCareers.Services.DatabaseService { MySqlCommand ResumeCertificationCommand = new MySqlCommand("SELECT * FROM ResumeCertification WHERE ResumeID = @ResumeID;", ResumeCertificationConnection); MySqlCommand ResumeProjectCommand = new MySqlCommand("SELECT * FROM ResumeProject WHERE ResumeID = @ResumeID;", ResumeProjectConnection); + // Add parameters to the commands resumeCommand.Parameters.AddWithValue("@ResumeID", ResumeID); ResumeExperienceCommand.Parameters.AddWithValue("@ResumeID", ResumeID); ResumeExperienceBulletCommand.Parameters.AddWithValue("@ResumeID", ResumeID); @@ -106,7 +110,8 @@ namespace BoredCareers.Services.DatabaseService { ResumeCertificationCommand.Parameters.AddWithValue("@ResumeID", ResumeID); ResumeProjectCommand.Parameters.AddWithValue("@ResumeID", ResumeID); - Task resumeReader = resumeCommand.ExecuteReaderAsync(); + // Run the commands + Task ResumeReader = resumeCommand.ExecuteReaderAsync(); Task ResumeExperienceReader = ResumeExperienceCommand.ExecuteReaderAsync(); Task ResumeExperienceBulletReader = ResumeExperienceBulletCommand.ExecuteReaderAsync(); Task ResumeMilitaryReader = ResumeMilitaryCommand.ExecuteReaderAsync(); @@ -117,293 +122,100 @@ namespace BoredCareers.Services.DatabaseService { Task ResumeCertificationReader = ResumeCertificationCommand.ExecuteReaderAsync(); Task ResumeProjectReader = ResumeProjectCommand.ExecuteReaderAsync(); - await Task.WhenAll(resumeReader, ResumeExperienceReader, ResumeExperienceBulletReader, ResumeMilitaryReader, ResumeMilitaryBulletReader, + // Wait for all the commands to process + await Task.WhenAll(ResumeReader, ResumeExperienceReader, ResumeExperienceBulletReader, ResumeMilitaryReader, ResumeMilitaryBulletReader, ResumeEducationReader, ResumeSkillReader, ResumeLanguageReader, ResumeCertificationReader, ResumeProjectReader); - using (DbDataReader reader = await resumeReader) { - while (await reader.ReadAsync()) { - if (reader == null) { break; } - int _id = reader.GetInt32("ID"); - int _accountid = reader.GetInt32("AccountID"); - string _name = reader.GetString("Name"); - string _field = reader.GetString("Field"); - string _email = reader.GetString("Email"); - string _phonenumber = reader.GetString("PhoneNumber"); - string _postalcode = reader.GetString("PostalCode"); - string _country = reader.GetString("Country"); - string _state = reader.GetString("StateOrRegion"); - string _city = reader.GetString("City"); - bool _isactive = reader.GetBoolean("IsActive"); - resume = new Resume() { - ID = _id, - AccountID = _accountid, - Name = _name, - Field = _field, - Email = _email, - PhoneNumber = _phonenumber, - PostalCode = _postalcode, - Country = _country, - StateOrRegion = _state, - City = _city, - IsActive = _isactive - }; - } - } + // Get Resume + Resume? resume = await GetResume( await ResumeReader ); + if (resume != null) { - if (resume == null) { - return resume; - } + // Get Resume Parts + ResumeExperience[] experience = await GetResumeExperience(await ResumeExperienceReader); + ResumeExperienceBullet[] experienceBullets = await GetResumeExperienceBullets(await ResumeExperienceBulletReader); + ResumeMilitary? military = await GetResumeMilitary(await ResumeMilitaryReader); + ResumeMilitaryBullet[] militaryBullets = await GetResumeMilitaryBullets(await ResumeMilitaryBulletReader); + ResumeEducation[] education = await GetResumeEducation(await ResumeEducationReader); + ResumeSkill[] skills = await GetResumeSkills(await ResumeSkillReader); + ResumeLanguage[] languages = await GetResumeLanguages(await ResumeLanguageReader); + ResumeCertification[] certs = await GetResumeCertification(await ResumeCertificationReader); + ResumeProject[] projects = await GetResumeProjects(await ResumeProjectReader); - List experienceBullets = new List(); - using (DbDataReader reader = await ResumeExperienceBulletReader) { - while (await reader.ReadAsync()) { - if (reader == null) { break; } - int _id = reader.GetInt32("ID"); - int _resumeid = reader.GetInt32("ResumeID"); - int _experienceid = reader.GetInt32("ResumeExperienceID"); - string _jobfunction = reader.GetString("JobFunction"); - experienceBullets.Add( new ResumeExperienceBullet() { - ID = _id, - ResumeID = _resumeid, - ResumeExperienceID = _experienceid, - JobFunction = _jobfunction - }); + // Split into grouped lists and add to experience + Dictionary groupedExperienceBullets = experienceBullets.GroupBy(b => b.ResumeExperienceID).ToDictionary(g => g.Key, g => g.ToArray()); + foreach (ResumeExperience cur in experience) { + cur.ExperienceBullets = groupedExperienceBullets[cur.ID]; } - } - Dictionary> groupedExperienceBullets = experienceBullets - .GroupBy(b => b.ResumeExperienceID).ToDictionary(g => g.Key, g => g.ToList()); - using (DbDataReader reader = await ResumeExperienceReader) { - List experience = new List(); - while (await reader.ReadAsync()) { - if (reader == null) { - break; - } - int _id = reader.GetInt32("ID"); - int _resumeid = reader.GetInt32("ResumeID"); - string _jobtitle = reader.GetString("JobTitle"); - string _company = reader.GetString("Company"); - string _postalcode = reader.GetString("PostalCode"); - string _country = reader.GetString("Country"); - string _state = reader.GetString("StateOrRegion"); - string _city = reader.GetString("City"); - DateTime _datestarted = reader.GetDateTime("DateStarted"); - bool _stillempoyed = reader.GetBoolean("StillEmployed"); - DateTime _dateended = reader.GetDateTime("DateEnded"); - experience.Add(new ResumeExperience() { - ID = _id, - ResumeID = _resumeid, - JobTitle = _jobtitle, - Company = _company, - PostalCode = _postalcode, - Country = _country, - StateOrRegion = _state, - City = _city, - DateStarted = _datestarted, - StillEmployed = _stillempoyed, - DateEnded = _dateended, - ExperienceBullets = groupedExperienceBullets[_id].ToArray() - }); - } - resume.Experience = experience.ToArray(); - } - List militaryBullets = new List(); - using (DbDataReader reader = await ResumeMilitaryBulletReader) { - while (await reader.ReadAsync()) { - if (reader == null) { break; } - int _id = reader.GetInt32("ID"); - int _resumeid = reader.GetInt32("ResumeID"); - int _experienceid = reader.GetInt32("ResumeMilitaryID"); - string _achievement = reader.GetString("Achevement"); - string _description = reader.GetString("Description"); - militaryBullets.Add(new ResumeMilitaryBullet() { - ID = _id, - ResumeID = _resumeid, - ResumeMilitaryID = _experienceid, - Achevement = _achievement, - Description = _description - }); - } - } - Dictionary> groupedMilitaryBullets = militaryBullets - .GroupBy(b => b.ResumeMilitaryID).ToDictionary(g => g.Key, g => g.ToList()); - using (DbDataReader reader = await ResumeMilitaryReader) { - ResumeMilitary? military = null; - while (await reader.ReadAsync()) { - if (reader == null) { break; } - int _id = reader.GetInt32("ID"); - int _resumeid = reader.GetInt32("ResumeID"); - string _country = reader.GetString("Country"); - string _rank = reader.GetString("Rank"); - DateTime _datestarted = reader.GetDateTime("DateStarted"); - bool _stillServing = reader.GetBoolean("StillServing"); - DateTime _dateended = reader.GetDateTime("DateEnded"); - military = new ResumeMilitary() { - ID = _id, - ResumeID = _resumeid, - Country = _country, - Rank = _rank, - DateStarted = _datestarted, - StillServing = _stillServing, - DateEnded = _dateended, - MillitaryBullets = groupedMilitaryBullets[_id].ToArray() - }; - } + // Add the parts to the resume if (military != null) { + military.MillitaryBullets = militaryBullets; resume.Millitary = military; } - } + resume.Experience = experience; + resume.Educations = education; + resume.Skills = skills; + resume.Languages = languages; + resume.Certification = certs; + resume.Projects = projects; - using (DbDataReader reader = await ResumeEducationReader) { - List? education = new List(); - while (await reader.ReadAsync()) { - if (reader == null) { break; } - int _id = reader.GetInt32("ID"); - int _resumeid = reader.GetInt32("ResumeID"); - string _degreetype = reader.GetString("DegreeType"); - string _degreefield = reader.GetString("DegreeField"); - string _school = reader.GetString("School"); - string _postalcode = reader.GetString("PostalCode"); - string _country = reader.GetString("Country"); - string _state = reader.GetString("StateOrRegion"); - string _city = reader.GetString("City"); - DateTime _datestarted = reader.GetDateTime("DateStarted"); - bool _stillstudying = reader.GetBoolean("StillStudying"); - DateTime _dateended = reader.GetDateTime("DateEnded"); - education.Add( new ResumeEducation { - ID = _id, - ResumeID = _resumeid, - DegreeType = _degreetype, - DegreeField = _degreefield, - School = _school, - PostalCode = _postalcode, - Country = _country, - StateOrRegion = _state, - City = _city, - DateStarted = _datestarted, - StillStudying = _stillstudying, - DateEnded = _dateended - } ); - } - resume.Educations = education.ToArray(); + return resume; } - - using (DbDataReader reader = await ResumeSkillReader) { - List? skills = new List(); - while (await reader.ReadAsync()) { - if (reader == null) { break; } - int _id = reader.GetInt32("ID"); - int _resumeid = reader.GetInt32("ResumeID"); - string _name = reader.GetString("Name"); - string _description = reader.GetString("Description"); - skills.Add( new ResumeSkill { - ID = _id, - ResumeID = _resumeid, - Name = _name, - Description = _description - } ); - } - resume.Skills = skills.ToArray(); - } - - using (DbDataReader reader = await ResumeLanguageReader) { - List? languages = new List(); - while (await reader.ReadAsync()) { - if (reader == null) { break; } - int _id = reader.GetInt32("ID"); - int _resumeid = reader.GetInt32("ResumeID"); - string _language = reader.GetString("Language"); - string _proficiency = reader.GetString("Proficiency"); - languages.Add( new ResumeLanguage { - ID = _id, - ResumeID = _resumeid, - Language = _language, - Proficiency = _proficiency - } ); - } - resume.Languages = languages.ToArray(); - } - - using (DbDataReader reader = await ResumeCertificationReader) { - List? certs = new List(); - while (await reader.ReadAsync()) { - if (reader == null) { break; } - int _id = reader.GetInt32("ID"); - int _resumeid = reader.GetInt32("ResumeID"); - string _name = reader.GetString("Name"); - string _url = reader.GetString("VerificationURL"); - string _description = reader.GetString("Description"); - certs.Add( new ResumeCertification { - ID = _id, - ResumeID = _resumeid, - Name = _name, - VerificationURL = _url, - Description = _description - } ); - } - resume.Certification = certs.ToArray(); - } - - using (DbDataReader reader = await ResumeProjectReader) { - List? projects = new List(); - while (await reader.ReadAsync()) { - if (reader == null) { break; } - int _id = reader.GetInt32("ID"); - int _resumeid = reader.GetInt32("ResumeID"); - string _name = reader.GetString("Name"); - string _url = reader.GetString("URL"); - string _description = reader.GetString("Description"); - projects.Add( new ResumeProject { - ID = _id, - ResumeID = _resumeid, - Name = _name, - URL = _url, - Description = _description - } ); - } - resume.Projects = projects.ToArray(); - } - - return resume; + return null; } - public async Task SetResume( Resume resume ) { - using( MySqlConnection connection = GetConnection() ) { - connection.Open(); + public async Task SetResume(Resume resume) { - string command = @" - INSERT INTO Account - (ID,UserName,Email,EmailVerified,PasswordHash,FailedPasswordLock,PasswordAttempts,CurrentPasswordAttempts,Role,EmailToken,DataServer) - VALUES - (@ID,@UserName,@Email,@EmailVerified,@PasswordHash,@FailedPasswordLock,@PasswordAttempts,@CurrentPasswordAttempts,@Role,@EmailToken,@DataServer); - ON DUPLICATE KEY UPDATE - UserName = @UserName, - Email = @Email, - EmailVerified = @EmailVerified, - PasswordHash = @PasswordHash, - FailedPasswordLock = @FailedPasswordLock, - PasswordAttempts = @PasswordAttempts, - CurrentPasswordAttempts = @CurrentPasswordAttempts, - Role = @Role, - EmailToken = @EmailToken; - DataServer = @DataServer; - "; + // Open connections for multi-threaded request + MySqlConnection resumeConnection = GetConnection(); + MySqlConnection ResumeExperienceConnection = GetConnection(); + MySqlConnection ResumeExperienceBulletConnection = GetConnection(); + MySqlConnection ResumeMilitaryConnection = GetConnection(); + MySqlConnection ResumeMilitaryBulletConnection = GetConnection(); + MySqlConnection ResumeEducationConnection = GetConnection(); + MySqlConnection ResumeSkillConnection = GetConnection(); + MySqlConnection ResumeLanguageConnection = GetConnection(); + MySqlConnection ResumeCertificationConnection = GetConnection(); + MySqlConnection ResumeProjectConnection = GetConnection(); - MySqlCommand cmd = new MySqlCommand( command , connection); - cmd.Parameters.AddWithValue("@ID", Profile.ID); - cmd.Parameters.AddWithValue("@UserName", Profile.UserName); - cmd.Parameters.AddWithValue("@Email", Profile.Email); - cmd.Parameters.AddWithValue("@EmailVerified", Profile.EmailVerified); - cmd.Parameters.AddWithValue("@PasswordHash", Profile.PasswordHash); - cmd.Parameters.AddWithValue("@FailedPasswordLock", Profile.FailedPasswordLock); - cmd.Parameters.AddWithValue("@PasswordAttempts", Profile.PasswordAttempts); - cmd.Parameters.AddWithValue("@CurrentPasswordAttempts", Profile.CurrentPasswordAttempts); - cmd.Parameters.AddWithValue("@Role", Profile.Role); - cmd.Parameters.AddWithValue("@EmailToken", Profile.EmailToken); - cmd.Parameters.AddWithValue("@DataServer", Profile.DataServer); + // Open the connections + Task resumeopen = resumeConnection.OpenAsync(); + Task resumeexperienceopen = ResumeExperienceConnection.OpenAsync(); + Task resumeexperiencebulletopen = ResumeExperienceBulletConnection.OpenAsync(); + Task resumemilitaryopen = ResumeMilitaryConnection.OpenAsync(); + Task resumemilitarybulletopen = ResumeMilitaryBulletConnection.OpenAsync(); + Task resumeeducationopen = ResumeEducationConnection.OpenAsync(); + Task resumeskillopen = ResumeSkillConnection.OpenAsync(); + Task resumelanguageopen = ResumeLanguageConnection.OpenAsync(); + Task resumecertifcationopen = ResumeCertificationConnection.OpenAsync(); + Task resumeprojectopen = ResumeProjectConnection.OpenAsync(); - await cmd.ExecuteNonQueryAsync(); + // Wait for all the connections to open + await Task.WhenAll(resumeopen, resumeexperienceopen, resumeexperiencebulletopen, resumemilitaryopen, resumemilitarybulletopen, + resumeeducationopen, resumeskillopen, resumelanguageopen, resumecertifcationopen, resumeprojectopen); + + // Get all the experience bullets and run all the updates + List bullets = new List(); + foreach (ResumeExperience cur in resume.Experience) { + foreach (ResumeExperienceBullet bullet in cur.ExperienceBullets) { + bullets.Add(bullet); + } } + Task ResumeExperienceBulletTask = SetResumeExperienceBullets(ResumeExperienceBulletConnection, bullets.ToArray()); + Task ResumeTask = SetResume(resumeConnection, resume); + Task ResumeExperienceTask = SetResumeExperience(ResumeExperienceConnection, resume.Experience); + Task ResumeMilitaryTask = SetResumeMilitary(ResumeMilitaryConnection, resume.Millitary); + Task ResumeMilitaryBulletTask = SetResumeMilitaryBullets(ResumeMilitaryBulletConnection, resume.Millitary.MillitaryBullets); + Task ResumeEducationTask = SetResumeEducation(ResumeEducationConnection, resume.Educations); + Task ResumeSkllTask = SetResumeSkills(ResumeSkillConnection, resume.Skills); + Task ResumeLanguageTask = SetResumeLanguages(ResumeLanguageConnection, resume.Languages); + Task ResumeCertTask = SetResumeCertification(ResumeCertificationConnection, resume.Certification); + Task ResumeProjectTask = SetResumeProjects(ResumeProjectConnection, resume.Projects); + + // Allow update to finish before closing process + await Task.WhenAll(ResumeTask, ResumeExperienceTask, ResumeExperienceBulletTask, ResumeMilitaryTask, ResumeMilitaryBulletTask, + ResumeEducationTask, ResumeSkllTask, ResumeLanguageTask, ResumeCertTask, ResumeProjectTask); + } public async Task DeleteResume( int ID ) {