-
Notifications
You must be signed in to change notification settings - Fork 126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Project finish #139
base: main
Are you sure you want to change the base?
Project finish #139
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
using Dapper; | ||
using Flashcards.nikosnick13.DTOs; | ||
using Flashcards.nikosnick13.Models; | ||
using Microsoft.Data.SqlClient; | ||
using static System.Console; | ||
using Spectre.Console; | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Configuration; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Flashcards.nikosnick13.UI; | ||
using Mapster; | ||
|
||
|
||
namespace Flashcards.nikosnick13.Controllers; | ||
|
||
internal class FlashcardController | ||
{ | ||
private string? connectionString = ConfigurationManager.AppSettings.Get("ConnectionString"); | ||
|
||
|
||
public void InsertFlashcart(BasicFlashcardDTO flashcards) | ||
{ | ||
try | ||
{ | ||
using var conn = new SqlConnection(connectionString); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟢 Using ⭐ Using |
||
conn.Open(); | ||
|
||
string query = @"INSERT INTO Flashcards(Stack_Id, Question, Answer) VALUES(@stackId, @question, @answer)"; | ||
|
||
conn.Execute(query, new { | ||
stackId = flashcards.StackId, | ||
question = flashcards.Question, | ||
answer = flashcards.Answer | ||
}); | ||
|
||
|
||
AnsiConsole.MarkupLine($"\n[blue]A flashcard with the question '{flashcards.Question}' was added to Stack ID {flashcards.StackId}![/]"); | ||
|
||
AnsiConsole.Prompt(new TextPrompt<string>("\nPress [green]Enter[/] to continue...").AllowEmpty()); | ||
} | ||
catch(Exception ex) | ||
{ | ||
AnsiConsole.MarkupLine($"[red]Error: {ex.Message}[/]"); | ||
|
||
} | ||
} | ||
|
||
public List<DetailFlashcardDTO> ViewAllFlashcards() | ||
{ | ||
try | ||
{ | ||
using var conn = new SqlConnection(connectionString); | ||
conn.Open(); | ||
|
||
string query = @"SELECT Id, Question, Answer, Stack_Id AS StackId FROM Flashcards"; | ||
|
||
// Χρήση Dapper για την εκτέλεση του query | ||
var flashcards = conn.Query<DetailFlashcardDTO>(query).ToList(); | ||
|
||
WriteLine(flashcards); | ||
|
||
if (!flashcards.Any()) | ||
{ | ||
WriteLine("\n\nNo flashcards found.\n\n"); | ||
} | ||
|
||
TableVisualisation.DisplayFlashcards(flashcards); | ||
ReadKey(); | ||
return flashcards; | ||
} | ||
catch (Exception ex) | ||
{ | ||
WriteLine("Error: " + ex.Message); | ||
return new List<DetailFlashcardDTO>(); // Επιστροφή κενής λίστας σε περίπτωση σφάλματος | ||
} | ||
} | ||
|
||
public Flashcard GetFlashcardById(int id) | ||
{ | ||
try | ||
{ | ||
using var conn = new SqlConnection(connectionString); | ||
conn.Open(); | ||
|
||
string query = "SELECT Id, Question, Answer, Stack_Id FROM Flashcards WHERE Id = @Id"; | ||
|
||
var result = conn.QueryFirstOrDefault<Flashcard>(query, new { Id = id }); | ||
|
||
if (result == null) | ||
{ | ||
WriteLine($"No Flashcard found with ID {id}"); | ||
} | ||
|
||
return result; | ||
} | ||
catch (Exception ex) | ||
{ | ||
WriteLine($"Error fetching Flashcard with ID {id}: {ex.Message}"); | ||
return null; | ||
} | ||
} | ||
|
||
public void DeleteFlashcardById(int id) { | ||
|
||
try { | ||
using var conn = new SqlConnection(connectionString); | ||
|
||
conn.Open(); | ||
|
||
string query = @"DELETE FROM Flashcards WHERE Id = @id "; | ||
|
||
var rowsAffected = conn.Execute(query, new { id }); | ||
|
||
if (rowsAffected > 0) | ||
{ | ||
WriteLine($"Stack with ID {id} was deleted successfully."); | ||
AnsiConsole.Prompt(new TextPrompt<string>("\nPress [green]Enter[/] to continue...").AllowEmpty()); | ||
} | ||
else | ||
{ | ||
WriteLine($"\n\nNo record found with Id {id}. Nothing was deleted preess any key to return..\n\n"); | ||
} | ||
} | ||
catch(Exception ex) { | ||
|
||
WriteLine("Error " + ex.Message); | ||
} | ||
|
||
} | ||
|
||
public void EditFlashcard(BasicFlashcardDTO basicFlashcardDTO) { | ||
|
||
try { | ||
|
||
using var conn = new SqlConnection(connectionString); | ||
|
||
conn.Open(); | ||
|
||
string query = @"UPDATE Flashcards SET Question = @question, Answer = @answer,Stack_Id = @stack_id WHERE Id = @id "; | ||
|
||
conn.Execute(query, new | ||
{ | ||
id = basicFlashcardDTO.Id, | ||
question = basicFlashcardDTO.Question, | ||
answer = basicFlashcardDTO.Answer, | ||
stack_id = basicFlashcardDTO.StackId | ||
}); | ||
} | ||
catch(Exception ex) { | ||
WriteLine("Error " + ex.Message); | ||
} | ||
|
||
} | ||
|
||
//TODO: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟠 Correct comment? 💡 Is this a true statement? Is this TODO? If so, try make a rule that you don't deliver any thing with a TODO comment. Tech debt, you wont come back to it, it will live as a todo forever. If not, try to make sure you do a final revisit and clean up any code comments. |
||
public DetailFlashcardDTO ViewFlashcardById(int id) { | ||
|
||
try { | ||
using var conn = new SqlConnection(connectionString); | ||
conn.Open(); | ||
|
||
string query = "SELECT Id, Question, Answer, Stack_Id FROM Flashcards WHERE Id = @Id"; | ||
|
||
var result = conn.QueryFirstOrDefault<DetailFlashcardDTO>(query, new { Id = id }); | ||
|
||
return result; | ||
} | ||
catch(Exception ex){ | ||
|
||
WriteLine("Error: " + ex.Message); | ||
return null; | ||
|
||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
using Dapper; | ||
using Flashcards.nikosnick13.Models; | ||
using Microsoft.Data.SqlClient; | ||
using System; | ||
using static System.Console; | ||
using System.Collections.Generic; | ||
using System.Configuration; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Flashcards.nikosnick13.UI; | ||
using Spectre.Console; | ||
using Flashcards.nikosnick13.DTOs; | ||
|
||
namespace Flashcards.nikosnick13.Controllers; | ||
|
||
internal class StackController | ||
{ | ||
static string? connectionString = ConfigurationManager.AppSettings.Get("ConnectionString"); | ||
|
||
public void InsertStack(Stack stack) | ||
{ | ||
using var conn = new SqlConnection(connectionString); | ||
conn.Open(); | ||
|
||
var checkQuery = @"SELECT COUNT(1) FROM Stacks WHERE Name = @name"; | ||
var exists = conn.ExecuteScalar<int>(checkQuery, new { name = stack.Name }); | ||
|
||
if (exists > 0) | ||
{ | ||
AnsiConsole.MarkupLine("\n[red]A stack with this name already exists![/]"); | ||
AnsiConsole.Prompt(new TextPrompt<string>("\nPress [green]Enter[/] to continue...").AllowEmpty()); | ||
return; | ||
} | ||
|
||
string query = @"INSERT INTO Stacks (Name) VALUES (@name)"; | ||
conn.Execute(query, new { name = stack.Name }); | ||
AnsiConsole.MarkupLine($"\n[blue]A stack with this name {stack.Name} add to list![/]"); | ||
AnsiConsole.Prompt(new TextPrompt<string>("\nPress [green]Enter[/] to continue...").AllowEmpty()); | ||
} | ||
|
||
public List<DetailStackDTO> ViewAllStacks() | ||
{ | ||
Clear(); | ||
List<DetailStackDTO> recordsList = new List<DetailStackDTO>(); | ||
|
||
try | ||
{ | ||
using var conn = new SqlConnection(connectionString); | ||
conn.Open(); | ||
|
||
string query = @"SELECT * FROM Stacks"; | ||
|
||
using var command = new SqlCommand(query, conn); | ||
|
||
using var reader = command.ExecuteReader(); | ||
|
||
if (reader.HasRows) | ||
{ | ||
while (reader.Read()) | ||
{ | ||
recordsList.Add(new DetailStackDTO | ||
{ | ||
Id = reader.GetInt32(0), | ||
Name = reader.GetString(1), | ||
Flashcard = null, // Αν υπάρχουν Flashcards, θα χρειαστεί πρόσθετο query | ||
StudySession = null // Αν υπάρχουν StudySession, θα χρειαστεί πρόσθετο query | ||
}); | ||
} | ||
} | ||
else WriteLine("\n\nNo rows fount\n\n"); | ||
} | ||
catch (Exception ex) | ||
{ | ||
WriteLine("Error: " + ex.Message); | ||
} | ||
|
||
TableVisualisation.ShowTable(recordsList); | ||
|
||
return recordsList; | ||
|
||
} | ||
|
||
|
||
public Stack? GetById(int id) | ||
{ | ||
try | ||
{ | ||
using var conn = new SqlConnection(connectionString); | ||
conn.Open(); | ||
|
||
string query = @"SELECT * FROM Stacks WHERE Id = @id "; | ||
|
||
var result = conn.QueryFirstOrDefault<Stack>(query, new {id}); | ||
return result; | ||
} | ||
catch(Exception ex) | ||
{ | ||
WriteLine($"Error fetching Stack with ID {id}: {ex.Message}"); | ||
return null; | ||
} | ||
|
||
} | ||
|
||
public void DeleteStackById(int id) | ||
{ | ||
try | ||
{ | ||
using var conn = new SqlConnection(connectionString); | ||
conn.Open(); | ||
|
||
string query = @"DELETE FROM Stacks WHERE Id = @id"; | ||
|
||
var rowsAffected = conn.Execute(query, new { id }); | ||
|
||
if (rowsAffected > 0) | ||
{ | ||
WriteLine($"Stack with ID {id} was deleted successfully."); | ||
AnsiConsole.Prompt(new TextPrompt<string>("\nPress [green]Enter[/] to continue...").AllowEmpty()); | ||
} | ||
else | ||
{ | ||
WriteLine($"\n\nNo record found with Id {id}. Nothing was deleted preess any key to return..\n\n"); | ||
ReadKey(); | ||
} | ||
} | ||
catch(Exception ex) | ||
{ | ||
WriteLine("Error " + ex.Message); | ||
} | ||
} | ||
|
||
|
||
public void EditStackById(BasicStackDTO basicStackDTO) | ||
{ | ||
try | ||
{ | ||
using var conn = new SqlConnection(connectionString); | ||
conn.Open(); | ||
|
||
string query = @"UPDATE Stacks SET Name = @name WHERE Id = @id"; | ||
|
||
conn.Execute(query, new | ||
{ | ||
name = basicStackDTO.Name, | ||
id = basicStackDTO.Id | ||
}); | ||
} | ||
catch(Exception ex) | ||
{ | ||
WriteLine("Error " + ex.Message); | ||
} | ||
} | ||
|
||
public DetailStackDTO ViewStackById(int id) | ||
{ | ||
try | ||
{ | ||
using var conn = new SqlConnection(connectionString); | ||
conn.Open(); | ||
|
||
string query = @"SELECT Id, Name FROM Stacks WHERE Id = @id"; | ||
|
||
// Επιστρέφει DetailStackDTO | ||
return conn.QueryFirstOrDefault<DetailStackDTO>(query, new { id }); | ||
} | ||
catch (Exception ex) | ||
{ | ||
WriteLine("Error: " + ex.Message); | ||
return null; | ||
} | ||
} | ||
|
||
// ViewAllStack: this method return list.Interact with flashcardsUI | ||
public List<DetailStackDTO> ViewAllStack() | ||
{ | ||
try | ||
{ | ||
using var conn = new SqlConnection(connectionString); | ||
conn.Open(); | ||
|
||
string query = @"SELECT Id, Name FROM Stacks"; | ||
|
||
var stacks = conn.Query<DetailStackDTO>(query).ToList(); | ||
|
||
return stacks; | ||
} | ||
catch (Exception ex) | ||
{ | ||
WriteLine($"Error fetching stacks: {ex.Message}"); | ||
return new List<DetailStackDTO>(); | ||
} | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Typo
😆 Easily done!
❌
InsertFlashcart
✔️
InsertFlashcard