|
|
@ -140,13 +140,13 @@ To create a new book handler function, create an `api/handlers.go` file that wil |
|
|
|
package api |
|
|
|
package api |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"log" |
|
|
|
|
|
|
|
"net/http" |
|
|
|
|
|
|
|
"os" |
|
|
|
|
|
|
|
"github.com/gin-gonic/gin" |
|
|
|
"github.com/gin-gonic/gin" |
|
|
|
"github.com/joho/godotenv" |
|
|
|
"github.com/joho/godotenv" |
|
|
|
"gorm.io/driver/postgres" |
|
|
|
"gorm.io/driver/postgres" |
|
|
|
"gorm.io/gorm" |
|
|
|
"gorm.io/gorm" |
|
|
|
|
|
|
|
"log" |
|
|
|
|
|
|
|
"net/http" |
|
|
|
|
|
|
|
"os" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
var DB *gorm.DB |
|
|
|
var DB *gorm.DB |
|
|
@ -259,8 +259,8 @@ With the handlers set up, you need to create the application entry point and spe |
|
|
|
package main |
|
|
|
package main |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"go_book_api/api" |
|
|
|
|
|
|
|
"github.com/gin-gonic/gin" |
|
|
|
"github.com/gin-gonic/gin" |
|
|
|
|
|
|
|
"go_book_api/api" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
func main() { |
|
|
|
func main() { |
|
|
@ -368,14 +368,14 @@ package tests |
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"bytes" |
|
|
|
"bytes" |
|
|
|
"encoding/json" |
|
|
|
"encoding/json" |
|
|
|
|
|
|
|
"github.com/gin-gonic/gin" |
|
|
|
"go_book_api/api" |
|
|
|
"go_book_api/api" |
|
|
|
|
|
|
|
"gorm.io/driver/sqlite" |
|
|
|
|
|
|
|
"gorm.io/gorm" |
|
|
|
"net/http" |
|
|
|
"net/http" |
|
|
|
"net/http/httptest" |
|
|
|
"net/http/httptest" |
|
|
|
"strconv" |
|
|
|
"strconv" |
|
|
|
"testing" |
|
|
|
"testing" |
|
|
|
"github.com/gin-gonic/gin" |
|
|
|
|
|
|
|
"gorm.io/driver/sqlite" |
|
|
|
|
|
|
|
"gorm.io/gorm" |
|
|
|
|
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
func setupTestDB() { |
|
|
|
func setupTestDB() { |
|
|
@ -605,10 +605,10 @@ package api |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
"net/http" |
|
|
|
|
|
|
|
"os" |
|
|
|
|
|
|
|
"github.com/gin-gonic/gin" |
|
|
|
"github.com/gin-gonic/gin" |
|
|
|
"github.com/golang-jwt/jwt/v5" |
|
|
|
"github.com/golang-jwt/jwt/v5" |
|
|
|
|
|
|
|
"net/http" |
|
|
|
|
|
|
|
"os" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
// Secret key for signing JWT |
|
|
|
// Secret key for signing JWT |
|
|
@ -685,8 +685,8 @@ Lastly, update the `main.go` file inside the `cmd` folder to add a public route |
|
|
|
package main |
|
|
|
package main |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"go_book_api/api" |
|
|
|
|
|
|
|
"github.com/gin-gonic/gin" |
|
|
|
"github.com/gin-gonic/gin" |
|
|
|
|
|
|
|
"go_book_api/api" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
func main() { |
|
|
|
func main() { |
|
|
@ -872,16 +872,17 @@ It includes functionality for creating, reading, updating, and deleting books, a |
|
|
|
package api |
|
|
|
package api |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"log" |
|
|
|
|
|
|
|
"net/http" |
|
|
|
|
|
|
|
"os" |
|
|
|
|
|
|
|
"time" |
|
|
|
|
|
|
|
"github.com/gin-gonic/gin" |
|
|
|
"github.com/gin-gonic/gin" |
|
|
|
"github.com/golang-jwt/jwt/v5" |
|
|
|
"github.com/golang-jwt/jwt/v5" |
|
|
|
"github.com/joho/godotenv" |
|
|
|
"github.com/joho/godotenv" |
|
|
|
"gorm.io/driver/postgres" |
|
|
|
"gorm.io/driver/postgres" |
|
|
|
"gorm.io/gorm" |
|
|
|
"gorm.io/gorm" |
|
|
|
|
|
|
|
"log" |
|
|
|
|
|
|
|
"net/http" |
|
|
|
|
|
|
|
"os" |
|
|
|
|
|
|
|
"time" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
var DB *gorm.DB |
|
|
|
var DB *gorm.DB |
|
|
|
|
|
|
|
|
|
|
|
// InitDB initializes the database connection using environment variables. |
|
|
|
// InitDB initializes the database connection using environment variables. |
|
|
|