feat[BE]: menambahkan repo expense dan menhapus API API yang tidak akan digunakan di module repport

This commit is contained in:
aguhh18
2025-12-10 13:41:53 +07:00
parent 16d1358b3a
commit 3f9865d267
5 changed files with 65 additions and 138 deletions
@@ -22,27 +22,27 @@ func NewRepportController(repportService service.RepportService) *RepportControl
} }
} }
func (u *RepportController) GetAll(c *fiber.Ctx) error { func (c *RepportController) GetAll(ctx *fiber.Ctx) error {
query := &validation.Query{ query := &validation.Query{
Page: c.QueryInt("page", 1), Page: ctx.QueryInt("page", 1),
Limit: c.QueryInt("limit", 10), Limit: ctx.QueryInt("limit", 10),
Search: c.Query("search", ""), Search: ctx.Query("search", ""),
} }
if query.Page < 1 || query.Limit < 1 { if query.Page < 1 || query.Limit < 1 {
return fiber.NewError(fiber.StatusBadRequest, "page and limit must be greater than 0") return fiber.NewError(fiber.StatusBadRequest, "page and limit must be greater than 0")
} }
result, totalResults, err := u.RepportService.GetAll(c, query) result, totalResults, err := c.RepportService.GetAll(ctx, query)
if err != nil { if err != nil {
return err return err
} }
return c.Status(fiber.StatusOK). return ctx.Status(fiber.StatusOK).
JSON(response.SuccessWithPaginate[dto.RepportListDTO]{ JSON(response.SuccessWithPaginate[dto.RepportListDTO]{
Code: fiber.StatusOK, Code: fiber.StatusOK,
Status: "success", Status: "success",
Message: "Get all repports successfully", Message: "Get all reports successfully",
Meta: response.Meta{ Meta: response.Meta{
Page: query.Page, Page: query.Page,
Limit: query.Limit, Limit: query.Limit,
@@ -53,92 +53,46 @@ func (u *RepportController) GetAll(c *fiber.Ctx) error {
}) })
} }
func (u *RepportController) GetOne(c *fiber.Ctx) error { func (c *RepportController) GetOne(ctx *fiber.Ctx) error {
param := c.Params("id") param := ctx.Params("id")
id, err := strconv.Atoi(param) id, err := strconv.Atoi(param)
if err != nil { if err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id") return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
} }
result, err := u.RepportService.GetOne(c, uint(id)) result, err := c.RepportService.GetOne(ctx, uint(id))
if err != nil { if err != nil {
return err return err
} }
return c.Status(fiber.StatusOK). return ctx.Status(fiber.StatusOK).
JSON(response.Success{ JSON(response.Success{
Code: fiber.StatusOK, Code: fiber.StatusOK,
Status: "success", Status: "success",
Message: "Get repport successfully", Message: "Get report successfully",
Data: result, Data: result,
}) })
} }
func (u *RepportController) CreateOne(c *fiber.Ctx) error { func (c *RepportController) GetExpense(ctx *fiber.Ctx) error {
req := new(validation.Create) param := ctx.Params("id")
if err := c.BodyParser(req); err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
}
result, err := u.RepportService.CreateOne(c, req)
if err != nil {
return err
}
return c.Status(fiber.StatusCreated).
JSON(response.Success{
Code: fiber.StatusCreated,
Status: "success",
Message: "Create repport successfully",
Data: result,
})
}
func (u *RepportController) UpdateOne(c *fiber.Ctx) error {
req := new(validation.Update)
param := c.Params("id")
id, err := strconv.Atoi(param) id, err := strconv.Atoi(param)
if err != nil { if err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id") return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
} }
if err := c.BodyParser(req); err != nil { result, err := c.RepportService.GetOne(ctx, uint(id))
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
}
result, err := u.RepportService.UpdateOne(c, req, uint(id))
if err != nil { if err != nil {
return err return err
} }
return c.Status(fiber.StatusOK). return ctx.Status(fiber.StatusOK).
JSON(response.Success{ JSON(response.Success{
Code: fiber.StatusOK, Code: fiber.StatusOK,
Status: "success", Status: "success",
Message: "Update repport successfully", Message: "Get report successfully",
Data: result, Data: result,
}) })
} }
func (u *RepportController) 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.RepportService.DeleteOne(c, uint(id)); err != nil {
return err
}
return c.Status(fiber.StatusOK).
JSON(response.Common{
Code: fiber.StatusOK,
Status: "success",
Message: "Delete repport successfully",
})
}
+7 -1
View File
@@ -6,12 +6,18 @@ import (
"gorm.io/gorm" "gorm.io/gorm"
sRepport "gitlab.com/mbugroup/lti-api.git/internal/modules/repports/services" sRepport "gitlab.com/mbugroup/lti-api.git/internal/modules/repports/services"
expenseRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/expenses/repositories"
) )
type RepportModule struct{} type RepportModule struct{}
func (RepportModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Validate) { func (RepportModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Validate) {
repportService := sRepport.NewRepportService(validate) // Initialize expense realization repository
expRealizationRepo := expenseRepo.NewExpenseRealizationRepository(db)
// Initialize report service with expense realization repo
repportService := sRepport.NewRepportService(validate, expRealizationRepo)
RepportRoutes(router, repportService) RepportRoutes(router, repportService)
} }
+3 -3
View File
@@ -1,6 +1,7 @@
package repports package repports
import ( import (
controller "gitlab.com/mbugroup/lti-api.git/internal/modules/repports/controllers" controller "gitlab.com/mbugroup/lti-api.git/internal/modules/repports/controllers"
repport "gitlab.com/mbugroup/lti-api.git/internal/modules/repports/services" repport "gitlab.com/mbugroup/lti-api.git/internal/modules/repports/services"
@@ -13,8 +14,7 @@ func RepportRoutes(v1 fiber.Router, s repport.RepportService) {
route := v1.Group("/repports") route := v1.Group("/repports")
route.Get("/", ctrl.GetAll) route.Get("/", ctrl.GetAll)
route.Post("/", ctrl.CreateOne)
route.Get("/:id", ctrl.GetOne) route.Get("/:id", ctrl.GetOne)
route.Patch("/:id", ctrl.UpdateOne)
route.Delete("/:id", ctrl.DeleteOne) route.Get("expense", ctrl.GetExpense)
} }
@@ -4,10 +4,12 @@ import (
"strings" "strings"
"time" "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" validation "gitlab.com/mbugroup/lti-api.git/internal/modules/repports/validations"
"gitlab.com/mbugroup/lti-api.git/internal/utils" "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/go-playground/validator/v10"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@@ -16,32 +18,45 @@ import (
type RepportService interface { type RepportService interface {
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]dto.RepportListDTO, int64, error) GetAll(ctx *fiber.Ctx, params *validation.Query) ([]dto.RepportListDTO, int64, error)
GetOne(ctx *fiber.Ctx, id uint) (*dto.RepportListDTO, error) GetOne(ctx *fiber.Ctx, id uint) (*dto.RepportListDTO, error)
CreateOne(ctx *fiber.Ctx, req *validation.Create) (*dto.RepportListDTO, error) GetExpense(ctx *fiber.Ctx, id uint) (*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 { type repportService struct {
Log *logrus.Logger Log *logrus.Logger
Validate *validator.Validate Validate *validator.Validate
dummyData map[uint]dto.RepportListDTO dummyData map[uint]dto.RepportListDTO
nextID uint ExpenseRealizationRepo expenseRepo.ExpenseRealizationRepository
} }
func NewRepportService(validate *validator.Validate) RepportService { func NewRepportService(validate *validator.Validate, expenseRealizationRepo expenseRepo.ExpenseRealizationRepository) RepportService {
// Initialize with dummy data // Initialize with dummy data
now := time.Now().UTC() now := time.Now()
dummyData := map[uint]dto.RepportListDTO{ dummyData := map[uint]dto.RepportListDTO{
1: {Id: 1, Name: "Sales Report", CreatedAt: now, UpdatedAt: now}, 1: {
2: {Id: 2, Name: "Inventory Report", CreatedAt: now, UpdatedAt: now}, Id: 1,
3: {Id: 3, Name: "Production Report", CreatedAt: now, UpdatedAt: now}, 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{ return &repportService{
Log: utils.Log, Log: utils.Log,
Validate: validate, Validate: validate,
dummyData: dummyData, dummyData: dummyData,
nextID: 4, ExpenseRealizationRepo: expenseRealizationRepo,
} }
} }
@@ -60,10 +75,10 @@ func (s *repportService) GetAll(c *fiber.Ctx, params *validation.Query) ([]dto.R
results = append(results, v) results = append(results, v)
} }
total := int64(len(results))
// Apply pagination // Apply pagination
total := int64(len(results))
offset := (params.Page - 1) * params.Limit offset := (params.Page - 1) * params.Limit
if offset >= int(total) { if offset >= int(total) {
return []dto.RepportListDTO{}, total, nil 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") return nil, fiber.NewError(fiber.StatusNotFound, "Report not found")
} }
func (s *repportService) CreateOne(c *fiber.Ctx, req *validation.Create) (*dto.RepportListDTO, error) { func (s *repportService) GetExpense(c *fiber.Ctx, id uint) (*dto.RepportListDTO, error) {
if err := s.Validate.Struct(req); err != nil { if data, ok := s.dummyData[id]; ok {
return nil, err return &data, nil
} }
return nil, fiber.NewError(fiber.StatusNotFound, "Report not found")
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
} }
@@ -1,13 +1,5 @@
package validation package validation
type Create struct {
Name string `json:"name" validate:"required_strict,min=3"`
}
type Update struct {
Name *string `json:"name,omitempty" validate:"omitempty"`
}
type Query struct { type Query struct {
Page int `query:"page" validate:"omitempty,number,min=1,gt=0"` Page int `query:"page" validate:"omitempty,number,min=1,gt=0"`
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100,gt=0"` Limit int `query:"limit" validate:"omitempty,number,min=1,max=100,gt=0"`