Skip to content

Commit

Permalink
Refactor organization location handling: add DTOs for responses and i…
Browse files Browse the repository at this point in the history
…mplement FindByOrganizationID method
  • Loading branch information
IlhamSetiaji committed Dec 9, 2024
1 parent 24a26f1 commit 1a7697d
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 30 deletions.
Binary file modified go-sso.exe~
Binary file not shown.
30 changes: 30 additions & 0 deletions internal/http/dto/organization_location_dto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package dto

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

func ConvertToOrganizationLocationResponse(orgLocations *[]entity.OrganizationLocation) *[]response.OrganizationLocationResponse {
var responseLocations []response.OrganizationLocationResponse
for _, orgLocation := range *orgLocations {
responseLocations = append(responseLocations, response.OrganizationLocationResponse{
ID: orgLocation.ID,
OrganizationID: orgLocation.OrganizationID,
Name: orgLocation.Name,
CreatedAt: orgLocation.CreatedAt,
UpdatedAt: orgLocation.UpdatedAt,
})
}
return &responseLocations
}

func ConvertToSingleOrganizationLocationResponse(orgLocation *entity.OrganizationLocation) *response.OrganizationLocationResponse {
return &response.OrganizationLocationResponse{
ID: orgLocation.ID,
OrganizationID: orgLocation.OrganizationID,
Name: orgLocation.Name,
CreatedAt: orgLocation.CreatedAt,
UpdatedAt: orgLocation.UpdatedAt,
}
}
9 changes: 9 additions & 0 deletions internal/http/response/job_level_response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package response

import "github.com/google/uuid"

type JobLevelResponse struct {
ID uuid.UUID `json:"id"`
Name string `json:"name"`
Level string `json:"level"`
}
17 changes: 17 additions & 0 deletions internal/http/response/ogranization_structure_response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package response

import "github.com/google/uuid"

type OrganizationStructureResponse struct {
ID uuid.UUID `json:"id"`
OrganizationID uuid.UUID `json:"organization_id"`
Name string `json:"name"`
JobLevelID uuid.UUID `json:"job_level_id"`
ParentID *uuid.UUID `json:"parent_id,omitempty"`
Level int `json:"level"`
Path string `json:"path"`
Organization OrganizationResponse `json:"organization"`
JobLevel JobLevelResponse `json:"job_level"`
Parent *OrganizationStructureResponse `json:"parent,omitempty"`
Children []OrganizationStructureResponse `json:"children,omitempty"`
}
15 changes: 15 additions & 0 deletions internal/http/response/organization_location.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package response

import (
"time"

"github.com/google/uuid"
)

type OrganizationLocationResponse struct {
ID uuid.UUID `json:"id"`
OrganizationID uuid.UUID `json:"organization_id"`
Name string `json:"name"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
27 changes: 1 addition & 26 deletions internal/http/response/organization_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,4 @@ type OrganizationResponse struct {
OrganizationTypeID uuid.UUID `json:"organization_type_id"`
Name string `json:"name"`
OrganizationType OrganizationTypeResponse
}

type OrganizationTypeResponse struct {
ID uuid.UUID `json:"id"`
Name string `json:"name"`
}

type JobLevelResponse struct {
ID uuid.UUID `json:"id"`
Name string `json:"name"`
Level string `json:"level"`
}

type OrganizationStructureResponse struct {
ID uuid.UUID `json:"id"`
OrganizationID uuid.UUID `json:"organization_id"`
Name string `json:"name"`
JobLevelID uuid.UUID `json:"job_level_id"`
ParentID *uuid.UUID `json:"parent_id,omitempty"`
Level int `json:"level"`
Path string `json:"path"`
Organization OrganizationResponse `json:"organization"`
JobLevel JobLevelResponse `json:"job_level"`
Parent *OrganizationStructureResponse `json:"parent,omitempty"`
Children []OrganizationStructureResponse `json:"children,omitempty"`
}
}
8 changes: 8 additions & 0 deletions internal/http/response/organization_type_response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package response

import "github.com/google/uuid"

type OrganizationTypeResponse struct {
ID uuid.UUID `json:"id"`
Name string `json:"name"`
}
10 changes: 10 additions & 0 deletions internal/repository/organization_location_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
type IOrganizationLocationRepository interface {
FindAllPaginated(page int, pageSize int, search string) (*[]entity.OrganizationLocation, int64, error)
FindById(id uuid.UUID) (*entity.OrganizationLocation, error)
FindByOrganizationID(organizationID uuid.UUID) (*[]entity.OrganizationLocation, error)
}

type OrganizationLocationRepository struct {
Expand Down Expand Up @@ -56,6 +57,15 @@ func (r *OrganizationLocationRepository) FindById(id uuid.UUID) (*entity.Organiz
return &organizationLocation, nil
}

func (r *OrganizationLocationRepository) FindByOrganizationID(organizationID uuid.UUID) (*[]entity.OrganizationLocation, error) {
var organizationLocations []entity.OrganizationLocation
err := r.DB.Where("organization_id = ?", organizationID).Find(&organizationLocations).Error
if err != nil {
return nil, err
}
return &organizationLocations, nil
}

func OrganizationLocationRepositoryFactory(log *logrus.Logger) IOrganizationLocationRepository {
db := config.NewDatabase()
return NewOrganizationLocationRepository(log, db)
Expand Down
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,8 +15,8 @@ type IFindAllPaginatedUseCaseRequest struct {
}

type IFindAllPaginatedUseCaseResponse struct {
OrganizationLocations *[]entity.OrganizationLocation `json:"organization_locations"`
Total int64 `json:"total"`
OrganizationLocations *[]response.OrganizationLocationResponse `json:"organization_locations"`
Total int64 `json:"total"`
}

type IFindAllPaginatedUseCase interface {
Expand Down Expand Up @@ -44,7 +45,7 @@ func (uc *FindAllPaginatedUseCase) Execute(req *IFindAllPaginatedUseCaseRequest)
}

return &IFindAllPaginatedUseCaseResponse{
OrganizationLocations: organizationLocations,
OrganizationLocations: dto.ConvertToOrganizationLocationResponse(organizationLocations),
Total: total,
}, nil
}
Expand Down

0 comments on commit 1a7697d

Please sign in to comment.