Skip to content

Commit

Permalink
fix: golangci-lint linter error
Browse files Browse the repository at this point in the history
Signed-off-by: Saket Maurya <[email protected]>
  • Loading branch information
SaketV8 committed Feb 17, 2025
1 parent 00ab1d6 commit 5b29e6a
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 19 deletions.
1 change: 1 addition & 0 deletions book-store-inventory/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package main is the entry point of the application
package main

import (
Expand Down
16 changes: 15 additions & 1 deletion book-store-inventory/migrations/migration.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// Package main contains function for database migration
// It initialise the db with some default value
package main

// for database migration

import (
"database/sql"
"fmt"
Expand All @@ -21,7 +25,17 @@ func main() {
if err != nil {
log.Fatalf("Failed to open database: %v", err)
}
defer db.Close()

// defer db.Close()
// fixing linter issue for defer

// This is anonymous function :)
defer func() {
err := db.Close()
if err != nil {
log.Fatalf("Failed to close database: %v", err)
}
}()

// Here Goose is being initialized and setting up the db type to sqlite
err = goose.SetDialect("sqlite3")
Expand Down
1 change: 1 addition & 0 deletions book-store-inventory/pkg/database/db_sqlite.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package database contains helper functions for initializing and managing database connections.
package database

import (
Expand Down
7 changes: 4 additions & 3 deletions book-store-inventory/pkg/handlers/book_handler.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package handlers contains the main bussiness logic of the application
package handlers

import (
Expand All @@ -24,11 +25,11 @@ func (bookHandler *BookHandler) GetBooksHandler(ctx *gin.Context) {
ctx.JSON(http.StatusOK, books)
}

func (bookHandler *BookHandler) GetBookByIdHandler(ctx *gin.Context) {
func (bookHandler *BookHandler) GetBookByIDHandler(ctx *gin.Context) {
// getting param from the URL
/* http://localhost:9090/api/v1/book/id/3 --> 3 is param here */
book_id_param := ctx.Param("book_id")
book, err := bookHandler.BookModel.GetBookByID(book_id_param)
bookIDParam := ctx.Param("book_id")
book, err := bookHandler.BookModel.GetBookByID(bookIDParam)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"error": "Failed to get book by ID",
Expand Down
2 changes: 1 addition & 1 deletion book-store-inventory/pkg/handlers/test_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/gin-gonic/gin"
)

func TestApi(ctx *gin.Context) {
func TestAPI(ctx *gin.Context) {
ctx.JSON(http.StatusOK, gin.H{
"message": "API Working 💞 [Private route]",
})
Expand Down
1 change: 1 addition & 0 deletions book-store-inventory/pkg/middleware/basic_auth.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package middleware contains basic user and pass middleware for testing private routes
package middleware

import "github.com/gin-gonic/gin"
Expand Down
1 change: 1 addition & 0 deletions book-store-inventory/pkg/models/book_model.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package models contains struct for Data
package models

import "time"
Expand Down
9 changes: 5 additions & 4 deletions book-store-inventory/pkg/repositories/book_repository.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package repositories contains logic to interact with database uisng raw SQL queries
package repositories

import (
Expand Down Expand Up @@ -48,12 +49,12 @@ func (bookModel *BookModel) GetBooks() ([]models.Book, error) {
return books, nil
}

func (bookModel *BookModel) GetBookByID(book_id string) (models.Book, error) {
func (bookModel *BookModel) GetBookByID(bookID string) (models.Book, error) {
statement := `SELECT book_id, title, author, price, published_date, stock_quantity FROM books WHERE book_id = ? ;`
// empty book model
book := models.Book{}

err := bookModel.DB.QueryRow(statement, book_id).Scan(
err := bookModel.DB.QueryRow(statement, bookID).Scan(
&book.BookID,
&book.Title,
&book.Author,
Expand All @@ -76,14 +77,14 @@ func (bookModel *BookModel) AddBook(book models.Book) (models.Book, error) {
return models.Book{}, err
}

lastInsertedId, err := result.LastInsertId()
lastInsertedID, err := result.LastInsertId()
if err != nil {
return models.Book{}, err
}

// adding Id to book model inserted autmatically by sqlite before returning to end user
// removing int gives int64 cannot covert to int :)
book.BookID = int(lastInsertedId)
book.BookID = int(lastInsertedID)

return book, nil
}
Expand Down
13 changes: 10 additions & 3 deletions book-store-inventory/pkg/router/private_router.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// Package router have routes defined in it for REST API
package router

// https://stackoverflow.com/a/62608670
// https://stackoverflow.com/questions/62608429/how-to-combine-group-of-routes-in-gin

package router

import (
"github.com/gin-gonic/gin"
"github.com/saketV8/book-store-inventory/pkg/handlers"
"github.com/saketV8/book-store-inventory/pkg/middleware"
"github.com/saketV8/book-store-inventory/pkg/utils"
)
Expand All @@ -14,12 +16,17 @@ func SetupPrivateRouter(app *App, superRouterGroup *gin.Engine) {
// <ROUTER_PREFIX> = /api
// <ROUTER_PREFIX_VERSION> = /v1

// fixing linter issue
// app will be used when we pass data from app to handler :)
// see example in <public_router.go> SetupPublicRouter()
_ = app

routerGroup := superRouterGroup.Group(utils.ROUTER_PREFIX)
{
v1 := routerGroup.Group(utils.ROUTER_PREFIX_VERSION)
v1.Use(middleware.BasicAuthMiddleware())
{
// v1.GET("/test-private-api/", handlers.TestApi)
v1.GET("/test-private/", handlers.TestAPI)
}
}
}
2 changes: 1 addition & 1 deletion book-store-inventory/pkg/router/public_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func SetupPublicRouter(app *App, superRouterGroup *gin.Engine) {
{
// GET
v1.GET("/books/", app.Books.GetBooksHandler)
v1.GET("/book/id/:book_id", app.Books.GetBookByIdHandler)
v1.GET("/book/id/:book_id", app.Books.GetBookByIDHandler)

// POST
v1.POST("/book/add/", app.Books.AddBookHandler)
Expand Down
5 changes: 1 addition & 4 deletions book-store-inventory/pkg/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,13 @@ import (
"github.com/gin-gonic/gin"
"github.com/saketV8/book-store-inventory/pkg/handlers"
"github.com/saketV8/book-store-inventory/pkg/utils"

log "github.com/sirupsen/logrus"
)

type App struct {
// Add more handlers as needed
Books *handlers.BookHandler
}

// func SetupRouter(DbModel *querydb.DbModel) {
func SetupRouter(app *App) {
// Initializing the GIN Router
gin.SetMode(gin.ReleaseMode)
Expand All @@ -41,7 +38,7 @@ func SetupRouter(app *App) {
err := rtr.Run(utils.PORT)
if err != nil {
fmt.Println("============================================")
log.Fatal("ERROR: while Initializing the server")
fmt.Println("ERROR: while Initializing the server")
fmt.Println("============================================")
}
}
6 changes: 4 additions & 2 deletions book-store-inventory/pkg/utils/constants.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Package utils contains constant to be used across the application
package utils

// TODO:
// replace all with env variable
var PORT string = ":9090"
var ROUTER_PREFIX string = "/api"

var PORT = ":9090"
var ROUTER_PREFIX = "/api"
var ROUTER_PREFIX_VERSION = "/v1"
2 changes: 2 additions & 0 deletions book-store-inventory/pkg/utils/list_all_routes.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package utils contains function to print all available routes in application
package utils

import (
Expand All @@ -8,6 +9,7 @@ import (

// https://stackoverflow.com/questions/287871/how-do-i-print-colored-text-to-the-terminal
// https://stackoverflow.com/a/26445590

var Reset = "\033[0m"

var Blue = "\033[34m"
Expand Down

0 comments on commit 5b29e6a

Please sign in to comment.