Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions PhoneBook.Hillgrove/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"dotnet.automaticallyCreateSolutionInWorkspace": false
}
111 changes: 111 additions & 0 deletions PhoneBook.Hillgrove/PhoneBook.Hillgrove/AppRunner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using PhoneBook.Hillgrove.Controllers;
using PhoneBook.Hillgrove.Models.Enums;
using PhoneBook.Hillgrove.Views;

namespace PhoneBook.Hillgrove;

internal static class AppRunner
{
public static void Run()
{
var isRunning = true;

while (isRunning)
{
var option = MenuView.GetMainMenuOption();

switch (option)
{
case MainMenu.Contacts:
RunContactsMenu();
break;

case MainMenu.Categories:
RunCategoriesMenu();
break;

case MainMenu.SendEmail:
EmailController.SendEmail();
break;

case MainMenu.Quit:
isRunning = false;
break;
}
}
}

private static void RunContactsMenu()
{
var inContactsMenu = true;

while (inContactsMenu)
{
var option = MenuView.GetContactMenuOption();

switch (option)
{
case ContactMenu.AddContact:
ContactController.AddContact();
break;

case ContactMenu.UpdateContact:
ContactController.UpdateContact();
break;

case ContactMenu.ShowContact:
ContactController.ShowContact();
break;

case ContactMenu.ShowContacts:
ContactController.ShowContacts();
break;

case ContactMenu.ShowContactsByCategory:
ContactController.ShowContactsByCategory();
break;

case ContactMenu.DeleteContact:
ContactController.DeleteContact();
break;

case ContactMenu.Back:
inContactsMenu = false;
break;
}
}
}

private static void RunCategoriesMenu()
{
var inCategoriesMenu = true;

while (inCategoriesMenu)
{
var option = MenuView.GetCategoryMenuOption();

switch (option)
{
case CategoryMenu.AddCategory:
CategoryController.AddCategory();
break;

case CategoryMenu.UpdateCategory:
CategoryController.UpdateCategory();
break;

case CategoryMenu.ShowCategories:
CategoryController.ShowCategories();
break;

case CategoryMenu.DeleteCategory:
CategoryController.DeleteCategory();
break;

case CategoryMenu.Back:
inCategoriesMenu = false;
break;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using PhoneBook.Hillgrove.Services;
using PhoneBook.Hillgrove.Views;

namespace PhoneBook.Hillgrove.Controllers;

internal static class CategoryController
{
public static void AddCategory()
{
var name = PromptView.PromptUserForInput<string>("Category name: ");

try
{
CategoryService.AddCategory(name);
MessageView.ShowSuccess("Category added.");
}
catch (ArgumentException ex)
{
MessageView.ShowError(ex.Message);
}
catch (Exception)
{
MessageView.ShowError("Failed to add category. Please try again.");
}

PromptView.WaitForKeyPress();
}

public static void UpdateCategory()
{
try
{
var categories = CategoryService.GetCategories();
var category = PromptView.PromptUserForItemOrNotify(
"Select category: ",
categories,
c => c.Name,
"No categories found."
);
if (category is null)
return;

var newName = PromptView.PromptUserForOptionalInput(
$"Name ({category.Name}), leave empty to keep: "
);

CategoryService.UpdateCategory(
category,
string.IsNullOrWhiteSpace(newName) ? null : newName
);

MessageView.ShowSuccess("Category updated.");
}
catch (ArgumentException ex)
{
MessageView.ShowError(ex.Message);
}
catch (Exception)
{
MessageView.ShowError("Failed to update category. Please try again.");
}

PromptView.WaitForKeyPress();
}

public static void ShowCategories()
{
try
{
var categories = CategoryService.GetCategories();

CategoryView.ShowCategories(categories);
}
catch (Exception)
{
MessageView.ShowError("Failed to get categories. Please try again.");
}

PromptView.WaitForKeyPress();
}

public static void DeleteCategory()
{
try
{
var categories = CategoryService.GetCategories();
var category = PromptView.PromptUserForItemOrNotify(
"Select category: ",
categories,
c => c.Name,
"No categories found."
);
if (category is null)
return;

CategoryService.DeleteCategory(category);
MessageView.ShowSuccess("Category deleted.");
}
catch (Exception)
{
MessageView.ShowError("Failed to delete category. Please try again.");
}

PromptView.WaitForKeyPress();
}
}
Loading