Skip to content

Commit

Permalink
Data folder is commented
Browse files Browse the repository at this point in the history
  • Loading branch information
Proto69 committed Mar 18, 2023
1 parent 870a4a8 commit 60efbf0
Show file tree
Hide file tree
Showing 10 changed files with 212 additions and 136 deletions.
2 changes: 1 addition & 1 deletion PlanMe/Data/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public static class Database
{
private static string connectionString = Secrets.connectionString;

//Makes the connection to the database
// Gives the connection to the database
public static MySqlConnection GetConnection()
{
return new MySqlConnection(connectionString);
Expand Down
63 changes: 51 additions & 12 deletions PlanMe/Data/EventData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,67 @@
{
public static class EventData
{
//Uploads text, date, time and info into the database
public static bool Upload(Event @event, string username)
// Uploads text, date, time and info into the database
public static bool Upload(Event @event)
{
// Establish a database connection and open it
MySqlConnection conn = Database.GetConnection();
conn.Open();

// Use the connection object in a 'using' statement to ensure it gets closed properly
using (conn)
{
// Get the user ID from the MainModels class
int userId = MainModels.user.Id;

// Define the SQL query to insert a new event into the events table
string query = "INSERT INTO events (name, date, time, additional_info, user_id) " +
"VALUES (@name, @date, @time, @info, @user_id)";

// Create a new command object with the query and connection objects
MySqlCommand cmd = new MySqlCommand(query, conn);

// Add the event properties as parameters to the command object
cmd.Parameters.AddWithValue("@name", @event.Name);
cmd.Parameters.AddWithValue("@date", @event.Date);
cmd.Parameters.AddWithValue("@time", @event.Time);
cmd.Parameters.AddWithValue("@info", @event.Info);
cmd.Parameters.AddWithValue("@user_id", userId);

// Execute the command and return the result of the MainCommands.RunNonQuery method
return MainCommands.RunNonQuery(cmd);
}

}

//Gets all events for current user
// Gets all events for current user
public static List<Event> GetAll()
{
List<Event> userEvents = new List<Event>();
MySqlConnection conn = Database.GetConnection();

// Establish a database connection and open it
MySqlConnection conn = Database.GetConnection();
conn.Open();

// Use the connection object in a 'using' statement to ensure it gets closed properly
using (conn)
{
// Get the user ID from the MainModels class
int userId = MainModels.user.Id;

// Define the SQL query to retrieve all events for the specified user
string query = "SELECT * FROM events WHERE user_id = @userId";

// Create a new command object with the query and connection objects
MySqlCommand cmd = new MySqlCommand(query, conn);

// Add the user ID as a parameter to the command object
cmd.Parameters.AddWithValue("@userId", userId);

// Execute the query and retrieve a MySqlDataReader object
MySqlDataReader reader = cmd.ExecuteReader();

// Loop through each row in the reader object and create a new Event object for each one
while (reader.Read())
{
string name = reader["name"].ToString();
Expand All @@ -54,49 +74,68 @@ public static List<Event> GetAll()
userEvents.Add(newEvent);
}
}

// Return the list of Event objects
return userEvents;

}

//Updates text, date, time and info for the event with new parameters
// Updates text, date, time and info for the event with new parameters
public static bool Update(Event oldEvent, Event newEvent)
{
// Establish a database connection and open it
MySqlConnection conn = Database.GetConnection();

conn.Open();

// Use the connection object in a 'using' statement to ensure it gets closed properly
using (conn)
{
// Define the SQL query to update the event with the specified name, date, and time
string query = "UPDATE events SET name = @newName, date = @newDate, time = @newTime, additional_info = @newInfo " +
"WHERE name = @text AND date = @date AND time = @time";
"WHERE name = @text AND date = @date AND time = @time";

// Create a new command object with the query and connection objects
MySqlCommand cmd = new MySqlCommand(query, conn);

// Add parameters for the updated event data
cmd.Parameters.AddWithValue("@newName", newEvent.Name);
cmd.Parameters.AddWithValue("@newDate", newEvent.Date);
cmd.Parameters.AddWithValue("@newTime", newEvent.Time);
cmd.Parameters.AddWithValue("@newInfo", newEvent.Info);

// Add parameters for the old event data to identify the event to be updated
cmd.Parameters.AddWithValue("@text", oldEvent.Name);
cmd.Parameters.AddWithValue("@date", oldEvent.Date);
cmd.Parameters.AddWithValue("@time", oldEvent.Time);

// Execute the query and retrieve the number of rows affected
return MainCommands.RunNonQuery(cmd);
}

}

//Delete event by text, date and time for current user
// Delete event by text, date and time for current user
public static bool Delete(Event action)
{
// Establish a database connection and open it
MySqlConnection conn = Database.GetConnection();

conn.Open();

// Use the connection object in a 'using' statement to ensure it gets closed properly
using (conn)
{
// Define the SQL query to delete the event with the specified name, date, and time
string query = "DELETE FROM events WHERE name = @name AND date = @date AND time = @time";

// Create a new command object with the query and connection objects
MySqlCommand cmd = new MySqlCommand(query, conn);

cmd.Parameters.AddWithValue("@name", action.Name);
cmd.Parameters.AddWithValue("@date", action.Date);
cmd.Parameters.AddWithValue("@time", action.Time);
// Add parameters for the event data to identify the event to be deleted
cmd.Parameters.AddWithValue("@name", action.Name);
cmd.Parameters.AddWithValue("@date", action.Date);
cmd.Parameters.AddWithValue("@time", action.Time);

// Execute the query and retrieve the number of rows affected
return MainCommands.RunNonQuery(cmd);
}
}
Expand Down
70 changes: 44 additions & 26 deletions PlanMe/Data/ListOfTasksData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,106 +2,124 @@
{
public static class ListOfTasksData
{
// This method uploads a list of tasks to the database
public static bool Upload(ListOfTasks listOfTasks)
{
bool check = false;

// Get a database connection and open it
MySqlConnection conn = Database.GetConnection();
conn.Open();

// Use the connection in a using block to ensure proper disposal
using (conn)
{
// Construct a query to insert the new task list into the list_of_tasks table
string query = "INSERT INTO list_of_tasks (user_id, name) VALUES (@id, @name)";
MySqlCommand cmd = new MySqlCommand(query, conn);

// Add parameters to the query for the user ID and task list name
cmd.Parameters.AddWithValue("@id", MainModels.user.Id);
cmd.Parameters.AddWithValue("@name", listOfTasks.Name);

check = MainCommands.RunNonQuery(cmd);

query = "INSERT INTO tasks (list_id, text) VALUES";

int listId = MainCommands.GetListId(listOfTasks.Name, conn);

List<string> list = new();

foreach (var task in listOfTasks.Tasks)
{
list.Add($"({listId}, \"{task.Text}\")");
}

if (list.Count == 0)
return check;

string left = string.Join(",", list);
query += left;

cmd = new MySqlCommand(query, conn);

check = MainCommands.RunNonQuery(cmd);
// Execute the query and store the result
return MainCommands.RunNonQuery(cmd);
}
return check;
}

// This method removes list of tasks from the database
public static bool Remove(ListOfTasks listOfTasks)
{
// Delete all tasks in the list
listOfTasks.DeleteAllTasks();

// Open a database connection
MySqlConnection conn = Database.GetConnection();
conn.Open();

using (conn)
{
// Create a query to delete the list of tasks from the database
string query = "DELETE FROM list_of_tasks WHERE name = @name AND user_id = @user_id";
MySqlCommand cmd = new MySqlCommand(query, conn);

// Set parameters for the query
cmd.Parameters.AddWithValue("@name", listOfTasks.Name);
cmd.Parameters.AddWithValue("@user_id", MainModels.user.Id);

// Execute the query and return the result
return MainCommands.RunNonQuery(cmd);
}
}

// This method updates list of tasks' name
public static bool Update(string oldName, string newName)
{
// Get a connection to the MySQL database
MySqlConnection conn = Database.GetConnection();

// Open the connection and wrap it in a using statement to ensure proper disposal
conn.Open();
using (conn)
{
string query = "UPDATE list_of_tasks SET name = @newName " +
"WHERE name = @oldName AND user_id = @user_id";
// Get the user ID of the logged-in user
int userId = MainModels.user.Id;

// Create a SQL query to update the name of the list with the specified old name and user ID
// Set the new name using a parameter
string query = "UPDATE list_of_tasks SET name = @newName WHERE name = @oldName AND user_id = @userId";
MySqlCommand cmd = new MySqlCommand(query, conn);

// Add parameters for the old name, new name, and user ID
cmd.Parameters.AddWithValue("@newName", newName);
cmd.Parameters.AddWithValue("@oldName", oldName);
cmd.Parameters.AddWithValue("@user_id", MainModels.user.Id);
cmd.Parameters.AddWithValue("@userId", userId);

// Execute the SQL query using the MySqlCommand object and the MainCommands class to run non-query SQL commands
return MainCommands.RunNonQuery(cmd);
}
}


// This method retrieves all the list of tasks belonging to a user identified by their username
public static List<ListOfTasks> GetAll(string username)
{
List<ListOfTasks> result = new List<ListOfTasks>();

// Establish a database connection and open it
MySqlConnection conn = Database.GetConnection();
conn.Open();

// Use the connection object in a 'using' statement to ensure it gets closed properly
using (conn)
{
// Get the ID of the user from the MainModels class
int id = MainModels.user.Id;

// Define the query to retrieve the name of all the list of tasks for this user
string query = "SELECT name FROM list_of_tasks WHERE user_id = @id";

// Create a new command object with the query and connection objects
MySqlCommand cmd = new MySqlCommand(query, conn);

// Add the user ID as a parameter to the command object
cmd.Parameters.AddWithValue("@id", id);

// Execute the query and retrieve a MySqlDataReader object
MySqlDataReader reader = cmd.ExecuteReader();

// Loop through each row in the reader object and create a new ListOfTasks object for each one
while (reader.Read())
{
string name = reader["name"].ToString();
ListOfTasks tasks = new(name, username);
result.Add(tasks);
}
}

// Return the list of ListOfTasks objects
return result;
}

}
}
Loading

0 comments on commit 60efbf0

Please sign in to comment.