// Code review: Consider following convention like in int.TryParse(string intText, out int). Suggested method signature: bool TryGetTodoItem(int itemId, out todoitem). Throwing exceptions won't be necessary then. public todoitem TryGetTodoItem(int itemId) { todoitem td = new todoitem(); td = db.todoitem.SingleOrDefault(x => x.Id == itemId); if (td == null) { throw new ArgumentException("Invalid item ID number."); } return(td); }
internal void Add() { Console.Write("Description>"); var description = Console.ReadLine().Trim(); var addMe = new todoitem(); addMe.Description = description; addMe.TimeCreated = DateTime.Now; this.db.todoitem.Add(addMe); this.db.SaveChanges(); Console.WriteLine("Todo item has been added."); }
public static void removeTodoItem(todoitem ti) { // Code review: That's beyond basic EF update technique. Please present it during next lecture. ti.TimeDeactivated = DateTime.Now; db.todoitem.Attach(ti); var entry = db.Entry(ti); entry.State = EntityState.Modified; entry.Property(e => e.TimeDeactivated).IsModified = true; db.SaveChanges(); Console.WriteLine("Item {0} with an ID {1} has been removed.", ti.Description, ti.Id); }
public static void undoneTodoItem(todoitem ti) { // Code review. There is no change in database if actual value isn't set. ti.TimeSetToDone = null; db.todoitem.Attach(ti); var entry = db.Entry(ti); entry.State = EntityState.Modified; entry.Property(e => e.TimeSetToDone).IsModified = true; db.SaveChanges(); Console.WriteLine("Item {0} with an ID {1} is now set to undone.", ti.Description, ti.Id); }
internal void Run() { TodoApp app = new TodoApp(); app.List(); string commandText; do { Console.Write("> "); commandText = Console.ReadLine().ToLower().Trim(); if (commandText == "help") { WriteHelp(); } else if (commandText == "list") { app.List(); } else if (commandText == "add") { app.Add(); } else if (commandText == "done") { app.SetDone(); } else if (commandText == "remove") { todoitem item = new todoitem(); Console.WriteLine("item ID > "); try { item = app.TryGetTodoItem(int.Parse(Console.ReadLine().Trim().ToLower())); } // Code review: It's not a good practice to catch every exception. Only really expected exception should be handled. Perhaps one of int.Parse exceptions (FormatException, OverflowException) or ArgumentException that can be raised from TryGetTodoItem. // Consider using int.TryParse() instead of int.Parse. This crazy exception handling won't be needed. catch (OverflowException) { Console.WriteLine("Invalid ID number."); } catch (FormatException) { Console.WriteLine("Invalid ID number."); } catch (ArgumentException) { Console.WriteLine("Invalid ID number."); } //catch (Exception) //{ // Console.WriteLine("Invalid ID number."); // // Code review: No need to terminate application. Give user ability to enter another command. // //return; //} todoitem.removeTodoItem(item); } else if (commandText == "undone") { todoitem item = new todoitem(); Console.WriteLine("item ID > "); try { item = app.TryGetTodoItem(int.Parse(Console.ReadLine().Trim().ToLower())); } catch (Exception) { Console.WriteLine("Invalid ID number."); return; } todoitem.undoneTodoItem(item); } else if (commandText == "remove-all") { app.RemoveAllToDoItems(); } // Code review: exit command writes 'Unsupported command...' text to console. This is suggested fix. //else else if (commandText != "exit") { Console.Clear(); Console.WriteLine("Unsupported command. " + Environment.NewLine + "Type \"help\" to see a list of available commands"); } } while (commandText != "exit"); }