Bulk Operation option completed

This commit is contained in:
Im_Alpha 2021-12-15 04:59:29 -08:00
parent 9bba0ebcc1
commit 1a265f8239
5 changed files with 388 additions and 120 deletions

View File

@ -15,14 +15,26 @@ namespace TeamHobby.HobbyProjectGenerator.UserManagement
try try
{ {
// Insert into users table // Insert into users table
string sqlUser = $"INSERT INTO users (UserName, Password, Role, IsActive," + if (newUser.email == null || newUser.email == "NULL")
$"CreatedBy, CreatedDate, Email) VALUES ('{newUser.username}', " + {
$"'{newUser.password}','{newUser.role}', 1," + string sqlUser = $"INSERT INTO users (UserName, Password, Role, IsActive," +
$"'{CreatedBy}', NOW(),'{newUser.email}');"; $"CreatedBy, CreatedDate, Email) VALUES ('{newUser.username}', " +
$"'{newUser.password}','{newUser.role}', 1," +
$"'{CreatedBy}', NOW(),{newUser.email});";
bool insertNewUser = dbSource.WriteData(sqlUser); bool insertNewUser = dbSource.WriteData(sqlUser);
return insertNewUser; 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) catch (Exception ex)
{ {
@ -34,13 +46,24 @@ namespace TeamHobby.HobbyProjectGenerator.UserManagement
try try
{ {
// Insert into users table // Insert into users table
string sqlUser = $"UPDATE users t SET t.Password = '{editUser.password}', " + if (editUser.email == null || editUser.email == "NULL")
$"t.Role = '{editUser.role}',t.Email = '{editUser.email}' " + {
$"WHERE t.UserName = '{editUser.username}';"; 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); bool updateNewUser = dbSource.UpdateData(sqlUser);
return updateNewUser; 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) catch (Exception ex)
{ {
@ -98,13 +121,181 @@ namespace TeamHobby.HobbyProjectGenerator.UserManagement
return false; return false;
} }
} }
public bool BulkOperation() public bool BulkOperation(string signedUser, IDataSource<string> dbSource)
{ {
// List choices of operations try
List<string> ops = new List<string> {"Create Account", "Edit Account", {
"Delete Account", "Disable Account", "Enable Account"}; // Get the current directory.
string path = Directory.GetCurrentDirectory();
return true; // 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<string> ops = new List<string> {"Create Account", "Edit Account",
"Delete Account", "Disable Account", "Enable Account"};
// List to hold file operation and credentials
List<string> fileList = new List<string>();
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;
}
} }
} }
} }

View File

@ -11,13 +11,6 @@ namespace TeamHobby.HobbyProjectGenerator.UserManagement
{ {
public class SystemAccountManager public class SystemAccountManager
{ {
public bool BulkOperation()
{
// List choices of operations
List<string> ops = new List<string> {"Create Account", "Edit Account",
"Delete Account", "Disable Account", "Enable Account"};
return true;
}
public string IsInputValid(string checkUN, string checkPWD) public string IsInputValid(string checkUN, string checkPWD)
{ {
// Create bool variables to check if username and password are valid // Create bool variables to check if username and password are valid
@ -246,7 +239,6 @@ namespace TeamHobby.HobbyProjectGenerator.UserManagement
case 2: case 2:
// State what account is being edited // State what account is being edited
string userName = newCredentials.GetUserName(); string userName = newCredentials.GetUserName();
string userRole = newCredentials.GetRole();
// Notify the user of what can be edited // Notify the user of what can be edited
Console.WriteLine($"\n****The following information will be used to update {userName}"); 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)"); Console.WriteLine("Please input the name of the file:(Example.txt)");
string filename = $"{path}\\BulkOps\\{Console.ReadLine()}"; string filename = $"{path}\\BulkOps\\{Console.ReadLine()}";
// Read file
FileInfo fileInfo = new FileInfo(filename);
// Get filesize // Get filesize
long fileSize = filename.Length; long fileSize = filename.Length;
long fileSizeGB = fileSize / (1024 * 1024); long fileSizeGB = fileSize / (1024 * 1024);
@ -344,10 +333,17 @@ namespace TeamHobby.HobbyProjectGenerator.UserManagement
else else
{ {
// Begin bulk operation // Begin bulk operation
bulkOP.BulkOperation(); Console.WriteLine("Running bulk operation...");
bool bulkReq = bulkOP.BulkOperation(user.username, dbSource);
if (bulkReq is true)
break; {
Console.WriteLine("\nBulk operation complete");
break;
}
else
{
return "Bulk operation failed";
}
} }
} }
break; break;

View File

@ -47,7 +47,25 @@ namespace TeamHobby.HobbyProjectGenerator.UserManagement
} }
public string GetRole() public string GetRole()
{ {
Console.WriteLine("Please enter the role of the user:"); while (true)
{
// List of possible roles
List<string> roles = new List<string> {"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(); return Console.ReadLine();
} }
} }
@ -65,26 +83,62 @@ namespace TeamHobby.HobbyProjectGenerator.UserManagement
{ {
_userName = un; _userName = un;
_password = pwd; _password = pwd;
_Role = "regular";
_Email = "NULL";
_Time = TimeStamp; _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) public UserAccount(string un, string role)
{ {
_userName = un; _userName = un;
_Role = role; _Role = role;
_Email = "NULL";
_Time = DateTime.UtcNow; _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) 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; _userName = newUN;
_password = newPWD; _password = newPWD;
_Role = role;
_Email = Email;
_Time = newTime; _Time = newTime;
} }
public UserAccount() public UserAccount()
{ {
_Role = "regular"; _Role = "regular";
_Time= DateTime.Now; _Email = "NULL";
_Time = DateTime.Now;
} }
public string username { get { return _userName; } } public string username { get { return _userName; } }

View File

@ -1,12 +1,12 @@
/*using System; using System;
using System.Data.Odbc; using System.Data.Odbc;
using TeamHobby.HobbyProjectGenerator.Archive; using TeamHobby.HobbyProjectGenerator.Archive;
using TeamHobby.HobbyProjectGenerator.DataAccess; using TeamHobby.HobbyProjectGenerator.DataAccess;
using TeamHobby.HobbyProjectGenerator.UserManagement; using TeamHobby.HobbyProjectGenerator.UserManagement;
namespace TeamHobby.HobbyProjectGenerator.Main namespace TeamHobby.HobbyProjectGenerator.Main
{ {
public class Controller public class Controller
{ {
public static void Main(string[] args) public static void Main(string[] args)
@ -45,8 +45,6 @@ namespace TeamHobby.HobbyProjectGenerator.Main
// Get time of login attempt // Get time of login attempt
DateTime TimeStamp = DateTime.UtcNow; DateTime TimeStamp = DateTime.UtcNow;
// String for checking query return type // String for checking query return type
string dbType = "sql"; string dbType = "sql";
@ -96,55 +94,11 @@ namespace TeamHobby.HobbyProjectGenerator.Main
{ {
Console.WriteLine("******Access Denied: Unauthorized******"); 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. ");
} }
} }
} }
*/

View File

@ -11,8 +11,8 @@ namespace main
{ {
public class ExtractBulkOp public class ExtractBulkOp
{ {
//public void ExtractBulkOP() public void ExtractBulkOP(string signedUser, IDataSource<string> dbSource)
public static void Main(string[] args) //public static void Main(string[] args)
{ {
try try
{ {
@ -27,16 +27,20 @@ namespace main
List<string> ops = new List<string> {"Create Account", "Edit Account", List<string> ops = new List<string> {"Create Account", "Edit Account",
"Delete Account", "Disable Account", "Enable Account"}; "Delete Account", "Disable Account", "Enable Account"};
// Create Account service object
AccountService serviceTest = new AccountService();
// List to hold file operation and credentials // List to hold file operation and credentials
List<string[]> fileList = new List<string[]>(); List<string> fileList = new List<string>();
string operation = "";
string UN = "";
string PWD = "";
string Role = "";
string Email = "";
// Loop through file rows // Loop through file rows
while (!fileRead.EndOfStream) while (!fileRead.EndOfStream)
{ {
// Create variable to hold current line // Create variable to hold current line
string line = fileRead.ReadLine(); string line = fileRead.ReadLine();
// Check if line is not empty and contains the separator // Check if line is not empty and contains the separator
if (line != null && line.Contains(":")) if (line != null && line.Contains(":"))
{ {
@ -44,41 +48,110 @@ namespace main
string value = line.Split(':')[1].Trim(); string value = line.Split(':')[1].Trim();
// Check what operation is being called // Check what operation is being called
if(ops.Contains(value)) if (ops.Contains(value))
{ {
// Declare empty list each time operation is found // Add operation into temp
string[] temp = new string[] { }; //Console.WriteLine("Found operation");
// Loop until empty line is found //Console.WriteLine(value);
while (true) operation = value;
}
else
{
string checkCredential = line.Split(':')[0].Trim();
//Console.WriteLine("Found credential");
switch (checkCredential)
{ {
string currLine = fileRead.ReadLine(); case "Username":
// Check if line is not empty UN = value;
if(currLine != null && currLine.Contains(":")) break;
{ case "Password":
currLine = currLine.Split(':')[1].Trim(); PWD = value;
if (ops.Contains(currLine)) break;
{ case "Role":
break; Role = value;
} break;
else case "Email":
{ Email = value;
temp.Append(currLine); break;
} default:
} break;
} }
fileList.Add(temp);
} }
} }
} switch (operation)
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]); 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) catch (Exception e)
{ {