|
| 1 | +package routes |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log" |
| 6 | + "net/http" |
| 7 | + |
| 8 | + "github.com/gin-gonic/gin" |
| 9 | + "github.com/typesense/code-samples/typesense-gin-full-text-search/models" |
| 10 | + "github.com/typesense/code-samples/typesense-gin-full-text-search/utils" |
| 11 | +) |
| 12 | + |
| 13 | +// SetupBookRoutes configures all book CRUD routes |
| 14 | +func SetupBookRoutes(router *gin.Engine) { |
| 15 | + books := router.Group("/books") |
| 16 | + { |
| 17 | + books.POST("", createBook) |
| 18 | + books.GET("/:id", getBook) |
| 19 | + books.GET("", getAllBooks) |
| 20 | + books.PUT("/:id", updateBook) |
| 21 | + books.DELETE("/:id", deleteBook) |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +// createBook creates a new book in the database and syncs to Typesense |
| 26 | +func createBook(c *gin.Context) { |
| 27 | + var book models.Book |
| 28 | + |
| 29 | + if err := c.ShouldBindJSON(&book); err != nil { |
| 30 | + c.JSON(http.StatusBadRequest, gin.H{ |
| 31 | + "error": "Invalid request body: " + err.Error(), |
| 32 | + }) |
| 33 | + return |
| 34 | + } |
| 35 | + |
| 36 | + // Save to database (source of truth) |
| 37 | + if err := utils.SaveBook(c.Request.Context(), &book); err != nil { |
| 38 | + c.JSON(http.StatusInternalServerError, gin.H{ |
| 39 | + "error": "Failed to create book: " + err.Error(), |
| 40 | + }) |
| 41 | + return |
| 42 | + } |
| 43 | + |
| 44 | + // Sync to Typesense asynchronously (non-blocking) |
| 45 | + go func(bookCopy models.Book) { |
| 46 | + ctx := context.Background() |
| 47 | + if err := utils.SyncBookOnUpdate(ctx, &bookCopy); err != nil { |
| 48 | + log.Printf("Async Typesense sync failed for book %d: %v", bookCopy.ID, err) |
| 49 | + } |
| 50 | + }(book) |
| 51 | + |
| 52 | + c.JSON(http.StatusCreated, gin.H{ |
| 53 | + "message": "Book created successfully", |
| 54 | + "book": book, |
| 55 | + }) |
| 56 | +} |
| 57 | + |
| 58 | +// getBook retrieves a single book by ID |
| 59 | +func getBook(c *gin.Context) { |
| 60 | + var uri struct { |
| 61 | + ID uint `uri:"id" binding:"required"` |
| 62 | + } |
| 63 | + |
| 64 | + if err := c.ShouldBindUri(&uri); err != nil { |
| 65 | + c.JSON(http.StatusBadRequest, gin.H{ |
| 66 | + "error": "Invalid book ID", |
| 67 | + }) |
| 68 | + return |
| 69 | + } |
| 70 | + |
| 71 | + book, err := utils.GetBookByID(c.Request.Context(), uri.ID) |
| 72 | + if err != nil { |
| 73 | + c.JSON(http.StatusNotFound, gin.H{ |
| 74 | + "error": "Book not found", |
| 75 | + }) |
| 76 | + return |
| 77 | + } |
| 78 | + |
| 79 | + c.JSON(http.StatusOK, gin.H{ |
| 80 | + "book": book, |
| 81 | + }) |
| 82 | +} |
| 83 | + |
| 84 | +// getAllBooks retrieves all books from the database |
| 85 | +func getAllBooks(c *gin.Context) { |
| 86 | + books, err := utils.GetAllBooks(c.Request.Context()) |
| 87 | + if err != nil { |
| 88 | + c.JSON(http.StatusInternalServerError, gin.H{ |
| 89 | + "error": "Failed to fetch books: " + err.Error(), |
| 90 | + }) |
| 91 | + return |
| 92 | + } |
| 93 | + |
| 94 | + c.JSON(http.StatusOK, gin.H{ |
| 95 | + "count": len(books), |
| 96 | + "books": books, |
| 97 | + }) |
| 98 | +} |
| 99 | + |
| 100 | +// updateBook updates an existing book and syncs to Typesense |
| 101 | +func updateBook(c *gin.Context) { |
| 102 | + var uri struct { |
| 103 | + ID uint `uri:"id" binding:"required"` |
| 104 | + } |
| 105 | + |
| 106 | + if err := c.ShouldBindUri(&uri); err != nil { |
| 107 | + c.JSON(http.StatusBadRequest, gin.H{ |
| 108 | + "error": "Invalid book ID", |
| 109 | + }) |
| 110 | + return |
| 111 | + } |
| 112 | + |
| 113 | + // Fetch existing book |
| 114 | + book, err := utils.GetBookByID(c.Request.Context(), uri.ID) |
| 115 | + if err != nil { |
| 116 | + c.JSON(http.StatusNotFound, gin.H{ |
| 117 | + "error": "Book not found", |
| 118 | + }) |
| 119 | + return |
| 120 | + } |
| 121 | + |
| 122 | + // Bind updated data directly to existing book |
| 123 | + if err := c.ShouldBindJSON(&book); err != nil { |
| 124 | + c.JSON(http.StatusBadRequest, gin.H{ |
| 125 | + "error": "Invalid request body: " + err.Error(), |
| 126 | + }) |
| 127 | + return |
| 128 | + } |
| 129 | + |
| 130 | + // Preserve the ID (in case it was in the JSON) |
| 131 | + book.ID = uri.ID |
| 132 | + |
| 133 | + // Save to database |
| 134 | + if err := utils.SaveBook(c.Request.Context(), book); err != nil { |
| 135 | + c.JSON(http.StatusInternalServerError, gin.H{ |
| 136 | + "error": "Failed to update book: " + err.Error(), |
| 137 | + }) |
| 138 | + return |
| 139 | + } |
| 140 | + |
| 141 | + // Sync to Typesense asynchronously (non-blocking) |
| 142 | + go func(bookCopy models.Book) { |
| 143 | + ctx := context.Background() |
| 144 | + if err := utils.SyncBookOnUpdate(ctx, &bookCopy); err != nil { |
| 145 | + log.Printf("Async Typesense sync failed for book %d: %v", bookCopy.ID, err) |
| 146 | + } |
| 147 | + }(*book) |
| 148 | + |
| 149 | + c.JSON(http.StatusOK, gin.H{ |
| 150 | + "message": "Book updated successfully", |
| 151 | + "book": book, |
| 152 | + }) |
| 153 | +} |
| 154 | + |
| 155 | +// deleteBook soft-deletes a book and removes it from Typesense |
| 156 | +func deleteBook(c *gin.Context) { |
| 157 | + var uri struct { |
| 158 | + ID uint `uri:"id" binding:"required"` |
| 159 | + } |
| 160 | + |
| 161 | + if err := c.ShouldBindUri(&uri); err != nil { |
| 162 | + c.JSON(http.StatusBadRequest, gin.H{ |
| 163 | + "error": "Invalid book ID", |
| 164 | + }) |
| 165 | + return |
| 166 | + } |
| 167 | + |
| 168 | + // Check if book exists |
| 169 | + _, err := utils.GetBookByID(c.Request.Context(), uri.ID) |
| 170 | + if err != nil { |
| 171 | + c.JSON(http.StatusNotFound, gin.H{ |
| 172 | + "error": "Book not found", |
| 173 | + }) |
| 174 | + return |
| 175 | + } |
| 176 | + |
| 177 | + // Soft delete from database |
| 178 | + if err := utils.DeleteBook(c.Request.Context(), uri.ID); err != nil { |
| 179 | + c.JSON(http.StatusInternalServerError, gin.H{ |
| 180 | + "error": "Failed to delete book: " + err.Error(), |
| 181 | + }) |
| 182 | + return |
| 183 | + } |
| 184 | + |
| 185 | + // Remove from Typesense asynchronously (non-blocking) |
| 186 | + go func(bookID uint) { |
| 187 | + ctx := context.Background() |
| 188 | + if err := utils.SyncBookDeletionOnDelete(ctx, bookID); err != nil { |
| 189 | + log.Printf("Async Typesense deletion failed for book %d: %v", bookID, err) |
| 190 | + } |
| 191 | + }(uri.ID) |
| 192 | + |
| 193 | + c.JSON(http.StatusOK, gin.H{ |
| 194 | + "message": "Book deleted successfully", |
| 195 | + }) |
| 196 | +} |
0 commit comments