Files
billit/internal/database/service.go
2025-12-06 15:31:18 +05:30

142 lines
4.0 KiB
Go

package database
import (
"billit/internal/models"
"context"
"database/sql"
"fmt"
"log"
"os"
"strconv"
"time"
_ "github.com/mattn/go-sqlite3"
)
// Service represents the database service interface
type Service interface {
// Health returns database health status
Health() map[string]string
// Close closes the database connection
Close() error
// Buyer methods
CreateBuyerDetails(userID string, name string, details string) (*models.BuyerDetails, error)
UpdateBuyerDetails(id string, userID string, name string, details string) error
GetBuyerDetails(id string, userID string) (*models.BuyerDetails, error)
GetAllBuyerDetails(userID string) ([]models.BuyerDetails, error)
DeleteBuyerDetails(id string, userID string) error
// User methods
CreateUser(email, passwordHash string) (*models.User, error)
GetUserByEmail(email string) (*models.User, error)
GetUserByID(id string) (*models.User, error)
UpdateUserPassword(id string, passwordHash string) error
UpdateUserDetails(id string, companyDetails string, bankDetails string, invoicePrefix string) error
// Invoice methods
GetNextInvoiceNumber(userID string) (string, error)
CreateInvoice(id string, humanReadableID string, data interface{}, userID string) error
GetInvoice(id string, userID string) (*models.Invoice, error)
GetAllInvoices(userID string) ([]models.Invoice, error)
GetRecentInvoices(userID string, limit int) ([]models.Invoice, error)
// Product methods
CreateProduct(p models.Product, userID string) error
UpdateProduct(p models.Product, userID string) error
GetAllProducts(userID string) ([]models.Product, error)
GetProductBySKU(sku string, userID string) (*models.Product, error)
DeleteProduct(sku string, userID string) error
GetRecentProducts(userID string, limit int) ([]models.Product, error)
}
type service struct {
db *sql.DB
}
var dbInstance *service
// New creates a new database service
func New() Service {
// Reuse connection if already established
if dbInstance != nil {
return dbInstance
}
dbPath := os.Getenv("DB_PATH")
if dbPath == "" {
dbPath = "./db/dev.db"
}
db, err := sql.Open("sqlite3", dbPath)
if err != nil {
log.Fatal(err)
}
// Check connection
if err := db.Ping(); err != nil {
log.Fatal("Could not ping database:", err)
}
dbInstance = &service{
db: db,
}
return dbInstance
}
// Health checks the health of the database connection
func (s *service) Health() map[string]string {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
stats := make(map[string]string)
// Ping the database
err := s.db.PingContext(ctx)
if err != nil {
stats["status"] = "down"
stats["error"] = fmt.Sprintf("db down: %v", err)
log.Printf("db down: %v", err)
return stats
}
// Database is up, add more statistics
stats["status"] = "up"
stats["message"] = "It's healthy"
// Get database stats
dbStats := s.db.Stats()
stats["open_connections"] = strconv.Itoa(dbStats.OpenConnections)
stats["in_use"] = strconv.Itoa(dbStats.InUse)
stats["idle"] = strconv.Itoa(dbStats.Idle)
stats["wait_count"] = strconv.FormatInt(dbStats.WaitCount, 10)
stats["wait_duration"] = dbStats.WaitDuration.String()
stats["max_idle_closed"] = strconv.FormatInt(dbStats.MaxIdleClosed, 10)
stats["max_lifetime_closed"] = strconv.FormatInt(dbStats.MaxLifetimeClosed, 10)
if dbStats.OpenConnections > 40 {
stats["message"] = "The database is experiencing heavy load."
}
if dbStats.WaitCount > 1000 {
stats["message"] = "The database has a high number of wait events, indicating potential bottlenecks."
}
if dbStats.MaxIdleClosed > int64(dbStats.OpenConnections)/2 {
stats["message"] = "Many idle connections are being closed, consider revising the connection pool settings."
}
if dbStats.MaxLifetimeClosed > int64(dbStats.OpenConnections)/2 {
stats["message"] = "Many connections are being closed due to max lifetime, consider increasing max lifetime or revising connection usage."
}
return stats
}
// Close closes the database connection
func (s *service) Close() error {
return s.db.Close()
}