Add initial project structure with core functionality for book and stationery uploads

- Created main application entry point in `main.go`.
- Added configuration management in `config/config.go` and tests in `config/config_test.go`.
- Implemented handlers for book and stationery uploads in `handlers/book.go` and `handlers/stationery.go`, including validation logic.
- Established database connection and services in `services/database.go` and `services/book_service.go`.
- Defined models for books and stationery in `models/book.go` and `models/stationery.go`.
- Set up Firebase integration for image uploads in `services/firebase.go`.
- Created migration scripts for database schema in `migrations/001_create_tables.sql` and subsequent updates.
- Added CORS and error handling middleware.
- Included comprehensive tests for handlers, services, and utilities.
- Documented API endpoints and usage in `README.md` and migration instructions in `migrations/README.md`.
- Introduced `.gitignore` to exclude unnecessary files and directories.
- Added Go module support with `go.mod` and `go.sum` files.
- Implemented utility functions for slug generation and validation in `utils/slug.go` and `utils/validation.go`.
This commit is contained in:
ianshaloom
2025-11-21 08:50:27 +03:00
commit ebeae34e01
39 changed files with 4044 additions and 0 deletions

View File

@@ -0,0 +1,157 @@
package services
import (
"context"
"errors"
"fmt"
"time"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
"jd-book-uploader/models"
"jd-book-uploader/utils"
)
// CreateStationery creates a new stationery record in the database
func CreateStationery(ctx context.Context, req *models.StationeryCreateRequest) (*models.Stationery, error) {
if DB == nil {
return nil, fmt.Errorf("database connection not initialized")
}
// Generate UUID for stationery_code
stationeryCode := uuid.New().String()
// Generate slug from stationery name
slug := utils.GenerateSlug(req.StationeryName)
// Set default discount if not provided
discount := req.Discount
if discount < 0 {
discount = 0
}
now := time.Now()
// Insert stationery into database
query := `
INSERT INTO stationery (
stationery_code, stationery_name, cost, price, discount, quantity,
color, material, dimensions, category, description, image_url, slug,
created_at, updated_at
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15
) RETURNING
stationery_code, stationery_name, cost, price, discount, quantity,
color, material, dimensions, category, description, image_url, slug,
created_at, updated_at
`
var stationery models.Stationery
err := DB.QueryRow(ctx, query,
stationeryCode,
req.StationeryName,
req.Cost,
req.Price,
discount,
req.Quantity,
req.Color,
req.Material,
req.Dimensions,
req.Category,
req.Description,
req.ImageURL,
slug,
now,
now,
).Scan(
&stationery.StationeryCode,
&stationery.StationeryName,
&stationery.Cost,
&stationery.Price,
&stationery.Discount,
&stationery.Quantity,
&stationery.Color,
&stationery.Material,
&stationery.Dimensions,
&stationery.Category,
&stationery.Description,
&stationery.ImageURL,
&stationery.Slug,
&stationery.CreatedAt,
&stationery.UpdatedAt,
)
if err != nil {
// Check for duplicate key error
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) {
if pgErr.Code == "23505" { // unique_violation
return nil, fmt.Errorf("stationery with this code already exists")
}
}
return nil, fmt.Errorf("failed to create stationery: %w", err)
}
return &stationery, nil
}
// GetStationeryByCode retrieves a stationery item by its code
func GetStationeryByCode(ctx context.Context, stationeryCode string) (*models.Stationery, error) {
if DB == nil {
return nil, fmt.Errorf("database connection not initialized")
}
query := `
SELECT
stationery_code, stationery_name, cost, price, discount, quantity,
color, material, dimensions, category, description, image_url, slug,
created_at, updated_at
FROM stationery
WHERE stationery_code = $1
`
var stationery models.Stationery
err := DB.QueryRow(ctx, query, stationeryCode).Scan(
&stationery.StationeryCode,
&stationery.StationeryName,
&stationery.Cost,
&stationery.Price,
&stationery.Discount,
&stationery.Quantity,
&stationery.Color,
&stationery.Material,
&stationery.Dimensions,
&stationery.Category,
&stationery.Description,
&stationery.ImageURL,
&stationery.Slug,
&stationery.CreatedAt,
&stationery.UpdatedAt,
)
if err != nil {
if err == pgx.ErrNoRows {
return nil, fmt.Errorf("stationery not found")
}
return nil, fmt.Errorf("failed to get stationery: %w", err)
}
return &stationery, nil
}
// StationerySlugExists checks if a slug already exists in the stationery table
func StationerySlugExists(ctx context.Context, slug string) (bool, error) {
if DB == nil {
return false, fmt.Errorf("database connection not initialized")
}
query := `SELECT EXISTS(SELECT 1 FROM stationery WHERE slug = $1)`
var exists bool
err := DB.QueryRow(ctx, query, slug).Scan(&exists)
if err != nil {
return false, fmt.Errorf("failed to check slug existence: %w", err)
}
return exists, nil
}