refactor: restructure in entirety
This commit is contained in:
63
internal/database/buyer.go
Normal file
63
internal/database/buyer.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"billit/internal/models"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// CreateBuyerDetails creates a new buyer details entry
|
||||
func (s *service) CreateBuyerDetails(userID string, name string, details string) (*models.BuyerDetails, error) {
|
||||
id := fmt.Sprintf("%d", time.Now().UnixNano())
|
||||
_, err := s.db.Exec(`INSERT INTO buyer_details (id, user_id, name, details) VALUES (?, ?, ?, ?)`, id, userID, name, details)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.GetBuyerDetails(id, userID)
|
||||
}
|
||||
|
||||
// UpdateBuyerDetails updates an existing buyer details entry
|
||||
func (s *service) UpdateBuyerDetails(id string, userID string, name string, details string) error {
|
||||
_, err := s.db.Exec(`UPDATE buyer_details SET name = ?, details = ? WHERE id = ? AND user_id = ?`, name, details, id, userID)
|
||||
return err
|
||||
}
|
||||
|
||||
// GetBuyerDetails retrieves a buyer details entry by ID
|
||||
func (s *service) GetBuyerDetails(id string, userID string) (*models.BuyerDetails, error) {
|
||||
var b models.BuyerDetails
|
||||
err := s.db.QueryRow(`SELECT id, user_id, name, details, created_at FROM buyer_details WHERE id = ? AND user_id = ?`, id, userID).
|
||||
Scan(&b.ID, &b.UserID, &b.Name, &b.Details, &b.CreatedAt)
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &b, nil
|
||||
}
|
||||
|
||||
// GetAllBuyerDetails retrieves all buyer details for a user
|
||||
func (s *service) GetAllBuyerDetails(userID string) ([]models.BuyerDetails, error) {
|
||||
rows, err := s.db.Query(`SELECT id, user_id, name, details, created_at FROM buyer_details WHERE user_id = ? ORDER BY name`, userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var buyers []models.BuyerDetails
|
||||
for rows.Next() {
|
||||
var b models.BuyerDetails
|
||||
if err := rows.Scan(&b.ID, &b.UserID, &b.Name, &b.Details, &b.CreatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
buyers = append(buyers, b)
|
||||
}
|
||||
return buyers, nil
|
||||
}
|
||||
|
||||
// DeleteBuyerDetails removes a buyer details entry
|
||||
func (s *service) DeleteBuyerDetails(id string, userID string) error {
|
||||
_, err := s.db.Exec(`DELETE FROM buyer_details WHERE id = ? AND user_id = ?`, id, userID)
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user