Skip to content

Commit

Permalink
Enhance job response structure: add organization details and implemen…
Browse files Browse the repository at this point in the history
…t job retrieval by ID
  • Loading branch information
IlhamSetiaji committed Dec 16, 2024
1 parent f58623a commit 7da0723
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 28 deletions.
Binary file modified go-sso.exe~
Binary file not shown.
45 changes: 28 additions & 17 deletions internal/http/dto/job_dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,40 @@ func ConvertToJobResponse(jobs *[]entity.Job) *[]response.JobResponse {
parentResponse = &response.ParentJobResponse{ID: job.Parent.ID, Name: job.Parent.Name}
}
responseJobs = append(responseJobs, response.JobResponse{
ID: job.ID,
Name: job.Name,
OrganizationStructureID: job.OrganizationStructureID,
Level: job.Level,
ParentID: job.ParentID,
Path: job.Path,
Existing: job.Existing,
Parent: parentResponse,
Children: *ConvertToJobResponse(&job.Children),
ID: job.ID,
Name: job.Name,
OrganizationStructureID: job.OrganizationStructureID,
OrganizationStructureName: job.OrganizationStructure.Name,
OrganizationID: job.OrganizationStructure.OrganizationID,
OrganizationName: job.OrganizationStructure.Organization.Name,
Level: job.Level,
ParentID: job.ParentID,
Path: job.Path,
Existing: job.Existing,
Parent: parentResponse,
Children: *ConvertToJobResponse(&job.Children),
})
}
return &responseJobs
}

func ConvertToSingleJobResponse(job *entity.Job) *response.JobResponse {
var parentResponse *response.ParentJobResponse
if job.Parent != nil {
parentResponse = &response.ParentJobResponse{ID: job.Parent.ID, Name: job.Parent.Name}
}
return &response.JobResponse{
ID: job.ID,
Name: job.Name,
OrganizationStructureID: job.OrganizationStructureID,
Level: job.Level,
ParentID: job.ParentID,
Path: job.Path,
Existing: job.Existing,
Children: *ConvertToJobResponse(&job.Children),
ID: job.ID,
Name: job.Name,
OrganizationStructureID: job.OrganizationStructureID,
OrganizationStructureName: job.OrganizationStructure.Name,
OrganizationID: job.OrganizationStructure.OrganizationID,
OrganizationName: job.OrganizationStructure.Organization.Name,
Level: job.Level,
ParentID: job.ParentID,
Path: job.Path,
Existing: job.Existing,
Parent: parentResponse,
Children: *ConvertToJobResponse(&job.Children),
}
}
21 changes: 12 additions & 9 deletions internal/http/response/job_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ import (
)

type JobResponse struct {
ID uuid.UUID `json:"id"`
Name string `json:"name"`
OrganizationStructureID uuid.UUID `json:"organization_structure_id"`
ParentID *uuid.UUID `json:"parent_id"`
Level int `json:"level"` // Add level for hierarchy depth
Path string `json:"path"` // Store full path for easy traversal
Existing int `json:"existing"`
Parent *ParentJobResponse `json:"parent"`
Children []JobResponse `json:"children"`
ID uuid.UUID `json:"id"`
Name string `json:"name"`
OrganizationStructureID uuid.UUID `json:"organization_structure_id"`
OrganizationStructureName string `json:"organization_structure_name"`
OrganizationID uuid.UUID `json:"organization_id"`
OrganizationName string `json:"organization_name"`
ParentID *uuid.UUID `json:"parent_id"`
Level int `json:"level"` // Add level for hierarchy depth
Path string `json:"path"` // Store full path for easy traversal
Existing int `json:"existing"`
Parent *ParentJobResponse `json:"parent"`
Children []JobResponse `json:"children"`
}

type ParentJobResponse struct {
Expand Down
54 changes: 54 additions & 0 deletions internal/messaging/job/find_job_data_by_id_message.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package messaging

import (
"app/go-sso/internal/http/dto"
"app/go-sso/internal/http/response"
"app/go-sso/internal/repository"

"github.com/google/uuid"
"github.com/sirupsen/logrus"
)

type IFindJobDataByIdMessageRequest struct {
JobID uuid.UUID `json:"job_id"`
}

type IFindJobDataByIdMessageResponse struct {
JobID uuid.UUID `json:"job_id"`
Job *response.JobResponse `json:"job"`
}

type IFindJobDataByIdMessage interface {
Execute(request IFindJobDataByIdMessageRequest) (*IFindJobDataByIdMessageResponse, error)
}

type FindJobDataByIdMessage struct {
Log *logrus.Logger
JobRepository repository.IJobRepository
}

func NewFindJobDataByIdMessage(log *logrus.Logger, jobRepository repository.IJobRepository) IFindJobDataByIdMessage {
return &FindJobDataByIdMessage{
Log: log,
JobRepository: jobRepository,
}
}

func (m *FindJobDataByIdMessage) Execute(request IFindJobDataByIdMessageRequest) (*IFindJobDataByIdMessageResponse, error) {
job, err := m.JobRepository.FindById(request.JobID)
if err != nil {
return nil, err
}

jobResponse := dto.ConvertToSingleJobResponse(job)

return &IFindJobDataByIdMessageResponse{
JobID: job.ID,
Job: jobResponse,
}, nil
}

func FindJobDataByIdMessageFactory(log *logrus.Logger) IFindJobDataByIdMessage {
repository := repository.JobRepositoryFactory(log)
return NewFindJobDataByIdMessage(log, repository)
}
27 changes: 27 additions & 0 deletions internal/rabbitmq/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,33 @@ func handleMsg(docMsg *request.RabbitMQRequest, log *logrus.Logger) {
msgData = map[string]interface{}{
"user": message.User,
}
case "find_job_data_by_id":
jobID, ok := docMsg.MessageData["job_id"].(string)
if !ok {
log.Printf("Invalid request format: missing 'job_id'")
msgData = map[string]interface{}{
"error": errors.New("missing 'job_id'").Error(),
}
break
}

messageFactory := messaging.FindJobDataByIdMessageFactory(log)
message, err := messageFactory.Execute(messaging.IFindJobDataByIdMessageRequest{
JobID: uuid.MustParse(jobID),
})

if err != nil {
log.Printf("Failed to execute message: %v", err)
msgData = map[string]interface{}{
"error": err.Error(),
}
break
}

msgData = map[string]interface{}{
"job_id": jobID,
"job": message.Job,
}
default:
log.Printf("Unknown message type: %s", docMsg.MessageType)

Expand Down
4 changes: 2 additions & 2 deletions internal/repository/job_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (r *JobRepository) FindAllPaginated(page int, pageSize int, search string)
var jobs []entity.Job
var total int64

query := r.DB
query := r.DB.Preload("OrganizationStructure.Organization")

if search != "" {
query = query.Where("name LIKE ?", "%"+search+"%")
Expand All @@ -52,7 +52,7 @@ func (r *JobRepository) FindAllPaginated(page int, pageSize int, search string)

func (r *JobRepository) FindById(id uuid.UUID) (*entity.Job, error) {
var job entity.Job
err := r.DB.Where("id = ?", id).First(&job).Error
err := r.DB.Preload("OrganizationStructure.Organization").Where("id = ?", id).First(&job).Error
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 7da0723

Please sign in to comment.