A simple Go REST API for booking visits with holiday validation and duplicate prevention.
- Visit Booking: Book visits with first name, last name, and visit date
- Holiday Validation: Prevents booking on UK public holidays using nager.date API
- Duplicate Prevention: Only one visit allowed per date
- Database: SQLite with migration support
- Testing: Comprehensive unit tests with mocking
Book a new visit.
Request Body:
{
"first_name": "Carlos",
"last_name": "Vieira",
"visit_date": "2025-12-25"
}Response (201):
{
"id": "550e8400-e29b-41d4-a716-446655440000",
"first_name": "Carlos",
"last_name": "Vieira",
"visit_date": "2025-12-25",
"created_at": "2025-09-23T13:00:00Z",
"updated_at": "2025-09-23T13:00:00Z"
}Error Responses:
400- Invalid request body500- Internal server error
.
├── cmd/
│ └── main.go # Application entry point
├── configs/ # Configuration management
├── migrations/ # Database migration
├── models/ # Data models
├── repository/ # Database operations
├── server/ # HTTP server and endpoint
├── services/
│ ├── holiday_service.go # Holiday API integration
- Basic handler-based approach for better maintainability
- Repository pattern for database abstraction
- Service pattern for business logic separation
- Used go-migrate for database migrations
- API accepts and returns dates in
YYYY-MM-DDformat - Custom
DateOnlytype for proper JSON marshaling/unmarshaling - Time component ignored for business logic
- Nager.date API for UK public holidays
- Custom
HolidayDatetype to handle API's date format - Proper error handling with meaningful error messages
- Interface-based design for easy mocking
- Echo validator with go-playground/validator/v10
- Business logic validation separate from input validation
- Past date validation using date-only comparison
Configure via environment variables:
export APP_PORT=8080
export APP_DB_PATH=./data/citynext.db
export APP_NAGER_URL=https://date.nager.at/api/v3- Go 1.24+
- SQLite3
# Run the application
go run cmd/main.go
# Run tests
make test
# Build
go build ./...The project includes comprehensive testing:
- Unit Tests: Repository, services, and utilities
- Integration Tests: HTTP handlers with mocking on
./server/book_test.go - Test Coverage: All critical paths covered
Testing tools used:
testifyfor assertions and mockingapitestfor HTTP handler testing- In-memory SQLite for repository tests
- Visit: Core entity with UUID, names, date, and timestamps
- DateOnly: Custom type for date-only JSON handling
- VisitRepository: Database operations with error handling
- HolidayService: Integration with nager.date API
- HolidayDate: Custom unmarshaling for API date format
- Echo Framework: HTTP routing and middleware
- Custom Validator: Input validation with go-playground/validator
- Error Handler: Consistent error responses
Benefits:
- Reduce API calls to Nager.Date
- Improve response times
- Handle API rate limits gracefully
- Retry Logic: Exponential backoff for external API calls