-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Go backend w/ database and basic rest API #4
base: dev
Are you sure you want to change the base?
Conversation
backend/internal/database/db.go
Outdated
db *sql.DB | ||
} | ||
|
||
func toInt(barcode string) (int64, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a redundant function because the function it wraps already does the thing it aims to wrap.
_ "github.com/mattn/go-sqlite3" | ||
) | ||
|
||
func getProductInfo(barcode_num string) (map[string]interface{}, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return a structure not interface. Returning an interface is equivalent of using any in typescript.
|
||
err := godotenv.Load() | ||
if err != nil { | ||
log.Fatalf("Error loading .env file") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use either fmt or log, don't use both for consistency sake.
return | ||
} | ||
|
||
c.JSON(200, gin.H{"message": "added item " + request.Barcode + " " + "(" + request.Name + ")"}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should use fmt here
validUsername := os.Getenv("AUTH_USERNAME") | ||
validPassword := os.Getenv("AUTH_PASSWORD") | ||
|
||
println(request.Password + " " + request.Username) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use log or fmt
println(validUsername + " " + validPassword) | ||
|
||
// Check if the credentials are correct | ||
if request.Username == validUsername && request.Password == validPassword { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could probably be decouples into its own function
return | ||
} | ||
|
||
err := godotenv.Load() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you already load this once outside of this function, you dont need to load it again
|
||
func UseItemRoutes(router *gin.RouterGroup, food *database.FoodItems) { | ||
router.GET("/items", func(c *gin.Context) { | ||
HandleGetAllItems(c, food) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is a good, but you an use a different wrapping methodology, to get rid of the surrounding func(c *gin.Context) {}
ex:
func HandleGetAllItems(c *gin.Context, food *database.FoodItems) func(c *gin.Context) {
return func(c *gin.Context) {
...
}
}
This PR introduces the foundation of a Go backend for the SCEats project, implementing a database connection and a basic REST API. The project is organized into distinct internal packages for database management and http services, adhering to Go’s best practices for package structuring.