Skip to content

Commit

Permalink
Enhance job and organization structure responses: add Existing field,…
Browse files Browse the repository at this point in the history
… update DTOs, and preload organization structures
  • Loading branch information
IlhamSetiaji committed Dec 9, 2024
1 parent 52913f9 commit c3bfae6
Show file tree
Hide file tree
Showing 14 changed files with 108 additions and 42 deletions.
5 changes: 5 additions & 0 deletions cmd/migration/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,22 +174,27 @@ func main() {
{
Name: "Kage",
OrganizationStructureID: createdStructures["Kage Department"].ID,
Existing: 30,
},
{
Name: "Jounin",
OrganizationStructureID: createdStructures["Jounin Department"].ID,
Existing: 30,
},
{
Name: "Chunin",
OrganizationStructureID: createdStructures["Chunin Department"].ID,
Existing: 30,
},
{
Name: "Genin",
OrganizationStructureID: createdStructures["Genin Department"].ID,
Existing: 30,
},
{
Name: "Academy",
OrganizationStructureID: createdStructures["Academy Department"].ID,
Existing: 30,
},
}

Expand Down
Binary file modified go-sso.exe~
Binary file not shown.
1 change: 1 addition & 0 deletions internal/entity/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Job struct {
ParentID *uuid.UUID `json:"parent_id" gorm:"type:char(36)"`
Level int `json:"level" gorm:"index"` // Add level for hierarchy depth
Path string `json:"path" gorm:"type:text"` // Store full path for easy traversal
Existing int `json:"existing" gorm:"default:0"`
OrganizationStructure OrganizationStructure `json:"organization_structure" gorm:"foreignKey:OrganizationStructureID;references:ID;constraint:OnDelete:CASCADE"`
Parent *Job `json:"parent" gorm:"foreignKey:ParentID;references:ID;constraint:OnDelete:CASCADE"`
Children []Job `json:"children" gorm:"foreignKey:ParentID;references:ID"`
Expand Down
2 changes: 2 additions & 0 deletions internal/http/dto/job_dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func ConvertToJobResponse(jobs *[]entity.Job) *[]response.JobResponse {
Level: job.Level,
ParentID: job.ParentID,
Path: job.Path,
Existing: job.Existing,
Children: *ConvertToJobResponse(&job.Children),
})
}
Expand All @@ -29,6 +30,7 @@ func ConvertToSingleJobResponse(job *entity.Job) *response.JobResponse {
Level: job.Level,
ParentID: job.ParentID,
Path: job.Path,
Existing: job.Existing,
Children: *ConvertToJobResponse(&job.Children),
}
}
52 changes: 52 additions & 0 deletions internal/http/dto/job_level_dto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package dto

import (
"app/go-sso/internal/entity"
"app/go-sso/internal/http/response"
)

func ConvertToJobLevelResponse(jobLevels *[]entity.JobLevel) *[]response.JobLevelResponse {
var responseJobLevels []response.JobLevelResponse
for _, jobLevel := range *jobLevels {
responseJobLevels = append(responseJobLevels, response.JobLevelResponse{
ID: jobLevel.ID,
Name: jobLevel.Name,
Level: jobLevel.Level,
OrganizationStructures: func() *[]response.OrganizationStructureMinimalResponse {
var responseOrganizationStructures []response.OrganizationStructureMinimalResponse
for _, organizationStructure := range jobLevel.OrganizationStructures {
responseOrganizationStructures = append(responseOrganizationStructures, response.OrganizationStructureMinimalResponse{
ID: organizationStructure.ID,
Name: organizationStructure.Name,
Level: organizationStructure.Level,
OrganizationID: organizationStructure.OrganizationID,
Path: organizationStructure.Path,
})
}
return &responseOrganizationStructures
}(),
})
}
return &responseJobLevels
}

func ConvertToSingleJobLevelResponse(jobLevel *entity.JobLevel) *response.JobLevelResponse {
return &response.JobLevelResponse{
ID: jobLevel.ID,
Name: jobLevel.Name,
Level: jobLevel.Level,
OrganizationStructures: func() *[]response.OrganizationStructureMinimalResponse {
var responseOrganizationStructures []response.OrganizationStructureMinimalResponse
for _, organizationStructure := range jobLevel.OrganizationStructures {
responseOrganizationStructures = append(responseOrganizationStructures, response.OrganizationStructureMinimalResponse{
ID: organizationStructure.ID,
Name: organizationStructure.Name,
Level: organizationStructure.Level,
OrganizationID: organizationStructure.OrganizationID,
Path: organizationStructure.Path,
})
}
return &responseOrganizationStructures
}(),
}
}
File renamed without changes.
7 changes: 4 additions & 3 deletions internal/http/response/job_level_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package response
import "github.com/google/uuid"

type JobLevelResponse struct {
ID uuid.UUID `json:"id"`
Name string `json:"name"`
Level string `json:"level"`
ID uuid.UUID `json:"id"`
Name string `json:"name"`
Level string `json:"level"`
OrganizationStructures *[]OrganizationStructureMinimalResponse `json:"organization_structures"`
}
1 change: 1 addition & 0 deletions internal/http/response/job_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ type JobResponse struct {
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"`
Children []JobResponse `json:"children"`
}
8 changes: 8 additions & 0 deletions internal/http/response/ogranization_structure_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,11 @@ type OrganizationStructureResponse struct {
Parent *OrganizationStructureResponse `json:"parent,omitempty"`
Children []OrganizationStructureResponse `json:"children,omitempty"`
}

type OrganizationStructureMinimalResponse struct {
ID uuid.UUID `json:"id"`
OrganizationID uuid.UUID `json:"organization_id"`
Name string `json:"name"`
Level int `json:"level"`
Path string `json:"path"`
}
4 changes: 2 additions & 2 deletions internal/repository/job_level_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (r *JobLevelRepository) FindAllPaginated(page int, pageSize int, search str
var jobLevels []entity.JobLevel
var total int64

query := r.DB
query := r.DB.Preload("OrganizationStructures")

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

func (r *JobLevelRepository) FindById(id uuid.UUID) (*entity.JobLevel, error) {
var jobLevel entity.JobLevel
err := r.DB.Where("id = ?", id).First(&jobLevel).Error
err := r.DB.Preload("OrganizationStructures").Where("id = ?", id).First(&jobLevel).Error
if err != nil {
return nil, err
}
Expand Down
15 changes: 0 additions & 15 deletions internal/repository/job_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package repository
import (
"app/go-sso/internal/config"
"app/go-sso/internal/entity"
"errors"

"github.com/google/uuid"
"github.com/sirupsen/logrus"
Expand All @@ -15,7 +14,6 @@ type IJobRepository interface {
FindById(id uuid.UUID) (*entity.Job, error)
GetJobsByOrganizationStructureIDs(organizationStructureIDs []uuid.UUID) (*[]entity.Job, error)
FindAllChildren(parentID uuid.UUID) ([]entity.Job, error)
FindParent(job entity.Job) (*entity.Job, error)
}

type JobRepository struct {
Expand Down Expand Up @@ -86,19 +84,6 @@ func (r *JobRepository) FindAllChildren(parentID uuid.UUID) ([]entity.Job, error
return children, nil
}

func (r *JobRepository) FindParent(job entity.Job) (*entity.Job, error) {
var parent entity.Job
err := r.DB.Where("id = ?", job.ParentID).First(&parent).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
} else {
return nil, errors.New("[JobRepository.FindParent] " + err.Error())
}
}
return &parent, nil
}

func JobRepositoryFactory(log *logrus.Logger) IJobRepository {
db := config.NewDatabase()
return NewJobRepository(log, db)
Expand Down
15 changes: 12 additions & 3 deletions internal/usecase/job/get_jobs_by_job_level_id_usecase.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package usecase

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

"github.com/google/uuid"
Expand All @@ -13,7 +14,7 @@ type IGetJobsByJobLevelIDUseCaseRequest struct {
}

type IGetJobsByJobLevelIDUseCaseResponse struct {
Jobs *[]entity.Job `json:"jobs"`
Jobs *[]response.JobResponse `json:"jobs"`
}

type IGetJobsByJobLevelIDUseCase interface {
Expand Down Expand Up @@ -56,8 +57,16 @@ func (uc *GetJobsByJobLevelIDUseCase) Execute(request *IGetJobsByJobLevelIDUseCa
return nil, err
}

for i, job := range *jobs {
children, err := uc.JobRepository.FindAllChildren(job.ID)
if err != nil {
return nil, err
}
(*jobs)[i].Children = children
}

return &IGetJobsByJobLevelIDUseCaseResponse{
Jobs: jobs,
Jobs: dto.ConvertToJobResponse(jobs),
}, nil
}

Expand Down
27 changes: 14 additions & 13 deletions internal/usecase/job_level/find_all_paginated_usecase.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package usecase

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

"github.com/sirupsen/logrus"
Expand All @@ -14,42 +15,42 @@ type IFindAllPaginatedUseCaseRequest struct {
}

type IFindAllPaginatedUseCaseResponse struct {
Jobs *[]entity.Job `json:"jobs"`
Total int64 `json:"total"`
JobLevels *[]response.JobLevelResponse `json:"job_levels"`
Total int64 `json:"total"`
}

type IFindAllPaginatedUseCase interface {
Execute(request *IFindAllPaginatedUseCaseRequest) (*IFindAllPaginatedUseCaseResponse, error)
}

type FindAllPaginatedUseCase struct {
Log *logrus.Logger
JobRepository repository.IJobRepository
Log *logrus.Logger
JobLevelRepository repository.IJobLevelRepository
}

func NewFindAllPaginatedUseCase(
log *logrus.Logger,
jobRepository repository.IJobRepository,
jobLevelRepository repository.IJobLevelRepository,
) IFindAllPaginatedUseCase {
return &FindAllPaginatedUseCase{
Log: log,
JobRepository: jobRepository,
Log: log,
JobLevelRepository: jobLevelRepository,
}
}

func (uc *FindAllPaginatedUseCase) Execute(req *IFindAllPaginatedUseCaseRequest) (*IFindAllPaginatedUseCaseResponse, error) {
jobs, total, err := uc.JobRepository.FindAllPaginated(req.Page, req.PageSize, req.Search)
jobs, total, err := uc.JobLevelRepository.FindAllPaginated(req.Page, req.PageSize, req.Search)
if err != nil {
return nil, err
}

return &IFindAllPaginatedUseCaseResponse{
Jobs: jobs,
Total: total,
JobLevels: dto.ConvertToJobLevelResponse(jobs),
Total: total,
}, nil
}

func FindAllPaginatedUseCaseFactory(log *logrus.Logger) IFindAllPaginatedUseCase {
jobRepository := repository.JobRepositoryFactory(log)
return NewFindAllPaginatedUseCase(log, jobRepository)
jobLevelRepository := repository.JobLevelRepositoryFactory(log)
return NewFindAllPaginatedUseCase(log, jobLevelRepository)
}
13 changes: 7 additions & 6 deletions internal/usecase/job_level/find_by_id_usecase.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package usecase

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

"github.com/google/uuid"
Expand All @@ -13,7 +14,7 @@ type IFindByIdUseCaseRequest struct {
}

type IFindByIdUseCaseResponse struct {
Job *entity.Job `json:"job"`
Job *response.JobLevelResponse `json:"job"`
}

type IFindByIdUseCase interface {
Expand All @@ -22,12 +23,12 @@ type IFindByIdUseCase interface {

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

func NewFindByIdUseCase(
log *logrus.Logger,
jobRepository repository.IJobRepository,
jobRepository repository.IJobLevelRepository,
) IFindByIdUseCase {
return &FindByIdUseCase{
Log: log,
Expand All @@ -42,11 +43,11 @@ func (uc *FindByIdUseCase) Execute(req *IFindByIdUseCaseRequest) (*IFindByIdUseC
}

return &IFindByIdUseCaseResponse{
Job: job,
Job: dto.ConvertToSingleJobLevelResponse(job),
}, nil
}

func FindByIdUseCaseFactory(log *logrus.Logger) IFindByIdUseCase {
jobRepository := repository.JobRepositoryFactory(log)
jobRepository := repository.JobLevelRepositoryFactory(log)
return NewFindByIdUseCase(log, jobRepository)
}

0 comments on commit c3bfae6

Please sign in to comment.