public void TestCreateUpdateDeleteStudents() { //DBConnection.ConnectionString = "Server=(local)\\sqlexpress;Database=Students;Trusted_Connection=True;"; //DBConnection.Type = DBType.SQLServer; DBConnection.Type = DBType.MySQL; DBConnection.ConnectionString = "Server=localhost;Port=3306;Uid=root;Pwd=system32;Database=Students;CheckParameters=false;"; var newStudent = new Student("Superman", "Male", StudentType.High); var result = StudentMapper.Insert(newStudent); Assert.AreEqual(true, result); var savedStudent = StudentMapper.GetById(newStudent.Id); Assert.AreEqual("Superman", savedStudent.Name); savedStudent.Name = "Clark Kent"; StudentMapper.Update(savedStudent); var updatedStudent = StudentMapper.GetById(savedStudent.Id); Assert.AreEqual("Clark Kent", updatedStudent.Name); StudentMapper.Hide(updatedStudent.Id); var hiddenStudent = StudentMapper.GetById(updatedStudent.Id); Assert.AreEqual(false, hiddenStudent.Enabled); StudentMapper.Delete(hiddenStudent.Id); var deleteStudent = StudentMapper.GetById(hiddenStudent.Id); Assert.AreEqual(null, deleteStudent); }
public OperationResponse Hide(long id) { var response = new OperationResponse(); try { response.HasError = !StudentMapper.Hide(id); } catch (Exception ex) { response.HasError = true; response.Message = ex.ToString(); } return(response); }
/// <summary> /// Entry point of the console application which presents a menu of options to do all the CRUD operations over the students table /// </summary> /// <param name="args">List of arguments which can be applied to the console client at the time of execution which can be the name of CSV file to import and given filter in the format expected: /// Ex: CmdClient.exe Test.csv name=leia /// Ex: CmdClient.exe Test.csv type=Kinder /// Ex: CmdClient.exe Test.csv type=Elementary gender=female /// </param> static void Main(string[] args) { //Setting background and color Console.BackgroundColor = ConsoleColor.White; Console.ForegroundColor = ConsoleColor.DarkBlue; Console.Clear(); //Reading the connection string from app.settings DBConnection.ConnectionString = ConfigurationManager.ConnectionStrings["SqlServer"].ConnectionString; //Verifying if some arguments were passed to the program and processing them if (args.Length > 0) { ImportCSVFile(args[0]); DefineFilter(args); if (!string.IsNullOrEmpty(currentFilter)) { ShowStudents(); } } //Showing the menu and doing the main logic of the program char option = '\0'; while (option != '9') { Console.Clear(); Console.WriteLine("===================== Console Students Administration ====================="); Console.WriteLine("1. Load students from CSV"); Console.WriteLine("2. Define a filter to show students"); Console.WriteLine("3. Clear current filter"); Console.WriteLine("4. Show students"); Console.WriteLine("5. Create new student"); Console.WriteLine("6. Edit student"); Console.WriteLine("7. Hide student"); Console.WriteLine("8. Delete student"); Console.WriteLine("9. Quit"); Console.WriteLine("Please choose an option number from 1 to 9 and press Enter: "); var userInput = Console.ReadLine(); if (!string.IsNullOrEmpty(userInput)) { option = userInput[0]; switch (option) { case '1': ImportCSVFile(); break; case '2': DefineFilter(); break; case '3': if (string.IsNullOrEmpty(currentFilter)) { ShowContinueMessage("There isn't set any filter right now."); } else { Console.WriteLine(Environment.NewLine + "CURRENT filter is: " + currentFilter); Console.WriteLine("Are you sure that do you want to clear it? (Y/N) [Y]: "); string answer = Console.ReadLine(); if (string.IsNullOrEmpty(answer) || answer[0].ToString().ToUpper() == "Y") { currentFilter = string.Empty; currentSqlFilter = string.Empty; currentFieldSort = "updated_on"; currentSortDir = "DESC"; } } break; case '4': ShowStudents(); break; case '5': CreateStudent(); break; case '6': EditStudent(); break; case '7': var student = GetStudent(); if (student != null) { Console.WriteLine(string.Format("Are you sure that you want to HIDE the student: {0}? (Y/N) [Y]: ", student.ToString())); string value = Console.ReadLine(); if (string.IsNullOrEmpty(value) || value[0].ToString().ToUpper() == "Y") { StudentMapper.Hide(student.Id); } else { if (!string.IsNullOrEmpty(value) && !(value[0].ToString().ToUpper() == "Y" || value[0].ToString().ToUpper() == "N")) { ShowContinueMessage("You should choose Y for yes or N for no, please try again."); } } } break; case '8': var studentToDelete = GetStudent(); if (studentToDelete != null) { Console.WriteLine(string.Format("Are you sure that you want to DELETE the student: {0}? (Y/N) [Y]: ", studentToDelete.ToString())); string value = Console.ReadLine(); if (string.IsNullOrEmpty(value) || value[0].ToString().ToUpper() == "Y") { StudentMapper.Delete(studentToDelete.Id); } else { if (!string.IsNullOrEmpty(value) && !(value[0].ToString().ToUpper() == "Y" || value[0].ToString().ToUpper() == "N")) { ShowContinueMessage("You should choose Y for yes or N for no, please try again."); } } } break; } } else { Console.WriteLine("You must type the respective number from 1 to 9 to pick and option from the menu. Press any key to continue ..."); Console.ReadKey(); } } Console.WriteLine("Thanks for using the Student's console client. Press any key to continue ..."); Console.ReadKey(); }