Skip to content

Commit

Permalink
Added Suppliers Adding system and fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
AriaFantom committed Sep 22, 2024
1 parent cbda92d commit e9395ba
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
30 changes: 30 additions & 0 deletions backend/handler/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,36 @@ func FetchAllCategories(c fiber.Ctx) error {
return c.Status(200).JSON(categories)
}

func CreateSupplier(c fiber.Ctx) error {
name, contactInfo := c.FormValue("name"), c.FormValue("contact_info")
if name == "" || contactInfo == "" {
return c.Status(400).JSON(fiber.Map{
"error": "Name or Contact Info is missing",
})
}
err := repository.InsertSupplier(
&models.Supplier{
Name: name,
ContactInfo: contactInfo,
})
if err != nil {
return c.Status(500).JSON(fiber.Map{
"error": err.Error(),
})
}
return c.Status(200).JSON(fiber.Map{"message": "Supplier created successfully"})
}

func FetchAllSuppliers(c fiber.Ctx) error {
suppliers, err := repository.GetSuppliers()
if err != nil {
return c.Status(500).JSON(fiber.Map{
"error": err.Error(),
})
}
return c.Status(200).JSON(suppliers)
}

func CreateInventory(c fiber.Ctx) error {
inventory := new(models.Inventory)
if err := c.Bind().Body(inventory); err != nil {
Expand Down
23 changes: 23 additions & 0 deletions backend/repository/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func GetCategories() ([]models.Category, error) {

return categories, nil
}

func InsertSupplier(supplier *models.Supplier) error {
postgres, _ := database.Connect()

Expand All @@ -55,6 +56,28 @@ func InsertSupplier(supplier *models.Supplier) error {
return nil
}

func GetSuppliers() ([]models.Supplier, error) {
postgres, _ := database.Connect()

rows, err := postgres.Query(context.Background(), "SELECT * FROM suppliers")
if err != nil {
return nil, err
}
defer rows.Close()

var suppliers []models.Supplier
for rows.Next() {
var supplier models.Supplier
err := rows.Scan(&supplier.ID, &supplier.Name, &supplier.ContactInfo)
if err != nil {
return nil, err
}
suppliers = append(suppliers, supplier)
}

return suppliers, nil
}

func InsertInventory(inventory *models.Inventory) error {
postgres, _ := database.Connect()
mongo, _ := database.ConnectMongo()
Expand Down
9 changes: 7 additions & 2 deletions backend/routes/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,20 @@ import (

func HandleRoutes(app *fiber.App) {

// Auth routes
auth := app.Group("/auth")

auth.Post("/login", handler.Login)
auth.Post("/register", handler.Register)
auth.Get("/verify", handler.VerifyToken)

// Inventory addition routes
app.Post("/categories", handler.CreateCategory, middleware.IsAuthorized)
app.Get("/categories", handler.FetchAllCategories)
app.Post("/suppliers", handler.CreateSupplier, middleware.IsAuthorized)
app.Post("/inventory", handler.CreateInventory, middleware.IsAuthorized)

// Inventory fetch routes
app.Get("/categories", handler.FetchAllCategories)
app.Get("/suppliers", handler.FetchAllSuppliers)
app.Get("/inventory", handler.FetchAllInventory)

}

0 comments on commit e9395ba

Please sign in to comment.