From 9bba0ebcc1d82850f0521b3d464091582aae6fa6 Mon Sep 17 00:00:00 2001 From: Im_Alpha Date: Tue, 14 Dec 2021 22:24:10 -0800 Subject: [PATCH 1/2] initial script for dummy bulk data --- Source Code/FileExtract.cs | 41 +++++++++ .../Implementations/UiPrint.cs | 5 ++ .../AccountService.cs | 8 ++ .../SystemAccountManager.cs | 51 ++++++++++- .../UnitTest1.cs | 6 +- Source Code/main/Controller.cs | 3 +- Source Code/main/ExtractBulkOp.cs | 90 +++++++++++++++++++ Source Code/main/GenerateOpFile.cs | 70 +++++++++++++++ 8 files changed, 268 insertions(+), 6 deletions(-) create mode 100644 Source Code/FileExtract.cs create mode 100644 Source Code/main/ExtractBulkOp.cs create mode 100644 Source Code/main/GenerateOpFile.cs diff --git a/Source Code/FileExtract.cs b/Source Code/FileExtract.cs new file mode 100644 index 0000000..0f214ae --- /dev/null +++ b/Source Code/FileExtract.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TeamHobby.HobbyProjectGenerator.DataAccess; +using TeamHobby.HobbyProjectGenerator.UserManagement; + +namespace main +{ + internal class FileExtract + { + public static void Main(string[] args) + { + string[] lines = { "First line", "Second line", "Third line" }; + using StreamWriter file = new ("BulkRequest.txt"); + + var serviceTest = new AccountService(); + string dbType = "sql"; + RDSFactory dbFactory = new RDSFactory(); + + + // Testing Data Access Layer + string dbInfo = "DRIVER={MariaDB ODBC 3.1 Driver};" + + "TCPIP=1;" + + "SERVER=localhost;" + + "DATABASE=hobby;" + + "UID=root;" + + "PASSWORD=hobby;" + + "OPTION=3"; + IDataSource datasource = dbFactory.getDataSource(dbType, dbInfo); + + // Create a file + for (int i = 0; i < 10000; i++) + { + var TestAcc = new UserAccount("newUser" + $"{i}", "4567", $"email{i}@a.com", "regular", DateTime.Now); + file.Write(serviceTest.CreateUserRecord(TestAcc, "Rifat", datasource)); + } + } + } +} diff --git a/Source Code/TeamHobby.HobbyProjectGenerator.DataAccess/Implementations/UiPrint.cs b/Source Code/TeamHobby.HobbyProjectGenerator.DataAccess/Implementations/UiPrint.cs index 6d06cb8..d9f19aa 100644 --- a/Source Code/TeamHobby.HobbyProjectGenerator.DataAccess/Implementations/UiPrint.cs +++ b/Source Code/TeamHobby.HobbyProjectGenerator.DataAccess/Implementations/UiPrint.cs @@ -6,6 +6,9 @@ namespace TeamHobby.HobbyProjectGenerator.DataAccess { public void UserManagementMenu(string username) { + // File must be within the main folder path + // Get the current directory. + string path = Directory.GetCurrentDirectory(); // Menu for all UserManagement options int menu = 0; Console.WriteLine("-------------------------------------\n"); @@ -23,6 +26,8 @@ namespace TeamHobby.HobbyProjectGenerator.DataAccess menu += 1; Console.WriteLine(menu + ") Enable an account.\n"); menu += 1; + Console.WriteLine(menu + $") Bulk operation from file within the directory.\n{path}\\BulkOps\n"); + menu += 1; Console.WriteLine(menu + ") View log path.\n"); menu += 1; Console.WriteLine(menu + ") View archive path.\n"); diff --git a/Source Code/TeamHobby.HobbyProjectGenerator.UserManagement/AccountService.cs b/Source Code/TeamHobby.HobbyProjectGenerator.UserManagement/AccountService.cs index cf332f7..6f4a942 100644 --- a/Source Code/TeamHobby.HobbyProjectGenerator.UserManagement/AccountService.cs +++ b/Source Code/TeamHobby.HobbyProjectGenerator.UserManagement/AccountService.cs @@ -98,5 +98,13 @@ namespace TeamHobby.HobbyProjectGenerator.UserManagement return false; } } + public bool BulkOperation() + { + // List choices of operations + List ops = new List {"Create Account", "Edit Account", + "Delete Account", "Disable Account", "Enable Account"}; + + return true; + } } } diff --git a/Source Code/TeamHobby.HobbyProjectGenerator.UserManagement/SystemAccountManager.cs b/Source Code/TeamHobby.HobbyProjectGenerator.UserManagement/SystemAccountManager.cs index 49b0e48..2743dd9 100644 --- a/Source Code/TeamHobby.HobbyProjectGenerator.UserManagement/SystemAccountManager.cs +++ b/Source Code/TeamHobby.HobbyProjectGenerator.UserManagement/SystemAccountManager.cs @@ -11,6 +11,13 @@ namespace TeamHobby.HobbyProjectGenerator.UserManagement { public class SystemAccountManager { + public bool BulkOperation() + { + // List choices of operations + List ops = new List {"Create Account", "Edit Account", + "Delete Account", "Disable Account", "Enable Account"}; + return true; + } public string IsInputValid(string checkUN, string checkPWD) { // Create bool variables to check if username and password are valid @@ -304,11 +311,51 @@ namespace TeamHobby.HobbyProjectGenerator.UserManagement return "User does not exist"; } break; - // View logs + // Bulk operation through file case 6: + AccountService bulkOP = new AccountService(); + // Get path to defualt bin location + string path = Directory.GetCurrentDirectory(); + while (true) + { + // Get name of file and update path to correct folder + Console.WriteLine("Please input the name of the file:(Example.txt)"); + string filename = $"{path}\\BulkOps\\{Console.ReadLine()}"; + + // Read file + FileInfo fileInfo = new FileInfo(filename); + + // Get filesize + long fileSize = filename.Length; + long fileSizeGB = fileSize / (1024 * 1024); + //Console.WriteLine($"File size is {fileSize}kb"); + + // Check if input is empty + if (filename == null) + { + Console.WriteLine("Invalid file name.\n"); + } + // Check if file is over 2GB + else if (fileSizeGB > 2) + { + Console.WriteLine($"File size is {fileSizeGB}GB\n"); + Console.WriteLine("File is too large, please enter a smaller file.\n"); + } + else + { + // Begin bulk operation + bulkOP.BulkOperation(); + + + break; + } + } + break; + // View logs + case 7: break; // View archive - case 7: + case 8: break; default: Console.WriteLine("Invalid input.\nPlease enter a valid option.\n"); diff --git a/Source Code/TeamHobby.HobbyProjectGenerator/TeamHobby.UserManagement.xTests/UnitTest1.cs b/Source Code/TeamHobby.HobbyProjectGenerator/TeamHobby.UserManagement.xTests/UnitTest1.cs index 60baa67..f983481 100644 --- a/Source Code/TeamHobby.HobbyProjectGenerator/TeamHobby.UserManagement.xTests/UnitTest1.cs +++ b/Source Code/TeamHobby.HobbyProjectGenerator/TeamHobby.UserManagement.xTests/UnitTest1.cs @@ -31,7 +31,7 @@ namespace TeamHobby.UserManagement.xTests "SERVER=localhost;" + "DATABASE=hobby;" + "UID=root;" + - "PASSWORD=hobby;" + + "PASSWORD=Teamhobby;" + "OPTION=3"; IDataSource datasource = dbFactory.getDataSource(dbType, dbInfo); @@ -72,7 +72,7 @@ namespace TeamHobby.UserManagement.xTests "SERVER=localhost;" + "DATABASE=hobby;" + "UID=root;" + - "PASSWORD=hobby;" + + "PASSWORD=Teamhobby;" + "OPTION=3"; IDataSource datasource = dbFactory.getDataSource(dbType, dbInfo); @@ -81,7 +81,7 @@ namespace TeamHobby.UserManagement.xTests for (int i = 0; i < 10000; i++) { var TestAcc = new UserAccount("newUser" + $"{i}", "4567", $"email{i}@a.com", "regular", sTime); - serviceTest.CreateUserRecord(TestAcc, "Rifat", datasource); + serviceTest.DeleteUserRecord(TestAcc, "Rifat", datasource); } DateTime eTime = DateTime.Now; diff --git a/Source Code/main/Controller.cs b/Source Code/main/Controller.cs index 1d74a58..5aec1be 100644 --- a/Source Code/main/Controller.cs +++ b/Source Code/main/Controller.cs @@ -1,4 +1,4 @@ -using System; +/*using System; using System.Data.Odbc; using TeamHobby.HobbyProjectGenerator.Archive; using TeamHobby.HobbyProjectGenerator.DataAccess; @@ -147,3 +147,4 @@ namespace TeamHobby.HobbyProjectGenerator.Main +*/ \ No newline at end of file diff --git a/Source Code/main/ExtractBulkOp.cs b/Source Code/main/ExtractBulkOp.cs new file mode 100644 index 0000000..c3e18fa --- /dev/null +++ b/Source Code/main/ExtractBulkOp.cs @@ -0,0 +1,90 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Data.Odbc; +using TeamHobby.HobbyProjectGenerator.DataAccess; +using TeamHobby.HobbyProjectGenerator.UserManagement; + +namespace main +{ + public class ExtractBulkOp + { + //public void ExtractBulkOP() + public static void Main(string[] args) + { + try + { + // Get the current directory. + string path = Directory.GetCurrentDirectory(); + // Open file reader stream + using StreamReader fileRead = new(path + "\\BulkOps\\Bulk.txt"); // path + "\\BulkOps\\{input}" + // Show location of the file being written + Console.WriteLine(path + "\\BulkOps\\Bulk.txt"); + + // List choices of operations + List ops = new List {"Create Account", "Edit Account", + "Delete Account", "Disable Account", "Enable Account"}; + + // Create Account service object + AccountService serviceTest = new AccountService(); + + // List to hold file operation and credentials + List fileList = new List(); + // Loop through file rows + while (!fileRead.EndOfStream) + { + // Create variable to hold current line + string line = fileRead.ReadLine(); + // Check if line is not empty and contains the separator + if (line != null && line.Contains(":")) + { + // Assign value to hold current line parameter + string value = line.Split(':')[1].Trim(); + + // Check what operation is being called + if(ops.Contains(value)) + { + // Declare empty list each time operation is found + string[] temp = new string[] { }; + // Loop until empty line is found + while (true) + { + string currLine = fileRead.ReadLine(); + // Check if line is not empty + if(currLine != null && currLine.Contains(":")) + { + currLine = currLine.Split(':')[1].Trim(); + if (ops.Contains(currLine)) + { + break; + } + else + { + temp.Append(currLine); + } + } + } + fileList.Add(temp); + } + } + } + Console.WriteLine(fileList[2]); +/* for (int i = 0; i < fileList.Count; i++) + { + for(int j = 0; j < fileList[i].Length; j++) + { + Console.WriteLine(fileList[i][j]); + } + }*/ + fileRead.Close(); + } + catch (Exception e) + { + Console.WriteLine("Error reading file"); + Console.WriteLine(e.Message); + } + } + } +} diff --git a/Source Code/main/GenerateOpFile.cs b/Source Code/main/GenerateOpFile.cs new file mode 100644 index 0000000..22fc33e --- /dev/null +++ b/Source Code/main/GenerateOpFile.cs @@ -0,0 +1,70 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TeamHobby.HobbyProjectGenerator.DataAccess; +using TeamHobby.HobbyProjectGenerator.UserManagement; + +namespace main +{ + public class GenerateOpFile + { + public void GenerateOPFile() + //public static void Main(string[] args) + { + try + { + // Get the current directory. + string path = Directory.GetCurrentDirectory(); + // Open file stream + using StreamWriter file = new(path + "\\BulkOps\\Bulk.txt", append: true); + // Show location of the file being written + Console.WriteLine(path + "\\BulkOps\\Bulk.txt"); + + // List choices of operations + List ops = new List {"Create Account", "Edit Account", + "Delete Account", "Disable Account", "Enable Account"}; + // Create Account service object + AccountService serviceTest = new AccountService(); + + // Find database and connect to it + string dbType = "sql"; + RDSFactory dbFactory = new RDSFactory(); + + // Testing Data Access Layer + string dbInfo = "DRIVER={MariaDB ODBC 3.1 Driver};" + + "TCPIP=1;" + + "SERVER=localhost;" + + "DATABASE=hobby;" + + "UID=root;" + + "PASSWORD=Teamhobby;" + + "OPTION=3"; + IDataSource datasource = dbFactory.getDataSource(dbType, dbInfo); + + // Create a file + // Initialize value for random operation + Random random = new Random(); + int index = 0; + for (int i = 0; i < 10000; i++) + { + // Choose random operation + index = random.Next(ops.Count); + // Create next dummy data + UserAccount testAcc = new UserAccount("newUser" + $"{i}", "4567", $"email{i}@aol.com", "regular", DateTime.Now); + // Write to file + // Write Operation: for now it is only create accounts to avoid errors + file.WriteLine($"Operation: {ops[0]}"); + // Write credentials in the format of: (UserName, Password, Role, IsActive, CreatedBy, CreatedDate, Email) + file.WriteLine($"Username: {testAcc.username}\nPassword: {testAcc.password}\nRole: {testAcc.role}\nEmail: {testAcc.email}\n"); + } + file.Close(); + } + catch (Exception e) + { + Console.WriteLine("Error reading file"); + Console.WriteLine(e.Message); + } + } + } +} From 1a265f823972c387b15bccad811844fcb8284e3a Mon Sep 17 00:00:00 2001 From: Im_Alpha Date: Wed, 15 Dec 2021 04:59:29 -0800 Subject: [PATCH 2/2] Bulk Operation option completed --- .../AccountService.cs | 225 ++++++++++++++++-- .../SystemAccountManager.cs | 26 +- .../UserAccount.cs | 62 ++++- Source Code/main/Controller.cs | 54 +---- Source Code/main/ExtractBulkOp.cs | 141 ++++++++--- 5 files changed, 388 insertions(+), 120 deletions(-) diff --git a/Source Code/TeamHobby.HobbyProjectGenerator.UserManagement/AccountService.cs b/Source Code/TeamHobby.HobbyProjectGenerator.UserManagement/AccountService.cs index 6f4a942..de6c7e8 100644 --- a/Source Code/TeamHobby.HobbyProjectGenerator.UserManagement/AccountService.cs +++ b/Source Code/TeamHobby.HobbyProjectGenerator.UserManagement/AccountService.cs @@ -15,14 +15,26 @@ namespace TeamHobby.HobbyProjectGenerator.UserManagement try { // Insert into users table - string sqlUser = $"INSERT INTO users (UserName, Password, Role, IsActive," + - $"CreatedBy, CreatedDate, Email) VALUES ('{newUser.username}', " + - $"'{newUser.password}','{newUser.role}', 1," + - $"'{CreatedBy}', NOW(),'{newUser.email}');"; + if (newUser.email == null || newUser.email == "NULL") + { + string sqlUser = $"INSERT INTO users (UserName, Password, Role, IsActive," + + $"CreatedBy, CreatedDate, Email) VALUES ('{newUser.username}', " + + $"'{newUser.password}','{newUser.role}', 1," + + $"'{CreatedBy}', NOW(),{newUser.email});"; - bool insertNewUser = dbSource.WriteData(sqlUser); - return insertNewUser; + bool insertNewUser = dbSource.WriteData(sqlUser); + return insertNewUser; + } + else + { + string sqlUser = $"INSERT INTO users (UserName, Password, Role, IsActive," + + $"CreatedBy, CreatedDate, Email) VALUES ('{newUser.username}', " + + $"'{newUser.password}','{newUser.role}', 1," + + $"'{CreatedBy}', NOW(),'{newUser.email}');"; + bool insertNewUser = dbSource.WriteData(sqlUser); + return insertNewUser; + } } catch (Exception ex) { @@ -34,13 +46,24 @@ namespace TeamHobby.HobbyProjectGenerator.UserManagement try { // Insert into users table - string sqlUser = $"UPDATE users t SET t.Password = '{editUser.password}', " + - $"t.Role = '{editUser.role}',t.Email = '{editUser.email}' " + - $"WHERE t.UserName = '{editUser.username}';"; + if (editUser.email == null || editUser.email == "NULL") + { + string sqlUser = $"UPDATE users t SET t.Password = '{editUser.password}', " + + $"t.Role = '{editUser.role}',t.Email = '{editUser.email}' " + + $"WHERE t.UserName = {editUser.username};"; - bool updateNewUser = dbSource.UpdateData(sqlUser); - return updateNewUser; + bool updateNewUser = dbSource.UpdateData(sqlUser); + return updateNewUser; + } + else + { + string sqlUser = $"UPDATE users t SET t.Password = '{editUser.password}', " + + $"t.Role = '{editUser.role}',t.Email = '{editUser.email}' " + + $"WHERE t.UserName = '{editUser.username}';"; + bool updateNewUser = dbSource.UpdateData(sqlUser); + return updateNewUser; + } } catch (Exception ex) { @@ -98,13 +121,181 @@ namespace TeamHobby.HobbyProjectGenerator.UserManagement return false; } } - public bool BulkOperation() + public bool BulkOperation(string signedUser, IDataSource dbSource) { - // List choices of operations - List ops = new List {"Create Account", "Edit Account", - "Delete Account", "Disable Account", "Enable Account"}; - - return true; + try + { + // Get the current directory. + string path = Directory.GetCurrentDirectory(); + // Open file reader stream + using StreamReader fileRead = new(path + "\\BulkOps\\Bulk.txt"); // path + "\\BulkOps\\{input}" + // Show location of the file being written + Console.WriteLine(path + "\\BulkOps\\Bulk.txt"); + + // List choices of operations + List ops = new List {"Create Account", "Edit Account", + "Delete Account", "Disable Account", "Enable Account"}; + + // List to hold file operation and credentials + List fileList = new List(); + string operation = ""; + string UN = ""; + string PWD = ""; + string Role = ""; + string Email = ""; + + // Loop through file rows + while (!fileRead.EndOfStream) + { + // Create variable to hold current line + string line = fileRead.ReadLine(); + + // Check if line is not empty and contains the separator + if (line != null && line.Contains(":")) + { + // Assign value to hold current line parameter + string value = line.Split(':')[1].Trim(); + + // Check what operation is being called + if (ops.Contains(value)) + { + // Add operation into temp + //Console.WriteLine("Found operation"); + //Console.WriteLine(value); + operation = value; + } + else + { + string checkCredential = line.Split(':')[0].Trim(); + //Console.WriteLine("Found credential"); + switch (checkCredential) + { + case "Username": + UN = value; + break; + case "Password": + PWD = value; + break; + case "Role": + Role = value; + break; + case "Email": + Email = value; + break; + default: + break; + } + } + } + else if(line != null) + { + switch (operation) + { + case "Create Account": + //Console.WriteLine(operation); + if (!String.IsNullOrEmpty(UN)) + { + // Create UserAccount object + UserAccount user = new UserAccount(UN, PWD, Email, DateTime.UtcNow); + // Create Account service object + AccountService serviceTest = new AccountService(); + serviceTest.CreateUserRecord(user, signedUser, dbSource); + } + break; + case "Edit Account": + //Console.WriteLine(operation); + //Console.WriteLine(operation); + if (String.IsNullOrEmpty(UN)) + { + // Create UserAccount object + UserAccount user = new UserAccount(UN, PWD, Email, DateTime.UtcNow); + // Create Account service object + AccountService serviceTest = new AccountService(); + serviceTest.CreateUserRecord(user, signedUser, dbSource); + } + break; + case "Delete Account": + //Console.WriteLine(operation); + //Console.WriteLine(operation); + if (String.IsNullOrEmpty(UN)) + { + // Create UserAccount object + UserAccount user = new UserAccount(UN, PWD, Email, DateTime.UtcNow); + // Create Account service object + AccountService serviceTest = new AccountService(); + serviceTest.CreateUserRecord(user, signedUser, dbSource); + } + break; + case "Disable Account": + //Console.WriteLine(operation); + //Console.WriteLine(operation); + if (String.IsNullOrEmpty(Role) && UN != null) + { + // Create UserAccount object + UserAccount user = new UserAccount(UN, PWD, Email, DateTime.UtcNow); + // Create Account service object + AccountService serviceTest = new AccountService(); + serviceTest.CreateUserRecord(user, signedUser, dbSource); + } + else if (String.IsNullOrEmpty(Email) && UN != null) + { + // Create UserAccount object + UserAccount user = new UserAccount(UN, PWD, Role); + // Create Account service object + AccountService serviceTest = new AccountService(); + serviceTest.CreateUserRecord(user, signedUser, dbSource); + } + else if (String.IsNullOrEmpty(Role) && String.IsNullOrEmpty(Email) && UN != null) + { + // Create UserAccount object + UserAccount user = new UserAccount(UN, PWD, DateTime.UtcNow); + // Create Account service object + AccountService serviceTest = new AccountService(); + serviceTest.CreateUserRecord(user, signedUser, dbSource); + } + break; + case "Enable Account": + //Console.WriteLine(operation); + //Console.WriteLine(operation); + if (String.IsNullOrEmpty(Role) && UN != null) + { + // Create UserAccount object + UserAccount user = new UserAccount(UN, PWD, Email, DateTime.UtcNow); + // Create Account service object + AccountService serviceTest = new AccountService(); + serviceTest.CreateUserRecord(user, signedUser, dbSource); + } + else if (String.IsNullOrEmpty(Email) && UN != null) + { + // Create UserAccount object + UserAccount user = new UserAccount(UN, PWD, Role); + // Create Account service object + AccountService serviceTest = new AccountService(); + serviceTest.CreateUserRecord(user, signedUser, dbSource); + } + else if (String.IsNullOrEmpty(Role) && String.IsNullOrEmpty(Email) && UN != null) + { + // Create UserAccount object + UserAccount user = new UserAccount(UN, PWD, DateTime.UtcNow); + // Create Account service object + AccountService serviceTest = new AccountService(); + serviceTest.CreateUserRecord(user, signedUser, dbSource); + } + break; + default: + break; + } + } + } + fileRead.Close(); + return true; + } + catch (Exception e) + { + Console.WriteLine("Error reading file"); + Console.WriteLine(e.Message); + return false; + } } } } diff --git a/Source Code/TeamHobby.HobbyProjectGenerator.UserManagement/SystemAccountManager.cs b/Source Code/TeamHobby.HobbyProjectGenerator.UserManagement/SystemAccountManager.cs index 2743dd9..4abe273 100644 --- a/Source Code/TeamHobby.HobbyProjectGenerator.UserManagement/SystemAccountManager.cs +++ b/Source Code/TeamHobby.HobbyProjectGenerator.UserManagement/SystemAccountManager.cs @@ -11,13 +11,6 @@ namespace TeamHobby.HobbyProjectGenerator.UserManagement { public class SystemAccountManager { - public bool BulkOperation() - { - // List choices of operations - List ops = new List {"Create Account", "Edit Account", - "Delete Account", "Disable Account", "Enable Account"}; - return true; - } public string IsInputValid(string checkUN, string checkPWD) { // Create bool variables to check if username and password are valid @@ -246,7 +239,6 @@ namespace TeamHobby.HobbyProjectGenerator.UserManagement case 2: // State what account is being edited string userName = newCredentials.GetUserName(); - string userRole = newCredentials.GetRole(); // Notify the user of what can be edited Console.WriteLine($"\n****The following information will be used to update {userName}"); @@ -322,9 +314,6 @@ namespace TeamHobby.HobbyProjectGenerator.UserManagement Console.WriteLine("Please input the name of the file:(Example.txt)"); string filename = $"{path}\\BulkOps\\{Console.ReadLine()}"; - // Read file - FileInfo fileInfo = new FileInfo(filename); - // Get filesize long fileSize = filename.Length; long fileSizeGB = fileSize / (1024 * 1024); @@ -344,10 +333,17 @@ namespace TeamHobby.HobbyProjectGenerator.UserManagement else { // Begin bulk operation - bulkOP.BulkOperation(); - - - break; + Console.WriteLine("Running bulk operation..."); + bool bulkReq = bulkOP.BulkOperation(user.username, dbSource); + if (bulkReq is true) + { + Console.WriteLine("\nBulk operation complete"); + break; + } + else + { + return "Bulk operation failed"; + } } } break; diff --git a/Source Code/TeamHobby.HobbyProjectGenerator.UserManagement/UserAccount.cs b/Source Code/TeamHobby.HobbyProjectGenerator.UserManagement/UserAccount.cs index 58d6b13..e048653 100644 --- a/Source Code/TeamHobby.HobbyProjectGenerator.UserManagement/UserAccount.cs +++ b/Source Code/TeamHobby.HobbyProjectGenerator.UserManagement/UserAccount.cs @@ -47,7 +47,25 @@ namespace TeamHobby.HobbyProjectGenerator.UserManagement } public string GetRole() { - Console.WriteLine("Please enter the role of the user:"); + while (true) + { + // List of possible roles + List roles = new List {"Admin", "regular" }; + // Get role + Console.WriteLine("Please enter the role of the user:(Admin or regular)"); + string? userRole = Console.ReadLine(); + // Check if passwords match + if (roles.Contains(userRole)) + { + return userRole; + } + else + { + Console.WriteLine("Not an available role.\n"); + } + } + + return Console.ReadLine(); } } @@ -65,26 +83,62 @@ namespace TeamHobby.HobbyProjectGenerator.UserManagement { _userName = un; _password = pwd; + _Role = "regular"; + _Email = "NULL"; _Time = TimeStamp; } + public UserAccount(string un, string pwd, string role) + { + if (String.IsNullOrEmpty(role)) + { + _Role = "regular"; + } + else { _Role = role; } + _userName = un; + _password = pwd; + _Email = "NULL"; + _Time = DateTime.UtcNow; + } public UserAccount(string un, string role) { _userName = un; _Role = role; + _Email = "NULL"; _Time = DateTime.UtcNow; } + public UserAccount(string un, string pwd, string Email, DateTime TimeStamp) + { + if (String.IsNullOrEmpty(Email)) + { + _Email = "NULL"; + } + else { _Email = Email; } + _userName = un; + _password = pwd; + _Role = "regular"; + _Time = TimeStamp; + } public UserAccount(string newUN, string newPWD, string Email, string role, DateTime newTime) { + if (String.IsNullOrEmpty(role)) + { + _Role = "regular"; + } + else { _Role = role; } + if (String.IsNullOrEmpty(Email)) + { + _Email = "NULL"; + } + else { _Email = Email; } _userName = newUN; _password = newPWD; - _Role = role; - _Email = Email; _Time = newTime; } public UserAccount() { _Role = "regular"; - _Time= DateTime.Now; + _Email = "NULL"; + _Time = DateTime.Now; } public string username { get { return _userName; } } diff --git a/Source Code/main/Controller.cs b/Source Code/main/Controller.cs index 5aec1be..cb526ab 100644 --- a/Source Code/main/Controller.cs +++ b/Source Code/main/Controller.cs @@ -1,12 +1,12 @@ -/*using System; +using System; using System.Data.Odbc; using TeamHobby.HobbyProjectGenerator.Archive; using TeamHobby.HobbyProjectGenerator.DataAccess; using TeamHobby.HobbyProjectGenerator.UserManagement; namespace TeamHobby.HobbyProjectGenerator.Main -{ - public class Controller +{ + public class Controller { public static void Main(string[] args) @@ -45,8 +45,6 @@ namespace TeamHobby.HobbyProjectGenerator.Main // Get time of login attempt DateTime TimeStamp = DateTime.UtcNow; - - // String for checking query return type string dbType = "sql"; @@ -96,55 +94,11 @@ namespace TeamHobby.HobbyProjectGenerator.Main { Console.WriteLine("******Access Denied: Unauthorized******"); } - + } - - //Creating the folder Archive - //Console.WriteLine("Creating a new folder ..."); - //archive.CreateArchiveFolder(); - //Console.WriteLine(""); - - //// Creating a file name: - //string filePath = @"C:\HobbyArchive"; - ////Console.WriteLine("Creating file name ... "); - ////string curPath = archive.CreateOutFileName(); - //string curPath = archive.CreateOutFileName(filePath); - - ////string pathForward = @"C:\Users\Chunchunmaru\Documents\csulbFall2021\HobbyProject\Source Code\main\bin\Debug\net6.0\HobbyArchive"; - ////string pathTemp = "C:/Temp/oldlogs10.txt"; - ////string pathTempBack = @"C:\Temp\oldlogs10.txt"; - //Console.WriteLine("----------------"); - - ////Output SQL to a text file - //sqlDS.CopyToFile(curPath); - - //// Compress the file - //sqlDS.CompressFile(curPath); - - ////Remove output file - //sqlDS.RemoveOutputFile(curPath); - - //// Remove entries fromt the database - //sqlDS.RemoveEntries(); - - - // 2.Inserting Data into the database: - //string sqlWrite = "INSERT into log(lvname, catname, userop, logmessage) values " + - // "('Info', 'View', 'Testing DAL stuffs', 'new DAL method tested');"; - - //Console.WriteLine("Writing to the database... "); - //datasource.WriteData(sqlWrite); - //Console.WriteLine("Writing completed. "); - - // 3. Removing from a database - //string sqlRemove = "DELETE from log where logID = 28;"; - //Console.WriteLine("Writing to the database... "); - //datasource.WriteData(sqlRemove); - //Console.WriteLine("Writing completed. "); } } } -*/ \ No newline at end of file diff --git a/Source Code/main/ExtractBulkOp.cs b/Source Code/main/ExtractBulkOp.cs index c3e18fa..e4396c5 100644 --- a/Source Code/main/ExtractBulkOp.cs +++ b/Source Code/main/ExtractBulkOp.cs @@ -11,8 +11,8 @@ namespace main { public class ExtractBulkOp { - //public void ExtractBulkOP() - public static void Main(string[] args) + public void ExtractBulkOP(string signedUser, IDataSource dbSource) + //public static void Main(string[] args) { try { @@ -27,16 +27,20 @@ namespace main List ops = new List {"Create Account", "Edit Account", "Delete Account", "Disable Account", "Enable Account"}; - // Create Account service object - AccountService serviceTest = new AccountService(); - // List to hold file operation and credentials - List fileList = new List(); + List fileList = new List(); + string operation = ""; + string UN = ""; + string PWD = ""; + string Role = ""; + string Email = ""; + // Loop through file rows while (!fileRead.EndOfStream) { // Create variable to hold current line string line = fileRead.ReadLine(); + // Check if line is not empty and contains the separator if (line != null && line.Contains(":")) { @@ -44,41 +48,110 @@ namespace main string value = line.Split(':')[1].Trim(); // Check what operation is being called - if(ops.Contains(value)) + if (ops.Contains(value)) { - // Declare empty list each time operation is found - string[] temp = new string[] { }; - // Loop until empty line is found - while (true) + // Add operation into temp + //Console.WriteLine("Found operation"); + //Console.WriteLine(value); + operation = value; + } + else + { + string checkCredential = line.Split(':')[0].Trim(); + //Console.WriteLine("Found credential"); + switch (checkCredential) { - string currLine = fileRead.ReadLine(); - // Check if line is not empty - if(currLine != null && currLine.Contains(":")) - { - currLine = currLine.Split(':')[1].Trim(); - if (ops.Contains(currLine)) - { - break; - } - else - { - temp.Append(currLine); - } - } + case "Username": + UN = value; + break; + case "Password": + PWD = value; + break; + case "Role": + Role = value; + break; + case "Email": + Email = value; + break; + default: + break; } - fileList.Add(temp); } } - } - Console.WriteLine(fileList[2]); -/* for (int i = 0; i < fileList.Count; i++) - { - for(int j = 0; j < fileList[i].Length; j++) + switch (operation) { - Console.WriteLine(fileList[i][j]); + case "Create Account": + //Console.WriteLine(operation); + if (String.IsNullOrEmpty(Role)) + { + Role = null; + // Create UserAccount object + UserAccount user = new UserAccount(UN, PWD, Email, DateTime.UtcNow); + // Create Account service object + AccountService serviceTest = new AccountService(); + Console.WriteLine($"VALUES ('{user.username}', " + + $"'{user.password}','{user.role}', 1," + + $"'Jacob', NOW(),'{user.email}');"); + //Console.WriteLine(serviceTest.CreateUserRecord(user, 'Jacob', dbSource)); + } + else if (String.IsNullOrEmpty(Email)) + { + Email = null; + // Create UserAccount object + UserAccount user = new UserAccount(UN, PWD, Role); + // Create Account service object + AccountService serviceTest = new AccountService(); + Console.WriteLine($"VALUES ('{user.username}', " + + $"'{user.password}','{user.role}', 1," + + $"'Jacob', NOW(),'{user.email}');"); + //serviceTest.CreateUserRecord(user, signedUser, dbSource); + } + else + { + // Console.WriteLine(UN); + // Create UserAccount object + UserAccount user = new UserAccount(UN, PWD, DateTime.UtcNow); + // Create Account service object + AccountService serviceTest = new AccountService(); + if (user.email == null) + { + Console.WriteLine("wokring correctly"); + } + else + { + Console.WriteLine($"VALUES ('{user.username}', " + + $"'{user.password}','{user.role}', 1," + + $"'Jacob', NOW(),'{user.email}');"); + //serviceTest.CreateUserRecord(user, signedUser, dbSource); + } + } + break; + case "Edit Account": + //Console.WriteLine(operation); + //Console.WriteLine(operation); + + break; + case "Delete Account": + //Console.WriteLine(operation); + //Console.WriteLine(operation); + + break; + case "Disable Account": + //Console.WriteLine(operation); + //Console.WriteLine(operation); + + break; + case "Enable Account": + //Console.WriteLine(operation); + //Console.WriteLine(operation); + + break; + default: + break; + } - }*/ - fileRead.Close(); + } + fileRead.Close(); } catch (Exception e) {