From e9395ba728c685fd6abb5b14fcd673d8809d031d Mon Sep 17 00:00:00 2001 From: AriaFantom Date: Sun, 22 Sep 2024 23:07:55 +0530 Subject: [PATCH] Added Suppliers Adding system and fetching --- backend/handler/inventory.go | 30 ++++++++++++++++++++++++++++++ backend/repository/inventory.go | 23 +++++++++++++++++++++++ backend/routes/route.go | 9 +++++++-- 3 files changed, 60 insertions(+), 2 deletions(-) diff --git a/backend/handler/inventory.go b/backend/handler/inventory.go index 1ee1df6..abc5e0f 100644 --- a/backend/handler/inventory.go +++ b/backend/handler/inventory.go @@ -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 { diff --git a/backend/repository/inventory.go b/backend/repository/inventory.go index 960116b..a24862e 100644 --- a/backend/repository/inventory.go +++ b/backend/repository/inventory.go @@ -42,6 +42,7 @@ func GetCategories() ([]models.Category, error) { return categories, nil } + func InsertSupplier(supplier *models.Supplier) error { postgres, _ := database.Connect() @@ -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() diff --git a/backend/routes/route.go b/backend/routes/route.go index e3b300b..1412d9d 100644 --- a/backend/routes/route.go +++ b/backend/routes/route.go @@ -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) }