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,63 @@
package middleware
import (
"errors"
"net/http/httptest"
"testing"
"github.com/gofiber/fiber/v2"
)
func TestErrorHandler(t *testing.T) {
app := fiber.New()
app.Use(ErrorHandler)
app.Get("/error", func(c *fiber.Ctx) error {
return fiber.NewError(fiber.StatusBadRequest, "Bad Request")
})
app.Get("/panic", func(c *fiber.Ctx) error {
return errors.New("some error")
})
// Test Fiber error
req := httptest.NewRequest("GET", "/error", nil)
resp, err := app.Test(req)
if err != nil {
t.Fatalf("Test request failed: %v", err)
}
if resp.StatusCode != fiber.StatusBadRequest {
t.Errorf("Expected status %d, got %d", fiber.StatusBadRequest, resp.StatusCode)
}
// Test generic error
req = httptest.NewRequest("GET", "/panic", nil)
resp, err = app.Test(req)
if err != nil {
t.Fatalf("Test request failed: %v", err)
}
if resp.StatusCode != fiber.StatusInternalServerError {
t.Errorf("Expected status %d, got %d", fiber.StatusInternalServerError, resp.StatusCode)
}
}
func TestRecoverHandler(t *testing.T) {
app := fiber.New()
app.Use(RecoverHandler())
app.Get("/panic", func(c *fiber.Ctx) error {
panic("test panic")
})
req := httptest.NewRequest("GET", "/panic", nil)
resp, err := app.Test(req)
if err != nil {
t.Fatalf("Test request failed: %v", err)
}
if resp.StatusCode != fiber.StatusInternalServerError {
t.Errorf("Expected status %d, got %d", fiber.StatusInternalServerError, resp.StatusCode)
}
}