mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-22 14:25:45 +00:00
feat[BE]: menambahkan repo expense dan menhapus API API yang tidak akan digunakan di module repport
This commit is contained in:
@@ -4,10 +4,12 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
dto "gitlab.com/mbugroup/lti-api.git/internal/modules/repports/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"
|
||||
|
||||
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"
|
||||
@@ -16,32 +18,45 @@ import (
|
||||
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
|
||||
GetExpense(ctx *fiber.Ctx, id uint) (*dto.RepportListDTO, error)
|
||||
}
|
||||
|
||||
type repportService struct {
|
||||
Log *logrus.Logger
|
||||
Validate *validator.Validate
|
||||
dummyData map[uint]dto.RepportListDTO
|
||||
nextID uint
|
||||
Log *logrus.Logger
|
||||
Validate *validator.Validate
|
||||
dummyData map[uint]dto.RepportListDTO
|
||||
ExpenseRealizationRepo expenseRepo.ExpenseRealizationRepository
|
||||
}
|
||||
|
||||
func NewRepportService(validate *validator.Validate) RepportService {
|
||||
func NewRepportService(validate *validator.Validate, expenseRealizationRepo expenseRepo.ExpenseRealizationRepository) RepportService {
|
||||
// Initialize with dummy data
|
||||
now := time.Now().UTC()
|
||||
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},
|
||||
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,
|
||||
Log: utils.Log,
|
||||
Validate: validate,
|
||||
dummyData: dummyData,
|
||||
ExpenseRealizationRepo: expenseRealizationRepo,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,10 +75,10 @@ func (s *repportService) GetAll(c *fiber.Ctx, params *validation.Query) ([]dto.R
|
||||
results = append(results, v)
|
||||
}
|
||||
|
||||
total := int64(len(results))
|
||||
|
||||
// Apply pagination
|
||||
total := int64(len(results))
|
||||
offset := (params.Page - 1) * params.Limit
|
||||
|
||||
if offset >= int(total) {
|
||||
return []dto.RepportListDTO{}, total, nil
|
||||
}
|
||||
@@ -83,49 +98,9 @@ func (s *repportService) GetOne(c *fiber.Ctx, id uint) (*dto.RepportListDTO, err
|
||||
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
|
||||
func (s *repportService) GetExpense(c *fiber.Ctx, id uint) (*dto.RepportListDTO, error) {
|
||||
if data, ok := s.dummyData[id]; ok {
|
||||
return &data, nil
|
||||
}
|
||||
|
||||
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
|
||||
return nil, fiber.NewError(fiber.StatusNotFound, "Report not found")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user