Skip to content

Commit

Permalink
fixed build issue
Browse files Browse the repository at this point in the history
  • Loading branch information
VikashChauhan51 committed Jul 26, 2024
1 parent 907c6f5 commit db78329
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
10 changes: 9 additions & 1 deletion cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,20 @@ 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/infra/databases"
"github.com/gin-gonic/gin"
swaggerfiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
)

func main() {

// Connect to the database
dsn := "sqlserver://username:password@localhost:1433?database=bookstore"
db, err := databases.NewDBConnection(dsn)
if err != nil {
fmt.Printf("failed to connect to database: %v \n", err)
}
r := gin.New()
// LoggerWithFormatter middleware will write the logs to gin.DefaultWriter
// By default gin.DefaultWriter = os.Stdout
Expand All @@ -36,7 +44,7 @@ func main() {
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))
v1 := r.Group("/api/v1")
{
for _, route := range routes.Routes {
for _, route := range *routes.GetRoutes(db) {
switch route.Method {
case http.MethodGet:
v1.GET(route.Path, route.Handler)
Expand Down
17 changes: 11 additions & 6 deletions cmd/api/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"net/http"

"github.com/VikashChauhan51/go-sample-api/internal/controllers"
"github.com/VikashChauhan51/go-sample-api/internal/core/interfaces"
"github.com/VikashChauhan51/go-sample-api/internal/infra/repositories"
"github.com/VikashChauhan51/go-sample-api/internal/infra/services"
"github.com/gin-gonic/gin"
)

Expand All @@ -13,10 +16,12 @@ type Route struct {
Handler gin.HandlerFunc
}

// Defind all routes
var Routes = []Route{
{"GET", "/", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"data": "hello world"})
}},
{"GET", "/books", controllers.GetBooks},
func GetRoutes(db interfaces.Database) *[]Route {
// Defind all routes
return &[]Route{
{"GET", "/", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"data": "hello world"})
}},
{"GET", "/books", controllers.NewBookController(services.NewBookService(repositories.NewBookRepository(db))).GetBooks},
}
}
14 changes: 11 additions & 3 deletions internal/controllers/books_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,26 @@ package controllers
import (
"net/http"

"github.com/VikashChauhan51/go-sample-api/internal/infra/services"
svc "github.com/VikashChauhan51/go-sample-api/internal/core/interfaces/services"
"github.com/gin-gonic/gin"
)

type BookController struct {
bookService svc.BookService
}

func NewBookController(service svc.BookService) *BookController {
return &BookController{service}
}

// @Summary Get books
// @Description Retrieves a list of books
// @Tags books
// @Produce json
// @Success 200 {array} models.Book
// @Router /books [get]
func GetBooks(c *gin.Context) {
books, err := services.BookService.FetchBooksAsync()
func (b *BookController) GetBooks(c *gin.Context) {
books, err := b.bookService.FetchBooksAsync()

if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
Expand Down

0 comments on commit db78329

Please sign in to comment.