feat: integrate logging and audit trail in category service and DTOs
This commit is contained in:
parent
6c757e2065
commit
cf51d7f0c9
|
|
@ -3,8 +3,8 @@ package dto
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
// staticDto "github.com/Caknoooo/go-gin-clean-starter/modules/static/dto"
|
"github.com/Caknoooo/go-gin-clean-starter/database/entities"
|
||||||
"github.com/Caknoooo/go-gin-clean-starter/pkg/dto"
|
pkgdto "github.com/Caknoooo/go-gin-clean-starter/pkg/dto"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
@ -43,10 +43,24 @@ type CategoryUpdateRequest struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type CategoryResponse struct {
|
type CategoryResponse struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
SearchKey string `json:"search_key"`
|
SearchKey string `json:"search_key"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
IsActive bool `json:"is_active"`
|
IsActive bool `json:"is_active"`
|
||||||
Client dto.ClientResponse `json:"client"`
|
Client pkgdto.IdNameResponse `json:"client"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func ToCategoryResponse(e entities.MCategoryEntity) CategoryResponse {
|
||||||
|
return CategoryResponse{
|
||||||
|
ID: e.ID.String(),
|
||||||
|
Name: e.Name,
|
||||||
|
SearchKey: e.SearchKey,
|
||||||
|
Description: e.Description,
|
||||||
|
IsActive: e.IsActive,
|
||||||
|
Client: pkgdto.IdNameResponse{
|
||||||
|
ID: e.Client.ID.String(),
|
||||||
|
Name: e.Client.Name,
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,8 +7,10 @@ import (
|
||||||
"github.com/Caknoooo/go-gin-clean-starter/modules/category/dto"
|
"github.com/Caknoooo/go-gin-clean-starter/modules/category/dto"
|
||||||
"github.com/Caknoooo/go-gin-clean-starter/modules/category/query"
|
"github.com/Caknoooo/go-gin-clean-starter/modules/category/query"
|
||||||
"github.com/Caknoooo/go-gin-clean-starter/modules/category/repository"
|
"github.com/Caknoooo/go-gin-clean-starter/modules/category/repository"
|
||||||
pkgdto "github.com/Caknoooo/go-gin-clean-starter/pkg/dto"
|
"github.com/Caknoooo/go-gin-clean-starter/pkg/constants"
|
||||||
|
"github.com/Caknoooo/go-gin-clean-starter/pkg/utils"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -23,12 +25,14 @@ type CategoryService interface {
|
||||||
type categoryService struct {
|
type categoryService struct {
|
||||||
db *gorm.DB
|
db *gorm.DB
|
||||||
categoryRepo repository.CategoryRepository
|
categoryRepo repository.CategoryRepository
|
||||||
|
log *logrus.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCategoryService(categoryRepo repository.CategoryRepository, db *gorm.DB) CategoryService {
|
func NewCategoryService(categoryRepo repository.CategoryRepository, db *gorm.DB, log *logrus.Logger) CategoryService {
|
||||||
return &categoryService{
|
return &categoryService{
|
||||||
categoryRepo: categoryRepo,
|
categoryRepo: categoryRepo,
|
||||||
db: db,
|
db: db,
|
||||||
|
log: log,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -40,10 +44,11 @@ func (s *categoryService) Create(ctx context.Context, req dto.CategoryCreateRequ
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
category := entities.MCategoryEntity{
|
category := entities.MCategoryEntity{
|
||||||
Name: req.Name,
|
Name: req.Name,
|
||||||
SearchKey: req.SearchKey,
|
SearchKey: req.SearchKey,
|
||||||
Description: req.Description,
|
Description: req.Description,
|
||||||
IsActive: req.IsActive,
|
IsActive: req.IsActive,
|
||||||
|
FullAuditTrail: utils.FillAuditTrail(ctx, constants.CREATE),
|
||||||
}
|
}
|
||||||
clientUUID, err := uuid.Parse(req.ClientID)
|
clientUUID, err := uuid.Parse(req.ClientID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -62,7 +67,13 @@ func (s *categoryService) Create(ctx context.Context, req dto.CategoryCreateRequ
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return dto.CategoryResponse{}, err
|
return dto.CategoryResponse{}, err
|
||||||
}
|
}
|
||||||
return toCategoryResponse(result), nil
|
s.log.WithFields(logrus.Fields{
|
||||||
|
"user_id": utils.GetUserID(ctx),
|
||||||
|
"action": "create",
|
||||||
|
"entity": "category",
|
||||||
|
"entity_id": created.ID.String(),
|
||||||
|
}).Info("Category created")
|
||||||
|
return dto.ToCategoryResponse(result), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *categoryService) GetById(ctx context.Context, categoryId string) (dto.CategoryResponse, error) {
|
func (s *categoryService) GetById(ctx context.Context, categoryId string) (dto.CategoryResponse, error) {
|
||||||
|
|
@ -70,7 +81,7 @@ func (s *categoryService) GetById(ctx context.Context, categoryId string) (dto.C
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return dto.CategoryResponse{}, err
|
return dto.CategoryResponse{}, err
|
||||||
}
|
}
|
||||||
return toCategoryResponse(category), nil
|
return dto.ToCategoryResponse(category), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *categoryService) GetAll(ctx context.Context, filter query.CategoryFilter) ([]dto.CategoryResponse, int64, error) {
|
func (s *categoryService) GetAll(ctx context.Context, filter query.CategoryFilter) ([]dto.CategoryResponse, int64, error) {
|
||||||
|
|
@ -80,7 +91,7 @@ func (s *categoryService) GetAll(ctx context.Context, filter query.CategoryFilte
|
||||||
}
|
}
|
||||||
var responses []dto.CategoryResponse
|
var responses []dto.CategoryResponse
|
||||||
for _, c := range categories {
|
for _, c := range categories {
|
||||||
responses = append(responses, toCategoryResponse(c))
|
responses = append(responses, dto.ToCategoryResponse(c))
|
||||||
}
|
}
|
||||||
if responses == nil {
|
if responses == nil {
|
||||||
responses = make([]dto.CategoryResponse, 0)
|
responses = make([]dto.CategoryResponse, 0)
|
||||||
|
|
@ -104,6 +115,7 @@ func (s *categoryService) Update(ctx context.Context, req dto.CategoryUpdateRequ
|
||||||
category.SearchKey = req.SearchKey
|
category.SearchKey = req.SearchKey
|
||||||
category.Description = req.Description
|
category.Description = req.Description
|
||||||
category.IsActive = req.IsActive
|
category.IsActive = req.IsActive
|
||||||
|
category.FullAuditTrail = utils.FillAuditTrail(ctx, constants.UPDATE)
|
||||||
updated, err := s.categoryRepo.Update(ctx, tx, category)
|
updated, err := s.categoryRepo.Update(ctx, tx, category)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
|
|
@ -115,7 +127,13 @@ func (s *categoryService) Update(ctx context.Context, req dto.CategoryUpdateRequ
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return dto.CategoryResponse{}, err
|
return dto.CategoryResponse{}, err
|
||||||
}
|
}
|
||||||
return toCategoryResponse(result), nil
|
s.log.WithFields(logrus.Fields{
|
||||||
|
"user_id": utils.GetUserID(ctx),
|
||||||
|
"action": "update",
|
||||||
|
"entity": "category",
|
||||||
|
"entity_id": categoryId,
|
||||||
|
}).Info("Category updated")
|
||||||
|
return dto.ToCategoryResponse(result), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *categoryService) Delete(ctx context.Context, categoryId string) error {
|
func (s *categoryService) Delete(ctx context.Context, categoryId string) error {
|
||||||
|
|
@ -125,24 +143,27 @@ func (s *categoryService) Delete(ctx context.Context, categoryId string) error {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
category, err := s.categoryRepo.GetById(ctx, tx, categoryId)
|
||||||
|
if err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
category.FullAuditTrail = utils.FillAuditTrail(ctx, constants.DELETE)
|
||||||
|
if _, err := s.categoryRepo.Update(ctx, tx, category); err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
return err
|
||||||
|
}
|
||||||
if err := s.categoryRepo.Delete(ctx, tx, categoryId); err != nil {
|
if err := s.categoryRepo.Delete(ctx, tx, categoryId); err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
tx.Commit()
|
tx.Commit()
|
||||||
|
|
||||||
|
s.log.WithFields(logrus.Fields{
|
||||||
|
"user_id": utils.GetUserID(ctx),
|
||||||
|
"action": "delete",
|
||||||
|
"entity": "category",
|
||||||
|
"entity_id": categoryId,
|
||||||
|
}).Info("Category deleted")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func toCategoryResponse(e entities.MCategoryEntity) dto.CategoryResponse {
|
|
||||||
return dto.CategoryResponse{
|
|
||||||
ID: e.ID.String(),
|
|
||||||
Name: e.Name,
|
|
||||||
SearchKey: e.SearchKey,
|
|
||||||
Description: e.Description,
|
|
||||||
IsActive: e.IsActive,
|
|
||||||
Client: pkgdto.ClientResponse{
|
|
||||||
ID: e.Client.ID.String(),
|
|
||||||
Name: e.Client.Name,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -181,7 +181,7 @@ func RegisterDependencies(injector *do.Injector) {
|
||||||
maintenanceGroupServ := maintGroupService.NewMaintenanceGroupService(maintenanceGroupRepository, maintenanceGroupRoleRepository, maintenanceGroupRoleUserRepository, db)
|
maintenanceGroupServ := maintGroupService.NewMaintenanceGroupService(maintenanceGroupRepository, maintenanceGroupRoleRepository, maintenanceGroupRoleUserRepository, db)
|
||||||
clientServ := clientService.NewClientService(clientRepository, db, log)
|
clientServ := clientService.NewClientService(clientRepository, db, log)
|
||||||
permissionsServ := permissionsService.NewPermissionsService(permissionsRepository, db)
|
permissionsServ := permissionsService.NewPermissionsService(permissionsRepository, db)
|
||||||
categoryServ := categoryService.NewCategoryService(categoryRepository, db)
|
categoryServ := categoryService.NewCategoryService(categoryRepository, db, log)
|
||||||
uomServ := uomService.NewUomService(uomRepository, sequenceServ, db, log)
|
uomServ := uomService.NewUomService(uomRepository, sequenceServ, db, log)
|
||||||
mvendorServ := mvendorService.NewVendorService(mvendorRepository, sequenceServ, db, log)
|
mvendorServ := mvendorService.NewVendorService(mvendorRepository, sequenceServ, db, log)
|
||||||
warehouseServ := warehouseService.NewWarehouseService(warehouseRepository, db, log)
|
warehouseServ := warehouseService.NewWarehouseService(warehouseRepository, db, log)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue