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{
Page: c.QueryInt("page", 1),
Limit: c.QueryInt("limit", 10),
Search: c.Query("search", ""),
Page: ctx.QueryInt("page", 1),
Limit: ctx.QueryInt("limit", 10),
Search: ctx.Query("search", ""),
}
if query.Page < 1 || query.Limit < 1 {
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 {
return err
}
return c.Status(fiber.StatusOK).
return ctx.Status(fiber.StatusOK).
JSON(response.SuccessWithPaginate[dto.RepportListDTO]{
Code: fiber.StatusOK,
Status: "success",
Message: "Get all repports successfully",
Message: "Get all reports successfully",
Meta: response.Meta{
Page: query.Page,
Limit: query.Limit,
@@ -53,92 +53,46 @@ func (u *RepportController) GetAll(c *fiber.Ctx) error {
})
}
func (u *RepportController) GetOne(c *fiber.Ctx) error {
param := c.Params("id")
func (c *RepportController) GetOne(ctx *fiber.Ctx) error {
param := ctx.Params("id")
id, err := strconv.Atoi(param)
if err != nil {
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 {
return err
}
return c.Status(fiber.StatusOK).
return ctx.Status(fiber.StatusOK).
JSON(response.Success{
Code: fiber.StatusOK,
Status: "success",
Message: "Get repport successfully",
Message: "Get report successfully",
Data: result,
})
}
func (u *RepportController) CreateOne(c *fiber.Ctx) error {
req := new(validation.Create)
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")
func (c *RepportController) GetExpense(ctx *fiber.Ctx) error {
param := ctx.Params("id")
id, err := strconv.Atoi(param)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
}
if err := c.BodyParser(req); err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
}
result, err := u.RepportService.UpdateOne(c, req, uint(id))
result, err := c.RepportService.GetOne(ctx, uint(id))
if err != nil {
return err
}
return c.Status(fiber.StatusOK).
return ctx.Status(fiber.StatusOK).
JSON(response.Success{
Code: fiber.StatusOK,
Status: "success",
Message: "Update repport successfully",
Message: "Get report successfully",
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"
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{}
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)
}
+3 -3
View File
@@ -1,6 +1,7 @@
package repports
import (
controller "gitlab.com/mbugroup/lti-api.git/internal/modules/repports/controllers"
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.Get("/", ctrl.GetAll)
route.Post("/", ctrl.CreateOne)
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"
"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")
}
@@ -1,13 +1,5 @@
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 {
Page int `query:"page" validate:"omitempty,number,min=1,gt=0"`
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100,gt=0"`