186 lines
5.4 KiB
Go
186 lines
5.4 KiB
Go
package handlers
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"github.com/SamuelTariku/FortuneBet-Backend/internal/domain"
|
|
"github.com/gofiber/fiber/v2"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
// @Summary Create a new bank
|
|
// @Tags Institutions - Banks
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param bank body domain.Bank true "Bank Info"
|
|
// @Success 201 {object} domain.Bank
|
|
// @Failure 400 {object} domain.ErrorResponse
|
|
// @Router /api/v1/banks [post]
|
|
func (h *Handler) CreateBank(c *fiber.Ctx) error {
|
|
var bank domain.Bank
|
|
if err := c.BodyParser(&bank); err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "invalid payload"})
|
|
}
|
|
|
|
err := h.instSvc.Create(c.Context(), &bank)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
|
|
}
|
|
return c.Status(fiber.StatusCreated).JSON(bank)
|
|
}
|
|
|
|
// @Summary Get a bank by ID
|
|
// @Tags Institutions - Banks
|
|
// @Produce json
|
|
// @Param id path int true "Bank ID"
|
|
// @Success 200 {object} domain.Bank
|
|
// @Failure 400 {object} domain.ErrorResponse
|
|
// @Failure 404 {object} domain.ErrorResponse
|
|
// @Failure 500 {object} domain.ErrorResponse
|
|
// @Router /api/v1/banks/{id} [get]
|
|
func (h *Handler) GetBankByID(c *fiber.Ctx) error {
|
|
id, err := c.ParamsInt("id")
|
|
if err != nil || id <= 0 {
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "invalid bank ID"})
|
|
}
|
|
|
|
bank, err := h.instSvc.GetByID(c.Context(), int64(id))
|
|
if err != nil {
|
|
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{"error": "bank not found"})
|
|
}
|
|
|
|
return c.JSON(bank)
|
|
}
|
|
|
|
// @Summary Update a bank
|
|
// @Tags Institutions - Banks
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path int true "Bank ID"
|
|
// @Param bank body domain.Bank true "Bank Info"
|
|
// @Success 200 {object} domain.Bank
|
|
// @Failure 400 {object} domain.ErrorResponse
|
|
// @Failure 404 {object} domain.ErrorResponse
|
|
// @Failure 500 {object} domain.ErrorResponse
|
|
// @Router /api/v1/banks/{id} [put]
|
|
func (h *Handler) UpdateBank(c *fiber.Ctx) error {
|
|
id, err := c.ParamsInt("id")
|
|
if err != nil || id <= 0 {
|
|
// return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "invalid bank ID"})
|
|
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
|
|
Message: "Failed to update bank",
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
var bank domain.Bank
|
|
if err := c.BodyParser(&bank); err != nil {
|
|
// return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "invalid payload"})
|
|
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
|
|
Message: "Failed to update bank",
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
bank.ID = id
|
|
|
|
err = h.instSvc.Update(c.Context(), &bank)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{
|
|
Message: "Failed to update bank",
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).JSON(domain.Response{
|
|
Message: "Bank updated successfully",
|
|
StatusCode: fiber.StatusOK,
|
|
Success: true,
|
|
Data: bank,
|
|
})
|
|
// return c.JSON(bank)
|
|
}
|
|
|
|
// @Summary Delete a bank
|
|
// @Tags Institutions - Banks
|
|
// @Produce json
|
|
// @Param id path int true "Bank ID"
|
|
// @Success 204 {string} string "Deleted successfully"
|
|
// @Failure 400 {object} domain.ErrorResponse
|
|
// @Failure 404 {object} domain.ErrorResponse
|
|
// @Failure 500 {object} domain.ErrorResponse
|
|
// @Router /api/v1/banks/{id} [delete]
|
|
func (h *Handler) DeleteBank(c *fiber.Ctx) error {
|
|
id, err := c.ParamsInt("id")
|
|
if err != nil || id <= 0 {
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "invalid bank ID"})
|
|
}
|
|
|
|
err = h.instSvc.Delete(c.Context(), int64(id))
|
|
if err != nil {
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
|
|
}
|
|
|
|
return c.SendStatus(fiber.StatusNoContent)
|
|
}
|
|
|
|
// @Summary List all banks with pagination and filtering
|
|
// @Tags Institutions - Banks
|
|
// @Produce json
|
|
// @Param country_id query integer false "Filter by country ID"
|
|
// @Param is_active query boolean false "Filter by active status"
|
|
// @Param search query string false "Search term for bank name or code"
|
|
// @Param page query integer false "Page number" default(1)
|
|
// @Param page_size query integer false "Items per page" default(50) maximum(100)
|
|
// @Success 200 {object} domain.InstResponse
|
|
// @Failure 400 {object} domain.ErrorResponse
|
|
// @Failure 500 {object} domain.ErrorResponse
|
|
// @Router /api/v1/banks [get]
|
|
func (h *Handler) ListBanks(c *fiber.Ctx) error {
|
|
// Parse query parameters
|
|
countryID, _ := strconv.Atoi(c.Query("country_id"))
|
|
var countryIDPtr *int
|
|
if c.Query("country_id") != "" {
|
|
countryIDPtr = &countryID
|
|
}
|
|
|
|
isActive, _ := strconv.ParseBool(c.Query("is_active"))
|
|
var isActivePtr *bool
|
|
if c.Query("is_active") != "" {
|
|
isActivePtr = &isActive
|
|
}
|
|
|
|
var searchTermPtr *string
|
|
if searchTerm := c.Query("search"); searchTerm != "" {
|
|
searchTermPtr = &searchTerm
|
|
}
|
|
|
|
page, _ := strconv.Atoi(c.Query("page", "1"))
|
|
pageSize, _ := strconv.Atoi(c.Query("page_size", "50"))
|
|
|
|
banks, pagination, err := h.instSvc.List(
|
|
c.Context(),
|
|
countryIDPtr,
|
|
isActivePtr,
|
|
searchTermPtr,
|
|
page,
|
|
pageSize,
|
|
)
|
|
if err != nil {
|
|
h.mongoLoggerSvc.Error("failed to list banks",
|
|
zap.Error(err),
|
|
zap.Any("query_params", c.Queries()),
|
|
)
|
|
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{
|
|
Message: "Failed to retrieve banks",
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).JSON(domain.InstResponse{
|
|
Message: "Banks retrieved successfully",
|
|
Status: "success",
|
|
Data: banks,
|
|
Pagination: pagination,
|
|
})
|
|
}
|