From a0787ee59499a621ff97071db8d687abdcb300db Mon Sep 17 00:00:00 2001 From: Vikash Chauhan Date: Sat, 27 Jul 2024 13:57:11 +0530 Subject: [PATCH] fetch data from database --- .vscode/launch.json | 14 ++++--- cmd/api/main.go | 16 +++++--- cmd/api/routes/loader.go | 52 ++---------------------- docs/docs.go | 4 +- docs/swagger.json | 4 +- docs/swagger.yaml | 4 +- internal/controllers/books_controller.go | 2 +- 7 files changed, 29 insertions(+), 67 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 608d3c6..0bf6e34 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -1,15 +1,17 @@ { - // Use IntelliSense to learn about possible attributes. - // Hover to view descriptions of existing attributes. - // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "Launch Package", "type": "go", "request": "launch", - "mode": "auto", - "program": "${fileDirname}" + "mode": "debug", + "program": "${workspaceFolder}/cmd/api/main.go", + "cwd": "${workspaceFolder}", + "args": [], + "buildFlags": "", + "env": {}, + "port": 0 } ] -} \ No newline at end of file +} diff --git a/cmd/api/main.go b/cmd/api/main.go index 4192e94..42f769d 100644 --- a/cmd/api/main.go +++ b/cmd/api/main.go @@ -6,6 +6,7 @@ import ( "github.com/VikashChauhan51/go-sample-api/cmd/api/routes" docs "github.com/VikashChauhan51/go-sample-api/docs" + "github.com/VikashChauhan51/go-sample-api/internal/core/entities" "github.com/VikashChauhan51/go-sample-api/internal/infra/databases" "github.com/gin-gonic/gin" swaggerfiles "github.com/swaggo/files" @@ -15,11 +16,18 @@ import ( func main() { // Connect to the database - dsn := "sqlserver://username:password@localhost:1433?database=bookstore" + dsn := "sqlserver://Sa:Welcome@123@localhost:1433?database=bookstore" db, err := databases.NewDBConnection(dsn) if err != nil { fmt.Printf("failed to connect to database: %v \n", err) } + + // Auto migrate database (create tables) + err = db.AutoMigrate(&entities.Book{}) + if err != nil { + fmt.Printf("failed to migrate database: %v", err) + } + r := gin.New() // LoggerWithFormatter middleware will write the logs to gin.DefaultWriter // By default gin.DefaultWriter = os.Stdout @@ -43,11 +51,7 @@ func main() { r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler)) v1 := r.Group("/api/v1") { - allRoutes, err := routes.ScanRoutes(db) - if err != nil { - fmt.Printf("failed to load routes: %v\n", err) - return - } + allRoutes := routes.ScanRoutes(db) routes.RegisterRoutes(v1, allRoutes) } diff --git a/cmd/api/routes/loader.go b/cmd/api/routes/loader.go index 0893308..7dfa80d 100644 --- a/cmd/api/routes/loader.go +++ b/cmd/api/routes/loader.go @@ -1,56 +1,12 @@ package routes import ( - "fmt" - "os" - "path/filepath" - "plugin" - "strings" - "github.com/VikashChauhan51/go-sample-api/internal/core/interfaces" ) -// ScanRoutes scans all route files in the current directory and collects routes. -func ScanRoutes(db interfaces.Database) ([]Route, error) { +func ScanRoutes(db interfaces.Database) []Route { var allRoutes []Route - - // Find all *_routes.go files in the current directory - err := filepath.Walk(".", func(path string, info os.FileInfo, err error) error { - if err != nil { - return err - } - if strings.HasSuffix(info.Name(), "_routes.go") { - routes, err := loadRoutesFromPlugin(path, db) - if err != nil { - return err - } - allRoutes = append(allRoutes, routes...) - } - return nil - }) - if err != nil { - return nil, fmt.Errorf("failed to scan route files: %v", err) - } - - return allRoutes, nil -} - -// loadRoutesFromPlugin loads routes from a plugin file. -func loadRoutesFromPlugin(path string, db interfaces.Database) ([]Route, error) { - p, err := plugin.Open(path) - if err != nil { - return nil, fmt.Errorf("failed to open plugin %s: %v", path, err) - } - - sym, err := p.Lookup("Routes") - if err != nil { - return nil, fmt.Errorf("failed to find GetRoutes in plugin %s: %v", path, err) - } - - getRoutesFunc, ok := sym.(func(interfaces.Database) []Route) - if !ok { - return nil, fmt.Errorf("GetRoutes function has incorrect signature in plugin %s", path) - } - - return getRoutesFunc(db), nil + booksRoutes := GetBookRoutes(db) + allRoutes = append(allRoutes, booksRoutes...) + return allRoutes } diff --git a/docs/docs.go b/docs/docs.go index 0a1b316..85f573f 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -31,7 +31,7 @@ const docTemplate = `{ "schema": { "type": "array", "items": { - "$ref": "#/definitions/models.Book" + "$ref": "#/definitions/dto.Book" } } } @@ -40,7 +40,7 @@ const docTemplate = `{ } }, "definitions": { - "models.Book": { + "dto.Book": { "type": "object", "properties": { "author": { diff --git a/docs/swagger.json b/docs/swagger.json index 552ca3c..1b5cf82 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -20,7 +20,7 @@ "schema": { "type": "array", "items": { - "$ref": "#/definitions/models.Book" + "$ref": "#/definitions/dto.Book" } } } @@ -29,7 +29,7 @@ } }, "definitions": { - "models.Book": { + "dto.Book": { "type": "object", "properties": { "author": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 82a3eb8..844dfd2 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -1,5 +1,5 @@ definitions: - models.Book: + dto.Book: properties: author: type: string @@ -21,7 +21,7 @@ paths: description: OK schema: items: - $ref: '#/definitions/models.Book' + $ref: '#/definitions/dto.Book' type: array summary: Get books tags: diff --git a/internal/controllers/books_controller.go b/internal/controllers/books_controller.go index 3dbe3f7..1d0e1b5 100644 --- a/internal/controllers/books_controller.go +++ b/internal/controllers/books_controller.go @@ -19,7 +19,7 @@ func NewBookController(service svc.BookService) *BookController { // @Description Retrieves a list of books // @Tags books // @Produce json -// @Success 200 {array} models.Book +// @Success 200 {array} dto.Book // @Router /books [get] func (b *BookController) GetBooks(c *gin.Context) { books, err := b.bookService.FetchBooksAsync()