package controller import ( "math" "strconv" "strings" "gitlab.com/mbugroup/lti-api.git/internal/modules/finance/transactions/dto" service "gitlab.com/mbugroup/lti-api.git/internal/modules/finance/transactions/services" validation "gitlab.com/mbugroup/lti-api.git/internal/modules/finance/transactions/validations" "gitlab.com/mbugroup/lti-api.git/internal/response" "github.com/gofiber/fiber/v2" ) type TransactionController struct { TransactionService service.TransactionService } func NewTransactionController(transactionService service.TransactionService) *TransactionController { return &TransactionController{ TransactionService: transactionService, } } func (u *TransactionController) GetAll(c *fiber.Ctx) error { parseOptionalUint := func(key string) (*uint, error) { raw := strings.TrimSpace(c.Query(key, "")) if raw == "" { return nil, nil } parsed, err := strconv.ParseUint(raw, 10, 64) if err != nil { return nil, fiber.NewError(fiber.StatusBadRequest, "Invalid "+key) } if parsed == 0 { return nil, nil } value := uint(parsed) return &value, nil } bankId, err := parseOptionalUint("bank_id") if err != nil { return err } customerId, err := parseOptionalUint("customer_id") if err != nil { return err } supplierId, err := parseOptionalUint("supplier_id") if err != nil { return err } query := &validation.Query{ Page: c.QueryInt("page", 1), Limit: c.QueryInt("limit", 10), Search: c.Query("search", ""), TransactionType: c.Query("transaction_type", ""), BankId: bankId, CustomerId: customerId, SupplierId: supplierId, SortDate: c.Query("sort_date", ""), StartDate: c.Query("start_date", ""), EndDate: c.Query("end_date", ""), } if query.Page < 1 || query.Limit < 1 { return fiber.NewError(fiber.StatusBadRequest, "page and limit must be greater than 0") } result, totalResults, err := u.TransactionService.GetAll(c, query) if err != nil { return err } return c.Status(fiber.StatusOK). JSON(response.SuccessWithPaginate[dto.TransactionListDTO]{ Code: fiber.StatusOK, Status: "success", Message: "Get all transactions successfully", Meta: response.Meta{ Page: query.Page, Limit: query.Limit, TotalPages: int64(math.Ceil(float64(totalResults) / float64(query.Limit))), TotalResults: totalResults, }, Data: dto.ToTransactionListDTOs(result), }) } func (u *TransactionController) GetOne(c *fiber.Ctx) error { param := c.Params("id") id, err := strconv.Atoi(param) if err != nil { return fiber.NewError(fiber.StatusBadRequest, "Invalid Id") } result, err := u.TransactionService.GetOne(c, uint(id)) if err != nil { return err } return c.Status(fiber.StatusOK). JSON(response.Success{ Code: fiber.StatusOK, Status: "success", Message: "Get transaction successfully", Data: dto.ToTransactionListDTO(*result), }) } func (u *TransactionController) DeleteOne(c *fiber.Ctx) error { param := c.Params("id") id, err := strconv.Atoi(param) if err != nil { return fiber.NewError(fiber.StatusBadRequest, "Invalid Id") } if err := u.TransactionService.DeleteOne(c, uint(id)); err != nil { return err } return c.Status(fiber.StatusOK). JSON(response.Common{ Code: fiber.StatusOK, Status: "success", Message: "Delete transaction successfully", }) }