refactor: restructure in entirety
This commit is contained in:
107
internal/handler/buyer.go
Normal file
107
internal/handler/buyer.go
Normal file
@@ -0,0 +1,107 @@
|
||||
package handler
|
||||
|
||||
import "billit/internal/view"
|
||||
import (
|
||||
"billit/internal/database"
|
||||
"net/http"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
// BuyerHandlers holds db reference for buyer operations
|
||||
type BuyerHandlers struct {
|
||||
db database.Service
|
||||
}
|
||||
|
||||
// NewBuyerHandlers creates handlers with db access
|
||||
func NewBuyerHandlers(db database.Service) *BuyerHandlers {
|
||||
return &BuyerHandlers{db: db}
|
||||
}
|
||||
|
||||
// BuyerListHandler renders the /buyer page with all buyers
|
||||
func (h *BuyerHandlers) BuyerListHandler(c echo.Context) error {
|
||||
userID := getUserID(c)
|
||||
buyers, err := h.db.GetAllBuyerDetails(userID)
|
||||
if err != nil {
|
||||
return view.RenderServerError(c, "Failed to load buyers. Please try again.")
|
||||
}
|
||||
return view.Render(c, view.BuyerListPage(buyers))
|
||||
}
|
||||
|
||||
// BuyerCreatePageHandler renders the /buyer/create form page
|
||||
func (h *BuyerHandlers) BuyerCreatePageHandler(c echo.Context) error {
|
||||
return view.Render(c, view.BuyerCreatePage())
|
||||
}
|
||||
|
||||
// BuyerEditPageHandler renders the /buyer/edit/:id form page
|
||||
func (h *BuyerHandlers) BuyerEditPageHandler(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
userID := getUserID(c)
|
||||
|
||||
buyer, err := h.db.GetBuyerDetails(id, userID)
|
||||
if err != nil || buyer == nil {
|
||||
return view.RenderNotFound(c, "Buyer not found or you don't have access to it.")
|
||||
}
|
||||
return view.Render(c, view.BuyerEditPage(*buyer))
|
||||
}
|
||||
|
||||
// BuyerCreateHandler handles POST /buyer/create
|
||||
func (h *BuyerHandlers) BuyerCreateHandler(c echo.Context) error {
|
||||
userID := getUserID(c)
|
||||
if userID == "" {
|
||||
return c.Redirect(http.StatusFound, "/")
|
||||
}
|
||||
|
||||
name := c.FormValue("name")
|
||||
if name == "" {
|
||||
return c.String(http.StatusBadRequest, "Name is required")
|
||||
}
|
||||
|
||||
details := c.FormValue("details")
|
||||
|
||||
_, err := h.db.CreateBuyerDetails(userID, name, details)
|
||||
if err != nil {
|
||||
return c.String(http.StatusInternalServerError, "Failed to create buyer")
|
||||
}
|
||||
|
||||
return c.Redirect(http.StatusFound, "/buyer")
|
||||
}
|
||||
|
||||
// BuyerUpdateHandler handles POST /buyer/edit/:id
|
||||
func (h *BuyerHandlers) BuyerUpdateHandler(c echo.Context) error {
|
||||
userID := getUserID(c)
|
||||
if userID == "" {
|
||||
return c.Redirect(http.StatusFound, "/")
|
||||
}
|
||||
|
||||
id := c.Param("id")
|
||||
name := c.FormValue("name")
|
||||
if name == "" {
|
||||
return c.String(http.StatusBadRequest, "Name is required")
|
||||
}
|
||||
|
||||
details := c.FormValue("details")
|
||||
|
||||
err := h.db.UpdateBuyerDetails(id, userID, name, details)
|
||||
if err != nil {
|
||||
return c.String(http.StatusInternalServerError, "Failed to update buyer")
|
||||
}
|
||||
|
||||
return c.Redirect(http.StatusFound, "/buyer")
|
||||
}
|
||||
|
||||
// BuyerDeleteHandler handles DELETE /buyer/:id
|
||||
func (h *BuyerHandlers) BuyerDeleteHandler(c echo.Context) error {
|
||||
userID := getUserID(c)
|
||||
if userID == "" {
|
||||
return c.String(http.StatusUnauthorized, "Unauthorized")
|
||||
}
|
||||
|
||||
id := c.Param("id")
|
||||
err := h.db.DeleteBuyerDetails(id, userID)
|
||||
if err != nil {
|
||||
return c.String(http.StatusInternalServerError, "Failed to delete buyer")
|
||||
}
|
||||
|
||||
return c.NoContent(http.StatusOK)
|
||||
}
|
||||
Reference in New Issue
Block a user