136 lines
4.0 KiB
Go
136 lines
4.0 KiB
Go
package handlers
|
|
|
|
import (
|
|
"github.com/SamuelTariku/FortuneBet-Backend/internal/domain"
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
// @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
|
|
// @Tags Institutions - Banks
|
|
// @Produce json
|
|
// @Success 200 {array} domain.Bank
|
|
// @Failure 500 {object} domain.ErrorResponse
|
|
// @Router /api/v1/banks [get]
|
|
func (h *Handler) ListBanks(c *fiber.Ctx) error {
|
|
banks, err := h.instSvc.List(c.Context())
|
|
if err != nil {
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
|
|
}
|
|
return c.JSON(banks)
|
|
}
|