Skip to content
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

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from

Conversation

epicgdog
Copy link

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.

db *sql.DB
}

func toInt(barcode string) (int64, error) {

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.

backend/internal/database/db.go Outdated Show resolved Hide resolved
_ "github.com/mattn/go-sqlite3"
)

func getProductInfo(barcode_num string) (map[string]interface{}, error) {

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")

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 + ")"})

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)

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 {

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()

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)

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) {
...
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants