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
178 changes: 178 additions & 0 deletions Dania.Flashcards/Controllers/FlashcardController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
using Flashcards.Models;

namespace Flashcards.Controllers
{
internal class FlashcardController
{
List<FlashcardDto> flashcards = new List<FlashcardDto>();
DatabaseManager databaseManager;
Helpers helpers = new Helpers();

public FlashcardController(DatabaseManager databaseManager)
{
this.databaseManager = databaseManager;
}

internal void ViewFlashcards(Stack stack)
{
GetFlashcards(stack);

Console.WriteLine("\nPress Enter to return to the manage flashcard menu...");
Console.ReadLine();
}

internal void CreateFlashcards(Stack stack)
{
Console.Clear();
Console.WriteLine($"---Create flashcards in Stack: {stack.Name}---");

Console.WriteLine("Enter the question for the flashcard: ");
string question = Console.ReadLine();

if (question == "0") return;

Console.WriteLine("\nEnter the answer for the flashcard: ");
string answer = Console.ReadLine();

if (answer == "0") return;

databaseManager.CreateFlashcard(stack.Id, question, answer);
Console.WriteLine("\nFlashcard created successfully!");

Console.Write("Would you like to create another flashcard? (y/n): ");
string input = Console.ReadLine();
if (!string.IsNullOrWhiteSpace(input) && input.ToLower() == "y")
CreateFlashcards(stack);
else
{
Console.WriteLine("\nPress Enter to return to the manage flashcard menu...");
Console.ReadLine();
}
}

internal void UpdateFlashcards(Stack stack)
{
Console.Clear();
Console.WriteLine("---Update flashcards---");
GetFlashcards(stack);
if (flashcards.Count > 0)
{
Console.WriteLine("\nEnter the flashcard id to update: ");
int flashcardDisplayId = helpers.CheckIntInput();

if (flashcardDisplayId == 0) return;

var flashcard = flashcards.FirstOrDefault(f => f.DisplayId == flashcardDisplayId);

if (flashcard == null)
Console.WriteLine($"Flashcard with ID '{flashcardDisplayId}' does not exist. Please try again.");
else
{
Console.WriteLine($"\nCurrent question: {flashcard.Question}");
Console.WriteLine("Enter the new question for the flashcard (Press Enter to keep the current question): ");
string newQuestion = Console.ReadLine();
if (!string.IsNullOrWhiteSpace(newQuestion))
flashcard.Question = newQuestion;

Console.WriteLine($"\nCurrent answer: {flashcard.Answer}");
Console.WriteLine("Enter the new answer for the flashcard (Press Enter to keep the current answer): ");
string newAnswer = Console.ReadLine();
if (!string.IsNullOrWhiteSpace(newAnswer))
flashcard.Answer = newAnswer;

databaseManager.UpdateFlashcard(flashcard.Id, flashcard.Question, flashcard.Answer);
Console.WriteLine($"\nFlashcard with ID '{flashcardDisplayId}' updated successfully!");
}

Console.WriteLine("\nWould you like to update another flashcard? (y/n): ");
string input = Console.ReadLine();
if (!string.IsNullOrWhiteSpace(input) && input.ToLower() == "y")
UpdateFlashcards(stack);
else
{
Console.WriteLine("\nPress Enter to return to the manage flashcard menu...");
Console.ReadLine();
}
}
else
{
Console.WriteLine("\nPress Enter to return to the manage flashcard menu...");
Console.ReadLine();
}
}

internal void DeleteFlashcards(Stack stack)
{
Console.Clear();
Console.WriteLine("---Delete flashcards---");

GetFlashcards(stack);
if (flashcards.Count > 0)
{
Console.WriteLine("\nEnter the flashcard id to delete: ");
int flashcardDisplayId = helpers.CheckIntInput();

if (flashcardDisplayId == 0) return;

// Find the flashcard by its display ID
var flashcard = flashcards.FirstOrDefault(f => f.DisplayId == flashcardDisplayId);

if (flashcard == null)
Console.WriteLine($"Flashcard with ID '{flashcardDisplayId}' does not exist. Please try again.");
else
{
Console.WriteLine($"\nAre you sure you want to delete flashcard with ID '{flashcardDisplayId}'? (y/n): ");
string confirmation = Console.ReadLine();

if (confirmation.Trim().ToLower() == "y")
{
databaseManager.DeleteFlashcard(flashcard.Id);
Console.WriteLine($"\nFlashcard with ID '{flashcardDisplayId}' deleted successfully!");
}
else
Console.WriteLine("\nDeletion cancelled.");

}

Console.WriteLine("\nWould you like to delete another flashcard? (y/n): ");
string input = Console.ReadLine();
if (!string.IsNullOrWhiteSpace(input) && input.ToLower() == "y")
DeleteFlashcards(stack);
else
{
Console.WriteLine("\nPress Enter to return to the manage flashcard menu...");
Console.ReadLine();
}
}
else
{
Console.WriteLine("\nPress Enter to return to the manage flashcard menu...");
Console.ReadLine();
}
}



internal void GetFlashcards(Stack stack)
{
flashcards.Clear();
flashcards = databaseManager.GetFlashcardsByStackId(stack.Id);

Console.Clear();
Console.WriteLine($"---Flashcards in Stack: {stack.Name}---");
if (flashcards.Count == 0)
{
Console.WriteLine("No flashcards found in this stack.");
}
else
{
Console.WriteLine("{0,-7} {1,-15} {2,-15}", "ID", "Question", "Answer");
foreach (var flashcard in flashcards)
{
Console.WriteLine("{0,-7} {1,-15} {2,-15}", flashcard.DisplayId, flashcard.Question, flashcard.Answer);

}
}
}
}
}
125 changes: 125 additions & 0 deletions Dania.Flashcards/Controllers/StackController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
using Flashcards.Models;

namespace Flashcards.Controllers
{
internal class StackController
{
List<Stack> stacks = new List<Stack>();
DatabaseManager databaseManager;

public StackController(DatabaseManager databaseManager)
{
this.databaseManager = databaseManager;
}

internal void CreateStack()
{
Console.Clear();
Console.WriteLine("---Create a new Stack---");

Console.WriteLine("Enter the name of the new stack (Press 0 to return to the manage stacks menu): ");
string name = Console.ReadLine();

if (name == "0") return;

Console.WriteLine();
if (string.IsNullOrWhiteSpace(name))
{
Console.WriteLine("Stack name cannot be empty. Please try again.");
}
else
{
if (databaseManager.CheckStackExist(name))
Console.WriteLine($"Stack '{name}' already exists. Please choose a different name.");
else
{
databaseManager.CreateStack(name);
Console.WriteLine($"Stack '{name}' created successfully!");
}
}

Console.WriteLine("\nWould you like to create another stack? (y/n): ");
string input = Console.ReadLine();
if (!string.IsNullOrWhiteSpace(input) && input.ToLower() == "y")
CreateStack();
else
{
Console.WriteLine("\nPress Enter to return to the manage stacks menu...");
Console.ReadLine();
}
}

internal void DeleteStack()
{
Console.Clear();
Console.WriteLine("---Delete Stacks---");

if (GetStacks() > 0)
{
Console.WriteLine("\nEnter the stack name to delete (Press 0 to return to the manage stacks menu):");
string stackName = Console.ReadLine();

if (stackName == "0") return;

Stack stack = GetStackByName(stackName);
if (stack != null)
{
Console.WriteLine($"\nAre you sure you want to delete stack '{stackName}' and all its flashcards? (y/n): ");
string confirmation = Console.ReadLine();

if (confirmation.Trim().ToLower() == "y")
{
databaseManager.DeleteStack(stack.Id);
Console.WriteLine($"\nStack '{stackName}' and all its flashcards deleted successfully!");
}
else
Console.WriteLine("\nDeletion cancelled.");
}

Console.WriteLine("\nWould you like to delete another stack? (y/n): ");
string input = Console.ReadLine();
if (!string.IsNullOrWhiteSpace(input) && input.ToLower() == "y")
DeleteStack();
else
{
Console.WriteLine("\nPress Enter to return to the manage stack menu...");
Console.ReadLine();
}
}
else
{
Console.WriteLine("\nPress Enter to return to the manage stack menu...");
Console.ReadLine();
}
}

internal int GetStacks()
{
stacks.Clear();
stacks = databaseManager.GetStacks();

if (stacks.Count == 0)
Console.WriteLine("No stacks found. Please create a stack first.");
else
{
foreach (var stack in stacks)
{
Console.WriteLine($"- {stack.Name}");
}
}

return stacks.Count;
}

internal Stack GetStackByName(string stackName)
{
// Use case-insensitive comparison to find the stack by name
var stack = stacks.FirstOrDefault(s => s.Name.Equals(stackName, StringComparison.OrdinalIgnoreCase));

if (stack == null)
Console.WriteLine($"\nStack '{stackName}' does not exist. Please try again.");

return stack;
}
}
}
Loading