196 lines
8.2 KiB
Go
196 lines
8.2 KiB
Go
package handlers
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
|
|
"github.com/SamuelTariku/FortuneBet-Backend/internal/domain"
|
|
"github.com/SamuelTariku/FortuneBet-Backend/internal/web_server/response"
|
|
"github.com/gofiber/fiber/v2"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
// InsertCompanyMarketSettings
|
|
// @Summary Insert company-specific market settings
|
|
// @Description Insert new market settings for a specific tenant/company
|
|
// @Tags market_settings
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param body body domain.CreateCompanyMarketSettings true "Market Settings"
|
|
// @Success 200 {object} response.APIResponse
|
|
// @Failure 400 {object} response.APIResponse
|
|
// @Failure 500 {object} response.APIResponse
|
|
// @Router /api/v1/{tenant_slug}/market-settings [post]
|
|
func (h *Handler) InsertCompanyMarketSettings(c *fiber.Ctx) error {
|
|
companyID := c.Locals("company_id").(domain.ValidInt64)
|
|
if !companyID.Valid {
|
|
h.BadRequestLogger().Error("invalid company id")
|
|
return fiber.NewError(fiber.StatusBadRequest, "invalid company id")
|
|
}
|
|
|
|
var req domain.CreateCompanyMarketSettingsReq
|
|
if err := c.BodyParser(&req); err != nil {
|
|
h.BadRequestLogger().Info("Failed to parse request", zap.Int64("company_id", companyID.Value), zap.Error(err))
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
}
|
|
valErrs, ok := h.validator.Validate(c, req)
|
|
if !ok {
|
|
errMsg := ""
|
|
for field, msg := range valErrs {
|
|
errMsg += fmt.Sprintf("%s: %s; ", field, msg)
|
|
}
|
|
h.BadRequestLogger().Error("Validation failed", zap.String("errors", errMsg))
|
|
return fiber.NewError(fiber.StatusBadRequest, errMsg)
|
|
}
|
|
|
|
if err := h.prematchSvc.InsertCompanyMarketSettings(c.Context(), domain.ConvertCreateCompanyMarketSettingsReq(
|
|
req, companyID.Value,
|
|
)); err != nil {
|
|
h.InternalServerErrorLogger().Error("Failed to insert company market settings", zap.Int64("company_id", companyID.Value), zap.Error(err))
|
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to insert market settings: "+err.Error())
|
|
}
|
|
|
|
return response.WriteJSON(c, fiber.StatusOK, "Company market settings inserted successfully", nil, nil)
|
|
}
|
|
|
|
// GetAllGlobalMarketSettings
|
|
// @Summary Retrieve all global market settings
|
|
// @Description Get all market settings that apply globally
|
|
// @Tags market_settings
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param limit query int false "Number of results to return (default 10)"
|
|
// @Param offset query int false "Number of results to skip (default 0)"
|
|
// @Success 200 {array} domain.MarketSettings
|
|
// @Failure 400 {object} response.APIResponse
|
|
// @Failure 500 {object} response.APIResponse
|
|
// @Router /api/v1/market-settings [get]
|
|
func (h *Handler) GetAllGlobalMarketSettings(c *fiber.Ctx) error {
|
|
limit, err := strconv.Atoi(c.Query("limit", "10"))
|
|
if err != nil || limit <= 0 {
|
|
h.BadRequestLogger().Info("Invalid limit value", zap.Error(err))
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid limit value")
|
|
}
|
|
|
|
offset, err := strconv.Atoi(c.Query("offset", "0"))
|
|
if err != nil || offset < 0 {
|
|
h.BadRequestLogger().Info("Invalid offset value", zap.Error(err))
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid offset value")
|
|
}
|
|
|
|
settings, err := h.prematchSvc.GetAllGlobalMarketSettings(c.Context(), domain.MarketSettingFilter{
|
|
Limit: domain.ValidInt32{Value: int32(limit), Valid: true},
|
|
Offset: domain.ValidInt32{Value: int32(offset), Valid: true},
|
|
})
|
|
if err != nil {
|
|
h.InternalServerErrorLogger().Error("Failed to retrieve global market settings", zap.Error(err))
|
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to retrieve global market settings: "+err.Error())
|
|
}
|
|
|
|
res := domain.ConvertMarketSettingsResList(settings)
|
|
|
|
return response.WriteJSON(c, fiber.StatusOK, "Global market settings retrieved successfully", res, nil)
|
|
}
|
|
|
|
// GetAllTenantMarketSettings
|
|
// @Summary Retrieve all market settings for a tenant
|
|
// @Description Get all market settings overridden for a specific tenant
|
|
// @Tags market_settings
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param limit query int false "Number of results to return (default 10)"
|
|
// @Param offset query int false "Number of results to skip (default 0)"
|
|
// @Success 200 {array} domain.CompanyMarketSettings
|
|
// @Failure 400 {object} response.APIResponse
|
|
// @Failure 500 {object} response.APIResponse
|
|
// @Router /api/v1/{tenant_slug}/market-settings [get]
|
|
func (h *Handler) GetAllTenantMarketSettings(c *fiber.Ctx) error {
|
|
companyID := c.Locals("company_id").(domain.ValidInt64)
|
|
if !companyID.Valid {
|
|
h.BadRequestLogger().Error("invalid company id")
|
|
return fiber.NewError(fiber.StatusBadRequest, "invalid company id")
|
|
}
|
|
|
|
limit, err := strconv.Atoi(c.Query("limit", "10"))
|
|
if err != nil || limit <= 0 {
|
|
h.BadRequestLogger().Info("Invalid limit value", zap.Error(err))
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid limit value")
|
|
}
|
|
|
|
offset, err := strconv.Atoi(c.Query("offset", "0"))
|
|
if err != nil || offset < 0 {
|
|
h.BadRequestLogger().Info("Invalid offset value", zap.Error(err))
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid offset value")
|
|
}
|
|
|
|
settings, err := h.prematchSvc.GetAllOverrideMarketSettings(c.Context(), companyID.Value, domain.MarketSettingFilter{
|
|
Limit: domain.ValidInt32{Value: int32(limit), Valid: true},
|
|
Offset: domain.ValidInt32{Value: int32(offset), Valid: true},
|
|
})
|
|
if err != nil {
|
|
h.InternalServerErrorLogger().Error("Failed to retrieve tenant market settings", zap.Int64("company_id", companyID.Value), zap.Error(err))
|
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to retrieve tenant market settings: "+err.Error())
|
|
}
|
|
res := domain.ConvertMarketSettingsResList(settings)
|
|
|
|
return response.WriteJSON(c, fiber.StatusOK, "Tenant market settings retrieved successfully", res, nil)
|
|
}
|
|
|
|
// DeleteAllCompanyMarketSettings
|
|
// @Summary Delete all market settings for a tenant
|
|
// @Description Remove all overridden market settings for a specific tenant
|
|
// @Tags market_settings
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Success 200 {object} response.APIResponse
|
|
// @Failure 400 {object} response.APIResponse
|
|
// @Failure 500 {object} response.APIResponse
|
|
// @Router /api/v1/{tenant_slug}/market-settings [delete]
|
|
func (h *Handler) DeleteAllCompanyMarketSettings(c *fiber.Ctx) error {
|
|
companyID := c.Locals("company_id").(domain.ValidInt64)
|
|
if !companyID.Valid {
|
|
h.BadRequestLogger().Error("invalid company id")
|
|
return fiber.NewError(fiber.StatusBadRequest, "invalid company id")
|
|
}
|
|
|
|
if err := h.prematchSvc.DeleteAllCompanyMarketSettings(c.Context(), companyID.Value); err != nil {
|
|
h.InternalServerErrorLogger().Error("Failed to delete all company market settings", zap.Int64("company_id", companyID.Value), zap.Error(err))
|
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to delete all company market settings: "+err.Error())
|
|
}
|
|
|
|
return response.WriteJSON(c, fiber.StatusOK, "All tenant market settings removed successfully", nil, nil)
|
|
}
|
|
|
|
// DeleteCompanyMarketSettings
|
|
// @Summary Delete a specific market setting for a tenant
|
|
// @Description Remove a specific overridden market setting for a tenant
|
|
// @Tags market_settings
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path int true "Market ID"
|
|
// @Success 200 {object} response.APIResponse
|
|
// @Failure 400 {object} response.APIResponse
|
|
// @Failure 500 {object} response.APIResponse
|
|
// @Router /api/v1/{tenant_slug}/market-settings/{id} [delete]
|
|
func (h *Handler) DeleteCompanyMarketSettings(c *fiber.Ctx) error {
|
|
companyID := c.Locals("company_id").(domain.ValidInt64)
|
|
if !companyID.Valid {
|
|
h.BadRequestLogger().Error("invalid company id")
|
|
return fiber.NewError(fiber.StatusBadRequest, "invalid company id")
|
|
}
|
|
|
|
marketIDStr := c.Params("id")
|
|
marketID, err := strconv.ParseInt(marketIDStr, 10, 64)
|
|
if err != nil {
|
|
h.BadRequestLogger().Info("Failed to parse market id", zap.String("id", marketIDStr))
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid market id")
|
|
}
|
|
|
|
if err := h.prematchSvc.DeleteCompanyMarketSettings(c.Context(), companyID.Value, marketID); err != nil {
|
|
h.InternalServerErrorLogger().Error("Failed to delete company market setting", zap.Int64("company_id", companyID.Value), zap.Int64("market_id", marketID), zap.Error(err))
|
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to delete market setting: "+err.Error())
|
|
}
|
|
|
|
return response.WriteJSON(c, fiber.StatusOK, "Tenant market setting removed successfully", nil, nil)
|
|
}
|