Files
lti-api/internal/modules/repports/services/repport.service.go
T

107 lines
2.8 KiB
Go

package service
import (
"strings"
"time"
"gitlab.com/mbugroup/lti-api.git/internal/modules/repports/dto"
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/repports/validations"
"gitlab.com/mbugroup/lti-api.git/internal/utils"
expenseRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/expenses/repositories"
"github.com/go-playground/validator/v10"
"github.com/gofiber/fiber/v2"
"github.com/sirupsen/logrus"
)
type RepportService interface {
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]dto.RepportListDTO, int64, error)
GetOne(ctx *fiber.Ctx, id uint) (*dto.RepportListDTO, error)
GetExpense(ctx *fiber.Ctx, id uint) (*dto.RepportListDTO, error)
}
type repportService struct {
Log *logrus.Logger
Validate *validator.Validate
dummyData map[uint]dto.RepportListDTO
ExpenseRealizationRepo expenseRepo.ExpenseRealizationRepository
}
func NewRepportService(validate *validator.Validate, expenseRealizationRepo expenseRepo.ExpenseRealizationRepository) RepportService {
// Initialize with dummy data
now := time.Now()
dummyData := map[uint]dto.RepportListDTO{
1: {
Id: 1,
Name: "Sales Report",
CreatedAt: now,
UpdatedAt: now,
},
2: {
Id: 2,
Name: "Inventory Report",
CreatedAt: now,
UpdatedAt: now,
},
3: {
Id: 3,
Name: "Production Report",
CreatedAt: now,
UpdatedAt: now,
},
}
return &repportService{
Log: utils.Log,
Validate: validate,
dummyData: dummyData,
ExpenseRealizationRepo: expenseRealizationRepo,
}
}
func (s *repportService) GetAll(c *fiber.Ctx, params *validation.Query) ([]dto.RepportListDTO, int64, error) {
if err := s.Validate.Struct(params); err != nil {
return nil, 0, err
}
// Convert map to slice
var results []dto.RepportListDTO
for _, v := range s.dummyData {
// Apply search filter if provided
if params.Search != "" && !strings.Contains(strings.ToLower(v.Name), strings.ToLower(params.Search)) {
continue
}
results = append(results, v)
}
// Apply pagination
total := int64(len(results))
offset := (params.Page - 1) * params.Limit
if offset >= int(total) {
return []dto.RepportListDTO{}, total, nil
}
end := offset + params.Limit
if end > int(total) {
end = int(total)
}
return results[offset:end], total, nil
}
func (s *repportService) GetOne(c *fiber.Ctx, id uint) (*dto.RepportListDTO, error) {
if data, ok := s.dummyData[id]; ok {
return &data, nil
}
return nil, fiber.NewError(fiber.StatusNotFound, "Report not found")
}
func (s *repportService) GetExpense(c *fiber.Ctx, id uint) (*dto.RepportListDTO, error) {
if data, ok := s.dummyData[id]; ok {
return &data, nil
}
return nil, fiber.NewError(fiber.StatusNotFound, "Report not found")
}