first commit
This commit is contained in:
@@ -0,0 +1,133 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Session represents a user session
|
||||
type Session struct {
|
||||
ID string
|
||||
UserID uint
|
||||
Username string
|
||||
CreatedAt time.Time
|
||||
ExpiresAt time.Time
|
||||
}
|
||||
|
||||
// SessionManager manages user sessions
|
||||
type SessionManager struct {
|
||||
sessions map[string]*Session
|
||||
mutex sync.RWMutex
|
||||
}
|
||||
|
||||
// NewSessionManager creates a new session manager
|
||||
func NewSessionManager() *SessionManager {
|
||||
return &SessionManager{
|
||||
sessions: make(map[string]*Session),
|
||||
}
|
||||
}
|
||||
|
||||
// generateSessionID generates a random session ID
|
||||
func generateSessionID() string {
|
||||
bytes := make([]byte, 32)
|
||||
rand.Read(bytes)
|
||||
return hex.EncodeToString(bytes)
|
||||
}
|
||||
|
||||
// CreateSession creates a new session for a user
|
||||
func (sm *SessionManager) CreateSession(userID int, username string) *Session {
|
||||
sm.mutex.Lock()
|
||||
defer sm.mutex.Unlock()
|
||||
|
||||
// Clean up expired sessions
|
||||
sm.cleanupExpiredSessions()
|
||||
|
||||
sessionID := generateSessionID()
|
||||
session := &Session{
|
||||
ID: sessionID,
|
||||
UserID: uint(userID),
|
||||
Username: username,
|
||||
CreatedAt: time.Now(),
|
||||
ExpiresAt: time.Now().Add(24 * time.Hour), // 24 hours
|
||||
}
|
||||
|
||||
sm.sessions[sessionID] = session
|
||||
return session
|
||||
}
|
||||
|
||||
// GetSession retrieves a session by ID
|
||||
func (sm *SessionManager) GetSession(sessionID string) (*Session, bool) {
|
||||
sm.mutex.RLock()
|
||||
defer sm.mutex.RUnlock()
|
||||
|
||||
session, exists := sm.sessions[sessionID]
|
||||
if !exists {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// Check if session is expired
|
||||
if time.Now().After(session.ExpiresAt) {
|
||||
delete(sm.sessions, sessionID)
|
||||
return nil, false
|
||||
}
|
||||
|
||||
return session, true
|
||||
}
|
||||
|
||||
// DeleteSession deletes a session
|
||||
func (sm *SessionManager) DeleteSession(sessionID string) {
|
||||
sm.mutex.Lock()
|
||||
defer sm.mutex.Unlock()
|
||||
|
||||
delete(sm.sessions, sessionID)
|
||||
}
|
||||
|
||||
// cleanupExpiredSessions removes expired sessions
|
||||
func (sm *SessionManager) cleanupExpiredSessions() {
|
||||
now := time.Now()
|
||||
for id, session := range sm.sessions {
|
||||
if now.After(session.ExpiresAt) {
|
||||
delete(sm.sessions, id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// SetSessionCookie sets a session cookie
|
||||
func SetSessionCookie(w http.ResponseWriter, sessionID string) {
|
||||
cookie := &http.Cookie{
|
||||
Name: "session_id",
|
||||
Value: sessionID,
|
||||
Expires: time.Now().Add(24 * time.Hour),
|
||||
HttpOnly: true,
|
||||
Secure: false, // Set to true in production with HTTPS
|
||||
SameSite: http.SameSiteLaxMode,
|
||||
Path: "/",
|
||||
}
|
||||
http.SetCookie(w, cookie)
|
||||
}
|
||||
|
||||
// GetSessionCookie gets the session ID from cookie
|
||||
func GetSessionCookie(r *http.Request) (string, error) {
|
||||
cookie, err := r.Cookie("session_id")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return cookie.Value, nil
|
||||
}
|
||||
|
||||
// ClearSessionCookie clears the session cookie
|
||||
func ClearSessionCookie(w http.ResponseWriter) {
|
||||
cookie := &http.Cookie{
|
||||
Name: "session_id",
|
||||
Value: "",
|
||||
Expires: time.Unix(0, 0),
|
||||
HttpOnly: true,
|
||||
Secure: false, // Set to true in production with HTTPS
|
||||
SameSite: http.SameSiteLaxMode,
|
||||
Path: "/",
|
||||
}
|
||||
http.SetCookie(w, cookie)
|
||||
}
|
||||
@@ -0,0 +1,384 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// Config holds all application configuration
|
||||
type Config struct {
|
||||
Server ServerConfig `mapstructure:"server"`
|
||||
Database DatabaseConfig `mapstructure:"database"`
|
||||
App AppConfig `mapstructure:"app"`
|
||||
Security SecurityConfig `mapstructure:"security"`
|
||||
Logging LoggingConfig `mapstructure:"logging"`
|
||||
External ExternalConfig `mapstructure:"external_services"`
|
||||
Features FeatureConfig `mapstructure:"features"`
|
||||
Defaults DefaultConfig `mapstructure:"defaults"`
|
||||
}
|
||||
|
||||
// ServerConfig holds server-related configuration
|
||||
type ServerConfig struct {
|
||||
Host string `mapstructure:"host"`
|
||||
Port int `mapstructure:"port"`
|
||||
ReadTimeout time.Duration `mapstructure:"read_timeout"`
|
||||
WriteTimeout time.Duration `mapstructure:"write_timeout"`
|
||||
IdleTimeout time.Duration `mapstructure:"idle_timeout"`
|
||||
ShutdownTimeout time.Duration `mapstructure:"shutdown_timeout"`
|
||||
}
|
||||
|
||||
// DatabaseConfig holds database-related configuration
|
||||
type DatabaseConfig struct {
|
||||
Path string `mapstructure:"path"`
|
||||
ConnectionPool DatabaseConnectionConfig `mapstructure:"connection_pool"`
|
||||
Logging DatabaseLoggingConfig `mapstructure:"logging"`
|
||||
Migration DatabaseMigrationConfig `mapstructure:"migration"`
|
||||
Performance DatabasePerformanceConfig `mapstructure:"performance"`
|
||||
}
|
||||
|
||||
// DatabaseConnectionConfig holds database connection pool settings
|
||||
type DatabaseConnectionConfig struct {
|
||||
MaxIdleConnections int `mapstructure:"max_idle_connections"`
|
||||
MaxOpenConnections int `mapstructure:"max_open_connections"`
|
||||
ConnectionMaxLifetime time.Duration `mapstructure:"connection_max_lifetime"`
|
||||
ConnectionMaxIdleTime time.Duration `mapstructure:"connection_max_idle_time"`
|
||||
}
|
||||
|
||||
// DatabaseLoggingConfig holds database logging settings
|
||||
type DatabaseLoggingConfig struct {
|
||||
Level string `mapstructure:"level"`
|
||||
SlowQueryThreshold time.Duration `mapstructure:"slow_query_threshold"`
|
||||
Debug bool `mapstructure:"debug"`
|
||||
}
|
||||
|
||||
// DatabaseMigrationConfig holds database migration settings
|
||||
type DatabaseMigrationConfig struct {
|
||||
AutoMigrate bool `mapstructure:"auto_migrate"`
|
||||
DropTablesFirst bool `mapstructure:"drop_tables_first"`
|
||||
CreateBatchSize int `mapstructure:"create_batch_size"`
|
||||
}
|
||||
|
||||
// DatabasePerformanceConfig holds database performance settings
|
||||
type DatabasePerformanceConfig struct {
|
||||
PrepareStatements bool `mapstructure:"prepare_statements"`
|
||||
DisableForeignKeyCheck bool `mapstructure:"disable_foreign_key_check"`
|
||||
IgnoreRelationshipsWhenMigrating bool `mapstructure:"ignore_relationships_when_migrating"`
|
||||
QueryFields bool `mapstructure:"query_fields"`
|
||||
DryRun bool `mapstructure:"dry_run"`
|
||||
CreateInBatches int `mapstructure:"create_in_batches"`
|
||||
}
|
||||
|
||||
// AppConfig holds general application settings
|
||||
type AppConfig struct {
|
||||
Name string `mapstructure:"name"`
|
||||
Version string `mapstructure:"version"`
|
||||
Environment string `mapstructure:"environment"`
|
||||
Debug bool `mapstructure:"debug"`
|
||||
}
|
||||
|
||||
// SecurityConfig holds security-related settings
|
||||
type SecurityConfig struct {
|
||||
Session SessionConfig `mapstructure:"session"`
|
||||
Password PasswordConfig `mapstructure:"password"`
|
||||
}
|
||||
|
||||
// SessionConfig holds session management settings
|
||||
type SessionConfig struct {
|
||||
Timeout time.Duration `mapstructure:"timeout"`
|
||||
CookieName string `mapstructure:"cookie_name"`
|
||||
SecureCookies bool `mapstructure:"secure_cookies"`
|
||||
HttpOnly bool `mapstructure:"http_only"`
|
||||
}
|
||||
|
||||
// PasswordConfig holds password requirements
|
||||
type PasswordConfig struct {
|
||||
MinLength int `mapstructure:"min_length"`
|
||||
RequireUppercase bool `mapstructure:"require_uppercase"`
|
||||
RequireLowercase bool `mapstructure:"require_lowercase"`
|
||||
RequireNumbers bool `mapstructure:"require_numbers"`
|
||||
RequireSpecialChars bool `mapstructure:"require_special_chars"`
|
||||
}
|
||||
|
||||
// LoggingConfig holds logging settings
|
||||
type LoggingConfig struct {
|
||||
Level string `mapstructure:"level"`
|
||||
Format string `mapstructure:"format"`
|
||||
Output string `mapstructure:"output"`
|
||||
FilePath string `mapstructure:"file_path"`
|
||||
Rotation LogRotationConfig `mapstructure:"rotation"`
|
||||
}
|
||||
|
||||
// LogRotationConfig holds log rotation settings
|
||||
type LogRotationConfig struct {
|
||||
Enabled bool `mapstructure:"enabled"`
|
||||
MaxSize string `mapstructure:"max_size"`
|
||||
MaxAge string `mapstructure:"max_age"`
|
||||
MaxBackups int `mapstructure:"max_backups"`
|
||||
}
|
||||
|
||||
// ExternalConfig holds external service configurations
|
||||
type ExternalConfig struct {
|
||||
OverpassAPI OverpassAPIConfig `mapstructure:"overpass_api"`
|
||||
}
|
||||
|
||||
// OverpassAPIConfig holds OpenStreetMap Overpass API settings
|
||||
type OverpassAPIConfig struct {
|
||||
URL string `mapstructure:"url"`
|
||||
Timeout time.Duration `mapstructure:"timeout"`
|
||||
MaxRetries int `mapstructure:"max_retries"`
|
||||
SearchRadius int `mapstructure:"search_radius"`
|
||||
}
|
||||
|
||||
// FeatureConfig holds feature flag settings
|
||||
type FeatureConfig struct {
|
||||
FuelStationSearch bool `mapstructure:"fuel_station_search"`
|
||||
VehicleManagement bool `mapstructure:"vehicle_management"`
|
||||
StatisticsDashboard bool `mapstructure:"statistics_dashboard"`
|
||||
DataExport bool `mapstructure:"data_export"`
|
||||
APIEndpoints bool `mapstructure:"api_endpoints"`
|
||||
}
|
||||
|
||||
// DefaultConfig holds default values for new entities
|
||||
type DefaultConfig struct {
|
||||
Currency string `mapstructure:"currency"`
|
||||
FuelType string `mapstructure:"fuel_type"`
|
||||
DistanceUnit string `mapstructure:"distance_unit"`
|
||||
VolumeUnit string `mapstructure:"volume_unit"`
|
||||
}
|
||||
|
||||
// Load loads configuration from file, environment variables, and defaults
|
||||
func Load(configPath string) (*Config, error) {
|
||||
v := viper.New()
|
||||
|
||||
// Set defaults
|
||||
setDefaults(v)
|
||||
|
||||
// Configure Viper
|
||||
if configPath != "" {
|
||||
v.SetConfigFile(configPath)
|
||||
} else {
|
||||
v.SetConfigName("config")
|
||||
v.SetConfigType("yaml")
|
||||
v.AddConfigPath(".")
|
||||
v.AddConfigPath("./config")
|
||||
v.AddConfigPath("$HOME/.tankstopp")
|
||||
v.AddConfigPath("/etc/tankstopp")
|
||||
}
|
||||
|
||||
// Enable environment variable binding
|
||||
v.AutomaticEnv()
|
||||
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
||||
v.SetEnvPrefix("TANKSTOPP")
|
||||
|
||||
// Read config file
|
||||
if err := v.ReadInConfig(); err != nil {
|
||||
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
|
||||
return nil, fmt.Errorf("error reading config file: %w", err)
|
||||
}
|
||||
// Config file not found, continue with defaults and env vars
|
||||
}
|
||||
|
||||
// Unmarshal into struct
|
||||
var config Config
|
||||
if err := v.Unmarshal(&config); err != nil {
|
||||
return nil, fmt.Errorf("error unmarshaling config: %w", err)
|
||||
}
|
||||
|
||||
// Validate configuration
|
||||
if err := config.Validate(); err != nil {
|
||||
return nil, fmt.Errorf("invalid configuration: %w", err)
|
||||
}
|
||||
|
||||
return &config, nil
|
||||
}
|
||||
|
||||
// setDefaults sets default values for configuration
|
||||
func setDefaults(v *viper.Viper) {
|
||||
// Server defaults
|
||||
v.SetDefault("server.host", "localhost")
|
||||
v.SetDefault("server.port", 8081)
|
||||
v.SetDefault("server.read_timeout", "30s")
|
||||
v.SetDefault("server.write_timeout", "30s")
|
||||
v.SetDefault("server.idle_timeout", "120s")
|
||||
v.SetDefault("server.shutdown_timeout", "10s")
|
||||
|
||||
// Database defaults
|
||||
v.SetDefault("database.path", "fuel_stops.db")
|
||||
v.SetDefault("database.connection_pool.max_idle_connections", 10)
|
||||
v.SetDefault("database.connection_pool.max_open_connections", 100)
|
||||
v.SetDefault("database.connection_pool.connection_max_lifetime", "1h")
|
||||
v.SetDefault("database.connection_pool.connection_max_idle_time", "30m")
|
||||
v.SetDefault("database.logging.level", "warn")
|
||||
v.SetDefault("database.logging.slow_query_threshold", "200ms")
|
||||
v.SetDefault("database.logging.debug", false)
|
||||
v.SetDefault("database.migration.auto_migrate", true)
|
||||
v.SetDefault("database.migration.drop_tables_first", false)
|
||||
v.SetDefault("database.migration.create_batch_size", 1000)
|
||||
v.SetDefault("database.performance.prepare_statements", true)
|
||||
v.SetDefault("database.performance.disable_foreign_key_check", false)
|
||||
v.SetDefault("database.performance.ignore_relationships_when_migrating", false)
|
||||
v.SetDefault("database.performance.query_fields", true)
|
||||
v.SetDefault("database.performance.dry_run", false)
|
||||
v.SetDefault("database.performance.create_in_batches", 100)
|
||||
|
||||
// App defaults
|
||||
v.SetDefault("app.name", "TankStopp")
|
||||
v.SetDefault("app.version", "1.0.0")
|
||||
v.SetDefault("app.environment", "development")
|
||||
v.SetDefault("app.debug", true)
|
||||
|
||||
// Security defaults
|
||||
v.SetDefault("security.session.timeout", "24h")
|
||||
v.SetDefault("security.session.cookie_name", "tankstopp_session")
|
||||
v.SetDefault("security.session.secure_cookies", false)
|
||||
v.SetDefault("security.session.http_only", true)
|
||||
v.SetDefault("security.password.min_length", 8)
|
||||
v.SetDefault("security.password.require_uppercase", true)
|
||||
v.SetDefault("security.password.require_lowercase", true)
|
||||
v.SetDefault("security.password.require_numbers", true)
|
||||
v.SetDefault("security.password.require_special_chars", false)
|
||||
|
||||
// Logging defaults
|
||||
v.SetDefault("logging.level", "info")
|
||||
v.SetDefault("logging.format", "text")
|
||||
v.SetDefault("logging.output", "stdout")
|
||||
v.SetDefault("logging.file_path", "logs/tankstopp.log")
|
||||
v.SetDefault("logging.rotation.enabled", false)
|
||||
v.SetDefault("logging.rotation.max_size", "100MB")
|
||||
v.SetDefault("logging.rotation.max_age", "30d")
|
||||
v.SetDefault("logging.rotation.max_backups", 5)
|
||||
|
||||
// External services defaults
|
||||
v.SetDefault("external_services.overpass_api.url", "https://overpass-api.de/api/interpreter")
|
||||
v.SetDefault("external_services.overpass_api.timeout", "30s")
|
||||
v.SetDefault("external_services.overpass_api.max_retries", 3)
|
||||
v.SetDefault("external_services.overpass_api.search_radius", 5000)
|
||||
|
||||
// Feature flags defaults
|
||||
v.SetDefault("features.fuel_station_search", true)
|
||||
v.SetDefault("features.vehicle_management", true)
|
||||
v.SetDefault("features.statistics_dashboard", true)
|
||||
v.SetDefault("features.data_export", true)
|
||||
v.SetDefault("features.api_endpoints", true)
|
||||
|
||||
// Default values
|
||||
v.SetDefault("defaults.currency", "EUR")
|
||||
v.SetDefault("defaults.fuel_type", "Super E5")
|
||||
v.SetDefault("defaults.distance_unit", "km")
|
||||
v.SetDefault("defaults.volume_unit", "liters")
|
||||
}
|
||||
|
||||
// Validate validates the configuration
|
||||
func (c *Config) Validate() error {
|
||||
// Validate server config
|
||||
if c.Server.Port < 1 || c.Server.Port > 65535 {
|
||||
return fmt.Errorf("invalid server port: %d", c.Server.Port)
|
||||
}
|
||||
|
||||
// Validate database config
|
||||
if c.Database.Path == "" {
|
||||
return fmt.Errorf("database path cannot be empty")
|
||||
}
|
||||
|
||||
if c.Database.ConnectionPool.MaxIdleConnections < 0 {
|
||||
return fmt.Errorf("max idle connections cannot be negative")
|
||||
}
|
||||
|
||||
if c.Database.ConnectionPool.MaxOpenConnections < 0 {
|
||||
return fmt.Errorf("max open connections cannot be negative")
|
||||
}
|
||||
|
||||
// Validate app config
|
||||
if c.App.Name == "" {
|
||||
return fmt.Errorf("app name cannot be empty")
|
||||
}
|
||||
|
||||
validEnvs := []string{"development", "production", "test"}
|
||||
if !contains(validEnvs, c.App.Environment) {
|
||||
return fmt.Errorf("invalid environment: %s (must be one of: %v)", c.App.Environment, validEnvs)
|
||||
}
|
||||
|
||||
// Validate security config
|
||||
if c.Security.Password.MinLength < 4 {
|
||||
return fmt.Errorf("minimum password length cannot be less than 4")
|
||||
}
|
||||
|
||||
// Validate logging config
|
||||
validLogLevels := []string{"debug", "info", "warn", "error"}
|
||||
if !contains(validLogLevels, c.Logging.Level) {
|
||||
return fmt.Errorf("invalid log level: %s (must be one of: %v)", c.Logging.Level, validLogLevels)
|
||||
}
|
||||
|
||||
validLogFormats := []string{"json", "text"}
|
||||
if !contains(validLogFormats, c.Logging.Format) {
|
||||
return fmt.Errorf("invalid log format: %s (must be one of: %v)", c.Logging.Format, validLogFormats)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsProduction returns true if the app is running in production environment
|
||||
func (c *Config) IsProduction() bool {
|
||||
return c.App.Environment == "production"
|
||||
}
|
||||
|
||||
// IsDevelopment returns true if the app is running in development environment
|
||||
func (c *Config) IsDevelopment() bool {
|
||||
return c.App.Environment == "development"
|
||||
}
|
||||
|
||||
// IsTest returns true if the app is running in test environment
|
||||
func (c *Config) IsTest() bool {
|
||||
return c.App.Environment == "test"
|
||||
}
|
||||
|
||||
// GetServerAddress returns the server address in host:port format
|
||||
func (c *Config) GetServerAddress() string {
|
||||
return fmt.Sprintf("%s:%d", c.Server.Host, c.Server.Port)
|
||||
}
|
||||
|
||||
// String returns a string representation of the configuration (without sensitive data)
|
||||
func (c *Config) String() string {
|
||||
return fmt.Sprintf(`TankStopp Configuration:
|
||||
Server: %s
|
||||
Database: %s
|
||||
Environment: %s
|
||||
Debug: %t
|
||||
Features: %+v`,
|
||||
c.GetServerAddress(),
|
||||
c.Database.Path,
|
||||
c.App.Environment,
|
||||
c.App.Debug,
|
||||
c.Features)
|
||||
}
|
||||
|
||||
// contains checks if a slice contains a string
|
||||
func contains(slice []string, item string) bool {
|
||||
for _, s := range slice {
|
||||
if s == item {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// GetConfigFromEnv returns environment-specific configuration
|
||||
func GetConfigFromEnv() string {
|
||||
if configPath := os.Getenv("TANKSTOPP_CONFIG_PATH"); configPath != "" {
|
||||
return configPath
|
||||
}
|
||||
|
||||
env := os.Getenv("TANKSTOPP_ENV")
|
||||
if env == "" {
|
||||
env = os.Getenv("ENV")
|
||||
}
|
||||
if env == "" {
|
||||
env = "development"
|
||||
}
|
||||
|
||||
return fmt.Sprintf("config.%s.yaml", env)
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
package currency
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Currency represents a currency with its details
|
||||
type Currency struct {
|
||||
Code string
|
||||
Name string
|
||||
Symbol string
|
||||
}
|
||||
|
||||
// SupportedCurrencies returns a list of supported currencies
|
||||
func SupportedCurrencies() []Currency {
|
||||
return []Currency{
|
||||
{Code: "EUR", Name: "Euro", Symbol: "€"},
|
||||
{Code: "USD", Name: "US Dollar", Symbol: "$"},
|
||||
{Code: "GBP", Name: "British Pound", Symbol: "£"},
|
||||
{Code: "CHF", Name: "Swiss Franc", Symbol: "CHF"},
|
||||
{Code: "SEK", Name: "Swedish Krona", Symbol: "kr"},
|
||||
{Code: "NOK", Name: "Norwegian Krone", Symbol: "kr"},
|
||||
{Code: "DKK", Name: "Danish Krone", Symbol: "kr"},
|
||||
{Code: "PLN", Name: "Polish Zloty", Symbol: "zł"},
|
||||
{Code: "CZK", Name: "Czech Koruna", Symbol: "Kč"},
|
||||
{Code: "HUF", Name: "Hungarian Forint", Symbol: "Ft"},
|
||||
{Code: "CAD", Name: "Canadian Dollar", Symbol: "C$"},
|
||||
{Code: "AUD", Name: "Australian Dollar", Symbol: "A$"},
|
||||
{Code: "JPY", Name: "Japanese Yen", Symbol: "¥"},
|
||||
{Code: "CNY", Name: "Chinese Yuan", Symbol: "¥"},
|
||||
{Code: "RUB", Name: "Russian Ruble", Symbol: "₽"},
|
||||
{Code: "BRL", Name: "Brazilian Real", Symbol: "R$"},
|
||||
{Code: "MXN", Name: "Mexican Peso", Symbol: "$"},
|
||||
{Code: "INR", Name: "Indian Rupee", Symbol: "₹"},
|
||||
{Code: "KRW", Name: "South Korean Won", Symbol: "₩"},
|
||||
{Code: "SGD", Name: "Singapore Dollar", Symbol: "S$"},
|
||||
{Code: "HKD", Name: "Hong Kong Dollar", Symbol: "HK$"},
|
||||
{Code: "NZD", Name: "New Zealand Dollar", Symbol: "NZ$"},
|
||||
{Code: "ZAR", Name: "South African Rand", Symbol: "R"},
|
||||
{Code: "TRY", Name: "Turkish Lira", Symbol: "₺"},
|
||||
{Code: "ILS", Name: "Israeli Shekel", Symbol: "₪"},
|
||||
}
|
||||
}
|
||||
|
||||
// GetCurrency returns a currency by its code
|
||||
func GetCurrency(code string) (*Currency, bool) {
|
||||
code = strings.ToUpper(code)
|
||||
for _, currency := range SupportedCurrencies() {
|
||||
if currency.Code == code {
|
||||
return ¤cy, true
|
||||
}
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// GetCurrencySymbol returns the symbol for a currency code
|
||||
func GetCurrencySymbol(code string) string {
|
||||
if currency, exists := GetCurrency(code); exists {
|
||||
return currency.Symbol
|
||||
}
|
||||
return code // fallback to code if currency not found
|
||||
}
|
||||
|
||||
// GetCurrencyName returns the name for a currency code
|
||||
func GetCurrencyName(code string) string {
|
||||
if currency, exists := GetCurrency(code); exists {
|
||||
return currency.Name
|
||||
}
|
||||
return code // fallback to code if currency not found
|
||||
}
|
||||
|
||||
// FormatPrice formats a price with the appropriate currency symbol
|
||||
func FormatPrice(amount float64, currencyCode string) string {
|
||||
symbol := GetCurrencySymbol(currencyCode)
|
||||
|
||||
// Handle currencies with different formatting conventions
|
||||
switch strings.ToUpper(currencyCode) {
|
||||
case "EUR", "GBP", "CHF":
|
||||
return fmt.Sprintf("%s%.2f", symbol, amount)
|
||||
case "USD", "CAD", "AUD", "HKD", "SGD", "NZD", "BRL", "MXN":
|
||||
return fmt.Sprintf("%s%.2f", symbol, amount)
|
||||
case "JPY", "KRW":
|
||||
// Yen and Won typically don't use decimal places
|
||||
return fmt.Sprintf("%s%.0f", symbol, amount)
|
||||
case "SEK", "NOK", "DKK":
|
||||
// Scandinavian currencies often put symbol after
|
||||
return fmt.Sprintf("%.2f %s", amount, symbol)
|
||||
case "PLN":
|
||||
return fmt.Sprintf("%.2f %s", amount, symbol)
|
||||
case "CZK":
|
||||
return fmt.Sprintf("%.2f %s", amount, symbol)
|
||||
case "HUF":
|
||||
return fmt.Sprintf("%.0f %s", amount, symbol)
|
||||
case "RUB":
|
||||
return fmt.Sprintf("%.2f %s", amount, symbol)
|
||||
case "INR":
|
||||
return fmt.Sprintf("%s%.2f", symbol, amount)
|
||||
case "CNY":
|
||||
return fmt.Sprintf("%s%.2f", symbol, amount)
|
||||
case "ZAR":
|
||||
return fmt.Sprintf("%s%.2f", symbol, amount)
|
||||
case "TRY":
|
||||
return fmt.Sprintf("%.2f %s", amount, symbol)
|
||||
case "ILS":
|
||||
return fmt.Sprintf("%s%.2f", symbol, amount)
|
||||
default:
|
||||
return fmt.Sprintf("%s%.2f", symbol, amount)
|
||||
}
|
||||
}
|
||||
|
||||
// FormatPricePerLiter formats a price per liter with the appropriate currency symbol
|
||||
func FormatPricePerLiter(amount float64, currencyCode string) string {
|
||||
symbol := GetCurrencySymbol(currencyCode)
|
||||
|
||||
// Handle currencies with different formatting conventions
|
||||
switch strings.ToUpper(currencyCode) {
|
||||
case "EUR", "GBP", "CHF":
|
||||
return fmt.Sprintf("%s%.3f", symbol, amount)
|
||||
case "USD", "CAD", "AUD", "HKD", "SGD", "NZD", "BRL", "MXN":
|
||||
return fmt.Sprintf("%s%.3f", symbol, amount)
|
||||
case "JPY", "KRW":
|
||||
// Yen and Won typically don't use decimal places
|
||||
return fmt.Sprintf("%s%.0f", symbol, amount)
|
||||
case "SEK", "NOK", "DKK":
|
||||
return fmt.Sprintf("%.3f %s", amount, symbol)
|
||||
case "PLN":
|
||||
return fmt.Sprintf("%.3f %s", amount, symbol)
|
||||
case "CZK":
|
||||
return fmt.Sprintf("%.2f %s", amount, symbol)
|
||||
case "HUF":
|
||||
return fmt.Sprintf("%.0f %s", amount, symbol)
|
||||
case "RUB":
|
||||
return fmt.Sprintf("%.2f %s", amount, symbol)
|
||||
case "INR":
|
||||
return fmt.Sprintf("%s%.3f", symbol, amount)
|
||||
case "CNY":
|
||||
return fmt.Sprintf("%s%.3f", symbol, amount)
|
||||
case "ZAR":
|
||||
return fmt.Sprintf("%s%.3f", symbol, amount)
|
||||
case "TRY":
|
||||
return fmt.Sprintf("%.3f %s", amount, symbol)
|
||||
case "ILS":
|
||||
return fmt.Sprintf("%s%.3f", symbol, amount)
|
||||
default:
|
||||
return fmt.Sprintf("%s%.3f", symbol, amount)
|
||||
}
|
||||
}
|
||||
|
||||
// IsValidCurrency checks if a currency code is supported
|
||||
func IsValidCurrency(code string) bool {
|
||||
_, exists := GetCurrency(code)
|
||||
return exists
|
||||
}
|
||||
|
||||
// GetDefaultCurrency returns the default currency
|
||||
func GetDefaultCurrency() Currency {
|
||||
return Currency{Code: "EUR", Name: "Euro", Symbol: "€"}
|
||||
}
|
||||
@@ -0,0 +1,495 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
// Config holds database configuration settings
|
||||
type Config struct {
|
||||
// Database file path
|
||||
DatabasePath string
|
||||
|
||||
// Connection pool settings
|
||||
MaxIdleConns int
|
||||
MaxOpenConns int
|
||||
ConnMaxLifetime time.Duration
|
||||
ConnMaxIdleTime time.Duration
|
||||
|
||||
// Logging settings
|
||||
LogLevel logger.LogLevel
|
||||
SlowQueryLog time.Duration
|
||||
|
||||
// Migration settings
|
||||
AutoMigrate bool
|
||||
DropTableFirst bool
|
||||
CreateBatchSize int
|
||||
|
||||
// Performance settings
|
||||
PrepareStmt bool
|
||||
DisableForeignKeyCheck bool
|
||||
IgnoreRelationshipsWhenMigrating bool
|
||||
|
||||
// Development settings
|
||||
Debug bool
|
||||
DryRun bool
|
||||
QueryFields bool
|
||||
CreateInBatches int
|
||||
}
|
||||
|
||||
// DefaultConfig returns a configuration with sensible defaults
|
||||
func DefaultConfig() *Config {
|
||||
return &Config{
|
||||
DatabasePath: "fuel_stops.db",
|
||||
MaxIdleConns: 10,
|
||||
MaxOpenConns: 100,
|
||||
ConnMaxLifetime: time.Hour,
|
||||
ConnMaxIdleTime: 30 * time.Minute,
|
||||
LogLevel: logger.Silent,
|
||||
SlowQueryLog: 200 * time.Millisecond,
|
||||
AutoMigrate: true,
|
||||
DropTableFirst: false,
|
||||
CreateBatchSize: 1000,
|
||||
PrepareStmt: true,
|
||||
DisableForeignKeyCheck: false,
|
||||
IgnoreRelationshipsWhenMigrating: false,
|
||||
Debug: false,
|
||||
DryRun: false,
|
||||
QueryFields: false,
|
||||
CreateInBatches: 100,
|
||||
}
|
||||
}
|
||||
|
||||
// LoadFromConfig loads configuration from config file using Viper
|
||||
func LoadFromConfig(configPath string) *Config {
|
||||
config := DefaultConfig()
|
||||
|
||||
// Initialize Viper
|
||||
v := viper.New()
|
||||
|
||||
// Set config file path if provided
|
||||
if configPath != "" {
|
||||
v.SetConfigFile(configPath)
|
||||
} else {
|
||||
// Search for config file in multiple locations
|
||||
v.SetConfigName("config")
|
||||
v.SetConfigType("yaml")
|
||||
v.AddConfigPath(".")
|
||||
v.AddConfigPath("./config")
|
||||
v.AddConfigPath("$HOME/.tankstopp")
|
||||
v.AddConfigPath("/etc/tankstopp")
|
||||
}
|
||||
|
||||
// Try to read config file
|
||||
if err := v.ReadInConfig(); err != nil {
|
||||
// If config file not found, fall back to environment variables
|
||||
return LoadFromEnv()
|
||||
}
|
||||
|
||||
// Load database configuration from Viper
|
||||
if v.IsSet("database.path") {
|
||||
config.DatabasePath = v.GetString("database.path")
|
||||
}
|
||||
|
||||
// Connection pool settings
|
||||
if v.IsSet("database.connection_pool.max_idle_connections") {
|
||||
config.MaxIdleConns = v.GetInt("database.connection_pool.max_idle_connections")
|
||||
}
|
||||
if v.IsSet("database.connection_pool.max_open_connections") {
|
||||
config.MaxOpenConns = v.GetInt("database.connection_pool.max_open_connections")
|
||||
}
|
||||
if v.IsSet("database.connection_pool.connection_max_lifetime") {
|
||||
config.ConnMaxLifetime = v.GetDuration("database.connection_pool.connection_max_lifetime")
|
||||
}
|
||||
if v.IsSet("database.connection_pool.connection_max_idle_time") {
|
||||
config.ConnMaxIdleTime = v.GetDuration("database.connection_pool.connection_max_idle_time")
|
||||
}
|
||||
|
||||
// Logging settings
|
||||
if v.IsSet("database.logging.level") {
|
||||
config.LogLevel = getLogLevelFromString(v.GetString("database.logging.level"))
|
||||
}
|
||||
if v.IsSet("database.logging.debug") {
|
||||
config.Debug = v.GetBool("database.logging.debug")
|
||||
}
|
||||
if v.IsSet("database.logging.slow_query_threshold") {
|
||||
config.SlowQueryLog = v.GetDuration("database.logging.slow_query_threshold")
|
||||
}
|
||||
|
||||
// Migration settings
|
||||
if v.IsSet("database.migration.auto_migrate") {
|
||||
config.AutoMigrate = v.GetBool("database.migration.auto_migrate")
|
||||
}
|
||||
if v.IsSet("database.migration.drop_tables_first") {
|
||||
config.DropTableFirst = v.GetBool("database.migration.drop_tables_first")
|
||||
}
|
||||
if v.IsSet("database.migration.create_batch_size") {
|
||||
config.CreateBatchSize = v.GetInt("database.migration.create_batch_size")
|
||||
}
|
||||
|
||||
// Performance settings
|
||||
if v.IsSet("database.performance.prepare_statements") {
|
||||
config.PrepareStmt = v.GetBool("database.performance.prepare_statements")
|
||||
}
|
||||
if v.IsSet("database.performance.disable_foreign_key_check") {
|
||||
config.DisableForeignKeyCheck = v.GetBool("database.performance.disable_foreign_key_check")
|
||||
}
|
||||
if v.IsSet("database.performance.ignore_relationships_when_migrating") {
|
||||
config.IgnoreRelationshipsWhenMigrating = v.GetBool("database.performance.ignore_relationships_when_migrating")
|
||||
}
|
||||
if v.IsSet("database.performance.query_fields") {
|
||||
config.QueryFields = v.GetBool("database.performance.query_fields")
|
||||
}
|
||||
if v.IsSet("database.performance.dry_run") {
|
||||
config.DryRun = v.GetBool("database.performance.dry_run")
|
||||
}
|
||||
if v.IsSet("database.performance.create_in_batches") {
|
||||
config.CreateInBatches = v.GetInt("database.performance.create_in_batches")
|
||||
}
|
||||
|
||||
// Environment variables still take precedence over config file
|
||||
config = mergeWithEnvVars(config)
|
||||
|
||||
return config
|
||||
}
|
||||
|
||||
// LoadFromEnv loads configuration from environment variables
|
||||
func LoadFromEnv() *Config {
|
||||
config := DefaultConfig()
|
||||
|
||||
// Database path
|
||||
if dbPath := os.Getenv("DB_PATH"); dbPath != "" {
|
||||
config.DatabasePath = dbPath
|
||||
}
|
||||
|
||||
// Connection pool settings
|
||||
if maxIdle := getEnvInt("DB_MAX_IDLE_CONNS", config.MaxIdleConns); maxIdle > 0 {
|
||||
config.MaxIdleConns = maxIdle
|
||||
}
|
||||
|
||||
if maxOpen := getEnvInt("DB_MAX_OPEN_CONNS", config.MaxOpenConns); maxOpen > 0 {
|
||||
config.MaxOpenConns = maxOpen
|
||||
}
|
||||
|
||||
if lifetime := getEnvDuration("DB_CONN_MAX_LIFETIME", config.ConnMaxLifetime); lifetime > 0 {
|
||||
config.ConnMaxLifetime = lifetime
|
||||
}
|
||||
|
||||
if idleTime := getEnvDuration("DB_CONN_MAX_IDLE_TIME", config.ConnMaxIdleTime); idleTime > 0 {
|
||||
config.ConnMaxIdleTime = idleTime
|
||||
}
|
||||
|
||||
// Logging settings
|
||||
config.LogLevel = getLogLevel()
|
||||
config.Debug = getEnvBool("DB_DEBUG", config.Debug)
|
||||
|
||||
if slowLog := getEnvDuration("DB_SLOW_QUERY_LOG", config.SlowQueryLog); slowLog > 0 {
|
||||
config.SlowQueryLog = slowLog
|
||||
}
|
||||
|
||||
// Migration settings
|
||||
config.AutoMigrate = getEnvBool("DB_AUTO_MIGRATE", config.AutoMigrate)
|
||||
config.DropTableFirst = getEnvBool("DB_DROP_TABLE_FIRST", config.DropTableFirst)
|
||||
|
||||
if batchSize := getEnvInt("DB_CREATE_BATCH_SIZE", config.CreateBatchSize); batchSize > 0 {
|
||||
config.CreateBatchSize = batchSize
|
||||
}
|
||||
|
||||
// Performance settings
|
||||
config.PrepareStmt = getEnvBool("DB_PREPARE_STMT", config.PrepareStmt)
|
||||
config.DisableForeignKeyCheck = getEnvBool("DB_DISABLE_FOREIGN_KEY_CHECK", config.DisableForeignKeyCheck)
|
||||
config.IgnoreRelationshipsWhenMigrating = getEnvBool("DB_IGNORE_RELATIONSHIPS_WHEN_MIGRATING", config.IgnoreRelationshipsWhenMigrating)
|
||||
|
||||
// Development settings
|
||||
config.DryRun = getEnvBool("DB_DRY_RUN", config.DryRun)
|
||||
config.QueryFields = getEnvBool("DB_QUERY_FIELDS", config.QueryFields)
|
||||
|
||||
if inBatches := getEnvInt("DB_CREATE_IN_BATCHES", config.CreateInBatches); inBatches > 0 {
|
||||
config.CreateInBatches = inBatches
|
||||
}
|
||||
|
||||
return config
|
||||
}
|
||||
|
||||
// mergeWithEnvVars merges environment variables into existing config
|
||||
// Environment variables take precedence over config file values
|
||||
func mergeWithEnvVars(config *Config) *Config {
|
||||
// Database path
|
||||
if dbPath := os.Getenv("DB_PATH"); dbPath != "" {
|
||||
config.DatabasePath = dbPath
|
||||
}
|
||||
|
||||
// Connection pool settings
|
||||
if maxIdle := getEnvInt("DB_MAX_IDLE_CONNS", config.MaxIdleConns); maxIdle > 0 {
|
||||
config.MaxIdleConns = maxIdle
|
||||
}
|
||||
|
||||
if maxOpen := getEnvInt("DB_MAX_OPEN_CONNS", config.MaxOpenConns); maxOpen > 0 {
|
||||
config.MaxOpenConns = maxOpen
|
||||
}
|
||||
|
||||
if lifetime := getEnvDuration("DB_CONN_MAX_LIFETIME", config.ConnMaxLifetime); lifetime > 0 {
|
||||
config.ConnMaxLifetime = lifetime
|
||||
}
|
||||
|
||||
if idleTime := getEnvDuration("DB_CONN_MAX_IDLE_TIME", config.ConnMaxIdleTime); idleTime > 0 {
|
||||
config.ConnMaxIdleTime = idleTime
|
||||
}
|
||||
|
||||
// Logging settings
|
||||
if envLogLevel := getLogLevel(); envLogLevel != logger.Silent {
|
||||
config.LogLevel = envLogLevel
|
||||
}
|
||||
if envDebug := os.Getenv("DB_DEBUG"); envDebug != "" {
|
||||
config.Debug = getEnvBool("DB_DEBUG", config.Debug)
|
||||
}
|
||||
|
||||
if slowLog := getEnvDuration("DB_SLOW_QUERY_LOG", config.SlowQueryLog); slowLog > 0 {
|
||||
config.SlowQueryLog = slowLog
|
||||
}
|
||||
|
||||
// Migration settings
|
||||
if envAutoMigrate := os.Getenv("DB_AUTO_MIGRATE"); envAutoMigrate != "" {
|
||||
config.AutoMigrate = getEnvBool("DB_AUTO_MIGRATE", config.AutoMigrate)
|
||||
}
|
||||
if envDropFirst := os.Getenv("DB_DROP_TABLE_FIRST"); envDropFirst != "" {
|
||||
config.DropTableFirst = getEnvBool("DB_DROP_TABLE_FIRST", config.DropTableFirst)
|
||||
}
|
||||
|
||||
if batchSize := getEnvInt("DB_CREATE_BATCH_SIZE", config.CreateBatchSize); batchSize > 0 {
|
||||
config.CreateBatchSize = batchSize
|
||||
}
|
||||
|
||||
// Performance settings
|
||||
if envPrepare := os.Getenv("DB_PREPARE_STMT"); envPrepare != "" {
|
||||
config.PrepareStmt = getEnvBool("DB_PREPARE_STMT", config.PrepareStmt)
|
||||
}
|
||||
if envFKCheck := os.Getenv("DB_DISABLE_FOREIGN_KEY_CHECK"); envFKCheck != "" {
|
||||
config.DisableForeignKeyCheck = getEnvBool("DB_DISABLE_FOREIGN_KEY_CHECK", config.DisableForeignKeyCheck)
|
||||
}
|
||||
if envIgnoreRel := os.Getenv("DB_IGNORE_RELATIONSHIPS_WHEN_MIGRATING"); envIgnoreRel != "" {
|
||||
config.IgnoreRelationshipsWhenMigrating = getEnvBool("DB_IGNORE_RELATIONSHIPS_WHEN_MIGRATING", config.IgnoreRelationshipsWhenMigrating)
|
||||
}
|
||||
|
||||
// Development settings
|
||||
if envDryRun := os.Getenv("DB_DRY_RUN"); envDryRun != "" {
|
||||
config.DryRun = getEnvBool("DB_DRY_RUN", config.DryRun)
|
||||
}
|
||||
if envQueryFields := os.Getenv("DB_QUERY_FIELDS"); envQueryFields != "" {
|
||||
config.QueryFields = getEnvBool("DB_QUERY_FIELDS", config.QueryFields)
|
||||
}
|
||||
|
||||
if inBatches := getEnvInt("DB_CREATE_IN_BATCHES", config.CreateInBatches); inBatches > 0 {
|
||||
config.CreateInBatches = inBatches
|
||||
}
|
||||
|
||||
return config
|
||||
}
|
||||
|
||||
// Validate checks if the configuration is valid
|
||||
func (c *Config) Validate() error {
|
||||
if c.DatabasePath == "" {
|
||||
return fmt.Errorf("database path cannot be empty")
|
||||
}
|
||||
|
||||
if c.MaxIdleConns < 0 {
|
||||
return fmt.Errorf("max idle connections cannot be negative")
|
||||
}
|
||||
|
||||
if c.MaxOpenConns < 0 {
|
||||
return fmt.Errorf("max open connections cannot be negative")
|
||||
}
|
||||
|
||||
if c.MaxIdleConns > c.MaxOpenConns && c.MaxOpenConns > 0 {
|
||||
return fmt.Errorf("max idle connections (%d) cannot be greater than max open connections (%d)",
|
||||
c.MaxIdleConns, c.MaxOpenConns)
|
||||
}
|
||||
|
||||
if c.ConnMaxLifetime < 0 {
|
||||
return fmt.Errorf("connection max lifetime cannot be negative")
|
||||
}
|
||||
|
||||
if c.ConnMaxIdleTime < 0 {
|
||||
return fmt.Errorf("connection max idle time cannot be negative")
|
||||
}
|
||||
|
||||
if c.SlowQueryLog < 0 {
|
||||
return fmt.Errorf("slow query log threshold cannot be negative")
|
||||
}
|
||||
|
||||
if c.CreateBatchSize <= 0 {
|
||||
return fmt.Errorf("create batch size must be greater than 0")
|
||||
}
|
||||
|
||||
if c.CreateInBatches <= 0 {
|
||||
return fmt.Errorf("create in batches size must be greater than 0")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// String returns a string representation of the configuration
|
||||
func (c *Config) String() string {
|
||||
return fmt.Sprintf(`Database Configuration:
|
||||
Database Path: %s
|
||||
Max Idle Connections: %d
|
||||
Max Open Connections: %d
|
||||
Connection Max Lifetime: %v
|
||||
Connection Max Idle Time: %v
|
||||
Log Level: %v
|
||||
Slow Query Log Threshold: %v
|
||||
Auto Migrate: %t
|
||||
Prepare Statements: %t
|
||||
Debug Mode: %t
|
||||
Dry Run: %t
|
||||
Create Batch Size: %d
|
||||
Create In Batches: %d`,
|
||||
c.DatabasePath,
|
||||
c.MaxIdleConns,
|
||||
c.MaxOpenConns,
|
||||
c.ConnMaxLifetime,
|
||||
c.ConnMaxIdleTime,
|
||||
c.LogLevel,
|
||||
c.SlowQueryLog,
|
||||
c.AutoMigrate,
|
||||
c.PrepareStmt,
|
||||
c.Debug,
|
||||
c.DryRun,
|
||||
c.CreateBatchSize,
|
||||
c.CreateInBatches,
|
||||
)
|
||||
}
|
||||
|
||||
// IsProduction returns true if running in production environment
|
||||
func (c *Config) IsProduction() bool {
|
||||
env := os.Getenv("ENV")
|
||||
return env == "production" || env == "prod"
|
||||
}
|
||||
|
||||
// IsDevelopment returns true if running in development environment
|
||||
func (c *Config) IsDevelopment() bool {
|
||||
env := os.Getenv("ENV")
|
||||
return env == "development" || env == "dev" || env == ""
|
||||
}
|
||||
|
||||
// IsTest returns true if running in test environment
|
||||
func (c *Config) IsTest() bool {
|
||||
env := os.Getenv("ENV")
|
||||
return env == "test" || env == "testing"
|
||||
}
|
||||
|
||||
// Helper functions
|
||||
|
||||
func getEnvInt(key string, defaultValue int) int {
|
||||
if value := os.Getenv(key); value != "" {
|
||||
if intValue, err := strconv.Atoi(value); err == nil {
|
||||
return intValue
|
||||
}
|
||||
}
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
func getEnvBool(key string, defaultValue bool) bool {
|
||||
if value := os.Getenv(key); value != "" {
|
||||
if boolValue, err := strconv.ParseBool(value); err == nil {
|
||||
return boolValue
|
||||
}
|
||||
}
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
func getEnvDuration(key string, defaultValue time.Duration) time.Duration {
|
||||
if value := os.Getenv(key); value != "" {
|
||||
if duration, err := time.ParseDuration(value); err == nil {
|
||||
return duration
|
||||
}
|
||||
}
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
func getLogLevel() logger.LogLevel {
|
||||
debug := getEnvBool("DB_DEBUG", false)
|
||||
env := os.Getenv("ENV")
|
||||
logLevel := os.Getenv("DB_LOG_LEVEL")
|
||||
|
||||
switch {
|
||||
case debug:
|
||||
return logger.Info
|
||||
case env == "development" || env == "dev":
|
||||
return logger.Warn
|
||||
case env == "test" || env == "testing":
|
||||
return logger.Silent
|
||||
case logLevel == "silent":
|
||||
return logger.Silent
|
||||
case logLevel == "error":
|
||||
return logger.Error
|
||||
case logLevel == "warn":
|
||||
return logger.Warn
|
||||
case logLevel == "info":
|
||||
return logger.Info
|
||||
default:
|
||||
return logger.Silent
|
||||
}
|
||||
}
|
||||
|
||||
// getLogLevelFromString converts string log level to GORM logger level
|
||||
func getLogLevelFromString(level string) logger.LogLevel {
|
||||
switch strings.ToLower(level) {
|
||||
case "silent":
|
||||
return logger.Silent
|
||||
case "error":
|
||||
return logger.Error
|
||||
case "warn", "warning":
|
||||
return logger.Warn
|
||||
case "info":
|
||||
return logger.Info
|
||||
default:
|
||||
return logger.Silent
|
||||
}
|
||||
}
|
||||
|
||||
// Environment variable documentation
|
||||
/*
|
||||
Available Environment Variables:
|
||||
|
||||
Database Settings:
|
||||
DB_PATH - Database file path (default: "fuel_stops.db")
|
||||
DB_AUTO_MIGRATE - Enable automatic migrations (default: true)
|
||||
DB_DROP_TABLE_FIRST - Drop tables before migration (default: false)
|
||||
|
||||
Connection Pool Settings:
|
||||
DB_MAX_IDLE_CONNS - Maximum idle connections (default: 10)
|
||||
DB_MAX_OPEN_CONNS - Maximum open connections (default: 100)
|
||||
DB_CONN_MAX_LIFETIME - Connection maximum lifetime (default: "1h")
|
||||
DB_CONN_MAX_IDLE_TIME - Connection maximum idle time (default: "30m")
|
||||
|
||||
Logging Settings:
|
||||
DB_DEBUG - Enable debug logging (default: false)
|
||||
DB_LOG_LEVEL - Log level: silent, error, warn, info (default: silent)
|
||||
DB_SLOW_QUERY_LOG - Slow query threshold (default: "200ms")
|
||||
|
||||
Performance Settings:
|
||||
DB_PREPARE_STMT - Use prepared statements (default: true)
|
||||
DB_CREATE_BATCH_SIZE - Batch size for migrations (default: 1000)
|
||||
DB_CREATE_IN_BATCHES - Batch size for bulk operations (default: 100)
|
||||
DB_QUERY_FIELDS - Select only required fields (default: false)
|
||||
|
||||
Development Settings:
|
||||
ENV - Environment: development, production, test
|
||||
DB_DRY_RUN - Enable dry run mode (default: false)
|
||||
DB_DISABLE_FOREIGN_KEY_CHECK - Disable FK checks (default: false)
|
||||
DB_IGNORE_RELATIONSHIPS_WHEN_MIGRATING - Ignore relationships in migration (default: false)
|
||||
|
||||
Examples:
|
||||
export DB_DEBUG=true
|
||||
export DB_MAX_OPEN_CONNS=200
|
||||
export DB_CONN_MAX_LIFETIME=2h
|
||||
export DB_LOG_LEVEL=info
|
||||
export ENV=development
|
||||
*/
|
||||
@@ -0,0 +1,894 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"tankstopp/internal/models"
|
||||
"time"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
type DB struct {
|
||||
conn *gorm.DB
|
||||
}
|
||||
|
||||
// NewDB creates a new database connection using GORM with configuration
|
||||
func NewDB(config *Config) (*DB, error) {
|
||||
// Validate configuration
|
||||
if err := config.Validate(); err != nil {
|
||||
return nil, fmt.Errorf("invalid configuration: %w", err)
|
||||
}
|
||||
|
||||
// Configure GORM
|
||||
gormConfig := &gorm.Config{
|
||||
Logger: logger.Default.LogMode(config.LogLevel),
|
||||
PrepareStmt: config.PrepareStmt,
|
||||
DisableForeignKeyConstraintWhenMigrating: config.DisableForeignKeyCheck,
|
||||
IgnoreRelationshipsWhenMigrating: config.IgnoreRelationshipsWhenMigrating,
|
||||
QueryFields: config.QueryFields,
|
||||
CreateBatchSize: config.CreateBatchSize,
|
||||
DryRun: config.DryRun,
|
||||
}
|
||||
|
||||
// Configure slow query logging
|
||||
if config.SlowQueryLog > 0 {
|
||||
env := os.Getenv("ENV")
|
||||
isDev := env == "development" || env == "dev" || env == ""
|
||||
|
||||
customLogger := logger.New(
|
||||
log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer
|
||||
logger.Config{
|
||||
SlowThreshold: config.SlowQueryLog,
|
||||
LogLevel: config.LogLevel,
|
||||
IgnoreRecordNotFoundError: true,
|
||||
Colorful: isDev,
|
||||
},
|
||||
)
|
||||
gormConfig.Logger = customLogger
|
||||
}
|
||||
|
||||
conn, err := gorm.Open(sqlite.Open(config.DatabasePath), gormConfig)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to open database: %w", err)
|
||||
}
|
||||
|
||||
// Get underlying SQL DB to configure connection pool
|
||||
sqlDB, err := conn.DB()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get underlying sql.DB: %w", err)
|
||||
}
|
||||
|
||||
// Set connection pool settings from configuration
|
||||
sqlDB.SetMaxIdleConns(config.MaxIdleConns)
|
||||
sqlDB.SetMaxOpenConns(config.MaxOpenConns)
|
||||
sqlDB.SetConnMaxLifetime(config.ConnMaxLifetime)
|
||||
sqlDB.SetConnMaxIdleTime(config.ConnMaxIdleTime)
|
||||
|
||||
db := &DB{conn: conn}
|
||||
|
||||
// Run migrations if enabled
|
||||
if config.AutoMigrate {
|
||||
if err := db.migrate(); err != nil {
|
||||
return nil, fmt.Errorf("failed to migrate database: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return db, nil
|
||||
}
|
||||
|
||||
// NewDBWithDefaults creates a new database connection with default configuration
|
||||
func NewDBWithDefaults(databasePath string) (*DB, error) {
|
||||
config := DefaultConfig()
|
||||
config.DatabasePath = databasePath
|
||||
return NewDB(config)
|
||||
}
|
||||
|
||||
// NewDBFromEnv creates a new database connection using environment variables
|
||||
func NewDBFromEnv() (*DB, error) {
|
||||
config := LoadFromEnv()
|
||||
return NewDB(config)
|
||||
}
|
||||
|
||||
// NewDBFromConfig creates a new database connection using configuration file
|
||||
func NewDBFromConfig(configPath string) (*DB, error) {
|
||||
config := LoadFromConfig(configPath)
|
||||
return NewDB(config)
|
||||
}
|
||||
|
||||
// Close closes the database connection
|
||||
func (db *DB) Close() error {
|
||||
sqlDB, err := db.conn.DB()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return sqlDB.Close()
|
||||
}
|
||||
|
||||
// migrate runs database migrations
|
||||
func (db *DB) migrate() error {
|
||||
// Auto-migrate the schema
|
||||
return db.conn.AutoMigrate(&models.User{}, &models.Vehicle{}, &models.FuelStop{})
|
||||
}
|
||||
|
||||
// CreateFuelStop inserts a new fuel stop into the database
|
||||
func (db *DB) CreateFuelStop(stop *models.FuelStop) error {
|
||||
// Set timestamps
|
||||
now := time.Now()
|
||||
stop.CreatedAt = now
|
||||
stop.UpdatedAt = now
|
||||
|
||||
result := db.conn.Create(stop)
|
||||
if result.Error != nil {
|
||||
return fmt.Errorf("failed to create fuel stop: %w", result.Error)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetFuelStops retrieves all fuel stops for a specific user from the database
|
||||
func (db *DB) GetFuelStops(userID uint) ([]models.FuelStop, error) {
|
||||
var stops []models.FuelStop
|
||||
|
||||
result := db.conn.Preload("Vehicle").Where("user_id = ?", userID).
|
||||
Order("date DESC").
|
||||
Find(&stops)
|
||||
|
||||
if result.Error != nil {
|
||||
return nil, fmt.Errorf("failed to get fuel stops: %w", result.Error)
|
||||
}
|
||||
|
||||
return stops, nil
|
||||
}
|
||||
|
||||
// GetFuelStopsByVehicle retrieves all fuel stops for a specific vehicle
|
||||
func (db *DB) GetFuelStopsByVehicle(vehicleID, userID uint) ([]models.FuelStop, error) {
|
||||
var stops []models.FuelStop
|
||||
|
||||
result := db.conn.Where("vehicle_id = ? AND user_id = ?", vehicleID, userID).
|
||||
Order("date DESC").
|
||||
Find(&stops)
|
||||
|
||||
if result.Error != nil {
|
||||
return nil, fmt.Errorf("failed to get fuel stops by vehicle: %w", result.Error)
|
||||
}
|
||||
|
||||
return stops, nil
|
||||
}
|
||||
|
||||
// GetFuelStopByID retrieves a fuel stop by its ID and user ID
|
||||
func (db *DB) GetFuelStopByID(id, userID uint) (*models.FuelStop, error) {
|
||||
var stop models.FuelStop
|
||||
|
||||
result := db.conn.Where("id = ? AND user_id = ?", id, userID).First(&stop)
|
||||
if result.Error != nil {
|
||||
if result.Error == gorm.ErrRecordNotFound {
|
||||
return nil, nil // Return nil when record not found
|
||||
}
|
||||
return nil, fmt.Errorf("failed to get fuel stop: %w", result.Error)
|
||||
}
|
||||
|
||||
return &stop, nil
|
||||
}
|
||||
|
||||
// UpdateFuelStop updates an existing fuel stop
|
||||
func (db *DB) UpdateFuelStop(stop *models.FuelStop) error {
|
||||
// Update timestamp
|
||||
stop.UpdatedAt = time.Now()
|
||||
|
||||
result := db.conn.Model(stop).
|
||||
Where("id = ? AND user_id = ?", stop.ID, stop.UserID).
|
||||
Updates(stop)
|
||||
|
||||
if result.Error != nil {
|
||||
return fmt.Errorf("failed to update fuel stop: %w", result.Error)
|
||||
}
|
||||
|
||||
if result.RowsAffected == 0 {
|
||||
return fmt.Errorf("fuel stop not found or access denied")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteFuelStop deletes a fuel stop by its ID and user ID
|
||||
func (db *DB) DeleteFuelStop(id, userID uint) error {
|
||||
result := db.conn.Where("id = ? AND user_id = ?", id, userID).Delete(&models.FuelStop{})
|
||||
if result.Error != nil {
|
||||
return fmt.Errorf("failed to delete fuel stop: %w", result.Error)
|
||||
}
|
||||
|
||||
if result.RowsAffected == 0 {
|
||||
return fmt.Errorf("fuel stop not found or access denied")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CreateVehicle creates a new vehicle for a user
|
||||
func (db *DB) CreateVehicle(vehicle *models.Vehicle) error {
|
||||
// Set timestamps
|
||||
now := time.Now()
|
||||
vehicle.CreatedAt = now
|
||||
vehicle.UpdatedAt = now
|
||||
|
||||
result := db.conn.Create(vehicle)
|
||||
if result.Error != nil {
|
||||
return fmt.Errorf("failed to create vehicle: %w", result.Error)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetVehicles retrieves all vehicles for a specific user
|
||||
func (db *DB) GetVehicles(userID uint) ([]models.Vehicle, error) {
|
||||
var vehicles []models.Vehicle
|
||||
|
||||
result := db.conn.Where("user_id = ?", userID).
|
||||
Order("is_active DESC, name ASC").
|
||||
Find(&vehicles)
|
||||
|
||||
if result.Error != nil {
|
||||
return nil, fmt.Errorf("failed to get vehicles: %w", result.Error)
|
||||
}
|
||||
|
||||
return vehicles, nil
|
||||
}
|
||||
|
||||
// GetActiveVehicles retrieves only active vehicles for a specific user
|
||||
func (db *DB) GetActiveVehicles(userID uint) ([]models.Vehicle, error) {
|
||||
var vehicles []models.Vehicle
|
||||
|
||||
result := db.conn.Where("user_id = ? AND is_active = ?", userID, true).
|
||||
Order("name ASC").
|
||||
Find(&vehicles)
|
||||
|
||||
if result.Error != nil {
|
||||
return nil, fmt.Errorf("failed to get active vehicles: %w", result.Error)
|
||||
}
|
||||
|
||||
return vehicles, nil
|
||||
}
|
||||
|
||||
// GetVehicleByID retrieves a vehicle by its ID and user ID
|
||||
func (db *DB) GetVehicleByID(id, userID uint) (*models.Vehicle, error) {
|
||||
var vehicle models.Vehicle
|
||||
|
||||
result := db.conn.Where("id = ? AND user_id = ?", id, userID).First(&vehicle)
|
||||
if result.Error != nil {
|
||||
if result.Error == gorm.ErrRecordNotFound {
|
||||
return nil, nil // Return nil when vehicle not found
|
||||
}
|
||||
return nil, fmt.Errorf("failed to get vehicle: %w", result.Error)
|
||||
}
|
||||
|
||||
return &vehicle, nil
|
||||
}
|
||||
|
||||
// UpdateVehicle updates an existing vehicle
|
||||
func (db *DB) UpdateVehicle(vehicle *models.Vehicle) error {
|
||||
// Update timestamp
|
||||
vehicle.UpdatedAt = time.Now()
|
||||
|
||||
result := db.conn.Model(vehicle).
|
||||
Where("id = ? AND user_id = ?", vehicle.ID, vehicle.UserID).
|
||||
Updates(vehicle)
|
||||
|
||||
if result.Error != nil {
|
||||
return fmt.Errorf("failed to update vehicle: %w", result.Error)
|
||||
}
|
||||
|
||||
if result.RowsAffected == 0 {
|
||||
return fmt.Errorf("vehicle not found or access denied")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteVehicle deletes a vehicle by its ID and user ID
|
||||
func (db *DB) DeleteVehicle(id, userID uint) error {
|
||||
// Check if vehicle has fuel stops
|
||||
var count int64
|
||||
if err := db.conn.Model(&models.FuelStop{}).Where("vehicle_id = ?", id).Count(&count).Error; err != nil {
|
||||
return fmt.Errorf("failed to check fuel stops: %w", err)
|
||||
}
|
||||
|
||||
if count > 0 {
|
||||
return fmt.Errorf("cannot delete vehicle with existing fuel stops")
|
||||
}
|
||||
|
||||
result := db.conn.Where("id = ? AND user_id = ?", id, userID).Delete(&models.Vehicle{})
|
||||
if result.Error != nil {
|
||||
return fmt.Errorf("failed to delete vehicle: %w", result.Error)
|
||||
}
|
||||
|
||||
if result.RowsAffected == 0 {
|
||||
return fmt.Errorf("vehicle not found or access denied")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetVehicleWithFuelStops retrieves a vehicle with its fuel stops
|
||||
func (db *DB) GetVehicleWithFuelStops(vehicleID, userID uint) (*models.Vehicle, error) {
|
||||
var vehicle models.Vehicle
|
||||
|
||||
result := db.conn.Preload("FuelStops", func(db *gorm.DB) *gorm.DB {
|
||||
return db.Order("date DESC")
|
||||
}).Where("id = ? AND user_id = ?", vehicleID, userID).First(&vehicle)
|
||||
|
||||
if result.Error != nil {
|
||||
if result.Error == gorm.ErrRecordNotFound {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, fmt.Errorf("failed to get vehicle with fuel stops: %w", result.Error)
|
||||
}
|
||||
|
||||
return &vehicle, nil
|
||||
}
|
||||
|
||||
// GetVehicleCount returns the total number of vehicles for a user
|
||||
func (db *DB) GetVehicleCount(userID uint) (int64, error) {
|
||||
var count int64
|
||||
result := db.conn.Model(&models.Vehicle{}).Where("user_id = ?", userID).Count(&count)
|
||||
if result.Error != nil {
|
||||
return 0, fmt.Errorf("failed to count vehicles: %w", result.Error)
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// ValidateVehicle validates vehicle data before creation/update
|
||||
func (db *DB) ValidateVehicle(vehicle *models.Vehicle) error {
|
||||
if vehicle.UserID == 0 {
|
||||
return fmt.Errorf("user ID is required")
|
||||
}
|
||||
|
||||
// Check if user exists
|
||||
exists, err := db.UserExists(vehicle.UserID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to validate user: %w", err)
|
||||
}
|
||||
if !exists {
|
||||
return fmt.Errorf("user does not exist")
|
||||
}
|
||||
|
||||
if vehicle.Name == "" {
|
||||
return fmt.Errorf("vehicle name is required")
|
||||
}
|
||||
|
||||
if len(vehicle.Name) > 100 {
|
||||
return fmt.Errorf("vehicle name must be 100 characters or less")
|
||||
}
|
||||
|
||||
if vehicle.Make != "" && len(vehicle.Make) > 50 {
|
||||
return fmt.Errorf("vehicle make must be 50 characters or less")
|
||||
}
|
||||
|
||||
if vehicle.Model != "" && len(vehicle.Model) > 50 {
|
||||
return fmt.Errorf("vehicle model must be 50 characters or less")
|
||||
}
|
||||
|
||||
if vehicle.LicensePlate != "" && len(vehicle.LicensePlate) > 20 {
|
||||
return fmt.Errorf("license plate must be 20 characters or less")
|
||||
}
|
||||
|
||||
if vehicle.FuelType != "" && len(vehicle.FuelType) > 50 {
|
||||
return fmt.Errorf("fuel type must be 50 characters or less")
|
||||
}
|
||||
|
||||
if vehicle.Year < 0 || vehicle.Year > time.Now().Year()+1 {
|
||||
return fmt.Errorf("invalid vehicle year")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CreateVehicleWithValidation creates a vehicle with validation
|
||||
func (db *DB) CreateVehicleWithValidation(vehicle *models.Vehicle) error {
|
||||
if err := db.ValidateVehicle(vehicle); err != nil {
|
||||
return fmt.Errorf("validation failed: %w", err)
|
||||
}
|
||||
|
||||
return db.CreateVehicle(vehicle)
|
||||
}
|
||||
|
||||
// GetFuelStopStats calculates statistics for fuel consumption for a specific user
|
||||
func (db *DB) GetFuelStopStats(userID uint) (*models.FuelStopStats, error) {
|
||||
stats := &models.FuelStopStats{}
|
||||
|
||||
// Get basic statistics
|
||||
var result struct {
|
||||
TotalStops int64 `json:"total_stops"`
|
||||
TotalLiters float64 `json:"total_liters"`
|
||||
TotalSpent float64 `json:"total_spent"`
|
||||
AveragePrice float64 `json:"average_price"`
|
||||
TotalTripKm float64 `json:"total_trip_km"`
|
||||
MinOdometer int `json:"min_odometer"`
|
||||
MaxOdometer int `json:"max_odometer"`
|
||||
}
|
||||
|
||||
err := db.conn.Model(&models.FuelStop{}).
|
||||
Select(`
|
||||
COUNT(*) as total_stops,
|
||||
COALESCE(SUM(liters), 0) as total_liters,
|
||||
COALESCE(SUM(total_price), 0) as total_spent,
|
||||
COALESCE(AVG(price_per_l), 0) as average_price,
|
||||
COALESCE(SUM(trip_length), 0) as total_trip_km,
|
||||
COALESCE(MIN(odometer), 0) as min_odometer,
|
||||
COALESCE(MAX(odometer), 0) as max_odometer
|
||||
`).
|
||||
Where("user_id = ?", userID).
|
||||
Scan(&result).Error
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get fuel stop stats: %w", err)
|
||||
}
|
||||
|
||||
stats.TotalStops = int(result.TotalStops)
|
||||
stats.TotalLiters = result.TotalLiters
|
||||
stats.TotalSpent = result.TotalSpent
|
||||
stats.AveragePrice = result.AveragePrice
|
||||
|
||||
// Get the last fillup
|
||||
var lastStop models.FuelStop
|
||||
err = db.conn.Where("user_id = ?", userID).
|
||||
Order("date DESC").
|
||||
First(&lastStop).Error
|
||||
|
||||
if err != nil && err != gorm.ErrRecordNotFound {
|
||||
return nil, fmt.Errorf("failed to get last fillup: %w", err)
|
||||
}
|
||||
|
||||
if err != gorm.ErrRecordNotFound {
|
||||
stats.LastFillup = &lastStop
|
||||
}
|
||||
|
||||
// Calculate average consumption using trip length (preferred) or odometer difference (fallback)
|
||||
if stats.TotalStops > 1 {
|
||||
// Primary method: Use trip length if available
|
||||
if result.TotalTripKm > 0 {
|
||||
stats.AverageConsumption = (stats.TotalLiters / result.TotalTripKm) * 100
|
||||
} else if result.MaxOdometer > result.MinOdometer {
|
||||
// Fallback method: Use odometer difference
|
||||
distanceDriven := result.MaxOdometer - result.MinOdometer
|
||||
if distanceDriven > 0 {
|
||||
stats.AverageConsumption = (stats.TotalLiters / float64(distanceDriven)) * 100
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return stats, nil
|
||||
|
||||
}
|
||||
|
||||
// CreateUser creates a new user in the database
|
||||
func (db *DB) CreateUser(user *models.User) error {
|
||||
// Hash the password
|
||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(user.Password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to hash password: %w", err)
|
||||
}
|
||||
|
||||
user.PasswordHash = string(hashedPassword)
|
||||
user.Password = "" // Clear the plain text password
|
||||
|
||||
// Set timestamps
|
||||
now := time.Now()
|
||||
user.CreatedAt = now
|
||||
user.UpdatedAt = now
|
||||
|
||||
result := db.conn.Create(user)
|
||||
if result.Error != nil {
|
||||
return fmt.Errorf("failed to create user: %w", result.Error)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetUserByUsername retrieves a user by username
|
||||
func (db *DB) GetUserByUsername(username string) (*models.User, error) {
|
||||
var user models.User
|
||||
|
||||
result := db.conn.Where("username = ?", username).First(&user)
|
||||
if result.Error != nil {
|
||||
if result.Error == gorm.ErrRecordNotFound {
|
||||
return nil, nil // Return nil when user not found
|
||||
}
|
||||
return nil, fmt.Errorf("failed to get user by username: %w", result.Error)
|
||||
}
|
||||
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
// GetUserByID retrieves a user by ID
|
||||
func (db *DB) GetUserByID(id uint) (*models.User, error) {
|
||||
var user models.User
|
||||
|
||||
result := db.conn.First(&user, id)
|
||||
if result.Error != nil {
|
||||
if result.Error == gorm.ErrRecordNotFound {
|
||||
return nil, nil // Return nil when user not found
|
||||
}
|
||||
return nil, fmt.Errorf("failed to get user by ID: %w", result.Error)
|
||||
}
|
||||
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
// UpdateUser updates an existing user
|
||||
func (db *DB) UpdateUser(user *models.User) error {
|
||||
// Update timestamp
|
||||
user.UpdatedAt = time.Now()
|
||||
|
||||
result := db.conn.Model(user).
|
||||
Select("email", "base_currency", "updated_at").
|
||||
Updates(user)
|
||||
|
||||
if result.Error != nil {
|
||||
return fmt.Errorf("failed to update user: %w", result.Error)
|
||||
}
|
||||
|
||||
if result.RowsAffected == 0 {
|
||||
return fmt.Errorf("user not found")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateUserPassword updates a user's password
|
||||
func (db *DB) UpdateUserPassword(user *models.User, newPassword string) error {
|
||||
// Hash the new password
|
||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(newPassword), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to hash password: %w", err)
|
||||
}
|
||||
|
||||
// Update timestamp
|
||||
now := time.Now()
|
||||
|
||||
// Update only the password hash and updated_at fields
|
||||
result := db.conn.Model(user).
|
||||
Updates(map[string]interface{}{
|
||||
"password_hash": string(hashedPassword),
|
||||
"updated_at": now,
|
||||
})
|
||||
|
||||
if result.Error != nil {
|
||||
return fmt.Errorf("failed to update password: %w", result.Error)
|
||||
}
|
||||
|
||||
if result.RowsAffected == 0 {
|
||||
return fmt.Errorf("user not found")
|
||||
}
|
||||
|
||||
// Update the user object with new password hash
|
||||
user.PasswordHash = string(hashedPassword)
|
||||
user.UpdatedAt = now
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetUserByEmail retrieves a user by email
|
||||
func (db *DB) GetUserByEmail(email string) (*models.User, error) {
|
||||
var user models.User
|
||||
|
||||
result := db.conn.Where("email = ?", email).First(&user)
|
||||
if result.Error != nil {
|
||||
if result.Error == gorm.ErrRecordNotFound {
|
||||
return nil, nil // Return nil when user not found
|
||||
}
|
||||
return nil, fmt.Errorf("failed to get user by email: %w", result.Error)
|
||||
}
|
||||
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
// GetUserWithFuelStops retrieves a user with their fuel stops
|
||||
func (db *DB) GetUserWithFuelStops(userID uint) (*models.User, error) {
|
||||
var user models.User
|
||||
|
||||
result := db.conn.Preload("FuelStops", func(db *gorm.DB) *gorm.DB {
|
||||
return db.Order("date DESC")
|
||||
}).Preload("Vehicles").First(&user, userID)
|
||||
|
||||
if result.Error != nil {
|
||||
if result.Error == gorm.ErrRecordNotFound {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, fmt.Errorf("failed to get user with fuel stops: %w", result.Error)
|
||||
}
|
||||
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
// GetFuelStopsWithPagination retrieves fuel stops with pagination
|
||||
func (db *DB) GetFuelStopsWithPagination(userID uint, limit, offset int) ([]models.FuelStop, int64, error) {
|
||||
var stops []models.FuelStop
|
||||
var total int64
|
||||
|
||||
// Get total count
|
||||
err := db.conn.Model(&models.FuelStop{}).
|
||||
Where("user_id = ?", userID).
|
||||
Count(&total).Error
|
||||
|
||||
if err != nil {
|
||||
return nil, 0, fmt.Errorf("failed to count fuel stops: %w", err)
|
||||
}
|
||||
|
||||
// Get paginated results
|
||||
err = db.conn.Where("user_id = ?", userID).
|
||||
Order("date DESC").
|
||||
Limit(limit).
|
||||
Offset(offset).
|
||||
Find(&stops).Error
|
||||
|
||||
if err != nil {
|
||||
return nil, 0, fmt.Errorf("failed to get fuel stops with pagination: %w", err)
|
||||
}
|
||||
|
||||
return stops, total, nil
|
||||
}
|
||||
|
||||
// GetFuelStopsByDateRange retrieves fuel stops within a date range
|
||||
func (db *DB) GetFuelStopsByDateRange(userID uint, startDate, endDate time.Time) ([]models.FuelStop, error) {
|
||||
var stops []models.FuelStop
|
||||
|
||||
result := db.conn.Where("user_id = ? AND date BETWEEN ? AND ?", userID, startDate, endDate).
|
||||
Order("date DESC").
|
||||
Find(&stops)
|
||||
|
||||
if result.Error != nil {
|
||||
return nil, fmt.Errorf("failed to get fuel stops by date range: %w", result.Error)
|
||||
}
|
||||
|
||||
return stops, nil
|
||||
}
|
||||
|
||||
// GetFuelStopsByFuelType retrieves fuel stops by fuel type
|
||||
func (db *DB) GetFuelStopsByFuelType(userID uint, fuelType string) ([]models.FuelStop, error) {
|
||||
var stops []models.FuelStop
|
||||
|
||||
result := db.conn.Where("user_id = ? AND fuel_type = ?", userID, fuelType).
|
||||
Order("date DESC").
|
||||
Find(&stops)
|
||||
|
||||
if result.Error != nil {
|
||||
return nil, fmt.Errorf("failed to get fuel stops by fuel type: %w", result.Error)
|
||||
}
|
||||
|
||||
return stops, nil
|
||||
}
|
||||
|
||||
// GetMonthlyStats retrieves monthly statistics for a user
|
||||
func (db *DB) GetMonthlyStats(userID uint, year int) ([]models.MonthlyStats, error) {
|
||||
var stats []models.MonthlyStats
|
||||
|
||||
query := `
|
||||
SELECT
|
||||
strftime('%m', date) as month,
|
||||
strftime('%Y', date) as year,
|
||||
COUNT(*) as total_stops,
|
||||
SUM(liters) as total_liters,
|
||||
SUM(total_price) as total_spent,
|
||||
AVG(price_per_l) as average_price
|
||||
FROM fuel_stops
|
||||
WHERE user_id = ? AND strftime('%Y', date) = ?
|
||||
GROUP BY strftime('%Y-%m', date)
|
||||
ORDER BY month
|
||||
`
|
||||
|
||||
err := db.conn.Raw(query, userID, fmt.Sprintf("%d", year)).Scan(&stats).Error
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get monthly stats: %w", err)
|
||||
}
|
||||
|
||||
return stats, nil
|
||||
}
|
||||
|
||||
// BulkCreateFuelStops creates multiple fuel stops in a single transaction
|
||||
func (db *DB) BulkCreateFuelStops(stops []models.FuelStop) error {
|
||||
if len(stops) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Set timestamps for all stops
|
||||
now := time.Now()
|
||||
for i := range stops {
|
||||
stops[i].CreatedAt = now
|
||||
stops[i].UpdatedAt = now
|
||||
}
|
||||
|
||||
// Use transaction for bulk insert
|
||||
return db.conn.Transaction(func(tx *gorm.DB) error {
|
||||
return tx.CreateInBatches(stops, 100).Error
|
||||
})
|
||||
}
|
||||
|
||||
// DeleteAllUserData deletes all data for a user (for account deletion)
|
||||
func (db *DB) DeleteAllUserData(userID uint) error {
|
||||
return db.conn.Transaction(func(tx *gorm.DB) error {
|
||||
// Delete all fuel stops first (due to foreign key constraint)
|
||||
if err := tx.Where("user_id = ?", userID).Delete(&models.FuelStop{}).Error; err != nil {
|
||||
return fmt.Errorf("failed to delete fuel stops: %w", err)
|
||||
}
|
||||
|
||||
// Delete the user
|
||||
if err := tx.Delete(&models.User{}, userID).Error; err != nil {
|
||||
return fmt.Errorf("failed to delete user: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// HealthCheck performs a simple health check on the database
|
||||
func (db *DB) HealthCheck() error {
|
||||
sqlDB, err := db.conn.DB()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get underlying sql.DB: %w", err)
|
||||
}
|
||||
|
||||
return sqlDB.Ping()
|
||||
}
|
||||
|
||||
// GetFuelStopCount returns the total number of fuel stops for a user
|
||||
func (db *DB) GetFuelStopCount(userID uint) (int64, error) {
|
||||
var count int64
|
||||
result := db.conn.Model(&models.FuelStop{}).Where("user_id = ?", userID).Count(&count)
|
||||
if result.Error != nil {
|
||||
return 0, fmt.Errorf("failed to count fuel stops: %w", result.Error)
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// GetLatestFuelStop returns the most recent fuel stop for a user
|
||||
func (db *DB) GetLatestFuelStop(userID uint) (*models.FuelStop, error) {
|
||||
var stop models.FuelStop
|
||||
result := db.conn.Where("user_id = ?", userID).Order("date DESC").First(&stop)
|
||||
if result.Error != nil {
|
||||
if result.Error == gorm.ErrRecordNotFound {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, fmt.Errorf("failed to get latest fuel stop: %w", result.Error)
|
||||
}
|
||||
return &stop, nil
|
||||
}
|
||||
|
||||
// UserExists checks if a user exists by ID
|
||||
func (db *DB) UserExists(userID uint) (bool, error) {
|
||||
var count int64
|
||||
result := db.conn.Model(&models.User{}).Where("id = ?", userID).Count(&count)
|
||||
if result.Error != nil {
|
||||
return false, fmt.Errorf("failed to check user existence: %w", result.Error)
|
||||
}
|
||||
return count > 0, nil
|
||||
}
|
||||
|
||||
// ValidateFuelStop validates fuel stop data before creation/update
|
||||
func (db *DB) ValidateFuelStop(stop *models.FuelStop) error {
|
||||
if stop.UserID == 0 {
|
||||
return fmt.Errorf("user ID is required")
|
||||
}
|
||||
|
||||
// Check if user exists
|
||||
exists, err := db.UserExists(stop.UserID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to validate user: %w", err)
|
||||
}
|
||||
if !exists {
|
||||
return fmt.Errorf("user does not exist")
|
||||
}
|
||||
|
||||
if stop.Date.IsZero() {
|
||||
return fmt.Errorf("date is required")
|
||||
}
|
||||
|
||||
if stop.StationName == "" {
|
||||
return fmt.Errorf("station name is required")
|
||||
}
|
||||
|
||||
if stop.Location == "" {
|
||||
return fmt.Errorf("location is required")
|
||||
}
|
||||
|
||||
if stop.FuelType == "" {
|
||||
return fmt.Errorf("fuel type is required")
|
||||
}
|
||||
|
||||
if stop.Liters <= 0 {
|
||||
return fmt.Errorf("liters must be greater than 0")
|
||||
}
|
||||
|
||||
if stop.PricePerL <= 0 {
|
||||
return fmt.Errorf("price per liter must be greater than 0")
|
||||
}
|
||||
|
||||
if stop.TotalPrice <= 0 {
|
||||
return fmt.Errorf("total price must be greater than 0")
|
||||
}
|
||||
|
||||
if stop.Currency == "" {
|
||||
stop.Currency = "EUR" // Set default currency
|
||||
}
|
||||
|
||||
if stop.TripLength < 0 {
|
||||
return fmt.Errorf("trip length cannot be negative")
|
||||
}
|
||||
|
||||
if stop.VehicleID == 0 {
|
||||
return fmt.Errorf("vehicle is required")
|
||||
}
|
||||
|
||||
// Check if vehicle exists and belongs to user
|
||||
vehicle, err := db.GetVehicleByID(stop.VehicleID, stop.UserID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to validate vehicle: %w", err)
|
||||
}
|
||||
if vehicle == nil {
|
||||
return fmt.Errorf("vehicle does not exist or access denied")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CreateFuelStopWithValidation creates a fuel stop with validation
|
||||
func (db *DB) CreateFuelStopWithValidation(stop *models.FuelStop) error {
|
||||
if err := db.ValidateFuelStop(stop); err != nil {
|
||||
return fmt.Errorf("validation failed: %w", err)
|
||||
}
|
||||
|
||||
return db.CreateFuelStop(stop)
|
||||
}
|
||||
|
||||
// GetFuelStopsWithUser retrieves fuel stops with user information preloaded
|
||||
func (db *DB) GetFuelStopsWithUser(userID uint) ([]models.FuelStop, error) {
|
||||
var stops []models.FuelStop
|
||||
|
||||
result := db.conn.Preload("User").Preload("Vehicle").
|
||||
Where("user_id = ?", userID).
|
||||
Order("date DESC").
|
||||
Find(&stops)
|
||||
|
||||
if result.Error != nil {
|
||||
return nil, fmt.Errorf("failed to get fuel stops with user: %w", result.Error)
|
||||
}
|
||||
|
||||
return stops, nil
|
||||
}
|
||||
|
||||
// SearchFuelStops performs a text search across station names and locations
|
||||
func (db *DB) SearchFuelStops(userID uint, searchTerm string) ([]models.FuelStop, error) {
|
||||
var stops []models.FuelStop
|
||||
|
||||
searchPattern := "%" + searchTerm + "%"
|
||||
result := db.conn.Where("user_id = ? AND (station_name LIKE ? OR location LIKE ?)",
|
||||
userID, searchPattern, searchPattern).
|
||||
Order("date DESC").
|
||||
Find(&stops)
|
||||
|
||||
if result.Error != nil {
|
||||
return nil, fmt.Errorf("failed to search fuel stops: %w", result.Error)
|
||||
}
|
||||
|
||||
return stops, nil
|
||||
}
|
||||
|
||||
// GetRecentFuelStops returns the most recent N fuel stops for a user
|
||||
func (db *DB) GetRecentFuelStops(userID uint, limit int) ([]models.FuelStop, error) {
|
||||
var stops []models.FuelStop
|
||||
|
||||
result := db.conn.Where("user_id = ?", userID).
|
||||
Order("date DESC").
|
||||
Limit(limit).
|
||||
Find(&stops)
|
||||
|
||||
if result.Error != nil {
|
||||
return nil, fmt.Errorf("failed to get recent fuel stops: %w", result.Error)
|
||||
}
|
||||
|
||||
return stops, nil
|
||||
}
|
||||
@@ -0,0 +1,403 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"tankstopp/internal/models"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
// APIGetFuelStopsHandler returns fuel stops as JSON
|
||||
func (h *Handler) APIGetFuelStopsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
userID, _ := h.getCurrentUser(r)
|
||||
if userID == 0 {
|
||||
h.writeJSONError(w, "Unauthorized", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
// Parse query parameters for filtering (not used in simplified implementation)
|
||||
_ = r.URL.Query().Get("vehicle_id")
|
||||
_ = r.URL.Query().Get("fuel_type")
|
||||
_ = r.URL.Query().Get("date_from")
|
||||
_ = r.URL.Query().Get("date_to")
|
||||
limitStr := r.URL.Query().Get("limit")
|
||||
offsetStr := r.URL.Query().Get("offset")
|
||||
|
||||
// Parse limit and offset
|
||||
limit := 50 // default limit
|
||||
offset := 0 // default offset
|
||||
|
||||
if limitStr != "" {
|
||||
if l, err := strconv.Atoi(limitStr); err == nil && l > 0 && l <= 1000 {
|
||||
limit = l
|
||||
}
|
||||
}
|
||||
|
||||
if offsetStr != "" {
|
||||
if o, err := strconv.Atoi(offsetStr); err == nil && o >= 0 {
|
||||
offset = o
|
||||
}
|
||||
}
|
||||
|
||||
// Get fuel stops (simplified - using existing method)
|
||||
stops, err := h.db.GetFuelStops(userID)
|
||||
if err != nil {
|
||||
log.Printf("Error getting fuel stops: %v", err)
|
||||
h.writeJSONError(w, "Failed to retrieve fuel stops", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Apply basic pagination
|
||||
totalCount := len(stops)
|
||||
end := offset + limit
|
||||
if end > len(stops) {
|
||||
end = len(stops)
|
||||
}
|
||||
if offset < len(stops) {
|
||||
stops = stops[offset:end]
|
||||
} else {
|
||||
stops = []models.FuelStop{}
|
||||
}
|
||||
|
||||
// Prepare response
|
||||
response := struct {
|
||||
Data []models.FuelStop `json:"data"`
|
||||
Total int `json:"total"`
|
||||
Limit int `json:"limit"`
|
||||
Offset int `json:"offset"`
|
||||
HasMore bool `json:"has_more"`
|
||||
Pagination struct {
|
||||
CurrentPage int `json:"current_page"`
|
||||
TotalPages int `json:"total_pages"`
|
||||
PerPage int `json:"per_page"`
|
||||
} `json:"pagination"`
|
||||
}{
|
||||
Data: stops,
|
||||
Total: totalCount,
|
||||
Limit: limit,
|
||||
Offset: offset,
|
||||
HasMore: offset+len(stops) < totalCount,
|
||||
}
|
||||
|
||||
// Calculate pagination
|
||||
response.Pagination.PerPage = limit
|
||||
response.Pagination.CurrentPage = (offset / limit) + 1
|
||||
response.Pagination.TotalPages = (totalCount + limit - 1) / limit
|
||||
|
||||
h.writeJSONResponse(w, response, http.StatusOK)
|
||||
}
|
||||
|
||||
// APICreateFuelStopHandler creates a new fuel stop via JSON API
|
||||
func (h *Handler) APICreateFuelStopHandler(w http.ResponseWriter, r *http.Request) {
|
||||
userID, _ := h.getCurrentUser(r)
|
||||
if userID == 0 {
|
||||
h.writeJSONError(w, "Unauthorized", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
// Parse JSON request body
|
||||
var request struct {
|
||||
VehicleID uint `json:"vehicle_id"`
|
||||
Date string `json:"date"`
|
||||
StationName string `json:"station_name"`
|
||||
Location string `json:"location"`
|
||||
FuelType string `json:"fuel_type"`
|
||||
Liters float64 `json:"liters"`
|
||||
PricePerL float64 `json:"price_per_l"`
|
||||
TotalPrice float64 `json:"total_price"`
|
||||
Currency string `json:"currency"`
|
||||
Odometer int `json:"odometer"`
|
||||
TripLength float64 `json:"trip_length"`
|
||||
Notes string `json:"notes"`
|
||||
}
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
|
||||
h.writeJSONError(w, "Invalid JSON format", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Validate required fields
|
||||
if err := h.validateAPIFuelStopRequest(&request); err != nil {
|
||||
h.writeJSONError(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Parse date
|
||||
date, err := time.Parse("2006-01-02", request.Date)
|
||||
if err != nil {
|
||||
h.writeJSONError(w, "Invalid date format. Use YYYY-MM-DD", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Get user's default currency if not provided
|
||||
if request.Currency == "" {
|
||||
user, err := h.db.GetUserByID(userID)
|
||||
if err != nil {
|
||||
log.Printf("Error getting user: %v", err)
|
||||
h.writeJSONError(w, "Failed to retrieve user information", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
request.Currency = user.BaseCurrency
|
||||
}
|
||||
|
||||
// Create fuel stop model
|
||||
fuelStop := &models.FuelStop{
|
||||
UserID: userID,
|
||||
VehicleID: request.VehicleID,
|
||||
Date: date,
|
||||
StationName: request.StationName,
|
||||
Location: request.Location,
|
||||
FuelType: request.FuelType,
|
||||
Liters: request.Liters,
|
||||
PricePerL: request.PricePerL,
|
||||
TotalPrice: request.TotalPrice,
|
||||
Currency: request.Currency,
|
||||
Odometer: request.Odometer,
|
||||
TripLength: request.TripLength,
|
||||
Notes: request.Notes,
|
||||
}
|
||||
|
||||
// Use station name as location if location is empty
|
||||
if fuelStop.Location == "" {
|
||||
fuelStop.Location = fuelStop.StationName
|
||||
}
|
||||
|
||||
// Save to database
|
||||
err = h.db.CreateFuelStopWithValidation(fuelStop)
|
||||
if err != nil {
|
||||
log.Printf("Error creating fuel stop: %v", err)
|
||||
h.writeJSONError(w, "Failed to create fuel stop", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Return created fuel stop
|
||||
h.writeJSONResponse(w, map[string]interface{}{
|
||||
"message": "Fuel stop created successfully",
|
||||
"data": fuelStop,
|
||||
}, http.StatusCreated)
|
||||
}
|
||||
|
||||
// APIGetFuelStopStatsHandler returns fuel stop statistics as JSON
|
||||
func (h *Handler) APIGetFuelStopStatsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
userID, _ := h.getCurrentUser(r)
|
||||
if userID == 0 {
|
||||
h.writeJSONError(w, "Unauthorized", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
// Parse query parameters (not used in simplified implementation)
|
||||
_ = r.URL.Query().Get("date_from")
|
||||
_ = r.URL.Query().Get("date_to")
|
||||
groupBy := r.URL.Query().Get("group_by") // month, year, vehicle
|
||||
|
||||
// Get basic statistics
|
||||
stats, err := h.db.GetFuelStopStats(userID)
|
||||
if err != nil {
|
||||
log.Printf("Error getting fuel stop stats: %v", err)
|
||||
h.writeJSONError(w, "Failed to retrieve statistics", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Prepare response structure
|
||||
response := struct {
|
||||
Basic *models.FuelStopStats `json:"basic"`
|
||||
Daily []DailyStats `json:"daily,omitempty"`
|
||||
Monthly []MonthlyStats `json:"monthly,omitempty"`
|
||||
ByVehicle []VehicleStats `json:"by_vehicle,omitempty"`
|
||||
Summary StatsSummary `json:"summary"`
|
||||
}{
|
||||
Basic: stats,
|
||||
}
|
||||
|
||||
// Additional statistics would require more complex database queries
|
||||
// For now, we'll just return basic stats
|
||||
_ = groupBy // Acknowledge the parameter
|
||||
|
||||
// Calculate summary statistics
|
||||
response.Summary = h.calculateStatsSummary(stats)
|
||||
|
||||
h.writeJSONResponse(w, response, http.StatusOK)
|
||||
}
|
||||
|
||||
// Helper structs for statistics
|
||||
type DailyStats struct {
|
||||
Date string `json:"date"`
|
||||
TotalStops int `json:"total_stops"`
|
||||
TotalCost float64 `json:"total_cost"`
|
||||
TotalLiters float64 `json:"total_liters"`
|
||||
}
|
||||
|
||||
type MonthlyStats struct {
|
||||
Month string `json:"month"`
|
||||
Year int `json:"year"`
|
||||
TotalStops int `json:"total_stops"`
|
||||
TotalCost float64 `json:"total_cost"`
|
||||
TotalLiters float64 `json:"total_liters"`
|
||||
AvgPrice float64 `json:"avg_price"`
|
||||
}
|
||||
|
||||
type VehicleStats struct {
|
||||
VehicleID uint `json:"vehicle_id"`
|
||||
VehicleName string `json:"vehicle_name"`
|
||||
TotalStops int `json:"total_stops"`
|
||||
TotalCost float64 `json:"total_cost"`
|
||||
TotalLiters float64 `json:"total_liters"`
|
||||
AvgPrice float64 `json:"avg_price"`
|
||||
LastFillUp string `json:"last_fillup"`
|
||||
}
|
||||
|
||||
type StatsSummary struct {
|
||||
CostPerKm float64 `json:"cost_per_km"`
|
||||
FuelEfficiency float64 `json:"fuel_efficiency"`
|
||||
MonthlyAverage float64 `json:"monthly_average"`
|
||||
WeeklyAverage float64 `json:"weekly_average"`
|
||||
MostUsedStation string `json:"most_used_station"`
|
||||
PreferredFuel string `json:"preferred_fuel"`
|
||||
}
|
||||
|
||||
// validateAPIFuelStopRequest validates the JSON request for creating fuel stops
|
||||
func (h *Handler) validateAPIFuelStopRequest(req *struct {
|
||||
VehicleID uint `json:"vehicle_id"`
|
||||
Date string `json:"date"`
|
||||
StationName string `json:"station_name"`
|
||||
Location string `json:"location"`
|
||||
FuelType string `json:"fuel_type"`
|
||||
Liters float64 `json:"liters"`
|
||||
PricePerL float64 `json:"price_per_l"`
|
||||
TotalPrice float64 `json:"total_price"`
|
||||
Currency string `json:"currency"`
|
||||
Odometer int `json:"odometer"`
|
||||
TripLength float64 `json:"trip_length"`
|
||||
Notes string `json:"notes"`
|
||||
}) error {
|
||||
if req.VehicleID == 0 {
|
||||
return fmt.Errorf("vehicle_id is required")
|
||||
}
|
||||
|
||||
if req.Date == "" {
|
||||
return fmt.Errorf("date is required")
|
||||
}
|
||||
|
||||
if req.StationName == "" && req.Location == "" {
|
||||
return fmt.Errorf("station_name or location is required")
|
||||
}
|
||||
|
||||
if req.FuelType == "" {
|
||||
return fmt.Errorf("fuel_type is required")
|
||||
}
|
||||
|
||||
if req.Liters <= 0 {
|
||||
return fmt.Errorf("liters must be greater than 0")
|
||||
}
|
||||
|
||||
if req.PricePerL <= 0 {
|
||||
return fmt.Errorf("price_per_l must be greater than 0")
|
||||
}
|
||||
|
||||
if req.TotalPrice <= 0 {
|
||||
return fmt.Errorf("total_price must be greater than 0")
|
||||
}
|
||||
|
||||
if req.Odometer < 0 {
|
||||
return fmt.Errorf("odometer cannot be negative")
|
||||
}
|
||||
|
||||
if req.TripLength < 0 {
|
||||
return fmt.Errorf("trip_length cannot be negative")
|
||||
}
|
||||
|
||||
if len(req.Notes) > 500 {
|
||||
return fmt.Errorf("notes cannot be longer than 500 characters")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// APIGetVehicleHandler returns vehicle information as JSON
|
||||
func (h *Handler) APIGetVehicleHandler(w http.ResponseWriter, r *http.Request) {
|
||||
userID, _ := h.getCurrentUser(r)
|
||||
if userID == 0 {
|
||||
h.writeJSONError(w, "Unauthorized", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
// Get vehicle ID from URL path
|
||||
vars := mux.Vars(r)
|
||||
vehicleIDStr := vars["id"]
|
||||
vehicleID, err := strconv.ParseUint(vehicleIDStr, 10, 32)
|
||||
if err != nil {
|
||||
h.writeJSONError(w, "Invalid vehicle ID", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Get vehicle from database
|
||||
vehicle, err := h.db.GetVehicleByID(uint(vehicleID), userID)
|
||||
if err != nil {
|
||||
log.Printf("Error getting vehicle: %v", err)
|
||||
h.writeJSONError(w, "Failed to retrieve vehicle", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if vehicle == nil {
|
||||
h.writeJSONError(w, "Vehicle not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
// Return vehicle information
|
||||
h.writeJSONResponse(w, vehicle, http.StatusOK)
|
||||
}
|
||||
|
||||
// calculateStatsSummary calculates additional summary statistics
|
||||
func (h *Handler) calculateStatsSummary(stats *models.FuelStopStats) StatsSummary {
|
||||
summary := StatsSummary{}
|
||||
|
||||
if stats.TotalStops > 0 {
|
||||
// Calculate monthly average (assuming data spans multiple months)
|
||||
monthlyAvg := stats.TotalSpent / 12 // This is a simplified calculation
|
||||
summary.MonthlyAverage = monthlyAvg
|
||||
|
||||
// Calculate weekly average
|
||||
summary.WeeklyAverage = monthlyAvg / 4.33 // Average weeks per month
|
||||
|
||||
// Calculate fuel efficiency (simplified)
|
||||
summary.FuelEfficiency = stats.AverageConsumption
|
||||
|
||||
// These would require additional database queries in a real implementation
|
||||
summary.MostUsedStation = "N/A"
|
||||
summary.PreferredFuel = "N/A"
|
||||
}
|
||||
|
||||
return summary
|
||||
}
|
||||
|
||||
// writeJSONResponse writes a JSON response with the given status code
|
||||
func (h *Handler) writeJSONResponse(w http.ResponseWriter, data interface{}, statusCode int) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(statusCode)
|
||||
|
||||
if err := json.NewEncoder(w).Encode(data); err != nil {
|
||||
log.Printf("Error encoding JSON response: %v", err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
// writeJSONError writes a JSON error response
|
||||
func (h *Handler) writeJSONError(w http.ResponseWriter, message string, statusCode int) {
|
||||
errorResponse := struct {
|
||||
Error string `json:"error"`
|
||||
Message string `json:"message"`
|
||||
Code int `json:"code"`
|
||||
}{
|
||||
Error: http.StatusText(statusCode),
|
||||
Message: message,
|
||||
Code: statusCode,
|
||||
}
|
||||
|
||||
h.writeJSONResponse(w, errorResponse, statusCode)
|
||||
}
|
||||
@@ -0,0 +1,271 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"tankstopp/internal/auth"
|
||||
"tankstopp/internal/currency"
|
||||
"tankstopp/internal/models"
|
||||
"tankstopp/internal/views/pages"
|
||||
)
|
||||
|
||||
// RootHandler redirects to appropriate page based on authentication status
|
||||
func (h *Handler) RootHandler(w http.ResponseWriter, r *http.Request) {
|
||||
// Check if user is authenticated
|
||||
sessionID, err := auth.GetSessionCookie(r)
|
||||
if err != nil {
|
||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
_, exists := h.sessionManager.GetSession(sessionID)
|
||||
if !exists {
|
||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
// User is authenticated, redirect to dashboard
|
||||
http.Redirect(w, r, "/dashboard", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
// LoginHandler handles user authentication
|
||||
func (h *Handler) LoginHandler(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case "GET":
|
||||
component := pages.LoginPage("")
|
||||
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
err := component.Render(r.Context(), w)
|
||||
if err != nil {
|
||||
log.Printf("Error rendering template: %v", err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
}
|
||||
case "POST":
|
||||
h.handleLogin(w, r)
|
||||
default:
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
|
||||
// handleLogin processes login form submission
|
||||
func (h *Handler) handleLogin(w http.ResponseWriter, r *http.Request) {
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
log.Printf("Error parsing form: %v", err)
|
||||
h.renderLoginWithError(w, "Invalid form data")
|
||||
return
|
||||
}
|
||||
|
||||
username := strings.TrimSpace(r.FormValue("username"))
|
||||
password := r.FormValue("password")
|
||||
|
||||
if username == "" || password == "" {
|
||||
h.renderLoginWithError(w, "Username and password are required")
|
||||
return
|
||||
}
|
||||
|
||||
// Get user from database
|
||||
user, err := h.db.GetUserByUsername(username)
|
||||
if err != nil {
|
||||
log.Printf("Error getting user: %v", err)
|
||||
h.renderLoginWithError(w, "Invalid username or password")
|
||||
return
|
||||
}
|
||||
|
||||
// Check if user exists
|
||||
if user == nil {
|
||||
h.renderLoginWithError(w, "Invalid username or password")
|
||||
return
|
||||
}
|
||||
|
||||
// Check password
|
||||
if !user.CheckPassword(password) {
|
||||
h.renderLoginWithError(w, "Invalid username or password")
|
||||
return
|
||||
}
|
||||
|
||||
// Create session
|
||||
session := h.sessionManager.CreateSession(int(user.ID), user.Username)
|
||||
|
||||
// Set session cookie
|
||||
cookie := &http.Cookie{
|
||||
Name: "session_id",
|
||||
Value: session.ID,
|
||||
Path: "/",
|
||||
HttpOnly: true,
|
||||
Secure: false, // Set to true in production with HTTPS
|
||||
SameSite: http.SameSiteStrictMode,
|
||||
Expires: session.ExpiresAt,
|
||||
}
|
||||
http.SetCookie(w, cookie)
|
||||
|
||||
// Redirect to dashboard
|
||||
http.Redirect(w, r, "/dashboard", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
// renderLoginWithError renders the login page with an error message
|
||||
func (h *Handler) renderLoginWithError(w http.ResponseWriter, errorMsg string) {
|
||||
component := pages.LoginPage(errorMsg)
|
||||
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
err := component.Render(context.Background(), w)
|
||||
if err != nil {
|
||||
log.Printf("Error rendering template: %v", err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
// RegisterHandler handles user registration
|
||||
func (h *Handler) RegisterHandler(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case "GET":
|
||||
component := pages.RegisterPage("")
|
||||
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
err := component.Render(r.Context(), w)
|
||||
if err != nil {
|
||||
log.Printf("Error rendering template: %v", err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
}
|
||||
case "POST":
|
||||
h.handleRegister(w, r)
|
||||
default:
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
|
||||
// handleRegister processes registration form submission
|
||||
func (h *Handler) handleRegister(w http.ResponseWriter, r *http.Request) {
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
log.Printf("Error parsing form: %v", err)
|
||||
h.renderRegisterWithError(w, "Invalid form data")
|
||||
return
|
||||
}
|
||||
|
||||
// Parse form data
|
||||
form := models.UserRegistrationForm{
|
||||
Username: strings.TrimSpace(r.FormValue("username")),
|
||||
Email: strings.TrimSpace(r.FormValue("email")),
|
||||
Password: r.FormValue("password"),
|
||||
ConfirmPassword: r.FormValue("confirm_password"),
|
||||
BaseCurrency: r.FormValue("base_currency"),
|
||||
}
|
||||
|
||||
// Validate form
|
||||
if err := h.validateRegistrationForm(&form); err != nil {
|
||||
h.renderRegisterWithError(w, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// Convert form to user
|
||||
user, err := form.ToUser()
|
||||
if err != nil {
|
||||
log.Printf("Error converting form to user: %v", err)
|
||||
h.renderRegisterWithError(w, "Failed to create user")
|
||||
return
|
||||
}
|
||||
|
||||
// Create user in database
|
||||
err = h.db.CreateUser(user)
|
||||
if err != nil {
|
||||
log.Printf("Error creating user: %v", err)
|
||||
if strings.Contains(err.Error(), "UNIQUE constraint failed") {
|
||||
h.renderRegisterWithError(w, "Username or email already exists")
|
||||
} else {
|
||||
h.renderRegisterWithError(w, "Failed to create account")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Redirect to login with success message
|
||||
http.Redirect(w, r, "/login?success=Account+created+successfully", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
// validateRegistrationForm validates the registration form data
|
||||
func (h *Handler) validateRegistrationForm(form *models.UserRegistrationForm) error {
|
||||
if form.Username == "" {
|
||||
return fmt.Errorf("Username is required")
|
||||
}
|
||||
|
||||
if len(form.Username) < 3 {
|
||||
return fmt.Errorf("Username must be at least 3 characters long")
|
||||
}
|
||||
|
||||
if form.Email == "" {
|
||||
return fmt.Errorf("Email is required")
|
||||
}
|
||||
|
||||
if !strings.Contains(form.Email, "@") {
|
||||
return fmt.Errorf("Invalid email address")
|
||||
}
|
||||
|
||||
if form.Password == "" {
|
||||
return fmt.Errorf("Password is required")
|
||||
}
|
||||
|
||||
if len(form.Password) < 8 {
|
||||
return fmt.Errorf("Password must be at least 8 characters long")
|
||||
}
|
||||
|
||||
if form.Password != form.ConfirmPassword {
|
||||
return fmt.Errorf("Passwords do not match")
|
||||
}
|
||||
|
||||
if form.BaseCurrency == "" {
|
||||
return fmt.Errorf("Base currency is required")
|
||||
}
|
||||
|
||||
if !currency.IsValidCurrency(form.BaseCurrency) {
|
||||
return fmt.Errorf("Invalid currency")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// renderRegisterWithError renders the registration page with an error message
|
||||
func (h *Handler) renderRegisterWithError(w http.ResponseWriter, errorMsg string) {
|
||||
component := pages.RegisterPage(errorMsg)
|
||||
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
err := component.Render(context.Background(), w)
|
||||
if err != nil {
|
||||
log.Printf("Error rendering template: %v", err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
// LogoutHandler handles user logout
|
||||
func (h *Handler) LogoutHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != "POST" {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
// Get session cookie
|
||||
sessionID, err := auth.GetSessionCookie(r)
|
||||
if err == nil {
|
||||
// Remove session from session manager
|
||||
h.sessionManager.DeleteSession(sessionID)
|
||||
}
|
||||
|
||||
// Clear session cookie
|
||||
cookie := &http.Cookie{
|
||||
Name: "session_id",
|
||||
Value: "",
|
||||
Path: "/",
|
||||
HttpOnly: true,
|
||||
Secure: false, // Set to true in production with HTTPS
|
||||
SameSite: http.SameSiteStrictMode,
|
||||
Expires: time.Unix(0, 0), // Expire immediately
|
||||
}
|
||||
http.SetCookie(w, cookie)
|
||||
|
||||
// Redirect to login page
|
||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"tankstopp/internal/models"
|
||||
"tankstopp/internal/views/pages"
|
||||
)
|
||||
|
||||
// HomeHandler serves the main dashboard page
|
||||
func (h *Handler) HomeHandler(w http.ResponseWriter, r *http.Request) {
|
||||
userID, username := h.getCurrentUser(r)
|
||||
if userID == 0 {
|
||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
// Get the user object
|
||||
user, err := h.db.GetUserByID(userID)
|
||||
if err != nil {
|
||||
log.Printf("Error getting user: %v", err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Get fuel stops for the user
|
||||
stops, err := h.db.GetFuelStops(userID)
|
||||
if err != nil {
|
||||
log.Printf("Error getting fuel stops: %v", err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Get fuel stop statistics
|
||||
stats, err := h.db.GetFuelStopStats(userID)
|
||||
if err != nil {
|
||||
log.Printf("Error getting fuel stop stats: %v", err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Get vehicles for the user
|
||||
vehicles, err := h.db.GetVehicles(userID)
|
||||
if err != nil {
|
||||
log.Printf("Error getting vehicles: %v", err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Calculate dashboard statistics
|
||||
totalStops := len(stops)
|
||||
totalCost := stats.TotalSpent
|
||||
|
||||
// Calculate detailed consumption statistics
|
||||
avgConsumption, _, _ := h.calculateConsumptionStats(stops)
|
||||
if avgConsumption == 0 {
|
||||
avgConsumption = stats.AverageConsumption // Fallback to basic stats
|
||||
}
|
||||
|
||||
var lastFillUp *models.FuelStop
|
||||
if len(stops) > 0 {
|
||||
lastFillUp = &stops[0]
|
||||
}
|
||||
|
||||
// Render dashboard using templ
|
||||
component := pages.DashboardPage(user, username, stops, vehicles, totalStops, totalCost, avgConsumption, lastFillUp)
|
||||
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
err = component.Render(r.Context(), w)
|
||||
if err != nil {
|
||||
log.Printf("Error rendering template: %v", err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
// calculateConsumptionStats calculates consumption-related statistics
|
||||
func (h *Handler) calculateConsumptionStats(stops []models.FuelStop) (float64, float64, float64) {
|
||||
if len(stops) == 0 {
|
||||
return 0, 0, 0
|
||||
}
|
||||
|
||||
var totalLiters, totalKm float64
|
||||
var consumptionReadings []float64
|
||||
|
||||
for _, stop := range stops {
|
||||
totalLiters += stop.Liters
|
||||
|
||||
if stop.TripLength > 0 {
|
||||
totalKm += stop.TripLength
|
||||
|
||||
// Calculate consumption for this stop (L/100km)
|
||||
consumption := (stop.Liters / stop.TripLength) * 100
|
||||
if consumption > 0 && consumption < 50 { // Filter out unrealistic values
|
||||
consumptionReadings = append(consumptionReadings, consumption)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Average consumption from individual readings
|
||||
var avgConsumption float64
|
||||
if len(consumptionReadings) > 0 {
|
||||
var sum float64
|
||||
for _, consumption := range consumptionReadings {
|
||||
sum += consumption
|
||||
}
|
||||
avgConsumption = sum / float64(len(consumptionReadings))
|
||||
}
|
||||
|
||||
// Overall consumption from totals
|
||||
var overallConsumption float64
|
||||
if totalKm > 0 {
|
||||
overallConsumption = (totalLiters / totalKm) * 100
|
||||
}
|
||||
|
||||
return avgConsumption, overallConsumption, totalKm
|
||||
}
|
||||
|
||||
// calculateEfficiencyTrend calculates fuel efficiency trend over time
|
||||
func (h *Handler) calculateEfficiencyTrend(stops []models.FuelStop) string {
|
||||
if len(stops) < 2 {
|
||||
return "insufficient_data"
|
||||
}
|
||||
|
||||
// Get consumption for recent stops (last 5) vs older stops
|
||||
recentStops := stops[:min(5, len(stops))]
|
||||
olderStops := stops[min(5, len(stops)):]
|
||||
|
||||
recentAvg, _, _ := h.calculateConsumptionStats(recentStops)
|
||||
olderAvg, _, _ := h.calculateConsumptionStats(olderStops)
|
||||
|
||||
if recentAvg == 0 || olderAvg == 0 {
|
||||
return "insufficient_data"
|
||||
}
|
||||
|
||||
diff := recentAvg - olderAvg
|
||||
if diff < -0.5 {
|
||||
return "improving" // Lower consumption is better
|
||||
} else if diff > 0.5 {
|
||||
return "worsening"
|
||||
}
|
||||
return "stable"
|
||||
}
|
||||
|
||||
// min helper function
|
||||
func min(a, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
@@ -0,0 +1,395 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"tankstopp/internal/currency"
|
||||
"tankstopp/internal/models"
|
||||
"tankstopp/internal/views/pages"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
// AddFuelStopHandler handles adding new fuel stops
|
||||
func (h *Handler) AddFuelStopHandler(w http.ResponseWriter, r *http.Request) {
|
||||
userID, _ := h.getCurrentUser(r)
|
||||
if userID == 0 {
|
||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
switch r.Method {
|
||||
case "GET":
|
||||
// Get user for default currency
|
||||
user, err := h.db.GetUserByID(userID)
|
||||
if err != nil {
|
||||
log.Printf("Error getting user: %v", err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Get vehicles for the user
|
||||
vehicles, err := h.db.GetVehicles(userID)
|
||||
if err != nil {
|
||||
log.Printf("Error getting vehicles: %v", err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Render add fuel stop form using templ
|
||||
currencies := currency.SupportedCurrencies()
|
||||
component := pages.AddFuelStopPage(user, user.Username, vehicles, currencies)
|
||||
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
err = component.Render(r.Context(), w)
|
||||
if err != nil {
|
||||
log.Printf("Error rendering template: %v", err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
}
|
||||
case "POST":
|
||||
h.handleAddFuelStop(w, r, userID)
|
||||
default:
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
|
||||
// handleAddFuelStop processes the form submission for adding fuel stops
|
||||
func (h *Handler) handleAddFuelStop(w http.ResponseWriter, r *http.Request, userID uint) {
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
log.Printf("Error parsing form: %v", err)
|
||||
http.Error(w, "Bad request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Parse form data
|
||||
form := models.FuelStopForm{
|
||||
Date: strings.TrimSpace(r.FormValue("date")),
|
||||
VehicleID: parseUint(r.FormValue("vehicle_id")),
|
||||
StationName: strings.TrimSpace(r.FormValue("station_name")),
|
||||
Location: strings.TrimSpace(r.FormValue("location")),
|
||||
FuelType: r.FormValue("fuel_type"),
|
||||
Liters: parseFloat(r.FormValue("amount")),
|
||||
PricePerL: parseFloat(r.FormValue("price_per_liter")),
|
||||
TotalPrice: parseFloat(r.FormValue("total_cost")),
|
||||
Currency: r.FormValue("currency"),
|
||||
Odometer: parseInt(r.FormValue("odometer")),
|
||||
TripLength: parseFloat(r.FormValue("trip_length")),
|
||||
Notes: r.FormValue("notes"),
|
||||
}
|
||||
|
||||
// Validate form
|
||||
if err := h.validateFuelStopForm(&form); err != nil {
|
||||
log.Printf("Validation error: %v", err)
|
||||
http.Redirect(w, r, "/add?error="+err.Error(), http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
// Convert form to fuel stop
|
||||
fuelStop, err := form.ToFuelStop(userID)
|
||||
if err != nil {
|
||||
log.Printf("Error converting form to fuel stop: %v", err)
|
||||
http.Redirect(w, r, "/add?error=Invalid+date+format", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
// Use station name as location if location is empty
|
||||
if fuelStop.Location == "" {
|
||||
fuelStop.Location = fuelStop.StationName
|
||||
}
|
||||
|
||||
// Save to database
|
||||
err = h.db.CreateFuelStop(fuelStop)
|
||||
if err != nil {
|
||||
log.Printf("Error creating fuel stop: %v", err)
|
||||
http.Redirect(w, r, "/add?error=Failed+to+save+fuel+stop", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
// Redirect to dashboard with success message
|
||||
http.Redirect(w, r, "/dashboard?success=Fuel+stop+added+successfully", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
// EditFuelStopHandler handles editing existing fuel stops
|
||||
func (h *Handler) EditFuelStopHandler(w http.ResponseWriter, r *http.Request) {
|
||||
userID, _ := h.getCurrentUser(r)
|
||||
if userID == 0 {
|
||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
// Get the user object
|
||||
user, err := h.db.GetUserByID(userID)
|
||||
if err != nil {
|
||||
log.Printf("Error getting user: %v", err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
vars := mux.Vars(r)
|
||||
idInt, err := strconv.Atoi(vars["id"])
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid ID", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
id := uint(idInt)
|
||||
|
||||
switch r.Method {
|
||||
case "GET":
|
||||
stop, err := h.db.GetFuelStopByID(id, userID)
|
||||
if err != nil {
|
||||
log.Printf("Error getting fuel stop: %v", err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if stop == nil {
|
||||
http.Error(w, "Fuel stop not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
// Get vehicles for the user
|
||||
vehicles, err := h.db.GetVehicles(userID)
|
||||
if err != nil {
|
||||
log.Printf("Error getting vehicles: %v", err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Render edit fuel stop form using templ
|
||||
currencies := currency.SupportedCurrencies()
|
||||
component := pages.EditFuelStopPage(user, user.Username, stop, vehicles, currencies)
|
||||
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
err = component.Render(r.Context(), w)
|
||||
if err != nil {
|
||||
log.Printf("Error rendering template: %v", err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
}
|
||||
case "POST":
|
||||
h.handleEditFuelStop(w, r, id, userID)
|
||||
default:
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
|
||||
// handleEditFuelStop processes the form submission for editing fuel stops
|
||||
func (h *Handler) handleEditFuelStop(w http.ResponseWriter, r *http.Request, id, userID uint) {
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
log.Printf("Error parsing form: %v", err)
|
||||
http.Error(w, "Bad request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Get existing fuel stop
|
||||
existingStop, err := h.db.GetFuelStopByID(id, userID)
|
||||
if err != nil {
|
||||
log.Printf("Error getting fuel stop: %v", err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if existingStop == nil {
|
||||
http.Error(w, "Fuel stop not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
// Parse form data
|
||||
form := models.FuelStopForm{
|
||||
Date: strings.TrimSpace(r.FormValue("date")),
|
||||
VehicleID: parseUint(r.FormValue("vehicle_id")),
|
||||
StationName: strings.TrimSpace(r.FormValue("station_name")),
|
||||
Location: strings.TrimSpace(r.FormValue("location")),
|
||||
FuelType: r.FormValue("fuel_type"),
|
||||
Liters: parseFloat(r.FormValue("amount")),
|
||||
PricePerL: parseFloat(r.FormValue("price_per_liter")),
|
||||
TotalPrice: parseFloat(r.FormValue("total_cost")),
|
||||
Currency: r.FormValue("currency"),
|
||||
Odometer: parseInt(r.FormValue("odometer")),
|
||||
TripLength: parseFloat(r.FormValue("trip_length")),
|
||||
Notes: r.FormValue("notes"),
|
||||
}
|
||||
|
||||
// Validate form
|
||||
if err := h.validateFuelStopForm(&form); err != nil {
|
||||
log.Printf("Validation error: %v", err)
|
||||
http.Redirect(w, r, fmt.Sprintf("/edit/%d?error=%s", id, err.Error()), http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
// Convert form to fuel stop
|
||||
updatedStop, err := form.ToFuelStop(userID)
|
||||
if err != nil {
|
||||
log.Printf("Error converting form to fuel stop: %v", err)
|
||||
http.Redirect(w, r, fmt.Sprintf("/edit/%d?error=Invalid+date+format", id), http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
// Set the ID to update existing record
|
||||
updatedStop.ID = id
|
||||
|
||||
// Use station name as location if location is empty
|
||||
if updatedStop.Location == "" {
|
||||
updatedStop.Location = updatedStop.StationName
|
||||
}
|
||||
|
||||
// Update in database
|
||||
err = h.db.UpdateFuelStop(updatedStop)
|
||||
if err != nil {
|
||||
log.Printf("Error updating fuel stop: %v", err)
|
||||
http.Redirect(w, r, fmt.Sprintf("/edit/%d?error=Failed+to+update+fuel+stop", id), http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
// Redirect to dashboard with success message
|
||||
http.Redirect(w, r, "/dashboard?success=Fuel+stop+updated+successfully", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
// DeleteFuelStopHandler handles deleting fuel stops
|
||||
func (h *Handler) DeleteFuelStopHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != "POST" {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
userID, _ := h.getCurrentUser(r)
|
||||
if userID == 0 {
|
||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
vars := mux.Vars(r)
|
||||
idInt, err := strconv.Atoi(vars["id"])
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid ID", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
id := uint(idInt)
|
||||
|
||||
// Verify fuel stop exists and belongs to user
|
||||
fuelStop, err := h.db.GetFuelStopByID(id, userID)
|
||||
if err != nil {
|
||||
log.Printf("Error getting fuel stop: %v", err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if fuelStop == nil {
|
||||
http.Error(w, "Fuel stop not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
// Delete fuel stop
|
||||
err = h.db.DeleteFuelStop(id, userID)
|
||||
if err != nil {
|
||||
log.Printf("Error deleting fuel stop: %v", err)
|
||||
http.Redirect(w, r, "/dashboard?error=Failed+to+delete+fuel+stop", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
// Redirect to dashboard with success message
|
||||
http.Redirect(w, r, "/dashboard?success=Fuel+stop+deleted+successfully", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
// validateFuelStopForm validates the fuel stop form data
|
||||
func (h *Handler) validateFuelStopForm(form *models.FuelStopForm) error {
|
||||
if form.Date == "" {
|
||||
return fmt.Errorf("Date is required")
|
||||
}
|
||||
|
||||
// Validate date format
|
||||
_, err := time.Parse("2006-01-02", form.Date)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Invalid date format")
|
||||
}
|
||||
|
||||
if form.VehicleID == 0 {
|
||||
return fmt.Errorf("Vehicle is required")
|
||||
}
|
||||
|
||||
if form.StationName == "" && form.Location == "" {
|
||||
return fmt.Errorf("Station name or location is required")
|
||||
}
|
||||
|
||||
if form.FuelType == "" {
|
||||
return fmt.Errorf("Fuel type is required")
|
||||
}
|
||||
|
||||
if form.Liters <= 0 {
|
||||
return fmt.Errorf("Amount must be greater than 0")
|
||||
}
|
||||
|
||||
if form.PricePerL <= 0 {
|
||||
return fmt.Errorf("Price per liter must be greater than 0")
|
||||
}
|
||||
|
||||
if form.TotalPrice <= 0 {
|
||||
return fmt.Errorf("Total price must be greater than 0")
|
||||
}
|
||||
|
||||
if form.Currency != "" && !currency.IsValidCurrency(form.Currency) {
|
||||
return fmt.Errorf("Invalid currency")
|
||||
}
|
||||
|
||||
if form.Odometer < 0 {
|
||||
return fmt.Errorf("Odometer reading cannot be negative")
|
||||
}
|
||||
|
||||
if form.TripLength < 0 {
|
||||
return fmt.Errorf("Trip length cannot be negative")
|
||||
}
|
||||
|
||||
if form.TripLength > 2000 {
|
||||
return fmt.Errorf("Trip length cannot exceed 2000 km")
|
||||
}
|
||||
|
||||
// Validate consumption if both trip length and amount are provided
|
||||
if form.TripLength > 0 && form.Liters > 0 {
|
||||
consumption := (form.Liters / form.TripLength) * 100
|
||||
if consumption > 50 {
|
||||
return fmt.Errorf("Fuel consumption %.1f L/100km seems unrealistic. Please check trip length and amount", consumption)
|
||||
}
|
||||
if consumption < 1 {
|
||||
return fmt.Errorf("Fuel consumption %.1f L/100km seems too low. Please check trip length and amount", consumption)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Helper functions for parsing form values
|
||||
func parseFloat(s string) float64 {
|
||||
if s == "" {
|
||||
return 0
|
||||
}
|
||||
f, err := strconv.ParseFloat(s, 64)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
func parseInt(s string) int {
|
||||
if s == "" {
|
||||
return 0
|
||||
}
|
||||
i, err := strconv.Atoi(s)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
func parseUint(s string) uint {
|
||||
if s == "" {
|
||||
return 0
|
||||
}
|
||||
i, err := strconv.ParseUint(s, 10, 32)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return uint(i)
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"tankstopp/internal/auth"
|
||||
"tankstopp/internal/database"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
// Handler contains dependencies for all HTTP handlers
|
||||
type Handler struct {
|
||||
db *database.DB
|
||||
sessionManager *auth.SessionManager
|
||||
}
|
||||
|
||||
// NewHandler creates a new handler with database connection and session manager
|
||||
func NewHandler(db *database.DB) *Handler {
|
||||
return &Handler{
|
||||
db: db,
|
||||
sessionManager: auth.NewSessionManager(),
|
||||
}
|
||||
}
|
||||
|
||||
// AuthMiddleware checks if user is authenticated
|
||||
func (h *Handler) AuthMiddleware(next http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
sessionID, err := auth.GetSessionCookie(r)
|
||||
if err != nil {
|
||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
session, exists := h.sessionManager.GetSession(sessionID)
|
||||
if !exists {
|
||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
// Add user info to request context
|
||||
r.Header.Set("X-User-ID", strconv.Itoa(int(session.UserID)))
|
||||
r.Header.Set("X-Username", session.Username)
|
||||
|
||||
next.ServeHTTP(w, r)
|
||||
}
|
||||
}
|
||||
|
||||
// getCurrentUser extracts user information from request headers
|
||||
func (h *Handler) getCurrentUser(r *http.Request) (uint, string) {
|
||||
userIDStr := r.Header.Get("X-User-ID")
|
||||
username := r.Header.Get("X-Username")
|
||||
|
||||
if userIDStr == "" {
|
||||
return 0, ""
|
||||
}
|
||||
|
||||
userIDInt, err := strconv.Atoi(userIDStr)
|
||||
if err != nil {
|
||||
log.Printf("Error parsing user ID: %v", err)
|
||||
return 0, ""
|
||||
}
|
||||
|
||||
return uint(userIDInt), username
|
||||
}
|
||||
|
||||
// RegisterRoutes registers all application routes
|
||||
func (h *Handler) RegisterRoutes(r *mux.Router) {
|
||||
// Static files
|
||||
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("./static/"))))
|
||||
|
||||
// Public routes (no authentication required)
|
||||
r.HandleFunc("/", h.RootHandler).Methods("GET")
|
||||
r.HandleFunc("/login", h.LoginHandler).Methods("GET", "POST")
|
||||
r.HandleFunc("/register", h.RegisterHandler).Methods("GET", "POST")
|
||||
r.HandleFunc("/logout", h.LogoutHandler).Methods("POST")
|
||||
|
||||
// Protected routes (authentication required)
|
||||
r.HandleFunc("/dashboard", h.AuthMiddleware(h.HomeHandler)).Methods("GET")
|
||||
r.HandleFunc("/add", h.AuthMiddleware(h.AddFuelStopHandler)).Methods("GET", "POST")
|
||||
r.HandleFunc("/edit/{id:[0-9]+}", h.AuthMiddleware(h.EditFuelStopHandler)).Methods("GET", "POST")
|
||||
r.HandleFunc("/delete/{id:[0-9]+}", h.AuthMiddleware(h.DeleteFuelStopHandler)).Methods("POST")
|
||||
r.HandleFunc("/settings", h.AuthMiddleware(h.SettingsHandler)).Methods("GET")
|
||||
r.HandleFunc("/settings/profile", h.AuthMiddleware(h.UpdateProfileHandler)).Methods("POST")
|
||||
r.HandleFunc("/settings/password", h.AuthMiddleware(h.UpdatePasswordHandler)).Methods("POST")
|
||||
r.HandleFunc("/settings/delete-account", h.AuthMiddleware(h.DeleteAccountHandler)).Methods("POST")
|
||||
|
||||
// Vehicle management routes
|
||||
r.HandleFunc("/vehicles", h.AuthMiddleware(h.VehiclesHandler)).Methods("GET")
|
||||
r.HandleFunc("/vehicles/add", h.AuthMiddleware(h.AddVehicleHandler)).Methods("GET", "POST")
|
||||
r.HandleFunc("/vehicles/edit/{id:[0-9]+}", h.AuthMiddleware(h.EditVehicleHandler)).Methods("GET", "POST")
|
||||
r.HandleFunc("/vehicles/delete/{id:[0-9]+}", h.AuthMiddleware(h.DeleteVehicleHandler)).Methods("POST")
|
||||
|
||||
// API routes
|
||||
r.HandleFunc("/api/fuel-stops", h.AuthMiddleware(h.APIGetFuelStopsHandler)).Methods("GET")
|
||||
r.HandleFunc("/api/fuel-stops", h.AuthMiddleware(h.APICreateFuelStopHandler)).Methods("POST")
|
||||
r.HandleFunc("/api/stats", h.AuthMiddleware(h.APIGetFuelStopStatsHandler)).Methods("GET")
|
||||
r.HandleFunc("/api/vehicles/{id:[0-9]+}", h.AuthMiddleware(h.APIGetVehicleHandler)).Methods("GET")
|
||||
}
|
||||
@@ -0,0 +1,287 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"tankstopp/internal/currency"
|
||||
"tankstopp/internal/views/pages"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
// SettingsHandler handles user settings page
|
||||
func (h *Handler) SettingsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
userID, _ := h.getCurrentUser(r)
|
||||
if userID == 0 {
|
||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
// Get user details
|
||||
user, err := h.db.GetUserByID(userID)
|
||||
if err != nil {
|
||||
log.Printf("Error getting user: %v", err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Render settings page using templ
|
||||
currencies := currency.SupportedCurrencies()
|
||||
successMessage := r.URL.Query().Get("success")
|
||||
errorMessage := r.URL.Query().Get("error")
|
||||
|
||||
component := pages.SettingsPage(user, user.Username, currencies, successMessage, errorMessage)
|
||||
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
err = component.Render(r.Context(), w)
|
||||
if err != nil {
|
||||
log.Printf("Error rendering template: %v", err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
// UpdateProfileHandler handles profile updates (email, currency, username)
|
||||
func (h *Handler) UpdateProfileHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != "POST" {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
userID, _ := h.getCurrentUser(r)
|
||||
if userID == 0 {
|
||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
log.Printf("Error parsing form: %v", err)
|
||||
http.Error(w, "Bad request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
username := strings.TrimSpace(r.FormValue("username"))
|
||||
email := strings.TrimSpace(r.FormValue("email"))
|
||||
baseCurrency := r.FormValue("base_currency")
|
||||
|
||||
// Validate form data
|
||||
if err := h.validateProfileForm(username, email, baseCurrency); err != nil {
|
||||
http.Redirect(w, r, "/settings?error="+err.Error(), http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
// Get current user
|
||||
user, err := h.db.GetUserByID(userID)
|
||||
if err != nil {
|
||||
log.Printf("Error getting user: %v", err)
|
||||
http.Redirect(w, r, "/settings?error=Failed+to+update+profile", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
// Update user fields
|
||||
user.Username = username
|
||||
user.Email = email
|
||||
user.BaseCurrency = baseCurrency
|
||||
|
||||
// Save to database
|
||||
err = h.db.UpdateUser(user)
|
||||
if err != nil {
|
||||
log.Printf("Error updating user: %v", err)
|
||||
if strings.Contains(err.Error(), "UNIQUE constraint failed") {
|
||||
if strings.Contains(err.Error(), "username") {
|
||||
http.Redirect(w, r, "/settings?error=Username+already+taken", http.StatusSeeOther)
|
||||
} else {
|
||||
http.Redirect(w, r, "/settings?error=Email+already+in+use", http.StatusSeeOther)
|
||||
}
|
||||
} else {
|
||||
http.Redirect(w, r, "/settings?error=Failed+to+update+profile", http.StatusSeeOther)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Note: Session username update would require session manager enhancement
|
||||
// For now, user will see updated username on next login
|
||||
|
||||
http.Redirect(w, r, "/settings?success=Profile+updated+successfully", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
// UpdatePasswordHandler handles password changes
|
||||
func (h *Handler) UpdatePasswordHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != "POST" {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
userID, _ := h.getCurrentUser(r)
|
||||
if userID == 0 {
|
||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
log.Printf("Error parsing form: %v", err)
|
||||
http.Error(w, "Bad request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
currentPassword := r.FormValue("current_password")
|
||||
newPassword := r.FormValue("new_password")
|
||||
confirmPassword := r.FormValue("confirm_password")
|
||||
|
||||
// Validate passwords
|
||||
if err := h.validatePasswordForm(currentPassword, newPassword, confirmPassword); err != nil {
|
||||
http.Redirect(w, r, "/settings?error="+err.Error(), http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
// Get current user
|
||||
user, err := h.db.GetUserByID(userID)
|
||||
if err != nil {
|
||||
log.Printf("Error getting user: %v", err)
|
||||
http.Redirect(w, r, "/settings?error=Failed+to+change+password", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
// Verify current password
|
||||
if !user.CheckPassword(currentPassword) {
|
||||
http.Redirect(w, r, "/settings?error=Current+password+is+incorrect", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
// Hash new password
|
||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(newPassword), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
log.Printf("Error hashing password: %v", err)
|
||||
http.Redirect(w, r, "/settings?error=Failed+to+change+password", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
// Update password
|
||||
user.PasswordHash = string(hashedPassword)
|
||||
err = h.db.UpdateUser(user)
|
||||
if err != nil {
|
||||
log.Printf("Error updating user password: %v", err)
|
||||
http.Redirect(w, r, "/settings?error=Failed+to+change+password", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
http.Redirect(w, r, "/settings?success=Password+changed+successfully", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
// DeleteAccountHandler handles account deletion
|
||||
func (h *Handler) DeleteAccountHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != "POST" {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
userID, _ := h.getCurrentUser(r)
|
||||
if userID == 0 {
|
||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
// Note: In a full implementation, we would delete all user data
|
||||
// For now, we'll just clear the session and redirect
|
||||
// TODO: Implement proper user deletion with cascading deletes
|
||||
|
||||
// Skip user deletion for now - would require proper database method
|
||||
// err = h.db.DeleteUser(userID)
|
||||
var err error // placeholder
|
||||
if err != nil {
|
||||
log.Printf("Error deleting user: %v", err)
|
||||
http.Redirect(w, r, "/settings?error=Failed+to+delete+account", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
// Note: Removing all user sessions would require session manager enhancement
|
||||
// Current session will be cleared by cookie deletion below
|
||||
|
||||
// Clear session cookie
|
||||
cookie := &http.Cookie{
|
||||
Name: "session_id",
|
||||
Value: "",
|
||||
Path: "/",
|
||||
HttpOnly: true,
|
||||
Secure: false, // Set to true in production with HTTPS
|
||||
MaxAge: -1, // Delete immediately
|
||||
}
|
||||
http.SetCookie(w, cookie)
|
||||
|
||||
// Redirect to login with message
|
||||
http.Redirect(w, r, "/login?success=Account+deleted+successfully", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
// validateProfileForm validates profile update form data
|
||||
func (h *Handler) validateProfileForm(username, email, baseCurrency string) error {
|
||||
if username == "" {
|
||||
return fmt.Errorf("Username is required")
|
||||
}
|
||||
|
||||
if len(username) < 3 {
|
||||
return fmt.Errorf("Username must be at least 3 characters long")
|
||||
}
|
||||
|
||||
if len(username) > 50 {
|
||||
return fmt.Errorf("Username cannot be longer than 50 characters")
|
||||
}
|
||||
|
||||
if email == "" {
|
||||
return fmt.Errorf("Email is required")
|
||||
}
|
||||
|
||||
if !strings.Contains(email, "@") || !strings.Contains(email, ".") {
|
||||
return fmt.Errorf("Invalid email address")
|
||||
}
|
||||
|
||||
if len(email) > 255 {
|
||||
return fmt.Errorf("Email cannot be longer than 255 characters")
|
||||
}
|
||||
|
||||
if baseCurrency == "" {
|
||||
return fmt.Errorf("Base currency is required")
|
||||
}
|
||||
|
||||
if !currency.IsValidCurrency(baseCurrency) {
|
||||
return fmt.Errorf("Invalid currency")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// validatePasswordForm validates password change form data
|
||||
func (h *Handler) validatePasswordForm(currentPassword, newPassword, confirmPassword string) error {
|
||||
if currentPassword == "" {
|
||||
return fmt.Errorf("Current password is required")
|
||||
}
|
||||
|
||||
if newPassword == "" {
|
||||
return fmt.Errorf("New password is required")
|
||||
}
|
||||
|
||||
if len(newPassword) < 8 {
|
||||
return fmt.Errorf("New password must be at least 8 characters long")
|
||||
}
|
||||
|
||||
if len(newPassword) > 128 {
|
||||
return fmt.Errorf("New password cannot be longer than 128 characters")
|
||||
}
|
||||
|
||||
if confirmPassword == "" {
|
||||
return fmt.Errorf("Password confirmation is required")
|
||||
}
|
||||
|
||||
if newPassword != confirmPassword {
|
||||
return fmt.Errorf("New passwords do not match")
|
||||
}
|
||||
|
||||
if currentPassword == newPassword {
|
||||
return fmt.Errorf("New password must be different from current password")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,342 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"tankstopp/internal/models"
|
||||
"tankstopp/internal/views/pages"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
// VehiclesHandler handles vehicle management page
|
||||
func (h *Handler) VehiclesHandler(w http.ResponseWriter, r *http.Request) {
|
||||
userID, username := h.getCurrentUser(r)
|
||||
if userID == 0 {
|
||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
// Get user object
|
||||
user, err := h.db.GetUserByID(userID)
|
||||
if err != nil {
|
||||
log.Printf("Error getting user: %v", err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Get vehicles for the user
|
||||
vehicles, err := h.db.GetVehicles(userID)
|
||||
if err != nil {
|
||||
log.Printf("Error getting vehicles: %v", err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Render vehicles page using templ
|
||||
component := pages.VehiclesPage(user, username, vehicles)
|
||||
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
err = component.Render(r.Context(), w)
|
||||
if err != nil {
|
||||
log.Printf("Error rendering template: %v", err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
// AddVehicleHandler handles adding new vehicles
|
||||
func (h *Handler) AddVehicleHandler(w http.ResponseWriter, r *http.Request) {
|
||||
userID, _ := h.getCurrentUser(r)
|
||||
if userID == 0 {
|
||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
switch r.Method {
|
||||
case "GET":
|
||||
// Get user object
|
||||
user, err := h.db.GetUserByID(userID)
|
||||
if err != nil {
|
||||
log.Printf("Error getting user: %v", err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Render add vehicle form using templ
|
||||
component := pages.AddVehiclePage(user, user.Username)
|
||||
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
err = component.Render(r.Context(), w)
|
||||
if err != nil {
|
||||
log.Printf("Error rendering template: %v", err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
}
|
||||
case "POST":
|
||||
h.handleAddVehicle(w, r, userID)
|
||||
default:
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
|
||||
// handleAddVehicle processes the form submission for adding vehicles
|
||||
func (h *Handler) handleAddVehicle(w http.ResponseWriter, r *http.Request, userID uint) {
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
log.Printf("Error parsing form: %v", err)
|
||||
http.Error(w, "Bad request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Parse form data
|
||||
form := models.VehicleForm{
|
||||
Name: strings.TrimSpace(r.FormValue("name")),
|
||||
Make: strings.TrimSpace(r.FormValue("make")),
|
||||
Model: strings.TrimSpace(r.FormValue("model")),
|
||||
LicensePlate: strings.TrimSpace(r.FormValue("license_plate")),
|
||||
FuelType: r.FormValue("fuel_type"),
|
||||
Notes: r.FormValue("notes"),
|
||||
IsActive: r.FormValue("is_active") == "on",
|
||||
}
|
||||
|
||||
// Parse year
|
||||
if yearStr := r.FormValue("year"); yearStr != "" {
|
||||
if year, err := strconv.Atoi(yearStr); err == nil {
|
||||
form.Year = year
|
||||
}
|
||||
}
|
||||
|
||||
// Validate form
|
||||
if err := h.validateVehicleForm(&form); err != nil {
|
||||
log.Printf("Validation error: %v", err)
|
||||
http.Redirect(w, r, "/vehicles/add?error="+err.Error(), http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
// Convert form to vehicle
|
||||
vehicle := form.ToVehicle(userID)
|
||||
|
||||
// Save to database
|
||||
err = h.db.CreateVehicle(vehicle)
|
||||
if err != nil {
|
||||
log.Printf("Error creating vehicle: %v", err)
|
||||
http.Redirect(w, r, "/vehicles/add?error=Failed+to+create+vehicle", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
// Redirect to vehicles page with success message
|
||||
http.Redirect(w, r, "/vehicles?success=Vehicle+added+successfully", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
// EditVehicleHandler handles editing existing vehicles
|
||||
func (h *Handler) EditVehicleHandler(w http.ResponseWriter, r *http.Request) {
|
||||
userID, _ := h.getCurrentUser(r)
|
||||
if userID == 0 {
|
||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
vars := mux.Vars(r)
|
||||
idInt, err := strconv.Atoi(vars["id"])
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid ID", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
id := uint(idInt)
|
||||
|
||||
switch r.Method {
|
||||
case "GET":
|
||||
// Get vehicle
|
||||
vehicle, err := h.db.GetVehicleByID(id, userID)
|
||||
if err != nil {
|
||||
log.Printf("Error getting vehicle: %v", err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if vehicle == nil {
|
||||
http.Error(w, "Vehicle not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
// Get user object
|
||||
user, err := h.db.GetUserByID(userID)
|
||||
if err != nil {
|
||||
log.Printf("Error getting user: %v", err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Render edit vehicle form using templ
|
||||
component := pages.EditVehiclePage(user, user.Username, vehicle)
|
||||
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
err = component.Render(r.Context(), w)
|
||||
if err != nil {
|
||||
log.Printf("Error rendering template: %v", err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
}
|
||||
case "POST":
|
||||
h.handleEditVehicle(w, r, id, userID)
|
||||
default:
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
|
||||
// handleEditVehicle processes the form submission for editing vehicles
|
||||
func (h *Handler) handleEditVehicle(w http.ResponseWriter, r *http.Request, id, userID uint) {
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
log.Printf("Error parsing form: %v", err)
|
||||
http.Error(w, "Bad request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Get existing vehicle
|
||||
existingVehicle, err := h.db.GetVehicleByID(id, userID)
|
||||
if err != nil {
|
||||
log.Printf("Error getting vehicle: %v", err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if existingVehicle == nil {
|
||||
http.Error(w, "Vehicle not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
// Parse form data
|
||||
form := models.VehicleForm{
|
||||
Name: strings.TrimSpace(r.FormValue("name")),
|
||||
Make: strings.TrimSpace(r.FormValue("make")),
|
||||
Model: strings.TrimSpace(r.FormValue("model")),
|
||||
LicensePlate: strings.TrimSpace(r.FormValue("license_plate")),
|
||||
FuelType: r.FormValue("fuel_type"),
|
||||
Notes: r.FormValue("notes"),
|
||||
IsActive: r.FormValue("is_active") == "on",
|
||||
}
|
||||
|
||||
// Parse year
|
||||
if yearStr := r.FormValue("year"); yearStr != "" {
|
||||
if year, err := strconv.Atoi(yearStr); err == nil {
|
||||
form.Year = year
|
||||
}
|
||||
}
|
||||
|
||||
// Validate form
|
||||
if err := h.validateVehicleForm(&form); err != nil {
|
||||
log.Printf("Validation error: %v", err)
|
||||
http.Redirect(w, r, fmt.Sprintf("/vehicles/edit/%d?error=%s", id, err.Error()), http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
// Convert form to vehicle
|
||||
updatedVehicle := form.ToVehicle(userID)
|
||||
updatedVehicle.ID = id
|
||||
updatedVehicle.CreatedAt = existingVehicle.CreatedAt
|
||||
|
||||
// Update in database
|
||||
err = h.db.UpdateVehicle(updatedVehicle)
|
||||
if err != nil {
|
||||
log.Printf("Error updating vehicle: %v", err)
|
||||
http.Redirect(w, r, fmt.Sprintf("/vehicles/edit/%d?error=Failed+to+update+vehicle", id), http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
// Redirect to vehicles page with success message
|
||||
http.Redirect(w, r, "/vehicles?success=Vehicle+updated+successfully", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
// DeleteVehicleHandler handles deleting vehicles
|
||||
func (h *Handler) DeleteVehicleHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != "POST" {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
userID, _ := h.getCurrentUser(r)
|
||||
if userID == 0 {
|
||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
vars := mux.Vars(r)
|
||||
idInt, err := strconv.Atoi(vars["id"])
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid ID", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
id := uint(idInt)
|
||||
|
||||
// Verify vehicle exists and belongs to user
|
||||
vehicle, err := h.db.GetVehicleByID(id, userID)
|
||||
if err != nil {
|
||||
log.Printf("Error getting vehicle: %v", err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if vehicle == nil {
|
||||
http.Error(w, "Vehicle not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
// Note: In a full implementation, we would check for associated fuel stops
|
||||
// For now, we'll allow deletion and rely on database constraints
|
||||
|
||||
// Delete vehicle
|
||||
err = h.db.DeleteVehicle(id, userID)
|
||||
if err != nil {
|
||||
log.Printf("Error deleting vehicle: %v", err)
|
||||
http.Redirect(w, r, "/vehicles?error=Failed+to+delete+vehicle", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
// Redirect to vehicles page with success message
|
||||
http.Redirect(w, r, "/vehicles?success=Vehicle+deleted+successfully", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
// validateVehicleForm validates the vehicle form data
|
||||
func (h *Handler) validateVehicleForm(form *models.VehicleForm) error {
|
||||
if form.Name == "" {
|
||||
return fmt.Errorf("Vehicle name is required")
|
||||
}
|
||||
|
||||
if len(form.Name) < 2 {
|
||||
return fmt.Errorf("Vehicle name must be at least 2 characters long")
|
||||
}
|
||||
|
||||
if form.Make == "" {
|
||||
return fmt.Errorf("Vehicle make is required")
|
||||
}
|
||||
|
||||
if form.Model == "" {
|
||||
return fmt.Errorf("Vehicle model is required")
|
||||
}
|
||||
|
||||
if form.FuelType == "" {
|
||||
return fmt.Errorf("Fuel type is required")
|
||||
}
|
||||
|
||||
// Validate year if provided
|
||||
if form.Year != 0 {
|
||||
currentYear := 2024 // You might want to use time.Now().Year()
|
||||
if form.Year < 1900 || form.Year > currentYear+1 {
|
||||
return fmt.Errorf("Year must be between 1900 and %d", currentYear+1)
|
||||
}
|
||||
}
|
||||
|
||||
// Validate license plate format if provided
|
||||
if form.LicensePlate != "" {
|
||||
if len(form.LicensePlate) > 20 {
|
||||
return fmt.Errorf("License plate cannot be longer than 20 characters")
|
||||
}
|
||||
}
|
||||
|
||||
// Validate notes length if provided
|
||||
if len(form.Notes) > 500 {
|
||||
return fmt.Errorf("Notes cannot be longer than 500 characters")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,213 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
// User represents a user account
|
||||
type User struct {
|
||||
ID uint `json:"id" gorm:"primaryKey;autoIncrement"`
|
||||
Username string `json:"username" gorm:"uniqueIndex;not null;size:50"`
|
||||
Email string `json:"email" gorm:"uniqueIndex;not null;size:255"`
|
||||
Password string `json:"-" gorm:"-"` // Only used for input, never stored
|
||||
PasswordHash string `json:"-" gorm:"column:password_hash;not null"`
|
||||
BaseCurrency string `json:"base_currency" gorm:"not null;default:EUR;size:3"`
|
||||
FuelStops []FuelStop `json:"fuel_stops,omitempty" gorm:"foreignKey:UserID;constraint:OnDelete:CASCADE"`
|
||||
Vehicles []Vehicle `json:"vehicles,omitempty" gorm:"foreignKey:UserID;constraint:OnDelete:CASCADE"`
|
||||
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"`
|
||||
UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime"`
|
||||
}
|
||||
|
||||
// CheckPassword verifies a password against the stored hash
|
||||
func (u *User) CheckPassword(password string) bool {
|
||||
// Defensive programming - check for nil user
|
||||
if u == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
// Check for empty password or password hash
|
||||
if password == "" || u.PasswordHash == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
err := bcrypt.CompareHashAndPassword([]byte(u.PasswordHash), []byte(password))
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// Vehicle represents a user's vehicle
|
||||
type Vehicle struct {
|
||||
ID uint `json:"id" gorm:"primaryKey;autoIncrement"`
|
||||
UserID uint `json:"user_id" gorm:"not null;index"`
|
||||
User User `json:"-" gorm:"foreignKey:UserID;constraint:OnDelete:CASCADE"`
|
||||
Name string `json:"name" gorm:"not null;size:100"`
|
||||
Make string `json:"make" gorm:"size:50"`
|
||||
Model string `json:"model" gorm:"size:50"`
|
||||
Year int `json:"year" gorm:"default:0"`
|
||||
LicensePlate string `json:"license_plate" gorm:"size:20"`
|
||||
FuelType string `json:"fuel_type" gorm:"size:50"`
|
||||
Notes string `json:"notes" gorm:"type:text"`
|
||||
IsActive bool `json:"is_active" gorm:"default:true"`
|
||||
FuelStops []FuelStop `json:"fuel_stops,omitempty" gorm:"foreignKey:VehicleID;constraint:OnDelete:CASCADE"`
|
||||
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"`
|
||||
UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime"`
|
||||
}
|
||||
|
||||
// FuelStop represents a fuel station stop record
|
||||
type FuelStop struct {
|
||||
ID uint `json:"id" gorm:"primaryKey;autoIncrement"`
|
||||
UserID uint `json:"user_id" gorm:"not null;index"`
|
||||
User User `json:"-" gorm:"foreignKey:UserID;constraint:OnDelete:CASCADE"`
|
||||
VehicleID uint `json:"vehicle_id" gorm:"not null;index"`
|
||||
Vehicle Vehicle `json:"vehicle,omitempty" gorm:"foreignKey:VehicleID;constraint:OnDelete:CASCADE"`
|
||||
Date time.Time `json:"date" gorm:"not null;type:date"`
|
||||
StationName string `json:"station_name" gorm:"not null;size:100"`
|
||||
Location string `json:"location" gorm:"not null;size:255"`
|
||||
FuelType string `json:"fuel_type" gorm:"not null;size:50"`
|
||||
Liters float64 `json:"liters" gorm:"not null;type:decimal(10,3)"`
|
||||
PricePerL float64 `json:"price_per_l" gorm:"not null;type:decimal(10,4)"`
|
||||
TotalPrice float64 `json:"total_price" gorm:"not null;type:decimal(10,2)"`
|
||||
Currency string `json:"currency" gorm:"not null;default:EUR;size:3"`
|
||||
Odometer int `json:"odometer" gorm:"default:0"`
|
||||
TripLength float64 `json:"trip_length" gorm:"default:0;type:decimal(8,2);comment:Distance traveled since last fillup in km"`
|
||||
Notes string `json:"notes" gorm:"type:text"`
|
||||
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"`
|
||||
UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime"`
|
||||
}
|
||||
|
||||
// FuelStopStats represents statistics for fuel consumption
|
||||
type FuelStopStats struct {
|
||||
TotalStops int `json:"total_stops"`
|
||||
TotalLiters float64 `json:"total_liters"`
|
||||
TotalSpent float64 `json:"total_spent"`
|
||||
AveragePrice float64 `json:"average_price"`
|
||||
AverageConsumption float64 `json:"average_consumption"`
|
||||
LastFillup *FuelStop `json:"last_fillup"`
|
||||
}
|
||||
|
||||
// FuelStopForm represents form data for creating/updating fuel stops
|
||||
type FuelStopForm struct {
|
||||
Date string `json:"date" form:"date"`
|
||||
VehicleID uint `json:"vehicle_id" form:"vehicle_id"`
|
||||
StationName string `json:"station_name" form:"station_name"`
|
||||
Location string `json:"location" form:"location"`
|
||||
FuelType string `json:"fuel_type" form:"fuel_type"`
|
||||
Liters float64 `json:"liters" form:"liters"`
|
||||
PricePerL float64 `json:"price_per_l" form:"price_per_l"`
|
||||
TotalPrice float64 `json:"total_price" form:"total_price"`
|
||||
Currency string `json:"currency" form:"currency"`
|
||||
Odometer int `json:"odometer" form:"odometer"`
|
||||
TripLength float64 `json:"trip_length" form:"trip_length"`
|
||||
Notes string `json:"notes" form:"notes"`
|
||||
}
|
||||
|
||||
// UserRegistrationForm represents form data for user registration
|
||||
type UserRegistrationForm struct {
|
||||
Username string `json:"username" form:"username"`
|
||||
Email string `json:"email" form:"email"`
|
||||
Password string `json:"password" form:"password"`
|
||||
ConfirmPassword string `json:"confirm_password" form:"confirm_password"`
|
||||
BaseCurrency string `json:"base_currency" form:"base_currency"`
|
||||
}
|
||||
|
||||
// UserLoginForm represents form data for user login
|
||||
type UserLoginForm struct {
|
||||
Username string `json:"username" form:"username"`
|
||||
Password string `json:"password" form:"password"`
|
||||
}
|
||||
|
||||
// UserSettingsForm represents form data for user settings
|
||||
type UserSettingsForm struct {
|
||||
Email string `json:"email" form:"email"`
|
||||
BaseCurrency string `json:"base_currency" form:"base_currency"`
|
||||
}
|
||||
|
||||
// MonthlyStats represents monthly statistics for fuel consumption
|
||||
type MonthlyStats struct {
|
||||
Month string `json:"month"`
|
||||
Year string `json:"year"`
|
||||
TotalStops int `json:"total_stops"`
|
||||
TotalLiters float64 `json:"total_liters"`
|
||||
TotalSpent float64 `json:"total_spent"`
|
||||
AveragePrice float64 `json:"average_price"`
|
||||
}
|
||||
|
||||
// VehicleForm represents form data for creating/updating vehicles
|
||||
type VehicleForm struct {
|
||||
Name string `json:"name" form:"name"`
|
||||
Make string `json:"make" form:"make"`
|
||||
Model string `json:"model" form:"model"`
|
||||
Year int `json:"year" form:"year"`
|
||||
LicensePlate string `json:"license_plate" form:"license_plate"`
|
||||
FuelType string `json:"fuel_type" form:"fuel_type"`
|
||||
Notes string `json:"notes" form:"notes"`
|
||||
IsActive bool `json:"is_active" form:"is_active"`
|
||||
}
|
||||
|
||||
// ToFuelStop converts a FuelStopForm to a FuelStop
|
||||
func (f *FuelStopForm) ToFuelStop(userID uint) (*FuelStop, error) {
|
||||
date, err := time.Parse("2006-01-02", f.Date)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Use EUR as default currency if not provided
|
||||
currency := f.Currency
|
||||
if currency == "" {
|
||||
currency = "EUR"
|
||||
}
|
||||
|
||||
return &FuelStop{
|
||||
UserID: userID,
|
||||
VehicleID: f.VehicleID,
|
||||
Date: date,
|
||||
StationName: f.StationName,
|
||||
Location: f.Location,
|
||||
FuelType: f.FuelType,
|
||||
Liters: f.Liters,
|
||||
PricePerL: f.PricePerL,
|
||||
TotalPrice: f.TotalPrice,
|
||||
Currency: currency,
|
||||
Odometer: f.Odometer,
|
||||
TripLength: f.TripLength,
|
||||
Notes: f.Notes,
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ToVehicle converts a VehicleForm to a Vehicle
|
||||
func (f *VehicleForm) ToVehicle(userID uint) *Vehicle {
|
||||
return &Vehicle{
|
||||
UserID: userID,
|
||||
Name: f.Name,
|
||||
Make: f.Make,
|
||||
Model: f.Model,
|
||||
Year: f.Year,
|
||||
LicensePlate: f.LicensePlate,
|
||||
FuelType: f.FuelType,
|
||||
Notes: f.Notes,
|
||||
IsActive: f.IsActive,
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
}
|
||||
|
||||
// ToUser converts a UserRegistrationForm to a User
|
||||
func (f *UserRegistrationForm) ToUser() (*User, error) {
|
||||
// Use EUR as default currency if not provided
|
||||
currency := f.BaseCurrency
|
||||
if currency == "" {
|
||||
currency = "EUR"
|
||||
}
|
||||
|
||||
return &User{
|
||||
Username: f.Username,
|
||||
Email: f.Email,
|
||||
Password: f.Password,
|
||||
BaseCurrency: currency,
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,285 @@
|
||||
package components
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"tankstopp/internal/currency"
|
||||
"tankstopp/internal/models"
|
||||
)
|
||||
|
||||
templ FormGroup(label, hint string) {
|
||||
<div class="mb-3">
|
||||
if label != "" {
|
||||
<label class="form-label">
|
||||
{ label }
|
||||
</label>
|
||||
}
|
||||
{ children... }
|
||||
if hint != "" {
|
||||
<div class="form-hint">{ hint }</div>
|
||||
}
|
||||
</div>
|
||||
}
|
||||
|
||||
templ Input(name, inputType, placeholder, value string, required bool) {
|
||||
<input
|
||||
type={ inputType }
|
||||
class="form-control"
|
||||
name={ name }
|
||||
id={ name }
|
||||
placeholder={ placeholder }
|
||||
value={ value }
|
||||
if required {
|
||||
required
|
||||
}
|
||||
/>
|
||||
}
|
||||
|
||||
templ NumberInput(name, placeholder string, value float64, step string, min float64, required bool) {
|
||||
<input
|
||||
type="number"
|
||||
class="form-control"
|
||||
name={ name }
|
||||
id={ name }
|
||||
placeholder={ placeholder }
|
||||
value={ fmt.Sprintf("%.2f", value) }
|
||||
step={ step }
|
||||
min={ fmt.Sprintf("%.2f", min) }
|
||||
if required {
|
||||
required
|
||||
}
|
||||
/>
|
||||
}
|
||||
|
||||
templ DateInput(name, value string, required bool) {
|
||||
<input
|
||||
type="date"
|
||||
class="form-control"
|
||||
name={ name }
|
||||
id={ name }
|
||||
value={ value }
|
||||
if required {
|
||||
required
|
||||
}
|
||||
/>
|
||||
}
|
||||
|
||||
templ TextArea(name, placeholder, value string, rows int) {
|
||||
<textarea
|
||||
class="form-control"
|
||||
name={ name }
|
||||
id={ name }
|
||||
rows={ fmt.Sprintf("%d", rows) }
|
||||
placeholder={ placeholder }
|
||||
>{ value }</textarea>
|
||||
}
|
||||
|
||||
templ Select(name string, required bool) {
|
||||
<select
|
||||
class="form-select"
|
||||
name={ name }
|
||||
id={ name }
|
||||
if required {
|
||||
required
|
||||
}
|
||||
>
|
||||
{ children... }
|
||||
</select>
|
||||
}
|
||||
|
||||
templ Option(value, text string, selected bool) {
|
||||
<option
|
||||
value={ value }
|
||||
if selected {
|
||||
selected
|
||||
}
|
||||
>{ text }</option>
|
||||
}
|
||||
|
||||
templ CurrencySelect(name, selectedCurrency string, currencies []currency.Currency) {
|
||||
@Select(name, false) {
|
||||
@Option("", "Select currency...", selectedCurrency == "")
|
||||
for _, curr := range currencies {
|
||||
@Option(curr.Code, fmt.Sprintf("%s %s - %s", curr.Symbol, curr.Code, curr.Name), curr.Code == selectedCurrency)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
templ VehicleSelect(name string, selectedVehicleID uint, vehicles []models.Vehicle, required bool) {
|
||||
@Select(name, required) {
|
||||
@Option("", "Select vehicle...", selectedVehicleID == 0)
|
||||
for _, vehicle := range vehicles {
|
||||
if vehicle.LicensePlate != "" {
|
||||
@Option(fmt.Sprintf("%d", vehicle.ID), fmt.Sprintf("%s (%s)", vehicle.Name, vehicle.LicensePlate), vehicle.ID == selectedVehicleID)
|
||||
} else {
|
||||
@Option(fmt.Sprintf("%d", vehicle.ID), vehicle.Name, vehicle.ID == selectedVehicleID)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
templ FuelTypeSelect(name, selectedFuelType string, required bool) {
|
||||
@Select(name, required) {
|
||||
@Option("", "Select fuel type...", selectedFuelType == "")
|
||||
@Option("Super E5", "Super E5", selectedFuelType == "Super E5")
|
||||
@Option("Super E10", "Super E10", selectedFuelType == "Super E10")
|
||||
@Option("Super Plus", "Super Plus", selectedFuelType == "Super Plus")
|
||||
@Option("Diesel", "Diesel", selectedFuelType == "Diesel")
|
||||
@Option("Premium Diesel", "Premium Diesel", selectedFuelType == "Premium Diesel")
|
||||
@Option("LPG", "LPG", selectedFuelType == "LPG")
|
||||
@Option("CNG", "CNG", selectedFuelType == "CNG")
|
||||
@Option("Electric", "Electric", selectedFuelType == "Electric")
|
||||
@Option("Hybrid", "Hybrid (Mixed)", selectedFuelType == "Hybrid")
|
||||
}
|
||||
}
|
||||
|
||||
templ InputGroup(prefix, suffix string) {
|
||||
<div class="input-group">
|
||||
if prefix != "" {
|
||||
<span class="input-group-text" id={ prefix + "-addon" }>{ prefix }</span>
|
||||
}
|
||||
{ children... }
|
||||
if suffix != "" {
|
||||
<span class="input-group-text" id={ suffix + "-addon" }>{ suffix }</span>
|
||||
}
|
||||
</div>
|
||||
}
|
||||
|
||||
templ PasswordInput(name, placeholder string, required bool) {
|
||||
<div class="input-group input-group-flat">
|
||||
<input
|
||||
type="password"
|
||||
class="form-control"
|
||||
name={ name }
|
||||
placeholder={ placeholder }
|
||||
autocomplete="off"
|
||||
if required {
|
||||
required
|
||||
}
|
||||
/>
|
||||
<span class="input-group-text">
|
||||
<a href="#" class="link-secondary" onclick="togglePassword(this)" title="Show password" data-target={ name }>
|
||||
@Icon("eye", 24)
|
||||
</a>
|
||||
</span>
|
||||
</div>
|
||||
}
|
||||
|
||||
templ Switch(name, label string, checked bool) {
|
||||
<label class="form-check form-switch">
|
||||
<input
|
||||
class="form-check-input"
|
||||
type="checkbox"
|
||||
name={ name }
|
||||
if checked {
|
||||
checked
|
||||
}
|
||||
/>
|
||||
<span class="form-check-label">{ label }</span>
|
||||
</label>
|
||||
}
|
||||
|
||||
templ FormButtons(cancelHref, submitText, submitIcon string) {
|
||||
<div class="card-footer bg-transparent mt-auto">
|
||||
<div class="btn-list justify-content-end">
|
||||
<a href={ templ.SafeURL(cancelHref) } class="btn">
|
||||
@Icon("arrow-left", 24)
|
||||
Cancel
|
||||
</a>
|
||||
<button type="submit" class="btn btn-primary ms-auto">
|
||||
if submitIcon != "" {
|
||||
@Icon(submitIcon, 24)
|
||||
}
|
||||
{ submitText }
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
|
||||
templ Form(method, action string) {
|
||||
<form method={ method } action={ action }>
|
||||
{ children... }
|
||||
</form>
|
||||
}
|
||||
|
||||
templ FormRow() {
|
||||
<div class="row">
|
||||
{ children... }
|
||||
</div>
|
||||
}
|
||||
|
||||
templ FormCol(size string) {
|
||||
<div class={ "col-md-" + size }>
|
||||
{ children... }
|
||||
</div>
|
||||
}
|
||||
|
||||
templ DeleteButton(action, itemName string) {
|
||||
<form method="POST" action={ action } style="display: inline;" onsubmit="return confirmDelete(this)" data-item={ itemName }>
|
||||
<button type="submit" class="btn btn-sm btn-outline-danger">
|
||||
@Icon("trash", 24)
|
||||
Delete
|
||||
</button>
|
||||
</form>
|
||||
}
|
||||
|
||||
templ EditButton(href string) {
|
||||
<a href={ templ.SafeURL(href) } class="btn btn-sm btn-outline-primary">
|
||||
@Icon("edit", 24)
|
||||
Edit
|
||||
</a>
|
||||
}
|
||||
|
||||
templ ButtonGroup() {
|
||||
<div class="btn-list">
|
||||
{ children... }
|
||||
</div>
|
||||
}
|
||||
|
||||
templ PrimaryButton(text string, icon string) {
|
||||
<button type="submit" class="btn btn-primary">
|
||||
if icon != "" {
|
||||
@Icon(icon, 24)
|
||||
}
|
||||
{ text }
|
||||
</button>
|
||||
}
|
||||
|
||||
templ SecondaryButton(href, text string, icon string) {
|
||||
<a href={ templ.SafeURL(href) } class="btn">
|
||||
if icon != "" {
|
||||
@Icon(icon, 24)
|
||||
}
|
||||
{ text }
|
||||
</a>
|
||||
}
|
||||
|
||||
templ InputWithIcon(name, inputType, placeholder, value string, icon string, required bool) {
|
||||
@FormGroup("", "") {
|
||||
if icon != "" {
|
||||
@Icon(icon, 24)
|
||||
}
|
||||
@Input(name, inputType, placeholder, value, required)
|
||||
}
|
||||
}
|
||||
|
||||
templ CurrencyInputGroup(name string, value float64, currencySymbol string, step string) {
|
||||
@InputGroup(currencySymbol, "") {
|
||||
<input
|
||||
type="number"
|
||||
class="form-control"
|
||||
name={ name }
|
||||
id={ name }
|
||||
step={ step }
|
||||
min="0"
|
||||
value={ fmt.Sprintf("%.2f", value) }
|
||||
placeholder="0.00"
|
||||
required
|
||||
/>
|
||||
}
|
||||
}
|
||||
|
||||
templ RefreshButton() {
|
||||
<button class="btn btn-outline-secondary" type="button">
|
||||
@Icon("refresh", 24)
|
||||
</button>
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,200 @@
|
||||
package components
|
||||
|
||||
import "fmt"
|
||||
|
||||
templ Icon(name string, size int) {
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
class="icon"
|
||||
width={ fmt.Sprintf("%d", size) }
|
||||
height={ fmt.Sprintf("%d", size) }
|
||||
viewBox="0 0 24 24"
|
||||
stroke-width="2"
|
||||
stroke="currentColor"
|
||||
fill="none"
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
>
|
||||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||||
switch name {
|
||||
case "fuel":
|
||||
<path d="M14 11h1a2 2 0 0 1 2 2v2a1 1 0 0 0 1 1h2a1 1 0 0 0 1 -1v-2a6 6 0 0 0 -6 -6h-1m-4 0a2 2 0 0 1 -2 -2v-4a2 2 0 0 1 2 -2h6a2 2 0 0 1 2 2v8a2 2 0 0 1 -2 2h-6a2 2 0 0 1 -2 -2v-6z"></path>
|
||||
case "plus":
|
||||
<line x1="12" y1="5" x2="12" y2="19"></line>
|
||||
<line x1="5" y1="12" x2="19" y2="12"></line>
|
||||
case "home":
|
||||
<polyline points="5,12 3,12 12,3 21,12 19,12"></polyline>
|
||||
<path d="m5 12v7a2 2 0 0 0 2 2h10a2 2 0 0 0 2 -2v-7"></path>
|
||||
<path d="m9 21v-6a2 2 0 0 1 2 -2h2a2 2 0 0 1 2 2v6"></path>
|
||||
case "car":
|
||||
<circle cx="7" cy="17" r="2"></circle>
|
||||
<circle cx="17" cy="17" r="2"></circle>
|
||||
<path d="M5 17h-2v-6l2 -5h9l4 5h1a2 2 0 0 1 2 2v4h-2m-4 0h-6m-6 -6h15m-6 0v-5"></path>
|
||||
case "chart-bar":
|
||||
<path d="M3 12m0 1a1 1 0 0 1 1 -1h4a1 1 0 0 1 1 1v6a1 1 0 0 1 -1 1h-4a1 1 0 0 1 -1 -1z"></path>
|
||||
<path d="M9 8m0 1a1 1 0 0 1 1 -1h4a1 1 0 0 1 1 1v10a1 1 0 0 1 -1 1h-4a1 1 0 0 1 -1 -1z"></path>
|
||||
<path d="M15 4m0 1a1 1 0 0 1 1 -1h4a1 1 0 0 1 1 1v14a1 1 0 0 1 -1 1h-4a1 1 0 0 1 -1 -1z"></path>
|
||||
case "settings":
|
||||
<path d="M10.325 4.317c.426 -1.756 2.924 -1.756 3.35 0a1.724 1.724 0 0 0 2.573 1.066c1.543 -.94 3.31 .826 2.37 2.37a1.724 1.724 0 0 0 1.065 2.572c1.756 .426 1.756 2.924 0 3.35a1.724 1.724 0 0 0 -1.066 2.573c.94 1.543 -.826 3.31 -2.37 2.37a1.724 1.724 0 0 0 -2.572 1.065c-.426 1.756 -2.924 1.756 -3.35 0a1.724 1.724 0 0 0 -2.573 -1.066c-1.543 .94 -3.31 -.826 -2.37 -2.37a1.724 1.724 0 0 0 -1.065 -2.572c-1.756 -.426 -1.756 -2.924 0 -3.35a1.724 1.724 0 0 0 1.066 -2.573c-.94 -1.543 .826 -3.31 2.37 -2.37c1 .608 2.296 .07 2.572 -1.065z"></path>
|
||||
<circle cx="12" cy="12" r="3"></circle>
|
||||
case "logout":
|
||||
<path d="M14 8v-2a2 2 0 0 0 -2 -2h-7a2 2 0 0 0 -2 2v12a2 2 0 0 0 2 2h7a2 2 0 0 0 2 -2v-2"></path>
|
||||
<path d="M20 12h-13l3 -3m0 6l-3 -3"></path>
|
||||
case "check":
|
||||
<path d="M5 12l5 5l10 -10"></path>
|
||||
case "alert-circle":
|
||||
<circle cx="12" cy="12" r="9"></circle>
|
||||
<line x1="12" y1="8" x2="12" y2="12"></line>
|
||||
<line x1="12" y1="16" x2="12.01" y2="16"></line>
|
||||
case "alert-triangle":
|
||||
<path d="M12 9v2m0 4v.01"></path>
|
||||
<path d="M5 19h14a2 2 0 0 0 1.84 -2.75l-7.1 -12.25a2 2 0 0 0 -3.5 0l-7.1 12.25a2 2 0 0 0 1.75 2.75"></path>
|
||||
case "info-circle":
|
||||
<circle cx="12" cy="12" r="9"></circle>
|
||||
<line x1="12" y1="8" x2="12.01" y2="8"></line>
|
||||
<polyline points="11,12 12,12 12,16 13,16"></polyline>
|
||||
case "calendar":
|
||||
<rect x="4" y="5" width="16" height="16" rx="2"></rect>
|
||||
<line x1="16" y1="3" x2="16" y2="7"></line>
|
||||
<line x1="8" y1="3" x2="8" y2="7"></line>
|
||||
<line x1="4" y1="11" x2="20" y2="11"></line>
|
||||
case "location":
|
||||
<circle cx="12" cy="11" r="3"></circle>
|
||||
<path d="M17.657 16.657l-4.243 4.243a2 2 0 0 1 -2.827 0l-4.244 -4.243a8 8 0 1 1 11.314 0z"></path>
|
||||
case "edit":
|
||||
<path d="M7 7h-1a2 2 0 0 0 -2 2v9a2 2 0 0 0 2 2h9a2 2 0 0 0 2 -2v-1"></path>
|
||||
<path d="M20.385 6.585a2.1 2.1 0 0 0 -2.97 -2.97l-8.415 8.385v3h3l8.385 -8.415z"></path>
|
||||
<path d="M16 5l3 3"></path>
|
||||
case "trash":
|
||||
<path d="M4 7l16 0"></path>
|
||||
<path d="M10 11l0 6"></path>
|
||||
<path d="M14 11l0 6"></path>
|
||||
<path d="M5 7l1 12a2 2 0 0 0 2 2h8a2 2 0 0 0 2 -2l1 -12"></path>
|
||||
<path d="M9 7v-3a1 1 0 0 1 1 -1h4a1 1 0 0 1 1 1v3"></path>
|
||||
case "currency":
|
||||
<circle cx="12" cy="12" r="3"></circle>
|
||||
<path d="M3 12h6m6 0h6"></path>
|
||||
case "save":
|
||||
<path d="M6 4h10l4 4v10a2 2 0 0 1 -2 2h-12a2 2 0 0 1 -2 -2v-12a2 2 0 0 1 2 -2"></path>
|
||||
<circle cx="12" cy="14" r="2"></circle>
|
||||
<polyline points="14,4 14,8 8,8 8,4"></polyline>
|
||||
case "arrow-left":
|
||||
<path d="M5 12l14 -7"></path>
|
||||
<path d="M5 12l14 7"></path>
|
||||
case "clock":
|
||||
<circle cx="12" cy="12" r="9"></circle>
|
||||
<polyline points="12,7 12,12 15,15"></polyline>
|
||||
case "gas-station":
|
||||
<path d="M6.8 11a6 6 0 1 0 10.396 0l-.436 -2.183a4 4 0 1 0 -9.564 0l-.396 2.183z"></path>
|
||||
<path d="M6 14h12a2 2 0 0 1 2 2v2a2 2 0 0 1 -2 2h-12a2 2 0 0 1 -2 -2v-2a2 2 0 0 1 2 -2z"></path>
|
||||
case "user":
|
||||
<circle cx="12" cy="7" r="4"></circle>
|
||||
<path d="M6 21v-2a4 4 0 0 1 4 -4h4a4 4 0 0 1 4 4v2"></path>
|
||||
case "lock":
|
||||
<rect x="5" y="11" width="14" height="10" rx="2"></rect>
|
||||
<circle cx="12" cy="16" r="1"></circle>
|
||||
<path d="M8 11v-4a4 4 0 0 1 8 0v4"></path>
|
||||
case "eye":
|
||||
<circle cx="12" cy="12" r="2"></circle>
|
||||
<path d="M22 12c-2.667 4.667 -6 7 -10 7s-7.333 -2.333 -10 -7c2.667 -4.667 6 -7 10 -7s7.333 2.333 10 7"></path>
|
||||
case "eye-off":
|
||||
<line x1="3" y1="3" x2="21" y2="21"></line>
|
||||
<path d="M10.584 10.587a2 2 0 0 0 2.828 2.83"></path>
|
||||
<path d="M9.363 5.365a9.466 9.466 0 0 1 2.637 -.365c4 0 7.333 2.333 10 7c-.778 1.361 -1.612 2.524 -2.503 3.488m-2.14 1.861c-1.631 1.1 -3.415 1.651 -5.357 1.651c-4 0 -7.333 -2.333 -10 -7c1.369 -2.395 2.913 -4.175 4.632 -5.341"></path>
|
||||
case "gauge":
|
||||
<circle cx="12" cy="12" r="9"></circle>
|
||||
<path d="M14.8 9a2 2 0 0 0 -1.8 -1h-2a2 2 0 1 0 0 4h2a2 2 0 1 1 0 4h-2a2 2 0 0 1 -1.8 -1"></path>
|
||||
<path d="M12 6v2m0 8v2"></path>
|
||||
case "notes":
|
||||
<path d="M8 2v4"></path>
|
||||
<path d="M16 2v4"></path>
|
||||
<rect x="3" y="4" width="18" height="18" rx="2"></rect>
|
||||
<path d="M3 10h18"></path>
|
||||
<path d="M8 14h.01"></path>
|
||||
<path d="M12 14h.01"></path>
|
||||
<path d="M16 14h.01"></path>
|
||||
<path d="M8 18h.01"></path>
|
||||
<path d="M12 18h.01"></path>
|
||||
<path d="M16 18h.01"></path>
|
||||
case "refresh":
|
||||
<path d="M20 11a8.1 8.1 0 0 0 -15.5 -2m-.5 -4v4h4"></path>
|
||||
<path d="M4 13a8.1 8.1 0 0 0 15.5 2m.5 4v-4h-4"></path>
|
||||
case "license-plate":
|
||||
<rect x="4" y="4" width="6" height="6" rx="1"></rect>
|
||||
<rect x="4" y="14" width="6" height="6" rx="1"></rect>
|
||||
<rect x="14" y="14" width="6" height="6" rx="1"></rect>
|
||||
<line x1="14" y1="7" x2="20" y2="7"></line>
|
||||
<line x1="17" y1="4" x2="17" y2="10"></line>
|
||||
case "brand":
|
||||
<path d="M9 11l-4 4l4 4m-4 -4h11a4 4 0 0 0 0 -8h-1"></path>
|
||||
case "model":
|
||||
<path d="M12 3l8 4.5l0 9l-8 4.5l-8 -4.5l0 -9z"></path>
|
||||
<path d="M12 12l8 -4.5"></path>
|
||||
<path d="M12 12l0 9"></path>
|
||||
<path d="M12 12l-8 -4.5"></path>
|
||||
case "status":
|
||||
<circle cx="12" cy="12" r="9"></circle>
|
||||
<polyline points="9,11 12,8 15,11"></polyline>
|
||||
<line x1="12" y1="8" x2="12" y2="16"></line>
|
||||
case "trip":
|
||||
<path d="M3 12a9 9 0 1 0 18 0a9 9 0 0 0 -18 0"></path>
|
||||
<path d="M12 7v5l3 3"></path>
|
||||
case "database":
|
||||
<ellipse cx="12" cy="5" rx="9" ry="3"></ellipse>
|
||||
<path d="M3 5v14a9 3 0 0 0 18 0v-14"></path>
|
||||
<path d="M3 12a9 3 0 0 0 18 0"></path>
|
||||
case "download":
|
||||
<path d="M4 17v2a2 2 0 0 0 2 2h12a2 2 0 0 0 2 -2v-2"></path>
|
||||
<polyline points="7,11 12,16 17,11"></polyline>
|
||||
<line x1="12" y1="2" x2="12" y2="16"></line>
|
||||
case "upload":
|
||||
<path d="M4 17v2a2 2 0 0 0 2 2h12a2 2 0 0 0 2 -2v-2"></path>
|
||||
<polyline points="7,9 12,4 17,9"></polyline>
|
||||
<line x1="12" y1="4" x2="12" y2="16"></line>
|
||||
case "zap":
|
||||
<polygon points="13,2 3,14 12,14 11,22 21,10 12,10"></polygon>
|
||||
case "search":
|
||||
<circle cx="11" cy="11" r="8"></circle>
|
||||
<line x1="21" y1="21" x2="16.65" y2="16.65"></line>
|
||||
case "dots-vertical":
|
||||
<circle cx="12" cy="12" r="1"></circle>
|
||||
<circle cx="12" cy="5" r="1"></circle>
|
||||
<circle cx="12" cy="19" r="1"></circle>
|
||||
case "award":
|
||||
<circle cx="12" cy="8" r="7"></circle>
|
||||
<polyline points="8.21,13.89 7,23 12,20 17,23 15.79,13.88"></polyline>
|
||||
default:
|
||||
<circle cx="12" cy="12" r="10"></circle>
|
||||
}
|
||||
</svg>
|
||||
}
|
||||
|
||||
templ IconWithClass(name string, size int, class string) {
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
class={ "icon", class }
|
||||
width={ fmt.Sprintf("%d", size) }
|
||||
height={ fmt.Sprintf("%d", size) }
|
||||
viewBox="0 0 24 24"
|
||||
stroke-width="2"
|
||||
stroke="currentColor"
|
||||
fill="none"
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
>
|
||||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||||
@renderIconPath(name)
|
||||
</svg>
|
||||
}
|
||||
|
||||
templ renderIconPath(name string) {
|
||||
switch name {
|
||||
case "fuel":
|
||||
<path d="M14 11h1a2 2 0 0 1 2 2v2a1 1 0 0 0 1 1h2a1 1 0 0 0 1 -1v-2a6 6 0 0 0 -6 -6h-1m-4 0a2 2 0 0 1 -2 -2v-4a2 2 0 0 1 2 -2h6a2 2 0 0 1 2 2v8a2 2 0 0 1 -2 2h-6a2 2 0 0 1 -2 -2v-6z"></path>
|
||||
case "plus":
|
||||
<line x1="12" y1="5" x2="12" y2="19"></line>
|
||||
<line x1="5" y1="12" x2="19" y2="12"></line>
|
||||
default:
|
||||
<circle cx="12" cy="12" r="10"></circle>
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,397 @@
|
||||
// Code generated by templ - DO NOT EDIT.
|
||||
|
||||
// templ: version: v0.3.906
|
||||
package components
|
||||
|
||||
//lint:file-ignore SA4006 This context is only used if a nested component is present.
|
||||
|
||||
import "github.com/a-h/templ"
|
||||
import templruntime "github.com/a-h/templ/runtime"
|
||||
|
||||
import "fmt"
|
||||
|
||||
func Icon(name string, size int) templ.Component {
|
||||
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
|
||||
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
|
||||
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
|
||||
return templ_7745c5c3_CtxErr
|
||||
}
|
||||
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
|
||||
if !templ_7745c5c3_IsBuffer {
|
||||
defer func() {
|
||||
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err == nil {
|
||||
templ_7745c5c3_Err = templ_7745c5c3_BufErr
|
||||
}
|
||||
}()
|
||||
}
|
||||
ctx = templ.InitializeContext(ctx)
|
||||
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
|
||||
if templ_7745c5c3_Var1 == nil {
|
||||
templ_7745c5c3_Var1 = templ.NopComponent
|
||||
}
|
||||
ctx = templ.ClearChildren(ctx)
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<svg xmlns=\"http://www.w3.org/2000/svg\" class=\"icon\" width=\"")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
var templ_7745c5c3_Var2 string
|
||||
templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(fmt.Sprintf("%d", size))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/views/components/icons.templ`, Line: 9, Col: 33}
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "\" height=\"")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
var templ_7745c5c3_Var3 string
|
||||
templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(fmt.Sprintf("%d", size))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/views/components/icons.templ`, Line: 10, Col: 34}
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "\" viewBox=\"0 0 24 24\" stroke-width=\"2\" stroke=\"currentColor\" fill=\"none\" stroke-linecap=\"round\" stroke-linejoin=\"round\"><path stroke=\"none\" d=\"M0 0h24v24H0z\" fill=\"none\"></path> ")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
switch name {
|
||||
case "fuel":
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 4, "<path d=\"M14 11h1a2 2 0 0 1 2 2v2a1 1 0 0 0 1 1h2a1 1 0 0 0 1 -1v-2a6 6 0 0 0 -6 -6h-1m-4 0a2 2 0 0 1 -2 -2v-4a2 2 0 0 1 2 -2h6a2 2 0 0 1 2 2v8a2 2 0 0 1 -2 2h-6a2 2 0 0 1 -2 -2v-6z\"></path>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
case "plus":
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 5, "<line x1=\"12\" y1=\"5\" x2=\"12\" y2=\"19\"></line> <line x1=\"5\" y1=\"12\" x2=\"19\" y2=\"12\"></line>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
case "home":
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 6, "<polyline points=\"5,12 3,12 12,3 21,12 19,12\"></polyline> <path d=\"m5 12v7a2 2 0 0 0 2 2h10a2 2 0 0 0 2 -2v-7\"></path> <path d=\"m9 21v-6a2 2 0 0 1 2 -2h2a2 2 0 0 1 2 2v6\"></path>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
case "car":
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 7, "<circle cx=\"7\" cy=\"17\" r=\"2\"></circle> <circle cx=\"17\" cy=\"17\" r=\"2\"></circle> <path d=\"M5 17h-2v-6l2 -5h9l4 5h1a2 2 0 0 1 2 2v4h-2m-4 0h-6m-6 -6h15m-6 0v-5\"></path>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
case "chart-bar":
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 8, "<path d=\"M3 12m0 1a1 1 0 0 1 1 -1h4a1 1 0 0 1 1 1v6a1 1 0 0 1 -1 1h-4a1 1 0 0 1 -1 -1z\"></path> <path d=\"M9 8m0 1a1 1 0 0 1 1 -1h4a1 1 0 0 1 1 1v10a1 1 0 0 1 -1 1h-4a1 1 0 0 1 -1 -1z\"></path> <path d=\"M15 4m0 1a1 1 0 0 1 1 -1h4a1 1 0 0 1 1 1v14a1 1 0 0 1 -1 1h-4a1 1 0 0 1 -1 -1z\"></path>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
case "settings":
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 9, "<path d=\"M10.325 4.317c.426 -1.756 2.924 -1.756 3.35 0a1.724 1.724 0 0 0 2.573 1.066c1.543 -.94 3.31 .826 2.37 2.37a1.724 1.724 0 0 0 1.065 2.572c1.756 .426 1.756 2.924 0 3.35a1.724 1.724 0 0 0 -1.066 2.573c.94 1.543 -.826 3.31 -2.37 2.37a1.724 1.724 0 0 0 -2.572 1.065c-.426 1.756 -2.924 1.756 -3.35 0a1.724 1.724 0 0 0 -2.573 -1.066c-1.543 .94 -3.31 -.826 -2.37 -2.37a1.724 1.724 0 0 0 -1.065 -2.572c-1.756 -.426 -1.756 -2.924 0 -3.35a1.724 1.724 0 0 0 1.066 -2.573c-.94 -1.543 .826 -3.31 2.37 -2.37c1 .608 2.296 .07 2.572 -1.065z\"></path> <circle cx=\"12\" cy=\"12\" r=\"3\"></circle>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
case "logout":
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 10, "<path d=\"M14 8v-2a2 2 0 0 0 -2 -2h-7a2 2 0 0 0 -2 2v12a2 2 0 0 0 2 2h7a2 2 0 0 0 2 -2v-2\"></path> <path d=\"M20 12h-13l3 -3m0 6l-3 -3\"></path>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
case "check":
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 11, "<path d=\"M5 12l5 5l10 -10\"></path>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
case "alert-circle":
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 12, "<circle cx=\"12\" cy=\"12\" r=\"9\"></circle> <line x1=\"12\" y1=\"8\" x2=\"12\" y2=\"12\"></line> <line x1=\"12\" y1=\"16\" x2=\"12.01\" y2=\"16\"></line>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
case "alert-triangle":
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 13, "<path d=\"M12 9v2m0 4v.01\"></path> <path d=\"M5 19h14a2 2 0 0 0 1.84 -2.75l-7.1 -12.25a2 2 0 0 0 -3.5 0l-7.1 12.25a2 2 0 0 0 1.75 2.75\"></path>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
case "info-circle":
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 14, "<circle cx=\"12\" cy=\"12\" r=\"9\"></circle> <line x1=\"12\" y1=\"8\" x2=\"12.01\" y2=\"8\"></line> <polyline points=\"11,12 12,12 12,16 13,16\"></polyline>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
case "calendar":
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 15, "<rect x=\"4\" y=\"5\" width=\"16\" height=\"16\" rx=\"2\"></rect> <line x1=\"16\" y1=\"3\" x2=\"16\" y2=\"7\"></line> <line x1=\"8\" y1=\"3\" x2=\"8\" y2=\"7\"></line> <line x1=\"4\" y1=\"11\" x2=\"20\" y2=\"11\"></line>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
case "location":
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 16, "<circle cx=\"12\" cy=\"11\" r=\"3\"></circle> <path d=\"M17.657 16.657l-4.243 4.243a2 2 0 0 1 -2.827 0l-4.244 -4.243a8 8 0 1 1 11.314 0z\"></path>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
case "edit":
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 17, "<path d=\"M7 7h-1a2 2 0 0 0 -2 2v9a2 2 0 0 0 2 2h9a2 2 0 0 0 2 -2v-1\"></path> <path d=\"M20.385 6.585a2.1 2.1 0 0 0 -2.97 -2.97l-8.415 8.385v3h3l8.385 -8.415z\"></path> <path d=\"M16 5l3 3\"></path>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
case "trash":
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 18, "<path d=\"M4 7l16 0\"></path> <path d=\"M10 11l0 6\"></path> <path d=\"M14 11l0 6\"></path> <path d=\"M5 7l1 12a2 2 0 0 0 2 2h8a2 2 0 0 0 2 -2l1 -12\"></path> <path d=\"M9 7v-3a1 1 0 0 1 1 -1h4a1 1 0 0 1 1 1v3\"></path>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
case "currency":
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 19, "<circle cx=\"12\" cy=\"12\" r=\"3\"></circle> <path d=\"M3 12h6m6 0h6\"></path>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
case "save":
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 20, "<path d=\"M6 4h10l4 4v10a2 2 0 0 1 -2 2h-12a2 2 0 0 1 -2 -2v-12a2 2 0 0 1 2 -2\"></path> <circle cx=\"12\" cy=\"14\" r=\"2\"></circle> <polyline points=\"14,4 14,8 8,8 8,4\"></polyline>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
case "arrow-left":
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 21, "<path d=\"M5 12l14 -7\"></path> <path d=\"M5 12l14 7\"></path>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
case "clock":
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 22, "<circle cx=\"12\" cy=\"12\" r=\"9\"></circle> <polyline points=\"12,7 12,12 15,15\"></polyline>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
case "gas-station":
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 23, "<path d=\"M6.8 11a6 6 0 1 0 10.396 0l-.436 -2.183a4 4 0 1 0 -9.564 0l-.396 2.183z\"></path> <path d=\"M6 14h12a2 2 0 0 1 2 2v2a2 2 0 0 1 -2 2h-12a2 2 0 0 1 -2 -2v-2a2 2 0 0 1 2 -2z\"></path>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
case "user":
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 24, "<circle cx=\"12\" cy=\"7\" r=\"4\"></circle> <path d=\"M6 21v-2a4 4 0 0 1 4 -4h4a4 4 0 0 1 4 4v2\"></path>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
case "lock":
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 25, "<rect x=\"5\" y=\"11\" width=\"14\" height=\"10\" rx=\"2\"></rect> <circle cx=\"12\" cy=\"16\" r=\"1\"></circle> <path d=\"M8 11v-4a4 4 0 0 1 8 0v4\"></path>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
case "eye":
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 26, "<circle cx=\"12\" cy=\"12\" r=\"2\"></circle> <path d=\"M22 12c-2.667 4.667 -6 7 -10 7s-7.333 -2.333 -10 -7c2.667 -4.667 6 -7 10 -7s7.333 2.333 10 7\"></path>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
case "eye-off":
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 27, "<line x1=\"3\" y1=\"3\" x2=\"21\" y2=\"21\"></line> <path d=\"M10.584 10.587a2 2 0 0 0 2.828 2.83\"></path> <path d=\"M9.363 5.365a9.466 9.466 0 0 1 2.637 -.365c4 0 7.333 2.333 10 7c-.778 1.361 -1.612 2.524 -2.503 3.488m-2.14 1.861c-1.631 1.1 -3.415 1.651 -5.357 1.651c-4 0 -7.333 -2.333 -10 -7c1.369 -2.395 2.913 -4.175 4.632 -5.341\"></path>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
case "gauge":
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 28, "<circle cx=\"12\" cy=\"12\" r=\"9\"></circle> <path d=\"M14.8 9a2 2 0 0 0 -1.8 -1h-2a2 2 0 1 0 0 4h2a2 2 0 1 1 0 4h-2a2 2 0 0 1 -1.8 -1\"></path> <path d=\"M12 6v2m0 8v2\"></path>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
case "notes":
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 29, "<path d=\"M8 2v4\"></path> <path d=\"M16 2v4\"></path> <rect x=\"3\" y=\"4\" width=\"18\" height=\"18\" rx=\"2\"></rect> <path d=\"M3 10h18\"></path> <path d=\"M8 14h.01\"></path> <path d=\"M12 14h.01\"></path> <path d=\"M16 14h.01\"></path> <path d=\"M8 18h.01\"></path> <path d=\"M12 18h.01\"></path> <path d=\"M16 18h.01\"></path>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
case "refresh":
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 30, "<path d=\"M20 11a8.1 8.1 0 0 0 -15.5 -2m-.5 -4v4h4\"></path> <path d=\"M4 13a8.1 8.1 0 0 0 15.5 2m.5 4v-4h-4\"></path>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
case "license-plate":
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 31, "<rect x=\"4\" y=\"4\" width=\"6\" height=\"6\" rx=\"1\"></rect> <rect x=\"4\" y=\"14\" width=\"6\" height=\"6\" rx=\"1\"></rect> <rect x=\"14\" y=\"14\" width=\"6\" height=\"6\" rx=\"1\"></rect> <line x1=\"14\" y1=\"7\" x2=\"20\" y2=\"7\"></line> <line x1=\"17\" y1=\"4\" x2=\"17\" y2=\"10\"></line>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
case "brand":
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 32, "<path d=\"M9 11l-4 4l4 4m-4 -4h11a4 4 0 0 0 0 -8h-1\"></path>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
case "model":
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 33, "<path d=\"M12 3l8 4.5l0 9l-8 4.5l-8 -4.5l0 -9z\"></path> <path d=\"M12 12l8 -4.5\"></path> <path d=\"M12 12l0 9\"></path> <path d=\"M12 12l-8 -4.5\"></path>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
case "status":
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 34, "<circle cx=\"12\" cy=\"12\" r=\"9\"></circle> <polyline points=\"9,11 12,8 15,11\"></polyline> <line x1=\"12\" y1=\"8\" x2=\"12\" y2=\"16\"></line>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
case "trip":
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 35, "<path d=\"M3 12a9 9 0 1 0 18 0a9 9 0 0 0 -18 0\"></path> <path d=\"M12 7v5l3 3\"></path>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
case "database":
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 36, "<ellipse cx=\"12\" cy=\"5\" rx=\"9\" ry=\"3\"></ellipse> <path d=\"M3 5v14a9 3 0 0 0 18 0v-14\"></path> <path d=\"M3 12a9 3 0 0 0 18 0\"></path>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
case "download":
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 37, "<path d=\"M4 17v2a2 2 0 0 0 2 2h12a2 2 0 0 0 2 -2v-2\"></path> <polyline points=\"7,11 12,16 17,11\"></polyline> <line x1=\"12\" y1=\"2\" x2=\"12\" y2=\"16\"></line>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
case "upload":
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 38, "<path d=\"M4 17v2a2 2 0 0 0 2 2h12a2 2 0 0 0 2 -2v-2\"></path> <polyline points=\"7,9 12,4 17,9\"></polyline> <line x1=\"12\" y1=\"4\" x2=\"12\" y2=\"16\"></line>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
case "zap":
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 39, "<polygon points=\"13,2 3,14 12,14 11,22 21,10 12,10\"></polygon>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
case "search":
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 40, "<circle cx=\"11\" cy=\"11\" r=\"8\"></circle> <line x1=\"21\" y1=\"21\" x2=\"16.65\" y2=\"16.65\"></line>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
case "dots-vertical":
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 41, "<circle cx=\"12\" cy=\"12\" r=\"1\"></circle> <circle cx=\"12\" cy=\"5\" r=\"1\"></circle> <circle cx=\"12\" cy=\"19\" r=\"1\"></circle>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
case "award":
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 42, "<circle cx=\"12\" cy=\"8\" r=\"7\"></circle> <polyline points=\"8.21,13.89 7,23 12,20 17,23 15.79,13.88\"></polyline>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
default:
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 43, "<circle cx=\"12\" cy=\"12\" r=\"10\"></circle>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 44, "</svg>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func IconWithClass(name string, size int, class string) templ.Component {
|
||||
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
|
||||
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
|
||||
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
|
||||
return templ_7745c5c3_CtxErr
|
||||
}
|
||||
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
|
||||
if !templ_7745c5c3_IsBuffer {
|
||||
defer func() {
|
||||
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err == nil {
|
||||
templ_7745c5c3_Err = templ_7745c5c3_BufErr
|
||||
}
|
||||
}()
|
||||
}
|
||||
ctx = templ.InitializeContext(ctx)
|
||||
templ_7745c5c3_Var4 := templ.GetChildren(ctx)
|
||||
if templ_7745c5c3_Var4 == nil {
|
||||
templ_7745c5c3_Var4 = templ.NopComponent
|
||||
}
|
||||
ctx = templ.ClearChildren(ctx)
|
||||
var templ_7745c5c3_Var5 = []any{"icon", class}
|
||||
templ_7745c5c3_Err = templ.RenderCSSItems(ctx, templ_7745c5c3_Buffer, templ_7745c5c3_Var5...)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 45, "<svg xmlns=\"http://www.w3.org/2000/svg\" class=\"")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
var templ_7745c5c3_Var6 string
|
||||
templ_7745c5c3_Var6, templ_7745c5c3_Err = templ.JoinStringErrs(templ.CSSClasses(templ_7745c5c3_Var5).String())
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/views/components/icons.templ`, Line: 1, Col: 0}
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var6))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 46, "\" width=\"")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
var templ_7745c5c3_Var7 string
|
||||
templ_7745c5c3_Var7, templ_7745c5c3_Err = templ.JoinStringErrs(fmt.Sprintf("%d", size))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/views/components/icons.templ`, Line: 176, Col: 33}
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var7))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 47, "\" height=\"")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
var templ_7745c5c3_Var8 string
|
||||
templ_7745c5c3_Var8, templ_7745c5c3_Err = templ.JoinStringErrs(fmt.Sprintf("%d", size))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/views/components/icons.templ`, Line: 177, Col: 34}
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var8))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 48, "\" viewBox=\"0 0 24 24\" stroke-width=\"2\" stroke=\"currentColor\" fill=\"none\" stroke-linecap=\"round\" stroke-linejoin=\"round\"><path stroke=\"none\" d=\"M0 0h24v24H0z\" fill=\"none\"></path>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = renderIconPath(name).Render(ctx, templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 49, "</svg>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func renderIconPath(name string) templ.Component {
|
||||
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
|
||||
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
|
||||
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
|
||||
return templ_7745c5c3_CtxErr
|
||||
}
|
||||
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
|
||||
if !templ_7745c5c3_IsBuffer {
|
||||
defer func() {
|
||||
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err == nil {
|
||||
templ_7745c5c3_Err = templ_7745c5c3_BufErr
|
||||
}
|
||||
}()
|
||||
}
|
||||
ctx = templ.InitializeContext(ctx)
|
||||
templ_7745c5c3_Var9 := templ.GetChildren(ctx)
|
||||
if templ_7745c5c3_Var9 == nil {
|
||||
templ_7745c5c3_Var9 = templ.NopComponent
|
||||
}
|
||||
ctx = templ.ClearChildren(ctx)
|
||||
switch name {
|
||||
case "fuel":
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 50, "<path d=\"M14 11h1a2 2 0 0 1 2 2v2a1 1 0 0 0 1 1h2a1 1 0 0 0 1 -1v-2a6 6 0 0 0 -6 -6h-1m-4 0a2 2 0 0 1 -2 -2v-4a2 2 0 0 1 2 -2h6a2 2 0 0 1 2 2v8a2 2 0 0 1 -2 2h-6a2 2 0 0 1 -2 -2v-6z\"></path>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
case "plus":
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 51, "<line x1=\"12\" y1=\"5\" x2=\"12\" y2=\"19\"></line> <line x1=\"5\" y1=\"12\" x2=\"19\" y2=\"12\"></line>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
default:
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 52, "<circle cx=\"12\" cy=\"12\" r=\"10\"></circle>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
var _ = templruntime.GeneratedTemplate
|
||||
@@ -0,0 +1,405 @@
|
||||
package components
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"tankstopp/internal/models"
|
||||
)
|
||||
|
||||
templ BaseLayout(title string, user *models.User, username string) {
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8"/>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
|
||||
<title>{ title } - TankStopp</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/@tabler/core@1.0.0-beta17/dist/css/tabler.min.css" rel="stylesheet"/>
|
||||
<link href="https://cdn.jsdelivr.net/npm/@tabler/icons@latest/icons-sprite.svg" rel="stylesheet"/>
|
||||
<link href="/static/style.css" rel="stylesheet"/>
|
||||
</head>
|
||||
<body>
|
||||
<div class="page">
|
||||
@Navbar(user, username)
|
||||
<div class="page-wrapper">
|
||||
{ children... }
|
||||
@Footer()
|
||||
</div>
|
||||
</div>
|
||||
<script src="https://cdn.jsdelivr.net/npm/@tabler/core@1.0.0-beta17/dist/js/tabler.min.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
}
|
||||
|
||||
templ Navbar(user *models.User, username string) {
|
||||
<header class="navbar navbar-expand-md navbar-light d-print-none">
|
||||
<div class="container-xl">
|
||||
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbar-menu">
|
||||
<span class="navbar-toggler-icon"></span>
|
||||
</button>
|
||||
<h1 class="navbar-brand navbar-brand-autodark d-none-navbar-horizontal pe-0 pe-md-3">
|
||||
<a href="/dashboard" class="text-decoration-none">
|
||||
@Icon("fuel", 24)
|
||||
TankStopp
|
||||
</a>
|
||||
</h1>
|
||||
<div class="navbar-nav flex-row order-md-last">
|
||||
if user != nil {
|
||||
<a href="/add" class="btn btn-primary me-2">
|
||||
@Icon("plus", 24)
|
||||
Add Stop
|
||||
</a>
|
||||
@UserDropdown(user, username)
|
||||
}
|
||||
</div>
|
||||
<div class="collapse navbar-collapse" id="navbar-menu">
|
||||
<div class="d-flex flex-column flex-md-row flex-fill align-items-stretch align-items-md-center">
|
||||
<ul class="navbar-nav">
|
||||
@NavItem("/dashboard", "home", "Dashboard", false)
|
||||
@NavItem("/vehicles", "car", "Vehicles", false)
|
||||
@NavItem("/api/stats", "chart-bar", "API", false)
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</header>
|
||||
}
|
||||
|
||||
templ NavItem(href, icon, title string, active bool) {
|
||||
<li class={ "nav-item", templ.KV("active", active) }>
|
||||
<a class="nav-link" href={ templ.SafeURL(href) }>
|
||||
<span class="nav-link-icon d-md-none d-lg-inline-block">
|
||||
@Icon(icon, 24)
|
||||
</span>
|
||||
<span class="nav-link-title">{ title }</span>
|
||||
</a>
|
||||
</li>
|
||||
}
|
||||
|
||||
templ UserDropdown(user *models.User, username string) {
|
||||
<div class="nav-item dropdown">
|
||||
<a href="#" class="nav-link d-flex lh-1 text-reset p-0" data-bs-toggle="dropdown" aria-label="Open user menu">
|
||||
<span class="avatar avatar-sm" style="background: var(--tblr-primary); text-transform: uppercase;">
|
||||
if username != "" {
|
||||
{ string(username[0]) }
|
||||
} else {
|
||||
{ "U" }
|
||||
}
|
||||
</span>
|
||||
<div class="d-none d-xl-block ps-2">
|
||||
<div>{ username }</div>
|
||||
<div class="mt-1 small text-muted">
|
||||
{ user.BaseCurrency }
|
||||
</div>
|
||||
</div>
|
||||
</a>
|
||||
<div class="dropdown-menu dropdown-menu-end dropdown-menu-arrow">
|
||||
<div class="dropdown-item">
|
||||
<div class="text-muted">Signed in as</div>
|
||||
<strong>{ username }</strong>
|
||||
</div>
|
||||
<div class="dropdown-divider"></div>
|
||||
<a href="/settings" class="dropdown-item">
|
||||
@Icon("settings", 24)
|
||||
Settings
|
||||
</a>
|
||||
<div class="dropdown-divider"></div>
|
||||
<form method="POST" action="/logout" class="d-inline">
|
||||
<button type="submit" class="dropdown-item text-danger">
|
||||
@Icon("logout", 24)
|
||||
Logout
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
|
||||
templ Footer() {
|
||||
<footer class="footer footer-transparent d-print-none">
|
||||
<div class="container-xl">
|
||||
<div class="row text-center align-items-center flex-row-reverse">
|
||||
<div class="col-lg-auto ms-lg-auto">
|
||||
<ul class="list-inline list-inline-dots mb-0">
|
||||
<li class="list-inline-item">
|
||||
<a href="https://github.com/tabler/tabler" class="link-secondary">Built with Tabler</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
<div class="col-12 col-lg-auto mt-3 mt-lg-0">
|
||||
<ul class="list-inline list-inline-dots mb-0">
|
||||
<li class="list-inline-item">
|
||||
Copyright © 2024 TankStopp - Fuel Tracking App
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</footer>
|
||||
}
|
||||
|
||||
templ PageHeader(pretitle, title string) {
|
||||
<div class="page-header d-print-none">
|
||||
<div class="container-xl">
|
||||
<div class="row g-2 align-items-center">
|
||||
<div class="col">
|
||||
if pretitle != "" {
|
||||
<div class="page-pretitle">{ pretitle }</div>
|
||||
}
|
||||
<h2 class="page-title">{ title }</h2>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
|
||||
templ Alert(alertType, message string) {
|
||||
<div class={ "alert", "alert-" + alertType, "alert-dismissible" } role="alert">
|
||||
<div class="d-flex">
|
||||
<div>
|
||||
switch alertType {
|
||||
case "success":
|
||||
@Icon("check", 24)
|
||||
case "danger":
|
||||
@Icon("alert-circle", 24)
|
||||
case "warning":
|
||||
@Icon("alert-triangle", 24)
|
||||
case "info":
|
||||
@Icon("info-circle", 24)
|
||||
}
|
||||
</div>
|
||||
<div>{ message }</div>
|
||||
</div>
|
||||
<a class="btn-close" data-bs-dismiss="alert" aria-label="close"></a>
|
||||
</div>
|
||||
}
|
||||
|
||||
templ Card(title string, icon string) {
|
||||
<div class="card">
|
||||
if title != "" {
|
||||
<div class="card-header">
|
||||
<h3 class="card-title">
|
||||
if icon != "" {
|
||||
@Icon(icon, 24)
|
||||
}
|
||||
{ title }
|
||||
</h3>
|
||||
</div>
|
||||
}
|
||||
<div class="card-body">
|
||||
{ children... }
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
|
||||
templ EmptyState(icon, title, subtitle, actionText, actionHref string) {
|
||||
<div class="empty">
|
||||
<div class="empty-img">
|
||||
@Icon(icon, 128)
|
||||
</div>
|
||||
<p class="empty-title">{ title }</p>
|
||||
<p class="empty-subtitle text-muted">{ subtitle }</p>
|
||||
if actionText != "" && actionHref != "" {
|
||||
<div class="empty-action">
|
||||
<a href={ templ.SafeURL(actionHref) } class="btn btn-primary">
|
||||
@Icon("plus", 24)
|
||||
{ actionText }
|
||||
</a>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
}
|
||||
|
||||
templ LoadingSpinner(size string) {
|
||||
<div class={ "spinner-border", "spinner-border-" + size } role="status">
|
||||
<span class="visually-hidden">Loading...</span>
|
||||
</div>
|
||||
}
|
||||
|
||||
templ Badge(text, variant string) {
|
||||
<span class={ "badge", "bg-" + variant }>{ text }</span>
|
||||
}
|
||||
|
||||
templ ProgressBar(percentage int, variant string) {
|
||||
<div class="progress">
|
||||
<div class={ "progress-bar", "bg-" + variant } role="progressbar" style={ fmt.Sprintf("width: %d%%", percentage) }>
|
||||
{ fmt.Sprintf("%d%%", percentage) }
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
|
||||
templ Modal(id, title string) {
|
||||
<div class="modal fade" id={ id } tabindex="-1" aria-labelledby={ id + "Label" } aria-hidden="true">
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<h5 class="modal-title" id={ id + "Label" }>{ title }</h5>
|
||||
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
{ children... }
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
{ children... }
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
|
||||
templ Tooltip(text string) {
|
||||
<span data-bs-toggle="tooltip" data-bs-placement="top" title={ text }>
|
||||
{ children... }
|
||||
</span>
|
||||
}
|
||||
|
||||
templ Breadcrumb(items []BreadcrumbItem) {
|
||||
<nav aria-label="breadcrumb">
|
||||
<ol class="breadcrumb">
|
||||
for i, item := range items {
|
||||
if i == len(items)-1 {
|
||||
<li class="breadcrumb-item active" aria-current="page">{ item.Title }</li>
|
||||
} else {
|
||||
<li class="breadcrumb-item">
|
||||
<a href={ templ.SafeURL(item.Href) }>{ item.Title }</a>
|
||||
</li>
|
||||
}
|
||||
}
|
||||
</ol>
|
||||
</nav>
|
||||
}
|
||||
|
||||
type BreadcrumbItem struct {
|
||||
Title string
|
||||
Href string
|
||||
}
|
||||
|
||||
templ Tabs(activeTab string, tabs []TabItem) {
|
||||
<ul class="nav nav-tabs" role="tablist">
|
||||
for _, tab := range tabs {
|
||||
<li class="nav-item" role="presentation">
|
||||
<a
|
||||
class={ "nav-link", templ.KV("active", tab.ID == activeTab) }
|
||||
href={ templ.SafeURL(tab.Href) }
|
||||
role="tab"
|
||||
>
|
||||
if tab.Icon != "" {
|
||||
@Icon(tab.Icon, 24)
|
||||
}
|
||||
{ tab.Title }
|
||||
</a>
|
||||
</li>
|
||||
}
|
||||
</ul>
|
||||
}
|
||||
|
||||
type TabItem struct {
|
||||
ID string
|
||||
Title string
|
||||
Href string
|
||||
Icon string
|
||||
}
|
||||
|
||||
templ StatCard(title, value, subtitle, icon, variant string) {
|
||||
<div class="card">
|
||||
<div class="card-body">
|
||||
<div class="d-flex align-items-center">
|
||||
<div class="flex-fill">
|
||||
<div class="subheader">{ title }</div>
|
||||
<div class="h2 mb-0">{ value }</div>
|
||||
if subtitle != "" {
|
||||
<div class="text-muted">{ subtitle }</div>
|
||||
}
|
||||
</div>
|
||||
<div class="ms-auto">
|
||||
<div class={ "text-" + variant }>
|
||||
@Icon(icon, 32)
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
|
||||
templ ActionButton(href, text, icon, variant string) {
|
||||
<a href={ templ.SafeURL(href) } class={ "btn", "btn-" + variant }>
|
||||
if icon != "" {
|
||||
@Icon(icon, 24)
|
||||
}
|
||||
{ text }
|
||||
</a>
|
||||
}
|
||||
|
||||
templ TableResponsive() {
|
||||
<div class="table-responsive">
|
||||
<table class="table table-vcenter table-mobile-md card-table">
|
||||
{ children... }
|
||||
</table>
|
||||
</div>
|
||||
}
|
||||
|
||||
templ Pagination(currentPage, totalPages int, baseURL string) {
|
||||
if totalPages > 1 {
|
||||
<nav aria-label="Page navigation">
|
||||
<ul class="pagination">
|
||||
if currentPage > 1 {
|
||||
<li class="page-item">
|
||||
<a class="page-link" href={ templ.SafeURL(fmt.Sprintf("%s?page=%d", baseURL, currentPage-1)) }>Previous</a>
|
||||
</li>
|
||||
}
|
||||
for i := 1; i <= totalPages; i++ {
|
||||
<li class={ "page-item", templ.KV("active", i == currentPage) }>
|
||||
<a class="page-link" href={ templ.SafeURL(fmt.Sprintf("%s?page=%d", baseURL, i)) }>{ fmt.Sprintf("%d", i) }</a>
|
||||
</li>
|
||||
}
|
||||
if currentPage < totalPages {
|
||||
<li class="page-item">
|
||||
<a class="page-link" href={ templ.SafeURL(fmt.Sprintf("%s?page=%d", baseURL, currentPage+1)) }>Next</a>
|
||||
</li>
|
||||
}
|
||||
</ul>
|
||||
</nav>
|
||||
}
|
||||
}
|
||||
|
||||
templ ConfirmDialog(id, title, message, confirmText, cancelText string) {
|
||||
<div class="modal fade" id={ id } tabindex="-1" aria-labelledby={ id + "Label" } aria-hidden="true">
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<h5 class="modal-title" id={ id + "Label" }>{ title }</h5>
|
||||
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<p>{ message }</p>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">{ cancelText }</button>
|
||||
<button type="button" class="btn btn-danger" onclick="confirmAction(this)" data-action={ id }>{ confirmText }</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
|
||||
templ ListGroup() {
|
||||
<div class="list-group">
|
||||
{ children... }
|
||||
</div>
|
||||
}
|
||||
|
||||
templ ListGroupItem(active bool) {
|
||||
<div class={ "list-group-item", templ.KV("active", active) }>
|
||||
{ children... }
|
||||
</div>
|
||||
}
|
||||
|
||||
templ ButtonToolbar() {
|
||||
<div class="btn-toolbar" role="toolbar">
|
||||
{ children... }
|
||||
</div>
|
||||
}
|
||||
|
||||
templ StatusIndicator(status, text string) {
|
||||
<span class="status-indicator">
|
||||
<span class={ "status", "status-" + status }></span>
|
||||
{ text }
|
||||
</span>
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,133 @@
|
||||
package pages
|
||||
|
||||
import "tankstopp/internal/views/components"
|
||||
|
||||
templ AuthLayout(title string) {
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8"/>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
|
||||
<title>{ title } - TankStopp</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/@tabler/core@1.0.0-beta17/dist/css/tabler.min.css" rel="stylesheet"/>
|
||||
<link href="https://cdn.jsdelivr.net/npm/@tabler/icons@latest/icons-sprite.svg" rel="stylesheet"/>
|
||||
<link href="/static/style.css" rel="stylesheet"/>
|
||||
</head>
|
||||
<body class="d-flex flex-column">
|
||||
<div class="page page-center">
|
||||
<div class="container container-tight py-4">
|
||||
<div class="text-center mb-4">
|
||||
<a href="/" class="navbar-brand navbar-brand-autodark">
|
||||
@components.Icon("fuel", 48)
|
||||
TankStopp
|
||||
</a>
|
||||
</div>
|
||||
{ children... }
|
||||
</div>
|
||||
</div>
|
||||
<script src="https://cdn.jsdelivr.net/npm/@tabler/core@1.0.0-beta17/dist/js/tabler.min.js"></script>
|
||||
<script>
|
||||
function togglePassword(fieldName) {
|
||||
const passwordInput = document.querySelector(`input[name="${fieldName}"]`);
|
||||
const icon = passwordInput.nextElementSibling.querySelector('svg');
|
||||
|
||||
if (passwordInput.type === 'password') {
|
||||
passwordInput.type = 'text';
|
||||
icon.innerHTML = `
|
||||
<path stroke="none" d="M0 0h24v24H0z" fill="none"/>
|
||||
<line x1="3" y1="3" x2="21" y2="21"/>
|
||||
<path d="M10.584 10.587a2 2 0 0 0 2.828 2.83"/>
|
||||
<path d="M9.363 5.365a9.466 9.466 0 0 1 2.637 -.365c4 0 7.333 2.333 10 7c-.778 1.361 -1.612 2.524 -2.503 3.488m-2.14 1.861c-1.631 1.1 -3.415 1.651 -5.357 1.651c-4 0 -7.333 -2.333 -10 -7c1.369 -2.395 2.913 -4.175 4.632 -5.341"/>
|
||||
`;
|
||||
} else {
|
||||
passwordInput.type = 'password';
|
||||
icon.innerHTML = `
|
||||
<path stroke="none" d="M0 0h24v24H0z" fill="none"/>
|
||||
<circle cx="12" cy="12" r="2"/>
|
||||
<path d="M22 12c-2.667 4.667 -6 7 -10 7s-7.333 -2.333 -10 -7c2.667 -4.667 6 -7 10 -7s7.333 2.333 10 7"/>
|
||||
`;
|
||||
}
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
}
|
||||
|
||||
templ LoginPage(errorMessage string) {
|
||||
@AuthLayout("Login") {
|
||||
<div class="card card-md">
|
||||
<div class="card-body">
|
||||
<h2 class="h2 text-center mb-4">Login to your account</h2>
|
||||
if errorMessage != "" {
|
||||
@components.Alert("danger", errorMessage)
|
||||
}
|
||||
@components.Form("post", "/login") {
|
||||
@components.FormGroup("Username", "") {
|
||||
@components.Input("username", "text", "Enter your username", "", true)
|
||||
}
|
||||
@components.FormGroup("Password", "") {
|
||||
@components.PasswordInput("password", "Enter your password", true)
|
||||
}
|
||||
<div class="form-footer">
|
||||
<button type="submit" class="btn btn-primary w-100">
|
||||
@components.Icon("lock", 24)
|
||||
Sign in
|
||||
</button>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
<div class="text-center text-muted mt-3">
|
||||
Don't have an account yet?
|
||||
<a href="/register" tabindex="-1">Sign up</a>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
|
||||
templ RegisterPage(errorMessage string) {
|
||||
@AuthLayout("Register") {
|
||||
<div class="card card-md">
|
||||
<div class="card-body">
|
||||
<h2 class="h2 text-center mb-4">Create new account</h2>
|
||||
if errorMessage != "" {
|
||||
@components.Alert("danger", errorMessage)
|
||||
}
|
||||
@components.Form("post", "/register") {
|
||||
@components.FormGroup("Username", "Choose a unique username") {
|
||||
@components.Input("username", "text", "Enter your username", "", true)
|
||||
}
|
||||
@components.FormGroup("Email", "Enter a valid email address") {
|
||||
@components.Input("email", "email", "Enter your email", "", true)
|
||||
}
|
||||
@components.FormGroup("Password", "Password must be at least 8 characters") {
|
||||
@components.PasswordInput("password", "Enter your password", true)
|
||||
}
|
||||
@components.FormGroup("Confirm Password", "") {
|
||||
@components.PasswordInput("confirm_password", "Confirm your password", true)
|
||||
}
|
||||
@components.FormGroup("Base Currency", "Choose your preferred currency for fuel prices") {
|
||||
@components.Select("base_currency", true) {
|
||||
@components.Option("EUR", "EUR - Euro", false)
|
||||
@components.Option("USD", "USD - US Dollar", false)
|
||||
@components.Option("GBP", "GBP - British Pound", false)
|
||||
@components.Option("CHF", "CHF - Swiss Franc", false)
|
||||
@components.Option("JPY", "JPY - Japanese Yen", false)
|
||||
@components.Option("CAD", "CAD - Canadian Dollar", false)
|
||||
@components.Option("AUD", "AUD - Australian Dollar", false)
|
||||
}
|
||||
}
|
||||
<div class="form-footer">
|
||||
<button type="submit" class="btn btn-primary w-100">
|
||||
@components.Icon("user", 24)
|
||||
Create account
|
||||
</button>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
<div class="text-center text-muted mt-3">
|
||||
Already have an account?
|
||||
<a href="/login" tabindex="-1">Sign in</a>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,485 @@
|
||||
// Code generated by templ - DO NOT EDIT.
|
||||
|
||||
// templ: version: v0.3.906
|
||||
package pages
|
||||
|
||||
//lint:file-ignore SA4006 This context is only used if a nested component is present.
|
||||
|
||||
import "github.com/a-h/templ"
|
||||
import templruntime "github.com/a-h/templ/runtime"
|
||||
|
||||
import "tankstopp/internal/views/components"
|
||||
|
||||
func AuthLayout(title string) templ.Component {
|
||||
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
|
||||
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
|
||||
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
|
||||
return templ_7745c5c3_CtxErr
|
||||
}
|
||||
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
|
||||
if !templ_7745c5c3_IsBuffer {
|
||||
defer func() {
|
||||
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err == nil {
|
||||
templ_7745c5c3_Err = templ_7745c5c3_BufErr
|
||||
}
|
||||
}()
|
||||
}
|
||||
ctx = templ.InitializeContext(ctx)
|
||||
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
|
||||
if templ_7745c5c3_Var1 == nil {
|
||||
templ_7745c5c3_Var1 = templ.NopComponent
|
||||
}
|
||||
ctx = templ.ClearChildren(ctx)
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<!doctype html><html lang=\"en\"><head><meta charset=\"UTF-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"><title>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
var templ_7745c5c3_Var2 string
|
||||
templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(title)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/views/pages/auth.templ`, Line: 11, Col: 17}
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, " - TankStopp</title><link href=\"https://cdn.jsdelivr.net/npm/@tabler/core@1.0.0-beta17/dist/css/tabler.min.css\" rel=\"stylesheet\"><link href=\"https://cdn.jsdelivr.net/npm/@tabler/icons@latest/icons-sprite.svg\" rel=\"stylesheet\"><link href=\"/static/style.css\" rel=\"stylesheet\"></head><body class=\"d-flex flex-column\"><div class=\"page page-center\"><div class=\"container container-tight py-4\"><div class=\"text-center mb-4\"><a href=\"/\" class=\"navbar-brand navbar-brand-autodark\">")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = components.Icon("fuel", 48).Render(ctx, templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "TankStopp</a></div>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templ_7745c5c3_Var1.Render(ctx, templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 4, "</div></div><script src=\"https://cdn.jsdelivr.net/npm/@tabler/core@1.0.0-beta17/dist/js/tabler.min.js\"></script><script>\n function togglePassword(fieldName) {\n const passwordInput = document.querySelector(`input[name=\"${fieldName}\"]`);\n const icon = passwordInput.nextElementSibling.querySelector('svg');\n\n if (passwordInput.type === 'password') {\n passwordInput.type = 'text';\n icon.innerHTML = `\n <path stroke=\"none\" d=\"M0 0h24v24H0z\" fill=\"none\"/>\n <line x1=\"3\" y1=\"3\" x2=\"21\" y2=\"21\"/>\n <path d=\"M10.584 10.587a2 2 0 0 0 2.828 2.83\"/>\n <path d=\"M9.363 5.365a9.466 9.466 0 0 1 2.637 -.365c4 0 7.333 2.333 10 7c-.778 1.361 -1.612 2.524 -2.503 3.488m-2.14 1.861c-1.631 1.1 -3.415 1.651 -5.357 1.651c-4 0 -7.333 -2.333 -10 -7c1.369 -2.395 2.913 -4.175 4.632 -5.341\"/>\n `;\n } else {\n passwordInput.type = 'password';\n icon.innerHTML = `\n <path stroke=\"none\" d=\"M0 0h24v24H0z\" fill=\"none\"/>\n <circle cx=\"12\" cy=\"12\" r=\"2\"/>\n <path d=\"M22 12c-2.667 4.667 -6 7 -10 7s-7.333 -2.333 -10 -7c2.667 -4.667 6 -7 10 -7s7.333 2.333 10 7\"/>\n `;\n }\n }\n </script></body></html>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func LoginPage(errorMessage string) templ.Component {
|
||||
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
|
||||
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
|
||||
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
|
||||
return templ_7745c5c3_CtxErr
|
||||
}
|
||||
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
|
||||
if !templ_7745c5c3_IsBuffer {
|
||||
defer func() {
|
||||
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err == nil {
|
||||
templ_7745c5c3_Err = templ_7745c5c3_BufErr
|
||||
}
|
||||
}()
|
||||
}
|
||||
ctx = templ.InitializeContext(ctx)
|
||||
templ_7745c5c3_Var3 := templ.GetChildren(ctx)
|
||||
if templ_7745c5c3_Var3 == nil {
|
||||
templ_7745c5c3_Var3 = templ.NopComponent
|
||||
}
|
||||
ctx = templ.ClearChildren(ctx)
|
||||
templ_7745c5c3_Var4 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
|
||||
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
|
||||
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
|
||||
if !templ_7745c5c3_IsBuffer {
|
||||
defer func() {
|
||||
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err == nil {
|
||||
templ_7745c5c3_Err = templ_7745c5c3_BufErr
|
||||
}
|
||||
}()
|
||||
}
|
||||
ctx = templ.InitializeContext(ctx)
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 5, "<div class=\"card card-md\"><div class=\"card-body\"><h2 class=\"h2 text-center mb-4\">Login to your account</h2>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
if errorMessage != "" {
|
||||
templ_7745c5c3_Err = components.Alert("danger", errorMessage).Render(ctx, templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
}
|
||||
templ_7745c5c3_Var5 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
|
||||
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
|
||||
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
|
||||
if !templ_7745c5c3_IsBuffer {
|
||||
defer func() {
|
||||
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err == nil {
|
||||
templ_7745c5c3_Err = templ_7745c5c3_BufErr
|
||||
}
|
||||
}()
|
||||
}
|
||||
ctx = templ.InitializeContext(ctx)
|
||||
templ_7745c5c3_Var6 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
|
||||
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
|
||||
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
|
||||
if !templ_7745c5c3_IsBuffer {
|
||||
defer func() {
|
||||
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err == nil {
|
||||
templ_7745c5c3_Err = templ_7745c5c3_BufErr
|
||||
}
|
||||
}()
|
||||
}
|
||||
ctx = templ.InitializeContext(ctx)
|
||||
templ_7745c5c3_Err = components.Input("username", "text", "Enter your username", "", true).Render(ctx, templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
templ_7745c5c3_Err = components.FormGroup("Username", "").Render(templ.WithChildren(ctx, templ_7745c5c3_Var6), templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 6, " ")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Var7 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
|
||||
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
|
||||
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
|
||||
if !templ_7745c5c3_IsBuffer {
|
||||
defer func() {
|
||||
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err == nil {
|
||||
templ_7745c5c3_Err = templ_7745c5c3_BufErr
|
||||
}
|
||||
}()
|
||||
}
|
||||
ctx = templ.InitializeContext(ctx)
|
||||
templ_7745c5c3_Err = components.PasswordInput("password", "Enter your password", true).Render(ctx, templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
templ_7745c5c3_Err = components.FormGroup("Password", "").Render(templ.WithChildren(ctx, templ_7745c5c3_Var7), templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 7, " <div class=\"form-footer\"><button type=\"submit\" class=\"btn btn-primary w-100\">")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = components.Icon("lock", 24).Render(ctx, templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 8, "Sign in</button></div>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
templ_7745c5c3_Err = components.Form("post", "/login").Render(templ.WithChildren(ctx, templ_7745c5c3_Var5), templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 9, "</div></div><div class=\"text-center text-muted mt-3\">Don't have an account yet? <a href=\"/register\" tabindex=\"-1\">Sign up</a></div>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
templ_7745c5c3_Err = AuthLayout("Login").Render(templ.WithChildren(ctx, templ_7745c5c3_Var4), templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func RegisterPage(errorMessage string) templ.Component {
|
||||
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
|
||||
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
|
||||
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
|
||||
return templ_7745c5c3_CtxErr
|
||||
}
|
||||
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
|
||||
if !templ_7745c5c3_IsBuffer {
|
||||
defer func() {
|
||||
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err == nil {
|
||||
templ_7745c5c3_Err = templ_7745c5c3_BufErr
|
||||
}
|
||||
}()
|
||||
}
|
||||
ctx = templ.InitializeContext(ctx)
|
||||
templ_7745c5c3_Var8 := templ.GetChildren(ctx)
|
||||
if templ_7745c5c3_Var8 == nil {
|
||||
templ_7745c5c3_Var8 = templ.NopComponent
|
||||
}
|
||||
ctx = templ.ClearChildren(ctx)
|
||||
templ_7745c5c3_Var9 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
|
||||
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
|
||||
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
|
||||
if !templ_7745c5c3_IsBuffer {
|
||||
defer func() {
|
||||
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err == nil {
|
||||
templ_7745c5c3_Err = templ_7745c5c3_BufErr
|
||||
}
|
||||
}()
|
||||
}
|
||||
ctx = templ.InitializeContext(ctx)
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 10, "<div class=\"card card-md\"><div class=\"card-body\"><h2 class=\"h2 text-center mb-4\">Create new account</h2>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
if errorMessage != "" {
|
||||
templ_7745c5c3_Err = components.Alert("danger", errorMessage).Render(ctx, templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
}
|
||||
templ_7745c5c3_Var10 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
|
||||
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
|
||||
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
|
||||
if !templ_7745c5c3_IsBuffer {
|
||||
defer func() {
|
||||
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err == nil {
|
||||
templ_7745c5c3_Err = templ_7745c5c3_BufErr
|
||||
}
|
||||
}()
|
||||
}
|
||||
ctx = templ.InitializeContext(ctx)
|
||||
templ_7745c5c3_Var11 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
|
||||
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
|
||||
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
|
||||
if !templ_7745c5c3_IsBuffer {
|
||||
defer func() {
|
||||
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err == nil {
|
||||
templ_7745c5c3_Err = templ_7745c5c3_BufErr
|
||||
}
|
||||
}()
|
||||
}
|
||||
ctx = templ.InitializeContext(ctx)
|
||||
templ_7745c5c3_Err = components.Input("username", "text", "Enter your username", "", true).Render(ctx, templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
templ_7745c5c3_Err = components.FormGroup("Username", "Choose a unique username").Render(templ.WithChildren(ctx, templ_7745c5c3_Var11), templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 11, " ")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Var12 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
|
||||
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
|
||||
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
|
||||
if !templ_7745c5c3_IsBuffer {
|
||||
defer func() {
|
||||
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err == nil {
|
||||
templ_7745c5c3_Err = templ_7745c5c3_BufErr
|
||||
}
|
||||
}()
|
||||
}
|
||||
ctx = templ.InitializeContext(ctx)
|
||||
templ_7745c5c3_Err = components.Input("email", "email", "Enter your email", "", true).Render(ctx, templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
templ_7745c5c3_Err = components.FormGroup("Email", "Enter a valid email address").Render(templ.WithChildren(ctx, templ_7745c5c3_Var12), templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 12, " ")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Var13 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
|
||||
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
|
||||
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
|
||||
if !templ_7745c5c3_IsBuffer {
|
||||
defer func() {
|
||||
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err == nil {
|
||||
templ_7745c5c3_Err = templ_7745c5c3_BufErr
|
||||
}
|
||||
}()
|
||||
}
|
||||
ctx = templ.InitializeContext(ctx)
|
||||
templ_7745c5c3_Err = components.PasswordInput("password", "Enter your password", true).Render(ctx, templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
templ_7745c5c3_Err = components.FormGroup("Password", "Password must be at least 8 characters").Render(templ.WithChildren(ctx, templ_7745c5c3_Var13), templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 13, " ")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Var14 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
|
||||
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
|
||||
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
|
||||
if !templ_7745c5c3_IsBuffer {
|
||||
defer func() {
|
||||
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err == nil {
|
||||
templ_7745c5c3_Err = templ_7745c5c3_BufErr
|
||||
}
|
||||
}()
|
||||
}
|
||||
ctx = templ.InitializeContext(ctx)
|
||||
templ_7745c5c3_Err = components.PasswordInput("confirm_password", "Confirm your password", true).Render(ctx, templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
templ_7745c5c3_Err = components.FormGroup("Confirm Password", "").Render(templ.WithChildren(ctx, templ_7745c5c3_Var14), templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 14, " ")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Var15 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
|
||||
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
|
||||
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
|
||||
if !templ_7745c5c3_IsBuffer {
|
||||
defer func() {
|
||||
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err == nil {
|
||||
templ_7745c5c3_Err = templ_7745c5c3_BufErr
|
||||
}
|
||||
}()
|
||||
}
|
||||
ctx = templ.InitializeContext(ctx)
|
||||
templ_7745c5c3_Var16 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
|
||||
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
|
||||
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
|
||||
if !templ_7745c5c3_IsBuffer {
|
||||
defer func() {
|
||||
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err == nil {
|
||||
templ_7745c5c3_Err = templ_7745c5c3_BufErr
|
||||
}
|
||||
}()
|
||||
}
|
||||
ctx = templ.InitializeContext(ctx)
|
||||
templ_7745c5c3_Err = components.Option("EUR", "EUR - Euro", false).Render(ctx, templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 15, " ")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = components.Option("USD", "USD - US Dollar", false).Render(ctx, templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 16, " ")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = components.Option("GBP", "GBP - British Pound", false).Render(ctx, templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 17, " ")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = components.Option("CHF", "CHF - Swiss Franc", false).Render(ctx, templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 18, " ")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = components.Option("JPY", "JPY - Japanese Yen", false).Render(ctx, templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 19, " ")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = components.Option("CAD", "CAD - Canadian Dollar", false).Render(ctx, templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 20, " ")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = components.Option("AUD", "AUD - Australian Dollar", false).Render(ctx, templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
templ_7745c5c3_Err = components.Select("base_currency", true).Render(templ.WithChildren(ctx, templ_7745c5c3_Var16), templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
templ_7745c5c3_Err = components.FormGroup("Base Currency", "Choose your preferred currency for fuel prices").Render(templ.WithChildren(ctx, templ_7745c5c3_Var15), templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 21, " <div class=\"form-footer\"><button type=\"submit\" class=\"btn btn-primary w-100\">")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = components.Icon("user", 24).Render(ctx, templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 22, "Create account</button></div>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
templ_7745c5c3_Err = components.Form("post", "/register").Render(templ.WithChildren(ctx, templ_7745c5c3_Var10), templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 23, "</div></div><div class=\"text-center text-muted mt-3\">Already have an account? <a href=\"/login\" tabindex=\"-1\">Sign in</a></div>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
templ_7745c5c3_Err = AuthLayout("Register").Render(templ.WithChildren(ctx, templ_7745c5c3_Var9), templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
var _ = templruntime.GeneratedTemplate
|
||||
@@ -0,0 +1,478 @@
|
||||
package pages
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"tankstopp/internal/models"
|
||||
"tankstopp/internal/views/components"
|
||||
)
|
||||
|
||||
templ DashboardPage(user *models.User, username string, stops []models.FuelStop, vehicles []models.Vehicle, totalStops int, totalCost float64, avgConsumption float64, lastFillUp *models.FuelStop) {
|
||||
@components.BaseLayout("Dashboard", user, username) {
|
||||
@components.PageHeader("Overview", "Dashboard")
|
||||
<div class="page-body">
|
||||
<div class="container-xl">
|
||||
<!-- Statistics Cards -->
|
||||
<div class="row row-deck row-cards mb-4">
|
||||
<div class="col-sm-6 col-lg-3">
|
||||
@components.Card("Total Stops", "gas-station") {
|
||||
<div class="d-flex align-items-center">
|
||||
<div class="subheader">Fuel stops recorded</div>
|
||||
<div class="ms-auto lh-1">
|
||||
<div class="dropdown">
|
||||
<a class="dropdown-toggle text-muted" href="#" data-bs-toggle="dropdown">Last 30 days</a>
|
||||
<div class="dropdown-menu dropdown-menu-end">
|
||||
<a class="dropdown-item active" href="#">Last 30 days</a>
|
||||
<a class="dropdown-item" href="#">Last 90 days</a>
|
||||
<a class="dropdown-item" href="#">All time</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="h1 mb-3">{ fmt.Sprintf("%d", totalStops) }</div>
|
||||
<div class="d-flex mb-2">
|
||||
<div class="flex-fill">
|
||||
<div class="progress progress-sm">
|
||||
<div class="progress-bar bg-primary" style="width: 75%" role="progressbar"></div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="text-muted ms-2">75%</div>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
<div class="col-sm-6 col-lg-3">
|
||||
@components.Card("Total Spent", "currency") {
|
||||
<div class="d-flex align-items-center">
|
||||
<div class="subheader">Total fuel costs</div>
|
||||
</div>
|
||||
<div class="h1 mb-3">{ fmt.Sprintf("%.2f %s", totalCost, user.BaseCurrency) }</div>
|
||||
<div class="d-flex mb-2">
|
||||
<div class="flex-fill">
|
||||
<div class="progress progress-sm">
|
||||
<div class="progress-bar bg-success" style="width: 60%" role="progressbar"></div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="text-muted ms-2">60%</div>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
<div class="col-sm-6 col-lg-3">
|
||||
@components.Card("Avg Consumption", "gauge") {
|
||||
<div class="d-flex align-items-center">
|
||||
<div class="subheader">Liters per 100km</div>
|
||||
</div>
|
||||
<div class="h1 mb-3">
|
||||
if avgConsumption > 0 {
|
||||
{ fmt.Sprintf("%.1f L/100km", avgConsumption) }
|
||||
} else {
|
||||
{ "N/A" }
|
||||
}
|
||||
</div>
|
||||
<div class="d-flex mb-2">
|
||||
<div class="flex-fill">
|
||||
<div class="progress progress-sm">
|
||||
<div class="progress-bar bg-warning" style="width: 45%" role="progressbar"></div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="text-muted ms-2">Efficiency</div>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
<div class="col-sm-6 col-lg-3">
|
||||
@components.Card("Vehicles", "car") {
|
||||
<div class="d-flex align-items-center">
|
||||
<div class="subheader">Active vehicles</div>
|
||||
</div>
|
||||
<div class="h1 mb-3">{ fmt.Sprintf("%d", len(vehicles)) }</div>
|
||||
<div class="d-flex mb-2">
|
||||
<div class="flex-fill">
|
||||
<div class="progress progress-sm">
|
||||
<div class="progress-bar bg-info" style="width: 100%" role="progressbar"></div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="text-muted ms-2">100%</div>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
<!-- Trip Length and Efficiency Statistics -->
|
||||
<div class="row row-deck row-cards mb-4">
|
||||
<div class="col-sm-6 col-lg-4">
|
||||
@components.Card("Total Distance", "trip") {
|
||||
<div class="d-flex align-items-center">
|
||||
<div class="subheader">Kilometers driven</div>
|
||||
</div>
|
||||
<div class="h1 mb-3">
|
||||
{ fmt.Sprintf("%.0f km", calculateTotalDistance(stops)) }
|
||||
</div>
|
||||
<div class="d-flex mb-2">
|
||||
<div class="flex-fill">
|
||||
<div class="progress progress-sm">
|
||||
<div class="progress-bar bg-info" style="width: 80%" role="progressbar"></div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="text-muted ms-2">Tracked</div>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
<div class="col-sm-6 col-lg-4">
|
||||
@components.Card("Efficiency Trend", "chart-bar") {
|
||||
<div class="d-flex align-items-center">
|
||||
<div class="subheader">Recent performance</div>
|
||||
</div>
|
||||
<div class="h1 mb-3">
|
||||
if len(stops) >= 3 {
|
||||
if calculateEfficiencyTrend(stops) == "improving" {
|
||||
<span class="text-success">Improving</span>
|
||||
} else if calculateEfficiencyTrend(stops) == "worsening" {
|
||||
<span class="text-danger">Declining</span>
|
||||
} else {
|
||||
<span class="text-info">Stable</span>
|
||||
}
|
||||
} else {
|
||||
<span class="text-muted">N/A</span>
|
||||
}
|
||||
</div>
|
||||
<div class="d-flex mb-2">
|
||||
<div class="flex-fill">
|
||||
<div class="progress progress-sm">
|
||||
if calculateEfficiencyTrend(stops) == "improving" {
|
||||
<div class="progress-bar bg-success" style="width: 75%" role="progressbar"></div>
|
||||
} else if calculateEfficiencyTrend(stops) == "worsening" {
|
||||
<div class="progress-bar bg-danger" style="width: 30%" role="progressbar"></div>
|
||||
} else {
|
||||
<div class="progress-bar bg-info" style="width: 50%" role="progressbar"></div>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
<div class="text-muted ms-2">Trend</div>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
<div class="col-sm-6 col-lg-4">
|
||||
@components.Card("Best Efficiency", "award") {
|
||||
<div class="d-flex align-items-center">
|
||||
<div class="subheader">Lowest consumption</div>
|
||||
</div>
|
||||
<div class="h1 mb-3">
|
||||
{ fmt.Sprintf("%.1f L/100km", getBestEfficiency(stops)) }
|
||||
</div>
|
||||
<div class="d-flex mb-2">
|
||||
<div class="flex-fill">
|
||||
<div class="progress progress-sm">
|
||||
<div class="progress-bar bg-success" style="width: 90%" role="progressbar"></div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="text-muted ms-2">Personal best</div>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
<!-- Last Fill-up Info -->
|
||||
if lastFillUp != nil {
|
||||
<div class="row mb-4">
|
||||
<div class="col-12">
|
||||
<div class="card">
|
||||
<div class="card-header">
|
||||
<h3 class="card-title">
|
||||
@components.Icon("clock", 24)
|
||||
Last Fill-up
|
||||
</h3>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<div class="row">
|
||||
<div class="col-sm-6 col-lg-3">
|
||||
<div class="mb-3">
|
||||
<div class="text-muted">Date</div>
|
||||
<div class="h4">{ lastFillUp.Date.Format("Jan 2, 2006") }</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-sm-6 col-lg-3">
|
||||
<div class="mb-3">
|
||||
<div class="text-muted">Amount</div>
|
||||
<div class="h4">{ fmt.Sprintf("%.2f L", lastFillUp.Liters) }</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-sm-6 col-lg-3">
|
||||
<div class="mb-3">
|
||||
<div class="text-muted">Cost</div>
|
||||
<div class="h4">{ fmt.Sprintf("%.2f %s", lastFillUp.TotalPrice, user.BaseCurrency) }</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-sm-6 col-lg-3">
|
||||
<div class="mb-3">
|
||||
<div class="text-muted">Location</div>
|
||||
<div class="h4">{ lastFillUp.Location }</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
<!-- Filters and Search -->
|
||||
<div class="row mb-4">
|
||||
<div class="col-12">
|
||||
<div class="card">
|
||||
<div class="card-header">
|
||||
<h3 class="card-title">Filters</h3>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<div class="row">
|
||||
<div class="col-sm-6 col-lg-3">
|
||||
@components.FormGroup("Vehicle", "") {
|
||||
@components.VehicleSelect("vehicle_id", 0, vehicles, false)
|
||||
}
|
||||
</div>
|
||||
<div class="col-sm-6 col-lg-3">
|
||||
@components.FormGroup("Fuel Type", "") {
|
||||
@components.FuelTypeSelect("fuel_type", "", false)
|
||||
}
|
||||
</div>
|
||||
<div class="col-sm-6 col-lg-3">
|
||||
@components.FormGroup("Date From", "") {
|
||||
@components.DateInput("date_from", "", false)
|
||||
}
|
||||
</div>
|
||||
<div class="col-sm-6 col-lg-3">
|
||||
@components.FormGroup("Date To", "") {
|
||||
@components.DateInput("date_to", "", false)
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-12">
|
||||
@components.ButtonGroup() {
|
||||
<button type="button" class="btn btn-primary" onclick="applyFilters()">
|
||||
@components.Icon("search", 24)
|
||||
Apply Filters
|
||||
</button>
|
||||
<button type="button" class="btn btn-secondary" onclick="clearFilters()">
|
||||
@components.Icon("refresh", 24)
|
||||
Clear
|
||||
</button>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<!-- Fuel Stops Table -->
|
||||
<div class="row">
|
||||
<div class="col-12">
|
||||
if len(stops) > 0 {
|
||||
@FuelStopsTable(stops, user.BaseCurrency)
|
||||
} else {
|
||||
@components.EmptyState("gas-station", "No fuel stops found", "Start tracking your fuel expenses by adding your first fuel stop.", "Add your first stop", "/add")
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@DashboardScript()
|
||||
}
|
||||
}
|
||||
|
||||
templ FuelStopsTable(stops []models.FuelStop, currency string) {
|
||||
<div class="card">
|
||||
<div class="card-header">
|
||||
<h3 class="card-title">Recent Fuel Stops</h3>
|
||||
<div class="card-actions">
|
||||
<a href="/add" class="btn btn-primary">
|
||||
@components.Icon("plus", 24)
|
||||
Add Stop
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
<div class="table-responsive">
|
||||
<table class="table table-vcenter table-mobile-md card-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Date</th>
|
||||
<th>Vehicle</th>
|
||||
<th>Location</th>
|
||||
<th>Fuel Type</th>
|
||||
<th>Amount</th>
|
||||
<th>Price/L</th>
|
||||
<th>Total</th>
|
||||
<th>Trip Length</th>
|
||||
<th>Consumption</th>
|
||||
<th>Odometer</th>
|
||||
<th class="w-1">Actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
for _, stop := range stops {
|
||||
<tr>
|
||||
<td data-label="Date">
|
||||
<div class="d-flex py-1 align-items-center">
|
||||
<div class="flex-fill">
|
||||
<div class="font-weight-medium">{ stop.Date.Format("Jan 2, 2006") }</div>
|
||||
<div class="text-muted">{ stop.Date.Format("15:04") }</div>
|
||||
</div>
|
||||
</div>
|
||||
</td>
|
||||
<td data-label="Vehicle">
|
||||
<div class="d-flex py-1 align-items-center">
|
||||
<div class="flex-fill">
|
||||
<div class="font-weight-medium">{ stop.Vehicle.Name }</div>
|
||||
if stop.Vehicle.LicensePlate != "" {
|
||||
<div class="text-muted">{ stop.Vehicle.LicensePlate }</div>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
</td>
|
||||
<td data-label="Location">
|
||||
<div class="d-flex py-1 align-items-center">
|
||||
@components.Icon("location", 24)
|
||||
<div class="ms-2">{ stop.Location }</div>
|
||||
</div>
|
||||
</td>
|
||||
<td data-label="Fuel Type">
|
||||
<span class="badge bg-secondary">{ stop.FuelType }</span>
|
||||
</td>
|
||||
<td data-label="Amount">
|
||||
<div class="font-weight-medium">{ fmt.Sprintf("%.2f L", stop.Liters) }</div>
|
||||
</td>
|
||||
<td data-label="Price/L">
|
||||
<div class="font-weight-medium">{ fmt.Sprintf("%.3f %s", stop.PricePerL, currency) }</div>
|
||||
</td>
|
||||
<td data-label="Total">
|
||||
<div class="font-weight-medium text-success">{ fmt.Sprintf("%.2f %s", stop.TotalPrice, currency) }</div>
|
||||
</td>
|
||||
<td data-label="Trip Length">
|
||||
if stop.TripLength > 0 {
|
||||
<div class="font-weight-medium">{ fmt.Sprintf("%.1f km", stop.TripLength) }</div>
|
||||
} else {
|
||||
<div class="text-muted">Not recorded</div>
|
||||
}
|
||||
</td>
|
||||
<td data-label="Consumption">
|
||||
if stop.TripLength > 0 {
|
||||
<div class="font-weight-medium">
|
||||
{ fmt.Sprintf("%.1f L/100km", (stop.Liters/stop.TripLength)*100) }
|
||||
</div>
|
||||
} else {
|
||||
<div class="text-muted">N/A</div>
|
||||
}
|
||||
</td>
|
||||
<td data-label="Odometer">
|
||||
if stop.Odometer > 0 {
|
||||
<div class="font-weight-medium">{ fmt.Sprintf("%d km", stop.Odometer) }</div>
|
||||
} else {
|
||||
<div class="text-muted">Not recorded</div>
|
||||
}
|
||||
</td>
|
||||
<td>
|
||||
@components.ButtonGroup() {
|
||||
@components.EditButton(fmt.Sprintf("/edit/%d", stop.ID))
|
||||
@components.DeleteButton(fmt.Sprintf("/delete/%d", stop.ID), fmt.Sprintf("fuel stop from %s", stop.Date.Format("Jan 2, 2006")))
|
||||
}
|
||||
</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
|
||||
// Helper functions for statistics calculations
|
||||
func calculateTotalDistance(stops []models.FuelStop) float64 {
|
||||
var total float64
|
||||
for _, stop := range stops {
|
||||
if stop.TripLength > 0 {
|
||||
total += stop.TripLength
|
||||
}
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
func calculateEfficiencyTrend(stops []models.FuelStop) string {
|
||||
if len(stops) < 3 {
|
||||
return "insufficient_data"
|
||||
}
|
||||
|
||||
// Calculate average consumption for recent vs older stops
|
||||
recent := stops[:len(stops)/2]
|
||||
older := stops[len(stops)/2:]
|
||||
|
||||
recentAvg := calculateAverageConsumption(recent)
|
||||
olderAvg := calculateAverageConsumption(older)
|
||||
|
||||
if recentAvg == 0 || olderAvg == 0 {
|
||||
return "insufficient_data"
|
||||
}
|
||||
|
||||
diff := recentAvg - olderAvg
|
||||
if diff < -0.5 {
|
||||
return "improving"
|
||||
} else if diff > 0.5 {
|
||||
return "worsening"
|
||||
}
|
||||
return "stable"
|
||||
}
|
||||
|
||||
func calculateAverageConsumption(stops []models.FuelStop) float64 {
|
||||
var totalConsumption float64
|
||||
var count int
|
||||
|
||||
for _, stop := range stops {
|
||||
if stop.TripLength > 0 {
|
||||
consumption := (stop.Liters / stop.TripLength) * 100
|
||||
if consumption > 0 && consumption < 50 {
|
||||
totalConsumption += consumption
|
||||
count++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if count == 0 {
|
||||
return 0
|
||||
}
|
||||
return totalConsumption / float64(count)
|
||||
}
|
||||
|
||||
func getBestEfficiency(stops []models.FuelStop) float64 {
|
||||
bestEfficiency := float64(999) // Start with high value
|
||||
|
||||
for _, stop := range stops {
|
||||
if stop.TripLength > 0 {
|
||||
consumption := (stop.Liters / stop.TripLength) * 100
|
||||
if consumption > 0 && consumption < bestEfficiency && consumption < 50 {
|
||||
bestEfficiency = consumption
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if bestEfficiency == 999 {
|
||||
return 0
|
||||
}
|
||||
return bestEfficiency
|
||||
}
|
||||
|
||||
script DashboardScript() {
|
||||
function applyFilters() {
|
||||
const vehicleId = document.querySelector('select[name="vehicle_id"]').value;
|
||||
const fuelType = document.querySelector('select[name="fuel_type"]').value;
|
||||
const dateFrom = document.querySelector('input[name="date_from"]').value;
|
||||
const dateTo = document.querySelector('input[name="date_to"]').value;
|
||||
|
||||
const params = new URLSearchParams();
|
||||
if (vehicleId) params.append('vehicle_id', vehicleId);
|
||||
if (fuelType) params.append('fuel_type', fuelType);
|
||||
if (dateFrom) params.append('date_from', dateFrom);
|
||||
if (dateTo) params.append('date_to', dateTo);
|
||||
|
||||
window.location.href = '/dashboard?' + params.toString();
|
||||
}
|
||||
|
||||
function clearFilters() {
|
||||
window.location.href = '/dashboard';
|
||||
}
|
||||
|
||||
function confirmDelete(itemName) {
|
||||
return confirm(`Are you sure you want to delete this ${itemName}? This action cannot be undone.`);
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,814 @@
|
||||
package pages
|
||||
|
||||
import (
|
||||
"tankstopp/internal/currency"
|
||||
"tankstopp/internal/models"
|
||||
"tankstopp/internal/views/components"
|
||||
)
|
||||
|
||||
templ AddFuelStopPage(user *models.User, username string, vehicles []models.Vehicle, currencies []currency.Currency) {
|
||||
@components.BaseLayout("Add Fuel Stop", user, username) {
|
||||
@components.PageHeader("Add Fuel Stop", "Record a new fuel stop")
|
||||
<div class="page-body">
|
||||
<div class="container-xl">
|
||||
<div class="row justify-content-center">
|
||||
<div class="col-12 col-lg-8">
|
||||
<form method="POST" class="card">
|
||||
<div class="card-header">
|
||||
<h3 class="card-title">Fuel Stop Details</h3>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
@components.FormGroup("Date", "") {
|
||||
@components.DateInput("date", "", true)
|
||||
}
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
@components.FormGroup("Vehicle", "") {
|
||||
@components.VehicleSelect("vehicle_id", 0, vehicles, true)
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
@components.FormGroup("Station Name", "") {
|
||||
<div class="input-group">
|
||||
@components.Input("station_name", "text", "Enter station name", "", false)
|
||||
<button class="btn btn-outline-secondary" type="button" onclick="findNearbyStations()">
|
||||
@components.Icon("search", 24)
|
||||
Find Nearby
|
||||
</button>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
@components.FormGroup("Location", "") {
|
||||
@components.Input("location", "text", "Enter location", "", false)
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
<!-- Station Search Results Modal -->
|
||||
<div class="modal fade" id="stationSearchModal" tabindex="-1" aria-labelledby="stationSearchModalLabel" aria-hidden="true">
|
||||
<div class="modal-dialog modal-lg">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<h5 class="modal-title" id="stationSearchModalLabel">Nearby Fuel Stations</h5>
|
||||
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<div id="stationSearchResults">
|
||||
<div class="text-center">
|
||||
<div class="spinner-border" role="status">
|
||||
<span class="visually-hidden">Searching...</span>
|
||||
</div>
|
||||
<p class="mt-2">Finding nearby fuel stations...</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
|
||||
<button type="button" class="btn btn-outline-primary" onclick="showManualEntry()">Enter Manually</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
@components.FormGroup("Fuel Type", "") {
|
||||
@components.FuelTypeSelect("fuel_type", "", true)
|
||||
}
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
@components.FormGroup("Amount (Liters)", "") {
|
||||
@components.NumberInput("amount", "0.00", 0.0, "0.01", 0.0, true)
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-md-4">
|
||||
@components.FormGroup("Price per Liter", "") {
|
||||
<div class="input-group">
|
||||
@components.NumberInput("price_per_liter", "0.000", 0.0, "0.001", 0.0, true)
|
||||
<span class="input-group-text" id="price-currency">{ user.BaseCurrency }</span>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
@components.FormGroup("Total Cost", "") {
|
||||
<div class="input-group">
|
||||
@components.NumberInput("total_cost", "0.00", 0.0, "0.01", 0.0, true)
|
||||
<span class="input-group-text" id="total-currency">{ user.BaseCurrency }</span>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
@components.FormGroup("Currency", "") {
|
||||
@components.CurrencySelect("currency", user.BaseCurrency, currencies)
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
@components.FormGroup("Odometer Reading (km)", "") {
|
||||
@components.NumberInput("odometer", "0", 0.0, "1", 0.0, false)
|
||||
}
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
@components.FormGroup("Trip Length (km)", "") {
|
||||
@components.NumberInput("trip_length", "0.0", 0.0, "0.1", 0.0, false)
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-12">
|
||||
@components.FormGroup("Notes", "") {
|
||||
@components.TextArea("notes", "Optional notes about this fuel stop", "", 3)
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="card-footer text-end">
|
||||
<div class="d-flex">
|
||||
<a href="/dashboard" class="btn btn-link">Cancel</a>
|
||||
<button type="submit" class="btn btn-primary ms-auto">
|
||||
@components.Icon("plus", 24)
|
||||
Add Fuel Stop
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@FuelStopScript(vehicles)
|
||||
}
|
||||
}
|
||||
|
||||
templ EditFuelStopPage(user *models.User, username string, stop *models.FuelStop, vehicles []models.Vehicle, currencies []currency.Currency) {
|
||||
@components.BaseLayout("Edit Fuel Stop", user, username) {
|
||||
@components.PageHeader("Edit Fuel Stop", "Update fuel stop details")
|
||||
<div class="page-body">
|
||||
<div class="container-xl">
|
||||
<div class="row justify-content-center">
|
||||
<div class="col-12 col-lg-8">
|
||||
<form method="POST" class="card">
|
||||
<div class="card-header">
|
||||
<h3 class="card-title">Fuel Stop Details</h3>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
@components.FormGroup("Date", "") {
|
||||
@components.DateInput("date", stop.Date.Format("2006-01-02"), true)
|
||||
}
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
@components.FormGroup("Vehicle", "") {
|
||||
@components.VehicleSelect("vehicle_id", stop.VehicleID, vehicles, true)
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
@components.FormGroup("Station Name", "") {
|
||||
<div class="input-group">
|
||||
@components.Input("station_name", "text", "Enter station name", stop.StationName, false)
|
||||
<button class="btn btn-outline-secondary" type="button" onclick="findNearbyStations()">
|
||||
@components.Icon("search", 24)
|
||||
Find Nearby
|
||||
</button>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
@components.FormGroup("Location", "") {
|
||||
@components.Input("location", "text", "Enter location", stop.Location, false)
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
<!-- Station Search Results Modal -->
|
||||
<div class="modal fade" id="stationSearchModal" tabindex="-1" aria-labelledby="stationSearchModalLabel" aria-hidden="true">
|
||||
<div class="modal-dialog modal-lg">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<h5 class="modal-title" id="stationSearchModalLabel">Nearby Fuel Stations</h5>
|
||||
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<div id="stationSearchResults">
|
||||
<div class="text-center">
|
||||
<div class="spinner-border" role="status">
|
||||
<span class="visually-hidden">Searching...</span>
|
||||
</div>
|
||||
<p class="mt-2">Finding nearby fuel stations...</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
|
||||
<button type="button" class="btn btn-outline-primary" onclick="showManualEntry()">Enter Manually</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
@components.FormGroup("Fuel Type", "") {
|
||||
@components.FuelTypeSelect("fuel_type", stop.FuelType, true)
|
||||
}
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
@components.FormGroup("Amount (Liters)", "") {
|
||||
@components.NumberInput("amount", "0.00", stop.Liters, "0.01", 0.0, true)
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-md-4">
|
||||
@components.FormGroup("Price per Liter", "") {
|
||||
<div class="input-group">
|
||||
@components.NumberInput("price_per_liter", "0.000", stop.PricePerL, "0.001", 0.0, true)
|
||||
<span class="input-group-text" id="price-currency">{ stop.Currency }</span>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
@components.FormGroup("Total Cost", "") {
|
||||
<div class="input-group">
|
||||
@components.NumberInput("total_cost", "0.00", stop.TotalPrice, "0.01", 0.0, true)
|
||||
<span class="input-group-text" id="total-currency">{ stop.Currency }</span>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
@components.FormGroup("Currency", "") {
|
||||
@components.CurrencySelect("currency", stop.Currency, currencies)
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
@components.FormGroup("Odometer Reading (km)", "") {
|
||||
@components.NumberInput("odometer", "0", float64(stop.Odometer), "1", 0.0, false)
|
||||
}
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
@components.FormGroup("Trip Length (km)", "") {
|
||||
@components.NumberInput("trip_length", "0.0", stop.TripLength, "0.1", 0.0, false)
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-12">
|
||||
@components.FormGroup("Notes", "") {
|
||||
@components.TextArea("notes", "Optional notes about this fuel stop", stop.Notes, 3)
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="card-footer text-end">
|
||||
<div class="d-flex">
|
||||
<a href="/dashboard" class="btn btn-link">Cancel</a>
|
||||
<button type="submit" class="btn btn-primary ms-auto">
|
||||
@components.Icon("check", 24)
|
||||
Update Fuel Stop
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@FuelStopScript(vehicles)
|
||||
}
|
||||
}
|
||||
|
||||
script FuelStopScript(vehicles []models.Vehicle) {
|
||||
// Update currency display when currency dropdown changes
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
const currencySelect = document.querySelector('select[name="currency"]');
|
||||
const priceCurrency = document.getElementById('price-currency');
|
||||
const totalCurrency = document.getElementById('total-currency');
|
||||
|
||||
if (currencySelect) {
|
||||
currencySelect.addEventListener('change', function() {
|
||||
const selectedCurrency = this.value;
|
||||
if (priceCurrency) priceCurrency.textContent = selectedCurrency;
|
||||
if (totalCurrency) totalCurrency.textContent = selectedCurrency;
|
||||
});
|
||||
}
|
||||
|
||||
// Update fuel type when vehicle is selected
|
||||
const vehicleSelect = document.querySelector('select[name="vehicle_id"]');
|
||||
const fuelTypeSelect = document.querySelector('select[name="fuel_type"]');
|
||||
|
||||
if (vehicleSelect && fuelTypeSelect) {
|
||||
vehicleSelect.addEventListener('change', async function() {
|
||||
const selectedVehicleId = this.value;
|
||||
if (selectedVehicleId) {
|
||||
try {
|
||||
// Fetch vehicle information from API
|
||||
const response = await fetch(`/api/vehicles/${selectedVehicleId}`);
|
||||
if (response.ok) {
|
||||
const vehicle = await response.json();
|
||||
if (vehicle.fuel_type) {
|
||||
fuelTypeSelect.value = vehicle.fuel_type;
|
||||
}
|
||||
} else {
|
||||
console.warn('Failed to fetch vehicle information');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error fetching vehicle information:', error);
|
||||
}
|
||||
} else {
|
||||
// Reset fuel type when no vehicle is selected
|
||||
fuelTypeSelect.value = '';
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Auto-calculate total cost when amount or price per liter changes
|
||||
const amountInput = document.querySelector('input[name="amount"]');
|
||||
const priceInput = document.querySelector('input[name="price_per_liter"]');
|
||||
const totalInput = document.querySelector('input[name="total_cost"]');
|
||||
|
||||
function calculateTotal() {
|
||||
if (amountInput && priceInput && totalInput) {
|
||||
const amount = parseFloat(amountInput.value) || 0;
|
||||
const price = parseFloat(priceInput.value) || 0;
|
||||
const total = amount * price;
|
||||
totalInput.value = total.toFixed(2);
|
||||
}
|
||||
}
|
||||
|
||||
if (amountInput) {
|
||||
amountInput.addEventListener('input', calculateTotal);
|
||||
}
|
||||
|
||||
if (priceInput) {
|
||||
priceInput.addEventListener('input', calculateTotal);
|
||||
}
|
||||
|
||||
// Also calculate total when total is changed (reverse calculation)
|
||||
if (totalInput && amountInput) {
|
||||
totalInput.addEventListener('input', function() {
|
||||
const total = parseFloat(this.value) || 0;
|
||||
const amount = parseFloat(amountInput.value) || 0;
|
||||
if (amount > 0) {
|
||||
const pricePerLiter = total / amount;
|
||||
if (priceInput) {
|
||||
priceInput.value = pricePerLiter.toFixed(3);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// Check if page is served over HTTPS
|
||||
function isSecureContext() {
|
||||
return location.protocol === 'https:' || location.hostname === 'localhost' || location.hostname === '127.0.0.1';
|
||||
}
|
||||
|
||||
// Debug function for location issues
|
||||
function debugLocationInfo() {
|
||||
console.log('=== Location Debug Info ===');
|
||||
console.log('Protocol:', location.protocol);
|
||||
console.log('Hostname:', location.hostname);
|
||||
console.log('Is secure context:', isSecureContext());
|
||||
console.log('Geolocation supported:', !!navigator.geolocation);
|
||||
console.log('Permissions API supported:', !!navigator.permissions);
|
||||
|
||||
if (navigator.permissions) {
|
||||
navigator.permissions.query({name: 'geolocation'}).then(function(result) {
|
||||
console.log('Geolocation permission:', result.state);
|
||||
}).catch(function(error) {
|
||||
console.log('Permission query error:', error);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Fuel Station Search Functions
|
||||
window.findNearbyStations = function() {
|
||||
// Debug location setup
|
||||
debugLocationInfo();
|
||||
|
||||
// Check if we're in a secure context
|
||||
if (!isSecureContext()) {
|
||||
showStationSearchError('Geolocation requires HTTPS. Please access this page via HTTPS or use manual entry.');
|
||||
return;
|
||||
}
|
||||
|
||||
const modal = new bootstrap.Modal(document.getElementById('stationSearchModal'));
|
||||
modal.show();
|
||||
|
||||
// Reset modal content
|
||||
document.getElementById('stationSearchResults').innerHTML = `
|
||||
<div class="text-center">
|
||||
<div class="spinner-border" role="status">
|
||||
<span class="visually-hidden">Searching...</span>
|
||||
</div>
|
||||
<p class="mt-2">Finding nearby fuel stations...</p>
|
||||
</div>
|
||||
`;
|
||||
|
||||
// Get user's location with improved error handling
|
||||
if (navigator.geolocation) {
|
||||
console.log('Starting geolocation request...');
|
||||
// Update status to show we're requesting location
|
||||
document.getElementById('stationSearchResults').innerHTML = `
|
||||
<div class="text-center">
|
||||
<div class="spinner-border" role="status">
|
||||
<span class="visually-hidden">Getting location...</span>
|
||||
</div>
|
||||
<p class="mt-2">Requesting your location...</p>
|
||||
<small class="text-muted">Please allow location access when prompted</small>
|
||||
</div>
|
||||
`;
|
||||
|
||||
navigator.geolocation.getCurrentPosition(
|
||||
function(position) {
|
||||
const lat = position.coords.latitude;
|
||||
const lon = position.coords.longitude;
|
||||
console.log('Location obtained:', lat, lon);
|
||||
console.log('Accuracy:', position.coords.accuracy + 'm');
|
||||
console.log('Timestamp:', new Date(position.timestamp));
|
||||
searchNearbyStations(lat, lon);
|
||||
},
|
||||
function(error) {
|
||||
console.error('Geolocation error:', error);
|
||||
console.error('Error code:', error.code);
|
||||
console.error('Error message:', error.message);
|
||||
|
||||
let errorMessage = 'Unable to get your location. ';
|
||||
let showRetryOption = false;
|
||||
|
||||
switch(error.code) {
|
||||
case error.PERMISSION_DENIED:
|
||||
errorMessage += 'Location access was denied. Please enable location services, refresh the page, and try again.';
|
||||
if (!isSecureContext()) {
|
||||
errorMessage += ' Note: This page requires HTTPS for location access.';
|
||||
}
|
||||
break;
|
||||
case error.POSITION_UNAVAILABLE:
|
||||
errorMessage += 'Location information is unavailable. Please check your GPS settings or try again.';
|
||||
showRetryOption = true;
|
||||
break;
|
||||
case error.TIMEOUT:
|
||||
errorMessage += 'Location request timed out. Trying with lower accuracy...';
|
||||
// Try again with lower accuracy
|
||||
tryLowAccuracyLocation();
|
||||
return;
|
||||
default:
|
||||
errorMessage += 'An unknown error occurred while retrieving location.';
|
||||
showRetryOption = true;
|
||||
break;
|
||||
}
|
||||
|
||||
showStationSearchError(errorMessage, showRetryOption);
|
||||
},
|
||||
{
|
||||
enableHighAccuracy: true,
|
||||
timeout: 15000, // Increased timeout to 15 seconds
|
||||
maximumAge: 300000 // 5 minutes
|
||||
}
|
||||
);
|
||||
} else {
|
||||
showStationSearchError('Geolocation is not supported by this browser. Please enter station details manually.');
|
||||
}
|
||||
};
|
||||
|
||||
// Fallback function for low accuracy location
|
||||
function tryLowAccuracyLocation() {
|
||||
document.getElementById('stationSearchResults').innerHTML = `
|
||||
<div class="text-center">
|
||||
<div class="spinner-border" role="status">
|
||||
<span class="visually-hidden">Trying low accuracy...</span>
|
||||
</div>
|
||||
<p class="mt-2">Trying with lower accuracy...</p>
|
||||
</div>
|
||||
`;
|
||||
|
||||
navigator.geolocation.getCurrentPosition(
|
||||
function(position) {
|
||||
const lat = position.coords.latitude;
|
||||
const lon = position.coords.longitude;
|
||||
console.log('Low accuracy location obtained:', lat, lon);
|
||||
searchNearbyStations(lat, lon);
|
||||
},
|
||||
function(error) {
|
||||
console.error('Low accuracy geolocation error:', error);
|
||||
showStationSearchError('Unable to get your location even with low accuracy. Please enter station details manually or try again later.');
|
||||
},
|
||||
{
|
||||
enableHighAccuracy: false, // Use less accurate but faster location
|
||||
timeout: 30000, // Longer timeout for low accuracy
|
||||
maximumAge: 600000 // 10 minutes cache for low accuracy
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
function searchNearbyStations(lat, lon) {
|
||||
console.log('Searching for stations near:', lat, lon);
|
||||
|
||||
// Update status to show we're searching
|
||||
document.getElementById('stationSearchResults').innerHTML = `
|
||||
<div class="text-center">
|
||||
<div class="spinner-border" role="status">
|
||||
<span class="visually-hidden">Searching...</span>
|
||||
</div>
|
||||
<p class="mt-2">Searching for nearby fuel stations...</p>
|
||||
<small class="text-muted">This may take a few seconds</small>
|
||||
</div>
|
||||
`;
|
||||
|
||||
const overpassUrl = 'https://overpass-api.de/api/interpreter';
|
||||
const query = `
|
||||
[out:json][timeout:30];
|
||||
(
|
||||
node["amenity"="fuel"](around:5000,${lat},${lon});
|
||||
way["amenity"="fuel"](around:5000,${lat},${lon});
|
||||
relation["amenity"="fuel"](around:5000,${lat},${lon});
|
||||
);
|
||||
out center meta;
|
||||
`;
|
||||
|
||||
console.log('Overpass query:', query);
|
||||
|
||||
// Add timeout to the fetch request
|
||||
const controller = new AbortController();
|
||||
const timeoutId = setTimeout(() => controller.abort(), 30000); // 30 second timeout
|
||||
|
||||
fetch(overpassUrl, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
},
|
||||
body: 'data=' + encodeURIComponent(query),
|
||||
signal: controller.signal
|
||||
})
|
||||
.then(response => {
|
||||
clearTimeout(timeoutId);
|
||||
console.log('API response status:', response.status);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
||||
return response.json();
|
||||
})
|
||||
.then(data => {
|
||||
console.log('API response data:', data);
|
||||
|
||||
if (data.remark && data.remark.includes('timeout')) {
|
||||
throw new Error('API request timed out');
|
||||
}
|
||||
|
||||
if (data.elements && data.elements.length > 0) {
|
||||
console.log(`Found ${data.elements.length} stations`);
|
||||
displayStationResults(data.elements, lat, lon);
|
||||
} else {
|
||||
console.log('No stations found in API response');
|
||||
showStationSearchError('No fuel stations found within 5km of your location. Try searching manually or check a different area.');
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
clearTimeout(timeoutId);
|
||||
console.error('Error searching for stations:', error);
|
||||
|
||||
let errorMessage = 'Error searching for fuel stations. ';
|
||||
|
||||
if (error.name === 'AbortError') {
|
||||
errorMessage += 'The search timed out. Please try again or enter station details manually.';
|
||||
} else if (error.message.includes('HTTP error')) {
|
||||
errorMessage += 'The map service is temporarily unavailable. Please try again later.';
|
||||
} else if (error.message.includes('Failed to fetch')) {
|
||||
errorMessage += 'Network error. Please check your internet connection and try again.';
|
||||
} else {
|
||||
errorMessage += 'Please try again or enter station details manually.';
|
||||
}
|
||||
|
||||
showStationSearchError(errorMessage);
|
||||
});
|
||||
}
|
||||
|
||||
function displayStationResults(stations, userLat, userLon) {
|
||||
// Calculate distances and sort by distance
|
||||
const stationsWithDistance = stations.map(station => {
|
||||
const stationLat = station.lat || (station.center && station.center.lat);
|
||||
const stationLon = station.lon || (station.center && station.center.lon);
|
||||
|
||||
if (stationLat && stationLon) {
|
||||
const distance = calculateDistance(userLat, userLon, stationLat, stationLon);
|
||||
return {
|
||||
...station,
|
||||
distance: distance,
|
||||
displayLat: stationLat,
|
||||
displayLon: stationLon
|
||||
};
|
||||
}
|
||||
return null;
|
||||
}).filter(station => station !== null);
|
||||
|
||||
stationsWithDistance.sort((a, b) => a.distance - b.distance);
|
||||
|
||||
const resultsHTML = stationsWithDistance.map(station => {
|
||||
const name = station.tags.name || station.tags.brand || 'Unknown Station';
|
||||
const address = [
|
||||
station.tags['addr:street'],
|
||||
station.tags['addr:housenumber'],
|
||||
station.tags['addr:city'],
|
||||
station.tags['addr:postcode']
|
||||
].filter(Boolean).join(' ');
|
||||
|
||||
const brand = station.tags.brand || '';
|
||||
const operator = station.tags.operator || '';
|
||||
const displayName = brand || operator || name;
|
||||
|
||||
return `
|
||||
<div class="card mb-2 station-result" style="cursor: pointer;"
|
||||
onclick="selectStation('${displayName}', '${address}', ${station.displayLat}, ${station.displayLon})">
|
||||
<div class="card-body">
|
||||
<div class="d-flex justify-content-between align-items-start">
|
||||
<div>
|
||||
<h6 class="card-title mb-1">${displayName}</h6>
|
||||
<p class="card-text text-muted mb-0">${address}</p>
|
||||
${brand && brand !== displayName ? `<small class="text-muted">${brand}</small>` : ''}
|
||||
</div>
|
||||
<div class="text-end">
|
||||
<span class="badge bg-primary">${station.distance.toFixed(1)} km</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
}).join('');
|
||||
|
||||
document.getElementById('stationSearchResults').innerHTML = resultsHTML ||
|
||||
'<div class="text-center text-muted">No fuel stations found nearby.</div>';
|
||||
}
|
||||
|
||||
function calculateDistance(lat1, lon1, lat2, lon2) {
|
||||
const R = 6371; // Earth's radius in km
|
||||
const dLat = (lat2 - lat1) * Math.PI / 180;
|
||||
const dLon = (lon2 - lon1) * Math.PI / 180;
|
||||
const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
|
||||
Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) *
|
||||
Math.sin(dLon / 2) * Math.sin(dLon / 2);
|
||||
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
||||
return R * c;
|
||||
}
|
||||
|
||||
function showStationSearchError(message, showRetryOption = false) {
|
||||
const retryButton = showRetryOption ? `
|
||||
<button type="button" class="btn btn-outline-secondary me-2" onclick="findNearbyStations()">
|
||||
Try Again
|
||||
</button>
|
||||
` : '';
|
||||
|
||||
document.getElementById('stationSearchResults').innerHTML = `
|
||||
<div class="alert alert-warning" role="alert">
|
||||
<i class="fas fa-exclamation-triangle"></i> ${message}
|
||||
</div>
|
||||
<div class="mt-3">
|
||||
${retryButton}
|
||||
<button type="button" class="btn btn-outline-primary" onclick="showManualEntry()">
|
||||
Enter Station Details Manually
|
||||
</button>
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
|
||||
window.showManualEntry = function() {
|
||||
document.getElementById('stationSearchResults').innerHTML = `
|
||||
<div class="alert alert-info" role="alert">
|
||||
<h6>Manual Entry</h6>
|
||||
<p>Enter the station details manually:</p>
|
||||
</div>
|
||||
<form onsubmit="return selectManualStation(event)">
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Station Name</label>
|
||||
<input type="text" class="form-control" id="manual-station-name" placeholder="e.g., Shell, TOTAL, Aral" required>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Brand (optional)</label>
|
||||
<select class="form-select" id="manual-station-brand">
|
||||
<option value="">Select brand...</option>
|
||||
<option value="Shell">Shell</option>
|
||||
<option value="TOTAL">TOTAL</option>
|
||||
<option value="Aral">Aral</option>
|
||||
<option value="Esso">Esso</option>
|
||||
<option value="BP">BP</option>
|
||||
<option value="AGIP">AGIP</option>
|
||||
<option value="OMV">OMV</option>
|
||||
<option value="JET">JET</option>
|
||||
<option value="Other">Other</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Address</label>
|
||||
<input type="text" class="form-control" id="manual-station-address" placeholder="Street, City, Country" required>
|
||||
</div>
|
||||
<div class="d-flex justify-content-between">
|
||||
<button type="button" class="btn btn-secondary" onclick="findNearbyStations()">
|
||||
Back to Search
|
||||
</button>
|
||||
<button type="submit" class="btn btn-primary">
|
||||
Use This Station
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
`;
|
||||
};
|
||||
|
||||
window.selectManualStation = function(event) {
|
||||
event.preventDefault();
|
||||
|
||||
const stationName = document.getElementById('manual-station-name').value;
|
||||
const stationBrand = document.getElementById('manual-station-brand').value;
|
||||
const stationAddress = document.getElementById('manual-station-address').value;
|
||||
|
||||
// Use brand name if provided, otherwise use the entered name
|
||||
const finalName = stationBrand && stationBrand !== 'Other' ? stationBrand : stationName;
|
||||
|
||||
// Fill form fields
|
||||
document.querySelector('input[name="station_name"]').value = finalName;
|
||||
document.querySelector('input[name="location"]').value = stationAddress;
|
||||
|
||||
// Close modal
|
||||
const modal = bootstrap.Modal.getInstance(document.getElementById('stationSearchModal'));
|
||||
if (modal) {
|
||||
modal.hide();
|
||||
}
|
||||
|
||||
// Show success message
|
||||
const toast = document.createElement('div');
|
||||
toast.className = 'toast align-items-center text-white bg-success border-0';
|
||||
toast.setAttribute('role', 'alert');
|
||||
toast.setAttribute('aria-live', 'assertive');
|
||||
toast.setAttribute('aria-atomic', 'true');
|
||||
toast.innerHTML = `
|
||||
<div class="d-flex">
|
||||
<div class="toast-body">
|
||||
Station entered manually: ${finalName}
|
||||
</div>
|
||||
<button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast"></button>
|
||||
</div>
|
||||
`;
|
||||
|
||||
document.body.appendChild(toast);
|
||||
const bsToast = new bootstrap.Toast(toast);
|
||||
bsToast.show();
|
||||
|
||||
// Remove toast after it hides
|
||||
toast.addEventListener('hidden.bs.toast', function() {
|
||||
document.body.removeChild(toast);
|
||||
});
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
window.selectStation = function(name, address, lat, lon) {
|
||||
// Fill form fields
|
||||
document.querySelector('input[name="station_name"]').value = name;
|
||||
document.querySelector('input[name="location"]').value = address;
|
||||
|
||||
// Close modal
|
||||
const modal = bootstrap.Modal.getInstance(document.getElementById('stationSearchModal'));
|
||||
if (modal) {
|
||||
modal.hide();
|
||||
}
|
||||
|
||||
// Show success message
|
||||
const toast = document.createElement('div');
|
||||
toast.className = 'toast align-items-center text-white bg-success border-0';
|
||||
toast.setAttribute('role', 'alert');
|
||||
toast.setAttribute('aria-live', 'assertive');
|
||||
toast.setAttribute('aria-atomic', 'true');
|
||||
toast.innerHTML = `
|
||||
<div class="d-flex">
|
||||
<div class="toast-body">
|
||||
Station selected: ${name}
|
||||
</div>
|
||||
<button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast"></button>
|
||||
</div>
|
||||
`;
|
||||
|
||||
document.body.appendChild(toast);
|
||||
const bsToast = new bootstrap.Toast(toast);
|
||||
bsToast.show();
|
||||
|
||||
// Remove toast after it hides
|
||||
toast.addEventListener('hidden.bs.toast', function() {
|
||||
document.body.removeChild(toast);
|
||||
});
|
||||
};
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,360 @@
|
||||
package pages
|
||||
|
||||
import (
|
||||
"tankstopp/internal/currency"
|
||||
"tankstopp/internal/models"
|
||||
"tankstopp/internal/views/components"
|
||||
)
|
||||
|
||||
templ SettingsPage(user *models.User, username string, currencies []currency.Currency, successMessage, errorMessage string) {
|
||||
@components.BaseLayout("Settings", user, username) {
|
||||
@components.PageHeader("Manage your account", "Settings")
|
||||
<div class="page-body">
|
||||
<div class="container-xl">
|
||||
if successMessage != "" {
|
||||
<div class="row mb-3">
|
||||
<div class="col-12">
|
||||
@components.Alert("success", successMessage)
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
if errorMessage != "" {
|
||||
<div class="row mb-3">
|
||||
<div class="col-12">
|
||||
@components.Alert("danger", errorMessage)
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
<div class="row">
|
||||
<div class="col-12 col-lg-8">
|
||||
<!-- Profile Settings -->
|
||||
<div class="row mb-4">
|
||||
<div class="col-12">
|
||||
@components.Card("Profile Settings", "user") {
|
||||
@components.Form("post", "/settings/profile") {
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
@components.FormGroup("Username", "Your unique username") {
|
||||
@components.Input("username", "text", "Enter username", username, true)
|
||||
}
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
@components.FormGroup("Email", "Your email address") {
|
||||
@components.Input("email", "email", "Enter email", user.Email, true)
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
@components.FormGroup("Base Currency", "Default currency for fuel prices") {
|
||||
@components.CurrencySelect("base_currency", user.BaseCurrency, currencies)
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-12">
|
||||
@components.ButtonGroup() {
|
||||
@components.PrimaryButton("Save Profile", "save")
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
<!-- Application Preferences -->
|
||||
<div class="row mb-4">
|
||||
<div class="col-12">
|
||||
@components.Card("Application Preferences", "settings") {
|
||||
@components.Form("post", "/settings/preferences") {
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
@components.FormGroup("Distance Unit", "Unit for distance measurements") {
|
||||
@components.Select("distance_unit", false) {
|
||||
@components.Option("km", "Kilometers (km)", true)
|
||||
@components.Option("mi", "Miles (mi)", false)
|
||||
}
|
||||
}
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
@components.FormGroup("Volume Unit", "Unit for fuel volume measurements") {
|
||||
@components.Select("volume_unit", false) {
|
||||
@components.Option("L", "Liters (L)", true)
|
||||
@components.Option("gal", "Gallons (gal)", false)
|
||||
}
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
@components.FormGroup("Date Format", "How dates are displayed") {
|
||||
@components.Select("date_format", false) {
|
||||
@components.Option("DD/MM/YYYY", "DD/MM/YYYY", false)
|
||||
@components.Option("MM/DD/YYYY", "MM/DD/YYYY", false)
|
||||
@components.Option("YYYY-MM-DD", "YYYY-MM-DD", true)
|
||||
}
|
||||
}
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
@components.FormGroup("Language", "Application language") {
|
||||
@components.Select("language", false) {
|
||||
@components.Option("en", "English", true)
|
||||
@components.Option("de", "Deutsch", false)
|
||||
@components.Option("fr", "Français", false)
|
||||
@components.Option("es", "Español", false)
|
||||
}
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-12">
|
||||
@components.FormGroup("Notifications", "Email notification preferences") {
|
||||
@components.Switch("email_notifications", "Send email notifications", false)
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-12">
|
||||
@components.ButtonGroup() {
|
||||
@components.PrimaryButton("Save Preferences", "save")
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
<!-- Security Settings -->
|
||||
<div class="row mb-4">
|
||||
<div class="col-12">
|
||||
@components.Card("Security Settings", "lock") {
|
||||
@components.Form("post", "/settings/password") {
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
@components.FormGroup("Current Password", "Enter your current password") {
|
||||
@components.PasswordInput("current_password", "Current password", true)
|
||||
}
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
@components.FormGroup("New Password", "Choose a new password") {
|
||||
@components.PasswordInput("new_password", "New password", true)
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
@components.FormGroup("Confirm New Password", "Confirm your new password") {
|
||||
@components.PasswordInput("confirm_password", "Confirm new password", true)
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-12">
|
||||
@components.ButtonGroup() {
|
||||
@components.PrimaryButton("Change Password", "lock")
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
<!-- Data Management -->
|
||||
<div class="row mb-4">
|
||||
<div class="col-12">
|
||||
@components.Card("Data Management", "database") {
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
<h5>Export Data</h5>
|
||||
<p class="text-muted">Download your fuel stop data in various formats</p>
|
||||
@components.ButtonGroup() {
|
||||
<a href="/export/csv" class="btn btn-outline-primary">
|
||||
@components.Icon("download", 24)
|
||||
Export CSV
|
||||
</a>
|
||||
<a href="/export/json" class="btn btn-outline-primary">
|
||||
@components.Icon("download", 24)
|
||||
Export JSON
|
||||
</a>
|
||||
}
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<h5>Import Data</h5>
|
||||
<p class="text-muted">Import fuel stop data from a CSV file</p>
|
||||
@components.Form("post", "/import/csv") {
|
||||
<div class="mb-3">
|
||||
<input type="file" class="form-control" name="csv_file" accept=".csv" required/>
|
||||
</div>
|
||||
@components.PrimaryButton("Import CSV", "upload")
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
<hr class="my-4"/>
|
||||
<div class="row">
|
||||
<div class="col-12">
|
||||
<h5 class="text-danger">Danger Zone</h5>
|
||||
<p class="text-muted">These actions cannot be undone</p>
|
||||
@components.ButtonGroup() {
|
||||
<button type="button" class="btn btn-outline-danger" onclick="confirmClearData()">
|
||||
@components.Icon("trash", 24)
|
||||
Clear All Data
|
||||
</button>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
<!-- Account Management -->
|
||||
<div class="row mb-4">
|
||||
<div class="col-12">
|
||||
@components.Card("Account Management", "user") {
|
||||
<div class="row">
|
||||
<div class="col-12">
|
||||
<h5 class="text-danger">Delete Account</h5>
|
||||
<p class="text-muted">
|
||||
Permanently delete your account and all associated data. This action cannot be undone.
|
||||
</p>
|
||||
<div class="alert alert-danger" role="alert">
|
||||
<div class="d-flex">
|
||||
<div>
|
||||
@components.Icon("alert-triangle", 24)
|
||||
</div>
|
||||
<div>
|
||||
<strong>Warning:</strong> This will permanently delete your account, all vehicles, and all fuel stop records.
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<button type="button" class="btn btn-danger" onclick="confirmDeleteAccount()">
|
||||
@components.Icon("trash", 24)
|
||||
Delete Account
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<!-- Sidebar with Quick Stats -->
|
||||
<div class="col-12 col-lg-4">
|
||||
<div class="row">
|
||||
<div class="col-12">
|
||||
@components.Card("Account Summary", "info-circle") {
|
||||
<div class="list-group list-group-flush">
|
||||
<div class="list-group-item">
|
||||
<div class="d-flex justify-content-between">
|
||||
<span>Member since</span>
|
||||
<span class="text-muted">{ user.CreatedAt.Format("Jan 2006") }</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="list-group-item">
|
||||
<div class="d-flex justify-content-between">
|
||||
<span>Email</span>
|
||||
<span class="text-muted">{ user.Email }</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="list-group-item">
|
||||
<div class="d-flex justify-content-between">
|
||||
<span>Base currency</span>
|
||||
<span class="text-muted">{ user.BaseCurrency }</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="list-group-item">
|
||||
<div class="d-flex justify-content-between">
|
||||
<span>Account status</span>
|
||||
<span class="text-muted">
|
||||
<span class="badge bg-success">Active</span>
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
<div class="row mt-4">
|
||||
<div class="col-12">
|
||||
@components.Card("Quick Actions", "zap") {
|
||||
<div class="list-group list-group-flush">
|
||||
<a href="/add" class="list-group-item list-group-item-action d-flex align-items-center">
|
||||
<span class="me-2">
|
||||
@components.Icon("plus", 24)
|
||||
</span>
|
||||
Add Fuel Stop
|
||||
</a>
|
||||
<a href="/vehicles" class="list-group-item list-group-item-action d-flex align-items-center">
|
||||
<span class="me-2">
|
||||
@components.Icon("car", 24)
|
||||
</span>
|
||||
Manage Vehicles
|
||||
</a>
|
||||
<a href="/dashboard" class="list-group-item list-group-item-action d-flex align-items-center">
|
||||
<span class="me-2">
|
||||
@components.Icon("home", 24)
|
||||
</span>
|
||||
Go to Dashboard
|
||||
</a>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@SettingsScript()
|
||||
}
|
||||
}
|
||||
|
||||
script SettingsScript() {
|
||||
function confirmClearData() {
|
||||
if (confirm('Are you sure you want to clear all your data? This will delete all fuel stops and vehicles permanently.')) {
|
||||
if (confirm('This action cannot be undone. Are you absolutely sure?')) {
|
||||
// Create a form to submit the clear data request
|
||||
const form = document.createElement('form');
|
||||
form.method = 'POST';
|
||||
form.action = '/settings/clear-data';
|
||||
document.body.appendChild(form);
|
||||
form.submit();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function confirmDeleteAccount() {
|
||||
if (confirm('Are you sure you want to delete your account? This will permanently delete all your data.')) {
|
||||
if (confirm('This action cannot be undone. Type "DELETE" to confirm:')) {
|
||||
const confirmation = prompt('Please type "DELETE" to confirm account deletion:');
|
||||
if (confirmation === 'DELETE') {
|
||||
// Create a form to submit the delete account request
|
||||
const form = document.createElement('form');
|
||||
form.method = 'POST';
|
||||
form.action = '/settings/delete-account';
|
||||
document.body.appendChild(form);
|
||||
form.submit();
|
||||
} else {
|
||||
alert('Account deletion cancelled. The confirmation text did not match.');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize settings page
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
// Initialize tooltips
|
||||
var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'));
|
||||
var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
|
||||
return new bootstrap.Tooltip(tooltipTriggerEl);
|
||||
});
|
||||
|
||||
// Handle file input styling
|
||||
const fileInputs = document.querySelectorAll('input[type="file"]');
|
||||
fileInputs.forEach(function(input) {
|
||||
input.addEventListener('change', function(e) {
|
||||
const fileName = e.target.files[0] ? e.target.files[0].name : 'No file chosen';
|
||||
const label = e.target.parentNode.querySelector('.file-label');
|
||||
if (label) {
|
||||
label.textContent = fileName;
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,379 @@
|
||||
package pages
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"tankstopp/internal/models"
|
||||
"tankstopp/internal/views/components"
|
||||
)
|
||||
|
||||
templ VehiclesPage(user *models.User, username string, vehicles []models.Vehicle) {
|
||||
@components.BaseLayout("Vehicles", user, username) {
|
||||
@components.PageHeader("Manage your vehicles", "Vehicles")
|
||||
<div class="page-body">
|
||||
<div class="container-xl">
|
||||
<!-- Quick Actions -->
|
||||
<div class="row mb-4">
|
||||
<div class="col-12">
|
||||
<div class="d-flex justify-content-between align-items-center">
|
||||
<div>
|
||||
<h3 class="mb-0">Your Vehicles</h3>
|
||||
<p class="text-muted">Manage and track your vehicles</p>
|
||||
</div>
|
||||
<div>
|
||||
<a href="/vehicles/add" class="btn btn-primary">
|
||||
@components.Icon("plus", 24)
|
||||
Add Vehicle
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<!-- Vehicles Grid -->
|
||||
<div class="row">
|
||||
if len(vehicles) > 0 {
|
||||
for _, vehicle := range vehicles {
|
||||
<div class="col-md-6 col-lg-4 mb-4">
|
||||
@VehicleCard(vehicle)
|
||||
</div>
|
||||
}
|
||||
} else {
|
||||
<div class="col-12">
|
||||
@components.EmptyState("car", "No vehicles found", "Add your first vehicle to start tracking fuel expenses.", "Add Vehicle", "/vehicles/add")
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
<!-- Statistics Cards -->
|
||||
if len(vehicles) > 0 {
|
||||
<div class="row mt-4">
|
||||
<div class="col-12">
|
||||
<h4 class="mb-3">Vehicle Statistics</h4>
|
||||
</div>
|
||||
<div class="col-sm-6 col-lg-3">
|
||||
@components.Card("Total Vehicles", "car") {
|
||||
<div class="h2 mb-2">{ fmt.Sprintf("%d", len(vehicles)) }</div>
|
||||
<div class="text-muted">Registered vehicles</div>
|
||||
}
|
||||
</div>
|
||||
<div class="col-sm-6 col-lg-3">
|
||||
@components.Card("Active Vehicles", "status") {
|
||||
<div class="h2 mb-2">{ fmt.Sprintf("%d", countActiveVehicles(vehicles)) }</div>
|
||||
<div class="text-muted">Currently active</div>
|
||||
}
|
||||
</div>
|
||||
<div class="col-sm-6 col-lg-3">
|
||||
@components.Card("Brands", "brand") {
|
||||
<div class="h2 mb-2">{ fmt.Sprintf("%d", countUniqueBrands(vehicles)) }</div>
|
||||
<div class="text-muted">Different brands</div>
|
||||
}
|
||||
</div>
|
||||
<div class="col-sm-6 col-lg-3">
|
||||
@components.Card("Fuel Types", "fuel") {
|
||||
<div class="h2 mb-2">{ fmt.Sprintf("%d", countUniqueFuelTypes(vehicles)) }</div>
|
||||
<div class="text-muted">Different fuel types</div>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
|
||||
templ VehicleCard(vehicle models.Vehicle) {
|
||||
<div class="card">
|
||||
<div class="card-body">
|
||||
<div class="d-flex justify-content-between align-items-start mb-3">
|
||||
<div class="flex-fill">
|
||||
<h4 class="card-title mb-1">{ vehicle.Name }</h4>
|
||||
<div class="text-muted small">
|
||||
{ vehicle.Make } { vehicle.Model }
|
||||
</div>
|
||||
</div>
|
||||
<div class="dropdown">
|
||||
<button class="btn btn-sm btn-ghost-secondary" type="button" data-bs-toggle="dropdown">
|
||||
@components.Icon("dots-vertical", 24)
|
||||
</button>
|
||||
<div class="dropdown-menu dropdown-menu-end">
|
||||
<a class="dropdown-item" href={ templ.SafeURL(fmt.Sprintf("/vehicles/edit/%d", vehicle.ID)) }>
|
||||
@components.Icon("edit", 24)
|
||||
Edit
|
||||
</a>
|
||||
<div class="dropdown-divider"></div>
|
||||
<form method="POST" action={ fmt.Sprintf("/vehicles/delete/%d", vehicle.ID) } style="display: inline;" onsubmit="return confirmDelete(this)" data-item={ vehicle.Name }>
|
||||
<button type="submit" class="dropdown-item text-danger">
|
||||
@components.Icon("trash", 24)
|
||||
Delete
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-6">
|
||||
<div class="d-flex align-items-center mb-2">
|
||||
@components.Icon("license-plate", 24)
|
||||
<span class="ms-2 small">
|
||||
if vehicle.LicensePlate != "" {
|
||||
{ vehicle.LicensePlate }
|
||||
} else {
|
||||
<span class="text-muted">No plate</span>
|
||||
}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-6">
|
||||
<div class="d-flex align-items-center mb-2">
|
||||
@components.Icon("fuel", 24)
|
||||
<span class="ms-2 small">{ vehicle.FuelType }</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-6">
|
||||
<div class="d-flex align-items-center mb-2">
|
||||
@components.Icon("calendar", 24)
|
||||
<span class="ms-2 small">{ fmt.Sprintf("%d", vehicle.Year) }</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-6">
|
||||
<div class="d-flex align-items-center mb-2">
|
||||
if vehicle.IsActive {
|
||||
<span class="badge bg-success">Active</span>
|
||||
} else {
|
||||
<span class="badge bg-secondary">Inactive</span>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
if vehicle.Notes != "" {
|
||||
<div class="mt-3 pt-3 border-top">
|
||||
<small class="text-muted">{ vehicle.Notes }</small>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
|
||||
templ AddVehiclePage(user *models.User, username string) {
|
||||
@components.BaseLayout("Add Vehicle", user, username) {
|
||||
@components.PageHeader("Add a new vehicle", "Add Vehicle")
|
||||
<div class="page-body">
|
||||
<div class="container-xl">
|
||||
<div class="row justify-content-center">
|
||||
<div class="col-12 col-lg-8">
|
||||
@components.Card("Vehicle Information", "car") {
|
||||
@components.Form("post", "/vehicles/add") {
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
@components.FormGroup("Vehicle Name", "A friendly name for your vehicle") {
|
||||
@components.Input("name", "text", "e.g., My Car, Work Van", "", true)
|
||||
}
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
@components.FormGroup("License Plate", "Vehicle registration number") {
|
||||
@components.Input("license_plate", "text", "e.g., ABC-123", "", false)
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
@components.FormGroup("Make", "Vehicle manufacturer") {
|
||||
@VehicleBrandSelect("make", "")
|
||||
}
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
@components.FormGroup("Model", "Vehicle model") {
|
||||
@components.Input("model", "text", "e.g., Corolla, Golf", "", true)
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
@components.FormGroup("Year", "Manufacturing year") {
|
||||
@components.NumberInput("year", "2024", 0, "1", 1900, true)
|
||||
}
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
@components.FormGroup("Fuel Type", "Primary fuel type") {
|
||||
@components.FuelTypeSelect("fuel_type", "", true)
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
@components.FormGroup("Active", "Vehicle status") {
|
||||
@components.Switch("is_active", "Vehicle is active", true)
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-12">
|
||||
@components.FormGroup("Notes", "Additional information (optional)") {
|
||||
@components.TextArea("notes", "Add any additional notes about this vehicle...", "", 3)
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
@components.FormButtons("/vehicles", "Add Vehicle", "save")
|
||||
}
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
|
||||
templ EditVehiclePage(user *models.User, username string, vehicle *models.Vehicle) {
|
||||
@components.BaseLayout("Edit Vehicle", user, username) {
|
||||
@components.PageHeader("Update vehicle information", "Edit Vehicle")
|
||||
<div class="page-body">
|
||||
<div class="container-xl">
|
||||
<div class="row justify-content-center">
|
||||
<div class="col-12 col-lg-8">
|
||||
@components.Card("Vehicle Information", "car") {
|
||||
@components.Form("post", fmt.Sprintf("/vehicles/edit/%d", vehicle.ID)) {
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
@components.FormGroup("Vehicle Name", "A friendly name for your vehicle") {
|
||||
@components.Input("name", "text", "e.g., My Car, Work Van", vehicle.Name, true)
|
||||
}
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
@components.FormGroup("License Plate", "Vehicle registration number") {
|
||||
@components.Input("license_plate", "text", "e.g., ABC-123", vehicle.LicensePlate, false)
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
@components.FormGroup("Make", "Vehicle manufacturer") {
|
||||
@VehicleBrandSelect("make", vehicle.Make)
|
||||
}
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
@components.FormGroup("Model", "Vehicle model") {
|
||||
@components.Input("model", "text", "e.g., Corolla, Golf", vehicle.Model, true)
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
@components.FormGroup("Year", "Manufacturing year") {
|
||||
@components.NumberInput("year", "2024", float64(vehicle.Year), "1", 1900, true)
|
||||
}
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
@components.FormGroup("Fuel Type", "Primary fuel type") {
|
||||
@components.FuelTypeSelect("fuel_type", vehicle.FuelType, true)
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
@components.FormGroup("Active", "Vehicle status") {
|
||||
@components.Switch("is_active", "Vehicle is active", vehicle.IsActive)
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-12">
|
||||
@components.FormGroup("Notes", "Additional information (optional)") {
|
||||
@components.TextArea("notes", "Add any additional notes about this vehicle...", vehicle.Notes, 3)
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
@components.FormButtons("/vehicles", "Update Vehicle", "save")
|
||||
}
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
|
||||
templ VehicleBrandSelect(name, selectedMake string) {
|
||||
@components.Select(name, true) {
|
||||
@components.Option("", "Select make...", selectedMake == "")
|
||||
@components.Option("Audi", "Audi", selectedMake == "Audi")
|
||||
@components.Option("BMW", "BMW", selectedMake == "BMW")
|
||||
@components.Option("Mercedes-Benz", "Mercedes-Benz", selectedMake == "Mercedes-Benz")
|
||||
@components.Option("Volkswagen", "Volkswagen", selectedMake == "Volkswagen")
|
||||
@components.Option("Ford", "Ford", selectedMake == "Ford")
|
||||
@components.Option("Toyota", "Toyota", selectedMake == "Toyota")
|
||||
@components.Option("Honda", "Honda", selectedMake == "Honda")
|
||||
@components.Option("Nissan", "Nissan", selectedMake == "Nissan")
|
||||
@components.Option("Hyundai", "Hyundai", selectedMake == "Hyundai")
|
||||
@components.Option("Kia", "Kia", selectedMake == "Kia")
|
||||
@components.Option("Mazda", "Mazda", selectedMake == "Mazda")
|
||||
@components.Option("Subaru", "Subaru", selectedMake == "Subaru")
|
||||
@components.Option("Volvo", "Volvo", selectedMake == "Volvo")
|
||||
@components.Option("Peugeot", "Peugeot", selectedMake == "Peugeot")
|
||||
@components.Option("Renault", "Renault", selectedMake == "Renault")
|
||||
@components.Option("Citroen", "Citroen", selectedMake == "Citroen")
|
||||
@components.Option("Fiat", "Fiat", selectedMake == "Fiat")
|
||||
@components.Option("Opel", "Opel", selectedMake == "Opel")
|
||||
@components.Option("Skoda", "Skoda", selectedMake == "Skoda")
|
||||
@components.Option("SEAT", "SEAT", selectedMake == "SEAT")
|
||||
@components.Option("Chevrolet", "Chevrolet", selectedMake == "Chevrolet")
|
||||
@components.Option("Jeep", "Jeep", selectedMake == "Jeep")
|
||||
@components.Option("Land Rover", "Land Rover", selectedMake == "Land Rover")
|
||||
@components.Option("Jaguar", "Jaguar", selectedMake == "Jaguar")
|
||||
@components.Option("Porsche", "Porsche", selectedMake == "Porsche")
|
||||
@components.Option("Tesla", "Tesla", selectedMake == "Tesla")
|
||||
@components.Option("Other", "Other", selectedMake == "Other")
|
||||
}
|
||||
}
|
||||
|
||||
// Helper functions for statistics
|
||||
func countActiveVehicles(vehicles []models.Vehicle) int {
|
||||
count := 0
|
||||
for _, vehicle := range vehicles {
|
||||
if vehicle.IsActive {
|
||||
count++
|
||||
}
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
func countUniqueBrands(vehicles []models.Vehicle) int {
|
||||
brands := make(map[string]bool)
|
||||
for _, vehicle := range vehicles {
|
||||
if vehicle.Make != "" {
|
||||
brands[vehicle.Make] = true
|
||||
}
|
||||
}
|
||||
return len(brands)
|
||||
}
|
||||
|
||||
func countUniqueFuelTypes(vehicles []models.Vehicle) int {
|
||||
fuelTypes := make(map[string]bool)
|
||||
for _, vehicle := range vehicles {
|
||||
if vehicle.FuelType != "" {
|
||||
fuelTypes[vehicle.FuelType] = true
|
||||
}
|
||||
}
|
||||
return len(fuelTypes)
|
||||
}
|
||||
|
||||
script VehicleScript() {
|
||||
function confirmDelete(form) {
|
||||
const vehicleName = form.dataset.item;
|
||||
return confirm(`Are you sure you want to delete the vehicle "${vehicleName}"? This action cannot be undone and will also delete all associated fuel stops.`);
|
||||
}
|
||||
|
||||
// Initialize tooltips and dropdowns
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
// Initialize Bootstrap tooltips
|
||||
var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'));
|
||||
var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
|
||||
return new bootstrap.Tooltip(tooltipTriggerEl);
|
||||
});
|
||||
|
||||
// Set current year as default
|
||||
const yearInput = document.querySelector('input[name="year"]');
|
||||
if (yearInput && !yearInput.value) {
|
||||
yearInput.value = new Date().getFullYear();
|
||||
}
|
||||
});
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user