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:
237
handlers/book.go
Normal file
237
handlers/book.go
Normal file
@@ -0,0 +1,237 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"strconv"
|
||||
|
||||
"jd-book-uploader/models"
|
||||
"jd-book-uploader/services"
|
||||
"jd-book-uploader/utils"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
// UploadBook handles book upload requests
|
||||
func UploadBook(c *fiber.Ctx) error {
|
||||
ctx := c.Context()
|
||||
|
||||
// Parse multipart form
|
||||
form, err := c.MultipartForm()
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"error": "Failed to parse form data",
|
||||
})
|
||||
}
|
||||
|
||||
// Extract image file
|
||||
imageFiles := form.File["image"]
|
||||
if len(imageFiles) == 0 {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"error": "Image file is required",
|
||||
})
|
||||
}
|
||||
|
||||
imageFile := imageFiles[0]
|
||||
|
||||
// Validate file type
|
||||
allowedTypes := map[string]bool{
|
||||
"image/png": true,
|
||||
"image/jpeg": true,
|
||||
"image/jpg": true,
|
||||
"image/webp": true,
|
||||
}
|
||||
|
||||
if !allowedTypes[imageFile.Header.Get("Content-Type")] {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"error": "Invalid file type. Only PNG, JPEG, and WEBP are allowed",
|
||||
})
|
||||
}
|
||||
|
||||
// Validate file size (max 10MB)
|
||||
maxSize := int64(10 * 1024 * 1024) // 10MB
|
||||
if imageFile.Size > maxSize {
|
||||
return c.Status(fiber.StatusRequestEntityTooLarge).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"error": "File size exceeds 10MB limit",
|
||||
})
|
||||
}
|
||||
|
||||
// Read image file
|
||||
file, err := imageFile.Open()
|
||||
if err != nil {
|
||||
log.Printf("Failed to open image file: %v", err)
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"error": "Failed to read image file",
|
||||
})
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
imageData, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
log.Printf("Failed to read image data: %v", err)
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"error": "Failed to read image data",
|
||||
})
|
||||
}
|
||||
|
||||
// Extract form fields
|
||||
bookName := c.FormValue("book_name")
|
||||
costStr := c.FormValue("cost")
|
||||
priceStr := c.FormValue("price")
|
||||
discountStr := c.FormValue("discount")
|
||||
quantityStr := c.FormValue("quantity")
|
||||
publisherAuthor := c.FormValue("publisher_author")
|
||||
category := c.FormValue("category")
|
||||
description := c.FormValue("description")
|
||||
|
||||
// Validate required fields
|
||||
if err := validateBookFields(bookName, costStr, priceStr, quantityStr, publisherAuthor, category); err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"error": err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
// Parse numeric fields
|
||||
cost, err := strconv.ParseFloat(costStr, 64)
|
||||
if err != nil || cost <= 0 {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"error": "Invalid cost value",
|
||||
})
|
||||
}
|
||||
|
||||
price, err := strconv.ParseFloat(priceStr, 64)
|
||||
if err != nil || price <= 0 {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"error": "Invalid price value",
|
||||
})
|
||||
}
|
||||
|
||||
discount := 0.0
|
||||
if discountStr != "" {
|
||||
discount, err = strconv.ParseFloat(discountStr, 64)
|
||||
if err != nil || discount < 0 {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"error": "Invalid discount value",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
quantity, err := strconv.Atoi(quantityStr)
|
||||
if err != nil || quantity <= 0 {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"error": "Invalid quantity value",
|
||||
})
|
||||
}
|
||||
|
||||
// Generate slug from book name
|
||||
slug := utils.GenerateSlug(utils.CleanString(bookName))
|
||||
|
||||
// Check if slug already exists
|
||||
slugExists, err := services.BookSlugExists(ctx, slug)
|
||||
if err != nil {
|
||||
log.Printf("Failed to check slug existence: %v", err)
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"error": "Failed to validate product",
|
||||
})
|
||||
}
|
||||
|
||||
if slugExists {
|
||||
return c.Status(fiber.StatusConflict).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"error": fmt.Sprintf("A book with slug '%s' already exists", slug),
|
||||
})
|
||||
}
|
||||
|
||||
// Prepare filename: slug.png
|
||||
filename := fmt.Sprintf("%s.png", slug)
|
||||
|
||||
// Upload image to Firebase Storage with correct path
|
||||
imageURL, err := services.UploadImage(ctx, imageData, "/jd-bookshop/books", filename)
|
||||
if err != nil {
|
||||
log.Printf("Firebase upload failed: %v", err)
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"error": "Failed to upload image",
|
||||
})
|
||||
}
|
||||
|
||||
// Create book request
|
||||
bookReq := &models.BookCreateRequest{
|
||||
BookName: utils.CleanString(bookName),
|
||||
Cost: cost,
|
||||
Price: price,
|
||||
Discount: discount,
|
||||
Quantity: quantity,
|
||||
PublisherAuthor: utils.CleanString(publisherAuthor),
|
||||
Category: utils.CleanString(category),
|
||||
Description: utils.CleanString(description),
|
||||
ImageURL: imageURL,
|
||||
}
|
||||
|
||||
// Create book in database
|
||||
book, err := services.CreateBook(ctx, bookReq)
|
||||
if err != nil {
|
||||
log.Printf("Database insert failed: %v", err)
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"error": "Failed to create book",
|
||||
})
|
||||
}
|
||||
|
||||
// Return success response
|
||||
return c.Status(fiber.StatusCreated).JSON(fiber.Map{
|
||||
"success": true,
|
||||
"data": fiber.Map{
|
||||
"book_code": book.BookCode,
|
||||
"book_name": book.BookName,
|
||||
"image_url": book.ImageURL,
|
||||
"slug": book.Slug,
|
||||
"created_at": book.CreatedAt,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// validateBookFields validates required book fields
|
||||
func validateBookFields(bookName, cost, price, quantity, publisherAuthor, category string) error {
|
||||
if bookName == "" {
|
||||
return fmt.Errorf("book_name is required")
|
||||
}
|
||||
if len(bookName) > 200 {
|
||||
return fmt.Errorf("book_name exceeds 200 characters")
|
||||
}
|
||||
if cost == "" {
|
||||
return fmt.Errorf("cost is required")
|
||||
}
|
||||
if price == "" {
|
||||
return fmt.Errorf("price is required")
|
||||
}
|
||||
if quantity == "" {
|
||||
return fmt.Errorf("quantity is required")
|
||||
}
|
||||
if publisherAuthor == "" {
|
||||
return fmt.Errorf("publisher_author is required")
|
||||
}
|
||||
if len(publisherAuthor) > 200 {
|
||||
return fmt.Errorf("publisher_author exceeds 200 characters")
|
||||
}
|
||||
if category == "" {
|
||||
return fmt.Errorf("category is required")
|
||||
}
|
||||
if len(category) > 100 {
|
||||
return fmt.Errorf("category exceeds 100 characters")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
72
handlers/book_test.go
Normal file
72
handlers/book_test.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"mime/multipart"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func TestUploadBook(t *testing.T) {
|
||||
// This test requires database and Firebase to be initialized
|
||||
// Skip if not available
|
||||
t.Skip("Skipping UploadBook test - requires database and Firebase")
|
||||
|
||||
app := fiber.New()
|
||||
app.Post("/api/books", UploadBook)
|
||||
|
||||
// Create multipart form with test data
|
||||
body := &bytes.Buffer{}
|
||||
writer := multipart.NewWriter(body)
|
||||
|
||||
// Add form fields
|
||||
writer.WriteField("book_name", "Test Book")
|
||||
writer.WriteField("cost", "10.50")
|
||||
writer.WriteField("price", "15.99")
|
||||
writer.WriteField("quantity", "100")
|
||||
writer.WriteField("publisher_author", "Test Publisher")
|
||||
writer.WriteField("category", "Fiction")
|
||||
|
||||
// Add image file
|
||||
part, _ := writer.CreateFormFile("image", "test.png")
|
||||
part.Write([]byte("fake image data"))
|
||||
|
||||
writer.Close()
|
||||
|
||||
req := httptest.NewRequest("POST", "/api/books", body)
|
||||
req.Header.Set("Content-Type", writer.FormDataContentType())
|
||||
|
||||
resp, err := app.Test(req)
|
||||
if err != nil {
|
||||
t.Fatalf("Test request failed: %v", err)
|
||||
}
|
||||
|
||||
if resp.StatusCode != fiber.StatusCreated {
|
||||
t.Errorf("Expected status %d, got %d", fiber.StatusCreated, resp.StatusCode)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUploadBook_ValidationErrors(t *testing.T) {
|
||||
app := fiber.New()
|
||||
app.Post("/api/books", UploadBook)
|
||||
|
||||
// Test missing required field
|
||||
body := &bytes.Buffer{}
|
||||
writer := multipart.NewWriter(body)
|
||||
writer.WriteField("book_name", "") // Empty book name
|
||||
writer.Close()
|
||||
|
||||
req := httptest.NewRequest("POST", "/api/books", body)
|
||||
req.Header.Set("Content-Type", writer.FormDataContentType())
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
52
handlers/health.go
Normal file
52
handlers/health.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"jd-book-uploader/services"
|
||||
)
|
||||
|
||||
// HealthCheck handles health check requests
|
||||
func HealthCheck(c *fiber.Ctx) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
status := fiber.Map{
|
||||
"status": "ok",
|
||||
}
|
||||
|
||||
// Check database connection
|
||||
if services.DB != nil {
|
||||
err := services.DB.Ping(ctx)
|
||||
if err != nil {
|
||||
status["database"] = "disconnected"
|
||||
status["status"] = "degraded"
|
||||
return c.Status(fiber.StatusServiceUnavailable).JSON(status)
|
||||
}
|
||||
status["database"] = "connected"
|
||||
} else {
|
||||
status["database"] = "not_initialized"
|
||||
status["status"] = "degraded"
|
||||
return c.Status(fiber.StatusServiceUnavailable).JSON(status)
|
||||
}
|
||||
|
||||
// Check Firebase connection
|
||||
if services.FirebaseClient != nil {
|
||||
// Try to get bucket to verify connection
|
||||
bucket, err := services.FirebaseClient.DefaultBucket()
|
||||
if err != nil || bucket == nil {
|
||||
status["firebase"] = "disconnected"
|
||||
status["status"] = "degraded"
|
||||
return c.Status(fiber.StatusServiceUnavailable).JSON(status)
|
||||
}
|
||||
status["firebase"] = "connected"
|
||||
} else {
|
||||
status["firebase"] = "not_initialized"
|
||||
status["status"] = "degraded"
|
||||
return c.Status(fiber.StatusServiceUnavailable).JSON(status)
|
||||
}
|
||||
|
||||
return c.JSON(status)
|
||||
}
|
||||
23
handlers/health_test.go
Normal file
23
handlers/health_test.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func TestHealthCheck(t *testing.T) {
|
||||
app := fiber.New()
|
||||
app.Get("/health", HealthCheck)
|
||||
|
||||
req := httptest.NewRequest("GET", "/health", nil)
|
||||
resp, err := app.Test(req)
|
||||
if err != nil {
|
||||
t.Fatalf("Test request failed: %v", err)
|
||||
}
|
||||
|
||||
if resp.StatusCode != fiber.StatusOK && resp.StatusCode != fiber.StatusServiceUnavailable {
|
||||
t.Errorf("Expected status %d or %d, got %d", fiber.StatusOK, fiber.StatusServiceUnavailable, resp.StatusCode)
|
||||
}
|
||||
}
|
||||
234
handlers/stationery.go
Normal file
234
handlers/stationery.go
Normal file
@@ -0,0 +1,234 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"strconv"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"jd-book-uploader/models"
|
||||
"jd-book-uploader/services"
|
||||
"jd-book-uploader/utils"
|
||||
)
|
||||
|
||||
// UploadStationery handles stationery upload requests
|
||||
func UploadStationery(c *fiber.Ctx) error {
|
||||
ctx := c.Context()
|
||||
|
||||
// Parse multipart form
|
||||
form, err := c.MultipartForm()
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"error": "Failed to parse form data",
|
||||
})
|
||||
}
|
||||
|
||||
// Extract image file
|
||||
imageFiles := form.File["image"]
|
||||
if len(imageFiles) == 0 {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"error": "Image file is required",
|
||||
})
|
||||
}
|
||||
|
||||
imageFile := imageFiles[0]
|
||||
|
||||
// Validate file type
|
||||
allowedTypes := map[string]bool{
|
||||
"image/png": true,
|
||||
"image/jpeg": true,
|
||||
"image/jpg": true,
|
||||
"image/webp": true,
|
||||
}
|
||||
|
||||
if !allowedTypes[imageFile.Header.Get("Content-Type")] {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"error": "Invalid file type. Only PNG, JPEG, and WEBP are allowed",
|
||||
})
|
||||
}
|
||||
|
||||
// Validate file size (max 10MB)
|
||||
maxSize := int64(10 * 1024 * 1024) // 10MB
|
||||
if imageFile.Size > maxSize {
|
||||
return c.Status(fiber.StatusRequestEntityTooLarge).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"error": "File size exceeds 10MB limit",
|
||||
})
|
||||
}
|
||||
|
||||
// Read image file
|
||||
file, err := imageFile.Open()
|
||||
if err != nil {
|
||||
log.Printf("Failed to open image file: %v", err)
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"error": "Failed to read image file",
|
||||
})
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
imageData, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
log.Printf("Failed to read image data: %v", err)
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"error": "Failed to read image data",
|
||||
})
|
||||
}
|
||||
|
||||
// Extract form fields
|
||||
stationeryName := c.FormValue("stationery_name")
|
||||
costStr := c.FormValue("cost")
|
||||
priceStr := c.FormValue("price")
|
||||
discountStr := c.FormValue("discount")
|
||||
quantityStr := c.FormValue("quantity")
|
||||
color := c.FormValue("color")
|
||||
material := c.FormValue("material")
|
||||
dimensions := c.FormValue("dimensions")
|
||||
category := c.FormValue("category")
|
||||
description := c.FormValue("description")
|
||||
|
||||
// Validate required fields
|
||||
if err := validateStationeryFields(stationeryName, costStr, priceStr, quantityStr, category); err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"error": err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
// Parse numeric fields
|
||||
cost, err := strconv.ParseFloat(costStr, 64)
|
||||
if err != nil || cost <= 0 {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"error": "Invalid cost value",
|
||||
})
|
||||
}
|
||||
|
||||
price, err := strconv.ParseFloat(priceStr, 64)
|
||||
if err != nil || price <= 0 {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"error": "Invalid price value",
|
||||
})
|
||||
}
|
||||
|
||||
discount := 0.0
|
||||
if discountStr != "" {
|
||||
discount, err = strconv.ParseFloat(discountStr, 64)
|
||||
if err != nil || discount < 0 {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"error": "Invalid discount value",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
quantity, err := strconv.Atoi(quantityStr)
|
||||
if err != nil || quantity <= 0 {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"error": "Invalid quantity value",
|
||||
})
|
||||
}
|
||||
|
||||
// Generate slug from stationery name
|
||||
slug := utils.GenerateSlug(utils.CleanString(stationeryName))
|
||||
|
||||
// Check if slug already exists
|
||||
slugExists, err := services.StationerySlugExists(ctx, slug)
|
||||
if err != nil {
|
||||
log.Printf("Failed to check slug existence: %v", err)
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"error": "Failed to validate product",
|
||||
})
|
||||
}
|
||||
|
||||
if slugExists {
|
||||
return c.Status(fiber.StatusConflict).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"error": fmt.Sprintf("A stationery item with slug '%s' already exists", slug),
|
||||
})
|
||||
}
|
||||
|
||||
// Prepare filename: slug.png
|
||||
filename := fmt.Sprintf("%s.png", slug)
|
||||
|
||||
// Upload image to Firebase Storage with correct path
|
||||
imageURL, err := services.UploadImage(ctx, imageData, "/jd-bookshop/stationery", filename)
|
||||
if err != nil {
|
||||
log.Printf("Firebase upload failed: %v", err)
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"error": "Failed to upload image",
|
||||
})
|
||||
}
|
||||
|
||||
// Create stationery request
|
||||
stationeryReq := &models.StationeryCreateRequest{
|
||||
StationeryName: utils.CleanString(stationeryName),
|
||||
Cost: cost,
|
||||
Price: price,
|
||||
Discount: discount,
|
||||
Quantity: quantity,
|
||||
Color: utils.CleanString(color),
|
||||
Material: utils.CleanString(material),
|
||||
Dimensions: utils.CleanString(dimensions),
|
||||
Category: utils.CleanString(category),
|
||||
Description: utils.CleanString(description),
|
||||
ImageURL: imageURL,
|
||||
}
|
||||
|
||||
// Create stationery in database
|
||||
stationery, err := services.CreateStationery(ctx, stationeryReq)
|
||||
if err != nil {
|
||||
log.Printf("Database insert failed: %v", err)
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"error": "Failed to create stationery",
|
||||
})
|
||||
}
|
||||
|
||||
// Return success response
|
||||
return c.Status(fiber.StatusCreated).JSON(fiber.Map{
|
||||
"success": true,
|
||||
"data": fiber.Map{
|
||||
"stationery_code": stationery.StationeryCode,
|
||||
"stationery_name": stationery.StationeryName,
|
||||
"image_url": stationery.ImageURL,
|
||||
"slug": stationery.Slug,
|
||||
"created_at": stationery.CreatedAt,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// validateStationeryFields validates required stationery fields
|
||||
func validateStationeryFields(stationeryName, cost, price, quantity, category string) error {
|
||||
if stationeryName == "" {
|
||||
return fmt.Errorf("stationery_name is required")
|
||||
}
|
||||
if len(stationeryName) > 200 {
|
||||
return fmt.Errorf("stationery_name exceeds 200 characters")
|
||||
}
|
||||
if cost == "" {
|
||||
return fmt.Errorf("cost is required")
|
||||
}
|
||||
if price == "" {
|
||||
return fmt.Errorf("price is required")
|
||||
}
|
||||
if quantity == "" {
|
||||
return fmt.Errorf("quantity is required")
|
||||
}
|
||||
if category == "" {
|
||||
return fmt.Errorf("category is required")
|
||||
}
|
||||
if len(category) > 100 {
|
||||
return fmt.Errorf("category exceeds 100 characters")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
72
handlers/stationery_test.go
Normal file
72
handlers/stationery_test.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"mime/multipart"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func TestUploadStationery(t *testing.T) {
|
||||
// This test requires database and Firebase to be initialized
|
||||
// Skip if not available
|
||||
t.Skip("Skipping UploadStationery test - requires database and Firebase")
|
||||
|
||||
app := fiber.New()
|
||||
app.Post("/api/stationery", UploadStationery)
|
||||
|
||||
// Create multipart form with test data
|
||||
body := &bytes.Buffer{}
|
||||
writer := multipart.NewWriter(body)
|
||||
|
||||
// Add form fields
|
||||
writer.WriteField("stationery_name", "Test Pen")
|
||||
writer.WriteField("cost", "2.50")
|
||||
writer.WriteField("price", "5.99")
|
||||
writer.WriteField("quantity", "200")
|
||||
writer.WriteField("category", "Writing")
|
||||
writer.WriteField("color", "Blue")
|
||||
|
||||
// Add image file
|
||||
part, _ := writer.CreateFormFile("image", "test.png")
|
||||
part.Write([]byte("fake image data"))
|
||||
|
||||
writer.Close()
|
||||
|
||||
req := httptest.NewRequest("POST", "/api/stationery", body)
|
||||
req.Header.Set("Content-Type", writer.FormDataContentType())
|
||||
|
||||
resp, err := app.Test(req)
|
||||
if err != nil {
|
||||
t.Fatalf("Test request failed: %v", err)
|
||||
}
|
||||
|
||||
if resp.StatusCode != fiber.StatusCreated {
|
||||
t.Errorf("Expected status %d, got %d", fiber.StatusCreated, resp.StatusCode)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUploadStationery_ValidationErrors(t *testing.T) {
|
||||
app := fiber.New()
|
||||
app.Post("/api/stationery", UploadStationery)
|
||||
|
||||
// Test missing required field
|
||||
body := &bytes.Buffer{}
|
||||
writer := multipart.NewWriter(body)
|
||||
writer.WriteField("stationery_name", "") // Empty stationery name
|
||||
writer.Close()
|
||||
|
||||
req := httptest.NewRequest("POST", "/api/stationery", body)
|
||||
req.Header.Set("Content-Type", writer.FormDataContentType())
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user