mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
Feat[BE}: inisiate repport module
This commit is contained in:
@@ -0,0 +1,131 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
dto "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"
|
||||
|
||||
"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)
|
||||
CreateOne(ctx *fiber.Ctx, req *validation.Create) (*dto.RepportListDTO, error)
|
||||
UpdateOne(ctx *fiber.Ctx, req *validation.Update, id uint) (*dto.RepportListDTO, error)
|
||||
DeleteOne(ctx *fiber.Ctx, id uint) error
|
||||
}
|
||||
|
||||
type repportService struct {
|
||||
Log *logrus.Logger
|
||||
Validate *validator.Validate
|
||||
dummyData map[uint]dto.RepportListDTO
|
||||
nextID uint
|
||||
}
|
||||
|
||||
func NewRepportService(validate *validator.Validate) RepportService {
|
||||
// Initialize with dummy data
|
||||
now := time.Now().UTC()
|
||||
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,
|
||||
nextID: 4,
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
total := int64(len(results))
|
||||
|
||||
// Apply pagination
|
||||
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) CreateOne(c *fiber.Ctx, req *validation.Create) (*dto.RepportListDTO, error) {
|
||||
if err := s.Validate.Struct(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
now := time.Now().UTC()
|
||||
newReport := dto.RepportListDTO{
|
||||
Id: s.nextID,
|
||||
Name: req.Name,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
s.dummyData[s.nextID] = newReport
|
||||
s.nextID++
|
||||
|
||||
return &newReport, nil
|
||||
}
|
||||
|
||||
func (s *repportService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*dto.RepportListDTO, error) {
|
||||
if err := s.Validate.Struct(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
data, ok := s.dummyData[id]
|
||||
if !ok {
|
||||
return nil, fiber.NewError(fiber.StatusNotFound, "Report not found")
|
||||
}
|
||||
|
||||
if req.Name != nil {
|
||||
data.Name = *req.Name
|
||||
}
|
||||
|
||||
data.UpdatedAt = time.Now().UTC()
|
||||
s.dummyData[id] = data
|
||||
|
||||
return &data, nil
|
||||
}
|
||||
|
||||
func (s *repportService) DeleteOne(c *fiber.Ctx, id uint) error {
|
||||
if _, ok := s.dummyData[id]; !ok {
|
||||
return fiber.NewError(fiber.StatusNotFound, "Report not found")
|
||||
}
|
||||
|
||||
delete(s.dummyData, id)
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user