Skip to content
Merged
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
31 changes: 13 additions & 18 deletions internal/admin/addCard.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,12 @@ import (
"strconv"
"strings"
"time"
structMessage "unicard-go/internal/pkg"
)

// We use this struct to pass data to the HTML template for the Add Cards page
type AddCardsData struct {
Error string
Success string
}

// This struct represents a card and its attributes.
// We can use it to easily pass card data around in our functions.
// It also helps to keep our code organized and makes it easier to manage card-related data.
// We can use it to easily pass card data around in our functions.
// It also helps to keep our code organized and makes it easier to manage card-related data.
type Card struct {
CardUID string
CardNumber string
Expand All @@ -34,7 +29,7 @@ type Card struct {
func (h *Handler) AddCardsView(w http.ResponseWriter, r *http.Request) {
fmt.Println("AddCardsView running...")
// Render the addCards.html template
h.Tpl.ExecuteTemplate(w, "addCards.html", AddCardsData{})
h.Tpl.ExecuteTemplate(w, "addCards.html", structMessage.MessageData{})
}

// This function handles the form submission from the addCards.html page.
Expand All @@ -44,7 +39,7 @@ func (h *Handler) AddCardHandler(w http.ResponseWriter, r *http.Request) {
fmt.Println("addcardshandler running...")

if err := r.ParseForm(); err != nil {
h.Tpl.ExecuteTemplate(w, "addCards.html", AddCardsData{Error: "Failed to parse form"})
h.Tpl.ExecuteTemplate(w, "addCards.html", structMessage.MessageData{Error: "Failed to parse form"})
return
}

Expand All @@ -54,7 +49,7 @@ func (h *Handler) AddCardHandler(w http.ResponseWriter, r *http.Request) {

// Validate required fields
if cardUID == "" || initialAmount == "" {
h.Tpl.ExecuteTemplate(w, "addCards.html", AddCardsData{Error: "Please fill in all required fields."})
h.Tpl.ExecuteTemplate(w, "addCards.html", structMessage.MessageData{Error: "Please fill in all required fields."})
return
}

Expand All @@ -76,7 +71,7 @@ func (h *Handler) AddCardHandler(w http.ResponseWriter, r *http.Request) {
amount, err := strconv.ParseFloat(initialAmount, 64)
if err != nil {
fmt.Printf("Error parsing amount: %v\n", err)
h.Tpl.ExecuteTemplate(w, "addCards.html", AddCardsData{Error: "Invalid amount format. Must be a number."})
h.Tpl.ExecuteTemplate(w, "addCards.html", structMessage.MessageData{Error: "Invalid amount format. Must be a number."})
return
}
fmt.Printf("Parsed amount: %.2f\n", amount)
Expand All @@ -86,12 +81,12 @@ func (h *Handler) AddCardHandler(w http.ResponseWriter, r *http.Request) {
cardUidExist, err := h.cardUIDExist(cardUID)
if err != nil {
fmt.Println("Error checking card UID existence:", err)
h.Tpl.ExecuteTemplate(w, "addCards.html", AddCardsData{Error: "Error checking card UID."})
h.Tpl.ExecuteTemplate(w, "addCards.html", structMessage.MessageData{Error: "Error checking card UID."})
return
}
if cardUidExist {
fmt.Println("Card UID already exists")
h.Tpl.ExecuteTemplate(w, "addCards.html", AddCardsData{Error: "Card UID already exists."})
h.Tpl.ExecuteTemplate(w, "addCards.html", structMessage.MessageData{Error: "Card UID already exists."})
return
}
fmt.Println("Card UID is unique")
Expand All @@ -110,12 +105,12 @@ func (h *Handler) AddCardHandler(w http.ResponseWriter, r *http.Request) {
cardNumExists, err := h.cardNumberExist(card)
if err != nil {
fmt.Println("Error checking card number existence:", err)
h.Tpl.ExecuteTemplate(w, "addCards.html", AddCardsData{Error: "Error checking card number."})
h.Tpl.ExecuteTemplate(w, "addCards.html", structMessage.MessageData{Error: "Error checking card number."})
return
}
if cardNumExists {
fmt.Println("Card number already exists")
h.Tpl.ExecuteTemplate(w, "addCards.html", AddCardsData{Error: "Card number already exists."})
h.Tpl.ExecuteTemplate(w, "addCards.html", structMessage.MessageData{Error: "Card number already exists."})
return
}
fmt.Println("Card number is unique")
Expand All @@ -139,13 +134,13 @@ func (h *Handler) AddCardHandler(w http.ResponseWriter, r *http.Request) {
createdAt)
if err != nil {
fmt.Println("Error inserting card into database:", err)
h.Tpl.ExecuteTemplate(w, "addCards.html", AddCardsData{Error: "Error while adding card."})
h.Tpl.ExecuteTemplate(w, "addCards.html", structMessage.MessageData{Error: "Error while adding card."})
return
}

// Successfully added the card
fmt.Printf("Card added successfully: %s\n", card.CardNumber)
h.Tpl.ExecuteTemplate(w, "addCards.html", AddCardsData{Success: "Card added successfully!"})
h.Tpl.ExecuteTemplate(w, "addCards.html", structMessage.MessageData{Success: "Card added successfully!"})
}

//--- HELPER FUNCTIONS ---
Expand Down
22 changes: 8 additions & 14 deletions internal/admin/deactivateCard.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,9 @@ import (
"fmt"
"net/http"
"strings"
structMessage "unicard-go/internal/pkg"
)

// This struct is used to pass data to the HTML template for the Deactivate Card page.
// It allows us to easily display error or success messages when we process the form submission for deactivating a card.
type CardData struct {
Error string
Success string
}

// This struct represents the details of a card that we want to deactivate.
// We can use it to easily pass card data around in our functions.
// It also helps to keep our code organized and makes it easier to manage card-related data when we process the deactivation form submission.
Expand All @@ -23,19 +17,19 @@ type CardDetails struct {

// This function renders the deactivateCard.html template when the admin visits the /admin/deactivatecard page.
// It doesn't do any processing yet, it just shows the form to the admin.
// We can also pass an empty CardData struct to the template, which allows us to easily display error or success messages later on when we process the form submission.
// We can also pass an empty structMessage.MessageData struct to the template, which allows us to easily display error or success messages later on when we process the form submission.
func (h *Handler) DeactivateView(w http.ResponseWriter, r *http.Request) {
fmt.Println("DeactivateView running...")
// Render the deactivateCard.html template
h.Tpl.ExecuteTemplate(w, "deactivateCard.html", CardData{})
h.Tpl.ExecuteTemplate(w, "deactivateCard.html", structMessage.MessageData{})
}

// This function handles the form submission from the deactivateCard.html page.
func (h *Handler) DeactivateCardHanlder(w http.ResponseWriter, r *http.Request) {
fmt.Println("Deactivate card handler running...")

if err := r.ParseForm(); err != nil {
h.Tpl.ExecuteTemplate(w, "deactivateCard.html", CardData{Error: "Failed to parse form"})
h.Tpl.ExecuteTemplate(w, "deactivateCard.html", structMessage.MessageData{Error: "Failed to parse form"})
return
}

Expand All @@ -45,26 +39,26 @@ func (h *Handler) DeactivateCardHanlder(w http.ResponseWriter, r *http.Request)

if cardNumber == "" || cardHolder == "" || cardType == "" {
fmt.Println("Missing required fields:", cardNumber, cardHolder, cardType)
h.Tpl.ExecuteTemplate(w, "deactivateCard.html", CardData{Error: "Please fill in all required fields."})
h.Tpl.ExecuteTemplate(w, "deactivateCard.html", structMessage.MessageData{Error: "Please fill in all required fields."})
return
}

// Check if the card exists and is active, then deactivate it
ok, err := h.deactivateCardIfActive(cardNumber, cardHolder, cardType)
if err != nil {
fmt.Println("Error while deactivating card:", err)
h.Tpl.ExecuteTemplate(w, "deactivateCard.html", CardData{Error: "Failed to deactivate card."})
h.Tpl.ExecuteTemplate(w, "deactivateCard.html", structMessage.MessageData{Error: "Failed to deactivate card."})
return
}

if !ok {
fmt.Printf("Card not found or already inactive: %s with Card Type: %s\n", cardNumber, cardType)
h.Tpl.ExecuteTemplate(w, "deactivateCard.html", CardData{Error: "Card not found or already inactive."})
h.Tpl.ExecuteTemplate(w, "deactivateCard.html", structMessage.MessageData{Error: "Card not found or already inactive."})
return
}

fmt.Println("Card deactivated successfully:", cardNumber, cardType)
h.Tpl.ExecuteTemplate(w, "deactivateCard.html", CardData{Success: "Card deactivated successfully!"})
h.Tpl.ExecuteTemplate(w, "deactivateCard.html", structMessage.MessageData{Success: "Card deactivated successfully!"})
}

// --- Helper functions ---
Expand Down
11 changes: 6 additions & 5 deletions internal/auth/forgotPassword.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"database/sql"
"fmt"
"net/http"
structMessage "unicard-go/internal/pkg"
"unicard-go/internal/pkg/account"
)

Expand Down Expand Up @@ -32,30 +33,30 @@ func (h *Handler) ForgotPassword(w http.ResponseWriter, r *http.Request) {
exists, err := h.checkEmailExist(email)
if err != nil {
fmt.Println("Error checking email existence:", err)
h.Tpl.ExecuteTemplate(w, "forgotPassword.html", ErrorMessage{Error: "System error. Please try again later."})
h.Tpl.ExecuteTemplate(w, "forgotPassword.html", structMessage.MessageData{Error: "System error. Please try again later."})
return
}
if !exists {
h.Tpl.ExecuteTemplate(w, "forgotPassword.html", ErrorMessage{Error: "Email not found."})
h.Tpl.ExecuteTemplate(w, "forgotPassword.html", structMessage.MessageData{Error: "Email not found."})
return
}

// Hash the new password
hashedPassword, err := account.HashPassword(password)
if err != nil {
fmt.Println("Error hashing password:", err)
h.Tpl.ExecuteTemplate(w, "forgotPassword.html", ErrorMessage{Error: "System error. Please try again later."})
h.Tpl.ExecuteTemplate(w, "forgotPassword.html", structMessage.MessageData{Error: "System error. Please try again later."})
return
}

// Update the password in the database
err = h.updatePassword(email, hashedPassword)
if err != nil {
fmt.Println("Error updating password:", err)
h.Tpl.ExecuteTemplate(w, "forgotPassword.html", ErrorMessage{Error: "System error. Please try again later."})
h.Tpl.ExecuteTemplate(w, "forgotPassword.html", structMessage.MessageData{Error: "System error. Please try again later."})
return
}
h.Tpl.ExecuteTemplate(w, "forgotPassword.html", ErrorMessage{Success: "Password updated successfully."})
h.Tpl.ExecuteTemplate(w, "forgotPassword.html", structMessage.MessageData{Success: "Password updated successfully."})
}

// ---Helper Function---
Expand Down
8 changes: 2 additions & 6 deletions internal/auth/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@ package authentication
import (
"fmt"
"net/http"
structMessage "unicard-go/internal/pkg"

"golang.org/x/crypto/bcrypt"
)

// We use this struct to pass data to the HTML
type LoginData struct {
Error string
}

// View Handler (GET)
// This function checks the URL for errors (e.g., ?error=invalid)
// and displays the red text if needed.
Expand All @@ -31,7 +27,7 @@ func (h *Handler) LoginView(w http.ResponseWriter, r *http.Request) {
}

// Render the template with the message
h.Tpl.ExecuteTemplate(w, "login.html", LoginData{Error: msg})
h.Tpl.ExecuteTemplate(w, "login.html", structMessage.MessageData{Error: msg})
}

// Auth Handler (POST)
Expand Down
11 changes: 3 additions & 8 deletions internal/auth/signup.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,10 @@ import (
"net/http"
"strings"
"time"
structMessage "unicard-go/internal/pkg"
"unicard-go/internal/pkg/account"
)

// Struct to handle error message display in signup template
type ErrorMessage struct {
Error string
Success string
}

// User struct to hold signup data
type User struct {
UserID string
Expand Down Expand Up @@ -77,7 +72,7 @@ func (h *Handler) SignupView(w http.ResponseWriter, r *http.Request) {
msg = "System error activating card. Please contact support."
}
// Render the signup template with error message
h.Tpl.ExecuteTemplate(w, "signup.html", ErrorMessage{Error: msg})
h.Tpl.ExecuteTemplate(w, "signup.html", structMessage.MessageData{Error: msg})
}

// This function processes the signup form submission.
Expand Down Expand Up @@ -186,7 +181,7 @@ func (h *Handler) SignupHandler(w http.ResponseWriter, r *http.Request) {
if exists, _ := account.IsEmailExist(h.DB, user.Email); exists {
fmt.Printf("Validation Failed: Email %s already exists.\n", user.Email)
http.Redirect(w, r, "/signup?error=email", http.StatusSeeOther)
//h.Tpl.ExecuteTemplate(w, "signup.html", ErrorMessage{Error: "Email already registered."})
//h.Tpl.ExecuteTemplate(w, "signup.html", structMessage.MessageData{Error: "Email already registered."})
return
}
fmt.Printf("Email %s is available.\n", user.Email)
Expand Down
8 changes: 8 additions & 0 deletions internal/pkg/structMessage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package structMessage

// This file defines the data structures used to pass information
// between the Go code and the HTML templates for the account-related pages.
type MessageData struct {
Error string
Success string
}
12 changes: 9 additions & 3 deletions templates/addCards.html
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="/assets/admin/addcard.css">
<title>Add Card</title>
</head>

<body>
<h1>Add Card</h1>
{{if .Error}}
<div class="error">{{.Error}}</div>
<div class="error">{{.Error}}</div>
{{end}}
{{if .Success}}
<div class="success">{{.Success}}</div>
<div class="success">{{.Success}}</div>
{{end}}
<form action="/admin/addcardauth" method="POST">
<div class="form-group">
Expand All @@ -24,7 +26,8 @@ <h1>Add Card</h1>
<input type="number" id="initialAmount" name="initialAmount" step="0.01" min="0" required>
</div>
<div class="form-group" style="background-color: #f5f5f5; padding: 10px; border-radius: 4px; margin-top: 20px;">
<p style="margin: 0 0 10px 0; font-size: 12px; color: #666;"><em>The following fields will be auto-generated:</em></p>
<p style="margin: 0 0 10px 0; font-size: 12px; color: #666;"><em>The following fields will be
auto-generated:</em></p>
<div>
<label>Card Number:</label>
<p style="margin: 5px 0; color: #999;"><em>Auto-generated (16-digit)</em></p>
Expand All @@ -38,7 +41,10 @@ <h1>Add Card</h1>
<p style="margin: 5px 0; color: #999;"><em>Auto-calculated (10 years from today)</em></p>
</div>
</div>

<input type="submit" value="Add Card">
<input type="button" value="Cancel" class="cancel-button" onclick="window.history.back();">
</form>
</body>

</html>