diff --git a/StudentManager/App.xaml.cs b/StudentManager/App.xaml.cs index 8b3f8d5..9932779 100644 --- a/StudentManager/App.xaml.cs +++ b/StudentManager/App.xaml.cs @@ -19,6 +19,8 @@ namespace StudentManager protected override void RegisterTypes(IContainerRegistry containerRegistry) { containerRegistry.RegisterForNavigation(); + containerRegistry.RegisterForNavigation(); + containerRegistry.RegisterForNavigation(); } } diff --git a/StudentManager/Common/SQLHelper.cs b/StudentManager/Common/SQLHelper.cs index 0e00431..09fd088 100644 --- a/StudentManager/Common/SQLHelper.cs +++ b/StudentManager/Common/SQLHelper.cs @@ -11,6 +11,14 @@ using System.Threading.Tasks; namespace StudentManager.Common { + static class Tables + { + static public string UserTable { get; } = "users"; + static public string QuesTable { get; } = "questions"; + static public string UQTable { get; } = "user_data"; + } + + public static class SQLMapping { public static void Mapping(MySqlDataReader reader) @@ -22,7 +30,6 @@ namespace StudentManager.Common { if (typeof(T) == typeof(StudentData)) { - (data as StudentData).UID = (int)reader[0]; (data as StudentData).Name = reader[1].ToString(); (data as StudentData).Gender = reader[2].ToString(); @@ -30,9 +37,21 @@ namespace StudentManager.Common (data as StudentData).Address = reader[4].ToString(); (data as StudentData).Grade = (int)reader[5]; } - else + else if(typeof(T) == typeof(QuestionData)) { - + DifficultyLevel dLevel = DifficultyLevel.easy; + QStatus qStatus = QStatus.published; + (data as QuestionData).Id = (int)reader[0]; + (data as QuestionData).Type = reader[1].ToString(); + (data as QuestionData).Stem = reader[2].ToString(); + (data as QuestionData).Answer = reader[3].ToString(); + Enum.TryParse(reader[4].ToString(), true, out dLevel); + (data as QuestionData).DifficultyLevel = dLevel; + (data as QuestionData).Category = reader[5].ToString(); + (data as QuestionData).Tags = reader[6].ToString(); + (data as QuestionData).Source = reader[7].ToString(); + Enum.TryParse(reader[8].ToString(), true, out qStatus); + (data as QuestionData).Status = qStatus; } @@ -62,28 +81,81 @@ namespace StudentManager.Common // update ** set ** = '**',... where ** = **; // insert into ** values('**','**'...); // delete from ** where ** = **; - public static List Query(string tableName, params string[] queryParams) where T : IDataCommon, new() + public static List Query(string tableName, int id = -1, params string[] queryParams) where T : IDataCommon, new() { connection = new MySqlConnection(config); List result = new List(); + + string idName = typeof(T) == typeof(StudentData) ? Tables.UserTable : Tables.QuesTable; try { connection.Open(); string queryStr = ""; if (queryParams.Length > 0) - queryStr = $"select * from {tableName};"; + { + string filedList = string.Join(",", queryParams); + queryStr = $"select {filedList} from {tableName};"; + + if (id != -1) + queryStr = $@"select {filedList} from {tableName} WHERE {idName} = {id};"; + } else + { queryStr = $"select * from {tableName};"; + if (id != -1) + queryStr = $@"select * from {tableName} WHERE {idName} = {id};"; + } cmd = new MySqlCommand(queryStr, connection); MySqlDataReader reader = cmd.ExecuteReader(); - T row = new T(); while (reader.Read()) { + T row = new T(); + SQLMapping.Mapping(row, reader); + result.Add(row); + } + + return result; + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + return null; + } + finally + { + cmd.Dispose(); + connection.Close(); + } + } + + public static List UnionQuery(int id = -1, string tableName = "questions", string otherTable = "user_data") where T : IDataCommon, new() + { + connection = new MySqlConnection(config); + + List result = new List(); + + try + { + connection.Open(); + string queryStr = ""; + if(id == -1) + queryStr = $@"SELECT u.user_id, q.* FROM {otherTable} u JOIN {tableName} q ON u.problem_id = q.problem_id"; + else + queryStr = $@"SELECT q.* FROM {tableName} q JOIN {otherTable} u ON q.problem_id = u.problem_id WHERE u.user_id = {id}"; + + cmd = new MySqlCommand(queryStr, connection); + MySqlDataReader reader = cmd.ExecuteReader(); + + + + while (reader.Read()) + { + T row = new T(); SQLMapping.Mapping(row, reader); result.Add(row); } diff --git a/StudentManager/Data/QuestionData.cs b/StudentManager/Data/QuestionData.cs new file mode 100644 index 0000000..47c0d4e --- /dev/null +++ b/StudentManager/Data/QuestionData.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace StudentManager.Data +{ + public enum DifficultyLevel + { + easy, + medium, + hard, + } + + public enum QStatus + { + published, + pending_review, + deprecated, + } + + public class QuestionData : IDataCommon + { + public int Id { get; set; } = 0; + public string Type { get; set; } = string.Empty; + public string Stem { get; set; } = string.Empty; + public string Answer { get; set; } = String.Empty; + public DifficultyLevel DifficultyLevel { get; set; } = DifficultyLevel.easy; + public string Category { get; set; } = string.Empty; + public string Tags { get; set; } = string.Empty; + public string Source { get; set; } = string.Empty; + public QStatus Status { get; set; } = QStatus.published; + + + public string TableName { get => "questions"; set => throw new NotImplementedException(); } + } +} diff --git a/StudentManager/Data/StudentData.cs b/StudentManager/Data/StudentData.cs index 5da109b..7f9a08d 100644 --- a/StudentManager/Data/StudentData.cs +++ b/StudentManager/Data/StudentData.cs @@ -14,6 +14,8 @@ namespace StudentManager.Data public string Contact { get; set; } = "Contact"; public string Address { get; set; } = "Address"; public int Grade { get; set; } = 1; + public int TotalsQuestions { get; set; } = 0; + public int CorrectionCount { get; set; } = 0; diff --git a/StudentManager/Editor/MainEditor.xaml b/StudentManager/Editor/MainEditor.xaml index 7618555..c2f09e7 100644 --- a/StudentManager/Editor/MainEditor.xaml +++ b/StudentManager/Editor/MainEditor.xaml @@ -4,32 +4,25 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:StudentManager.Editor" + xmlns:prism="http://prismlibrary.com/" + xmlns:extent="clr-namespace:StudentManager.Extensions" mc:Ignorable="d" Title="MainEditor" Height="450" Width="800"> - - - + - - - + + + + + + - - - - - - - - +