|
| 1 | +# Concert Management API |
| 2 | + |
| 3 | +A practical example of building a REST API with [Goa](https://goa.design/), |
| 4 | +covering design patterns, validation, error handling, and documentation |
| 5 | +generation. |
| 6 | + |
| 7 | +> **📚 Tutorial Reference**: This example is featured in the official |
| 8 | +> [Goa REST API Tutorial](https://goa.design/docs/3-tutorials/1-rest-api/) on |
| 9 | +> the Goa documentation site. The tutorial provides step-by-step guidance for |
| 10 | +> building REST APIs with Goa's design-first approach. |
| 11 | +
|
| 12 | +> **⚠️ Example Code**: This is a simplified example for learning purposes. For |
| 13 | +> simplicity, the entire service implementation lives in the `main.go` file. |
| 14 | +> In practice, you would typically organize code into separate packages as |
| 15 | +> shown in the |
| 16 | +> [Multiple Services guide](https://goa.design/docs/6-advanced/2-multiple-services/). |
| 17 | +
|
| 18 | +## What's This All About? |
| 19 | + |
| 20 | +This is a concert management system that demonstrates how to build a clean REST |
| 21 | +API. You can manage concerts, artists, venues, and ticket prices through |
| 22 | +well-structured endpoints. The key advantage is that everything is defined |
| 23 | +upfront in the design, and Goa generates all the boilerplate code for you. |
| 24 | + |
| 25 | +What makes this approach useful: |
| 26 | +- Clean, well-structured endpoints that follow REST conventions |
| 27 | +- Validation that happens before your business logic runs |
| 28 | +- Consistent error responses across all endpoints |
| 29 | +- Pagination that handles large datasets properly |
| 30 | +- Auto-generated OpenAPI documentation that stays current |
| 31 | +- Strong typing that catches issues at compile time |
| 32 | + |
| 33 | +## What You Can Do |
| 34 | + |
| 35 | +The API covers the standard concert management operations: |
| 36 | + |
| 37 | +**Concert Management**: List concerts with pagination, create new events, get |
| 38 | + details for specific concerts, update existing information, and remove |
| 39 | + concerts. |
| 40 | + |
| 41 | +**Data Model**: Each concert includes a unique ID (auto-generated), artist or |
| 42 | + band name, date in ISO format, venue information, and ticket price stored in |
| 43 | + cents. |
| 44 | + |
| 45 | +## Getting It Running |
| 46 | + |
| 47 | +### What You'll Need |
| 48 | +- Go 1.24 or later |
| 49 | +- [Goa v3](https://goa.design/) framework |
| 50 | + |
| 51 | +### Setup |
| 52 | + |
| 53 | +Get the code and dependencies: |
| 54 | +```bash |
| 55 | +git clone <repository> |
| 56 | +cd concerts |
| 57 | +go mod tidy |
| 58 | +``` |
| 59 | + |
| 60 | +Generate the Goa code: |
| 61 | +```bash |
| 62 | +goa gen concerts/design |
| 63 | +``` |
| 64 | + |
| 65 | +Start the server: |
| 66 | +```bash |
| 67 | +go run cmd/concerts/main.go |
| 68 | +``` |
| 69 | + |
| 70 | +The API will be available at `http://localhost:8080`. |
| 71 | + |
| 72 | +### Documentation |
| 73 | + |
| 74 | +Once running, you can view the auto-generated documentation: |
| 75 | +- **OpenAPI JSON**: http://localhost:8080/openapi3.json |
| 76 | +- **OpenAPI YAML**: http://localhost:8080/openapi3.yaml |
| 77 | + |
| 78 | +## Try It Out |
| 79 | + |
| 80 | +### List Concerts |
| 81 | + |
| 82 | +```bash |
| 83 | +# Get the first page of concerts |
| 84 | +curl "http://localhost:8080/concerts" |
| 85 | + |
| 86 | +# Get page 2 with 5 results |
| 87 | +curl "http://localhost:8080/concerts?page=2&limit=5" |
| 88 | +``` |
| 89 | + |
| 90 | +### Create a Concert |
| 91 | + |
| 92 | +```bash |
| 93 | +curl -X POST "http://localhost:8080/concerts" \ |
| 94 | + -H "Content-Type: application/json" \ |
| 95 | + -d '{ |
| 96 | + "artist": "The White Stripes", |
| 97 | + "date": "2024-12-25", |
| 98 | + "venue": "Madison Square Garden, New York, NY", |
| 99 | + "price": 8500 |
| 100 | + }' |
| 101 | +``` |
| 102 | + |
| 103 | +### Get Concert Details |
| 104 | + |
| 105 | +```bash |
| 106 | +curl "http://localhost:8080/concerts/550e8400-e29b-41d4-a716-446655440000" |
| 107 | +``` |
| 108 | + |
| 109 | +### Update a Concert |
| 110 | + |
| 111 | +You can update all fields or just specific ones: |
| 112 | + |
| 113 | +```bash |
| 114 | +# Update multiple fields |
| 115 | +curl -X PUT "http://localhost:8080/concerts/550e8400-e29b-41d4-a716-446655440000" \ |
| 116 | + -H "Content-Type: application/json" \ |
| 117 | + -d '{ |
| 118 | + "artist": "The White Stripes", |
| 119 | + "date": "2024-12-26", |
| 120 | + "venue": "Madison Square Garden, New York, NY", |
| 121 | + "price": 9000 |
| 122 | + }' |
| 123 | + |
| 124 | +# Update just the price |
| 125 | +curl -X PUT "http://localhost:8080/concerts/550e8400-e29b-41d4-a716-446655440000" \ |
| 126 | + -H "Content-Type: application/json" \ |
| 127 | + -d '{ |
| 128 | + "price": 9500 |
| 129 | + }' |
| 130 | +``` |
| 131 | + |
| 132 | +### Delete a Concert |
| 133 | + |
| 134 | +```bash |
| 135 | +curl -X DELETE "http://localhost:8080/concerts/550e8400-e29b-41d4-a716-446655440000" |
| 136 | +``` |
| 137 | + |
| 138 | +## Design Details |
| 139 | + |
| 140 | +### Type Structure |
| 141 | + |
| 142 | +The API uses a layered type approach for flexible operations: |
| 143 | + |
| 144 | +- **ConcertData**: Base type with all concert fields, no required constraints |
| 145 | +- **ConcertPayload**: For creation - extends base type and requires all fields |
| 146 | +- **UpdatePayload**: For updates - extends base type but only requires the concert ID |
| 147 | +- **Concert**: For responses - complete concert with ID and all details |
| 148 | + |
| 149 | +This structure allows creation to require complete data while updates can be partial. |
| 150 | + |
| 151 | +### Validation Rules |
| 152 | + |
| 153 | +All validation is defined in the design layer: |
| 154 | +- Artist names: 1-200 characters |
| 155 | +- Dates: ISO 8601 format (YYYY-MM-DD) |
| 156 | +- Venues: 1-300 characters |
| 157 | +- Prices: Non-negative integers, maximum $1000 (stored as cents) |
| 158 | +- Pagination: Page ≥ 1, Limit 1-100 |
| 159 | + |
| 160 | +### Error Handling |
| 161 | + |
| 162 | +The API returns consistent error responses: |
| 163 | + |
| 164 | +```json |
| 165 | +{ |
| 166 | + "message": "Concert with ID abc123 not found", |
| 167 | + "code": "not_found" |
| 168 | +} |
| 169 | +``` |
| 170 | + |
| 171 | +HTTP status codes follow standard conventions: |
| 172 | +- `200 OK` for successful reads and updates |
| 173 | +- `201 Created` for successful creation |
| 174 | +- `204 No Content` for successful deletion |
| 175 | +- `400 Bad Request` for validation errors |
| 176 | +- `404 Not Found` for missing resources |
| 177 | + |
| 178 | +## Project Structure |
| 179 | + |
| 180 | +```bash |
| 181 | +concerts/ |
| 182 | +├── design/ |
| 183 | +│ └── design.go # API design specification |
| 184 | +├── cmd/concerts/ |
| 185 | +│ └── main.go # Service implementation |
| 186 | +├── gen/ # Generated code (don't modify) |
| 187 | +│ ├── concerts/ # Service interfaces and types |
| 188 | +│ └── http/ # HTTP transport layer |
| 189 | +├── go.mod # Dependencies |
| 190 | +└── README.md # This file |
| 191 | +``` |
| 192 | + |
| 193 | +> **Note**: This example keeps everything in a single `main.go` file for |
| 194 | +> simplicity. In real applications, you would typically organize the service |
| 195 | +> implementation into separate packages, with clean separation between business |
| 196 | +> logic, transport handlers, and service interfaces as demonstrated in the |
| 197 | +> [Multiple Services documentation](https://goa.design/docs/6-advanced/2-multiple-services/). |
| 198 | +
|
| 199 | +## Development |
| 200 | + |
| 201 | +To modify the API, edit `design/design.go` and regenerate: |
| 202 | + |
| 203 | +```bash |
| 204 | +goa gen concerts/design |
| 205 | +``` |
| 206 | + |
| 207 | +Goa will update all the generated code while preserving your service implementation. |
| 208 | + |
| 209 | +## Key Benefits |
| 210 | + |
| 211 | +1. **Design-first development**: API specification drives implementation |
| 212 | +2. **Centralized validation**: Rules defined once in the design |
| 213 | +3. **Type safety**: Compile-time error detection |
| 214 | +4. **Consistent error handling**: Standardized response format |
| 215 | +5. **Always-current documentation**: Generated from the same source as code |
| 216 | +6. **RESTful design**: Proper HTTP methods and resource modeling |
| 217 | +7. **Efficient pagination**: Handles large datasets appropriately |
| 218 | +8. **Clean separation**: Business logic separate from transport concerns |
| 219 | + |
| 220 | +## Dependencies |
| 221 | + |
| 222 | +- [Goa v3](https://goa.design/): Design framework for building APIs |
| 223 | +- [UUID](https://github.com/google/uuid): For generating unique identifiers |
| 224 | + |
| 225 | +This example demonstrates Goa's design-first approach for educational purposes. |
0 commit comments