Skip to content

Commit

Permalink
Added comments in Models folder
Browse files Browse the repository at this point in the history
  • Loading branch information
Proto69 committed Mar 18, 2023
1 parent 6cdb5e5 commit 870a4a8
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 42 deletions.
3 changes: 2 additions & 1 deletion PlanMe/Data/UserData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public static bool Create(User user)
cmd.Parameters.AddWithValue("@password", user.Password);

bool smth = MainCommands.RunNonQuery(cmd);

if (smth)
MainModels.user = user;
MainModels.user.Id = MainCommands.GetUserId(user.Username, conn);

return smth;
Expand Down
18 changes: 2 additions & 16 deletions PlanMe/Models/Event.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace PlanMe.Models
{
public class Event : IComparable<Event>
public class Event
{
private string name;
private DateTime date;
Expand All @@ -24,6 +24,7 @@ public string Name
get { return name; }
set
{
//Checks the length to be less than 150
if (value.Length > 150)
throw new ArgumentException("The name is too long!");
this.name = value;
Expand Down Expand Up @@ -56,20 +57,5 @@ public string Info
this.info = value;
}
}

public int CompareTo(Event? other)
{
bool name = this.Name == other.Name;
bool date = this.Date == other.Date;
bool time = this.Time == other.Time;

if (name && time && date) return 0;
return 1;
}

public override string? ToString()
{
return $"{Name} on {Date} at {Time}";
}
}
}
32 changes: 24 additions & 8 deletions PlanMe/Models/MainCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,66 @@
{
public static class MainCommands
{
//Command to get user id by username
public static int GetUserId(string username, MySqlConnection conn)
{
//MySQL query and creating new command
string query = "SELECT id FROM users WHERE username = @username";
MySqlCommand cmd = new MySqlCommand(query, conn);


//Adding parameter to the command
cmd.Parameters.AddWithValue("@username", username);
MySqlDataReader reader = cmd.ExecuteReader();

int id = 0;
//Executing reader to read values from the result of the query
MySqlDataReader reader = cmd.ExecuteReader();

while (reader.Read())
{
id = (int)reader["id"];
}
//The reader reads the returned row
reader.Read();

//Gets the value in column "id"
int id = (int)reader["id"];

//Closing the reader to avoid conflicts
reader.Close();

return id;
}


//Using this method to run queries that don't return anything
public static bool RunNonQuery(MySqlCommand cmd)
{
//Running the command and getting the value of changed rows
int rows = cmd.ExecuteNonQuery();

//If it is 1, the command was successful and we return true
if (rows == 1)
return true;
return false;
}

//Command to get the id of a list
public static int GetListId(string name, MySqlConnection conn)
{
//MySQL query that will return id of a list
string query = "SELECT id FROM list_of_tasks WHERE name = @name AND user_id = @id";
MySqlCommand cmd = new MySqlCommand(query, conn);

//We fill the command with parameters
cmd.Parameters.AddWithValue("@name", name);
cmd.Parameters.AddWithValue("@id", MainModels.user.Id);

//Execute the reader
MySqlDataReader reader = cmd.ExecuteReader();

int id = 0;

//Reading the value and assigning it to a variable
while (reader.Read())
{
id = (int)reader["id"];
}

//Closing the reader and returning the result
reader.Close();
return id;
}
Expand Down
8 changes: 8 additions & 0 deletions PlanMe/Models/MainModels.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
namespace PlanMe.Models
{
//Models to use over the whole app
public static class MainModels
{
//The user that is currently logged in
public static User user = null;

//The main form that is loaded
public static MainForm form = null;

//The current task screen that is in use
public static TaskScreen lists = null;

//The current list of tasks that is opened on the TaskScreen
public static ListOfTasks tasks = null;
}
}
3 changes: 1 addition & 2 deletions PlanMe/Models/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ public User(string username, string password)
{
Username = username;
Password = password;
//Events = EventData.GetAll(username);
//AllTasks = ListOfTasksData.GetAll(username);
}

//Filling the data for the user
public void FillEventsAndTasks()
{
Events = EventData.GetAll();
Expand Down
13 changes: 0 additions & 13 deletions PlanMe/Models/UserTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@ public UserTask(string text, bool isDone)
this.IsDone = isDone;
}

public void Complete()
{
//Checks if users's task is done
if (isDone)
throw new InvalidOperationException("This task is already done!");
isDone = true;
}

public string Text
{
get { return text; }
Expand All @@ -38,10 +30,5 @@ public bool IsDone
isDone = value;
}
}

public override string ToString()
{
return $"{text} {isDone}";
}
}
}
3 changes: 1 addition & 2 deletions PlanMe/User_Controls/MainScreen.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 870a4a8

Please sign in to comment.