Skip to content

Commit 5b29e6a

Browse files
committed
fix: golangci-lint linter error
Signed-off-by: Saket Maurya <[email protected]>
1 parent 00ab1d6 commit 5b29e6a

File tree

13 files changed

+47
-19
lines changed

13 files changed

+47
-19
lines changed

book-store-inventory/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package main is the entry point of the application
12
package main
23

34
import (

book-store-inventory/migrations/migration.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
// Package main contains function for database migration
2+
// It initialise the db with some default value
13
package main
24

5+
// for database migration
6+
37
import (
48
"database/sql"
59
"fmt"
@@ -21,7 +25,17 @@ func main() {
2125
if err != nil {
2226
log.Fatalf("Failed to open database: %v", err)
2327
}
24-
defer db.Close()
28+
29+
// defer db.Close()
30+
// fixing linter issue for defer
31+
32+
// This is anonymous function :)
33+
defer func() {
34+
err := db.Close()
35+
if err != nil {
36+
log.Fatalf("Failed to close database: %v", err)
37+
}
38+
}()
2539

2640
// Here Goose is being initialized and setting up the db type to sqlite
2741
err = goose.SetDialect("sqlite3")

book-store-inventory/pkg/database/db_sqlite.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package database contains helper functions for initializing and managing database connections.
12
package database
23

34
import (

book-store-inventory/pkg/handlers/book_handler.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package handlers contains the main bussiness logic of the application
12
package handlers
23

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

27-
func (bookHandler *BookHandler) GetBookByIdHandler(ctx *gin.Context) {
28+
func (bookHandler *BookHandler) GetBookByIDHandler(ctx *gin.Context) {
2829
// getting param from the URL
2930
/* http://localhost:9090/api/v1/book/id/3 --> 3 is param here */
30-
book_id_param := ctx.Param("book_id")
31-
book, err := bookHandler.BookModel.GetBookByID(book_id_param)
31+
bookIDParam := ctx.Param("book_id")
32+
book, err := bookHandler.BookModel.GetBookByID(bookIDParam)
3233
if err != nil {
3334
ctx.JSON(http.StatusInternalServerError, gin.H{
3435
"error": "Failed to get book by ID",

book-store-inventory/pkg/handlers/test_handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"github.com/gin-gonic/gin"
77
)
88

9-
func TestApi(ctx *gin.Context) {
9+
func TestAPI(ctx *gin.Context) {
1010
ctx.JSON(http.StatusOK, gin.H{
1111
"message": "API Working 💞 [Private route]",
1212
})

book-store-inventory/pkg/middleware/basic_auth.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package middleware contains basic user and pass middleware for testing private routes
12
package middleware
23

34
import "github.com/gin-gonic/gin"

book-store-inventory/pkg/models/book_model.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package models contains struct for Data
12
package models
23

34
import "time"

book-store-inventory/pkg/repositories/book_repository.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package repositories contains logic to interact with database uisng raw SQL queries
12
package repositories
23

34
import (
@@ -48,12 +49,12 @@ func (bookModel *BookModel) GetBooks() ([]models.Book, error) {
4849
return books, nil
4950
}
5051

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

56-
err := bookModel.DB.QueryRow(statement, book_id).Scan(
57+
err := bookModel.DB.QueryRow(statement, bookID).Scan(
5758
&book.BookID,
5859
&book.Title,
5960
&book.Author,
@@ -76,14 +77,14 @@ func (bookModel *BookModel) AddBook(book models.Book) (models.Book, error) {
7677
return models.Book{}, err
7778
}
7879

79-
lastInsertedId, err := result.LastInsertId()
80+
lastInsertedID, err := result.LastInsertId()
8081
if err != nil {
8182
return models.Book{}, err
8283
}
8384

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

8889
return book, nil
8990
}
Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
// Package router have routes defined in it for REST API
2+
package router
3+
14
// https://stackoverflow.com/a/62608670
25
// https://stackoverflow.com/questions/62608429/how-to-combine-group-of-routes-in-gin
36

4-
package router
5-
67
import (
78
"github.com/gin-gonic/gin"
9+
"github.com/saketV8/book-store-inventory/pkg/handlers"
810
"github.com/saketV8/book-store-inventory/pkg/middleware"
911
"github.com/saketV8/book-store-inventory/pkg/utils"
1012
)
@@ -14,12 +16,17 @@ func SetupPrivateRouter(app *App, superRouterGroup *gin.Engine) {
1416
// <ROUTER_PREFIX> = /api
1517
// <ROUTER_PREFIX_VERSION> = /v1
1618

19+
// fixing linter issue
20+
// app will be used when we pass data from app to handler :)
21+
// see example in <public_router.go> SetupPublicRouter()
22+
_ = app
23+
1724
routerGroup := superRouterGroup.Group(utils.ROUTER_PREFIX)
1825
{
1926
v1 := routerGroup.Group(utils.ROUTER_PREFIX_VERSION)
2027
v1.Use(middleware.BasicAuthMiddleware())
2128
{
22-
// v1.GET("/test-private-api/", handlers.TestApi)
29+
v1.GET("/test-private/", handlers.TestAPI)
2330
}
2431
}
2532
}

book-store-inventory/pkg/router/public_router.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func SetupPublicRouter(app *App, superRouterGroup *gin.Engine) {
1818
{
1919
// GET
2020
v1.GET("/books/", app.Books.GetBooksHandler)
21-
v1.GET("/book/id/:book_id", app.Books.GetBookByIdHandler)
21+
v1.GET("/book/id/:book_id", app.Books.GetBookByIDHandler)
2222

2323
// POST
2424
v1.POST("/book/add/", app.Books.AddBookHandler)

book-store-inventory/pkg/router/router.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,13 @@ import (
99
"github.com/gin-gonic/gin"
1010
"github.com/saketV8/book-store-inventory/pkg/handlers"
1111
"github.com/saketV8/book-store-inventory/pkg/utils"
12-
13-
log "github.com/sirupsen/logrus"
1412
)
1513

1614
type App struct {
1715
// Add more handlers as needed
1816
Books *handlers.BookHandler
1917
}
2018

21-
// func SetupRouter(DbModel *querydb.DbModel) {
2219
func SetupRouter(app *App) {
2320
// Initializing the GIN Router
2421
gin.SetMode(gin.ReleaseMode)
@@ -41,7 +38,7 @@ func SetupRouter(app *App) {
4138
err := rtr.Run(utils.PORT)
4239
if err != nil {
4340
fmt.Println("============================================")
44-
log.Fatal("ERROR: while Initializing the server")
41+
fmt.Println("ERROR: while Initializing the server")
4542
fmt.Println("============================================")
4643
}
4744
}
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
// Package utils contains constant to be used across the application
12
package utils
23

34
// TODO:
45
// replace all with env variable
5-
var PORT string = ":9090"
6-
var ROUTER_PREFIX string = "/api"
6+
7+
var PORT = ":9090"
8+
var ROUTER_PREFIX = "/api"
79
var ROUTER_PREFIX_VERSION = "/v1"

book-store-inventory/pkg/utils/list_all_routes.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package utils contains function to print all available routes in application
12
package utils
23

34
import (
@@ -8,6 +9,7 @@ import (
89

910
// https://stackoverflow.com/questions/287871/how-do-i-print-colored-text-to-the-terminal
1011
// https://stackoverflow.com/a/26445590
12+
1113
var Reset = "\033[0m"
1214

1315
var Blue = "\033[34m"

0 commit comments

Comments
 (0)