|
2 | 2 |
|
3 | 3 | How to write simple CRUD operations.
|
4 | 4 |
|
| 5 | +:::tip |
| 6 | + |
| 7 | +Fuego comes with a **generator** that can generates CRUD routes and controllers for you! |
| 8 | + |
| 9 | +::: |
| 10 | + |
5 | 11 | ## Generation
|
6 | 12 |
|
| 13 | +```bash |
| 14 | +go install github.com/go-fuego/fuego/cmd/fuego@latest |
| 15 | +fuego controller books |
| 16 | + |
| 17 | +# or in one line: |
| 18 | +go run github.com/go-fuego/fuego/cmd/fuego@latest controller books |
| 19 | +``` |
| 20 | + |
7 | 21 | ## The routes
|
| 22 | + |
| 23 | +The generator will create the following routes: |
| 24 | + |
| 25 | +- `GET /books`: list all books |
| 26 | +- `POST /books`: create a new book |
| 27 | +- `GET /books/:id`: get a book by id |
| 28 | +- `PUT /books/:id`: update a book by id |
| 29 | +- `PATCH /books/:id`: update a book by id |
| 30 | +- `DELETE /books/:id`: delete a book by id |
| 31 | + |
| 32 | +If you want to create the routes manually, you can use the following code: |
| 33 | + |
| 34 | +```go title="main.go" |
| 35 | +package main |
| 36 | + |
| 37 | +import ( |
| 38 | + "github.com/go-fuego/fuego" |
| 39 | +) |
| 40 | + |
| 41 | +type Book struct { |
| 42 | + ID string `json:"id"` |
| 43 | + Title string `json:"title"` |
| 44 | +} |
| 45 | + |
| 46 | +func main() { |
| 47 | + s := fuego.NewServer() |
| 48 | + |
| 49 | + // List all books |
| 50 | + fuego.Get(s, "/books", getBooks) |
| 51 | + |
| 52 | + // Create a new book |
| 53 | + fuego.Post(s, "/books", createBook) |
| 54 | + |
| 55 | + // Get a book by id |
| 56 | + fuego.Get(s, "/books/:id", getBook) |
| 57 | + |
| 58 | + // Update a book by id |
| 59 | + fuego.Put(s, "/books/:id", updateBook) |
| 60 | + |
| 61 | + // Update a book by id |
| 62 | + fuego.Patch(s, "/books/:id", updateBook) |
| 63 | + |
| 64 | + // Delete a book by id |
| 65 | + fuego.Delete(s, "/books/:id", deleteBook) |
| 66 | + |
| 67 | + s.Run() |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +```go title="controllers/books.go" |
| 72 | +package controllers |
| 73 | + |
| 74 | +import ( |
| 75 | + "github.com/go-fuego/fuego" |
| 76 | +) |
| 77 | + |
| 78 | +type Book struct { |
| 79 | + ID string `json:"id"` |
| 80 | + Title string `json:"title"` |
| 81 | +} |
| 82 | + |
| 83 | +type BookToCreate struct { |
| 84 | + Title string `json:"title"` |
| 85 | +} |
| 86 | + |
| 87 | +func getBooks(c fuego.ContextNoBody) ([]Book, error) { |
| 88 | + // Your code here |
| 89 | + return nil, nil |
| 90 | +} |
| 91 | + |
| 92 | +func createBook(c fuego.ContextWithBody[BookToCreate]) (Book, error) { |
| 93 | + // Your code here |
| 94 | + return Book{}, nil |
| 95 | +} |
| 96 | + |
| 97 | +func getBook(c fuego.ContextNoBody) (Book, error) { |
| 98 | + // Your code here |
| 99 | + return Book{}, nil |
| 100 | +} |
| 101 | + |
| 102 | +func updateBook(c fuego.ContextWithBody[Book]) (Book, error) { |
| 103 | + // Your code here |
| 104 | + return Book{}, nil |
| 105 | +} |
| 106 | + |
| 107 | +func deleteBook(c fuego.ContextNoBody) error { |
| 108 | + // Your code here |
| 109 | + return nil |
| 110 | +} |
| 111 | + |
| 112 | +``` |
0 commit comments