Skip to content

Commit

Permalink
Add FindOrganizationLocationByOrganizationId method and use case for …
Browse files Browse the repository at this point in the history
…retrieving locations by organization ID
  • Loading branch information
IlhamSetiaji committed Dec 9, 2024
1 parent 1a7697d commit 52913f9
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
Binary file modified go-sso.exe~
Binary file not shown.
28 changes: 28 additions & 0 deletions internal/http/handler/organization_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type IOrganizationHandler interface {
FindOrganizationStructureById(ctx *gin.Context)
FindOrganizationLocationsPaginated(ctx *gin.Context)
FindOrganizationLocationById(ctx *gin.Context)
FindOrganizationLocationByOrganizationId(ctx *gin.Context)
FindOrganizationTypesPaginated(ctx *gin.Context)
FindOrganizationTypeById(ctx *gin.Context)
}
Expand Down Expand Up @@ -251,6 +252,33 @@ func (h *OrganizationHandler) FindOrganizationLocationById(ctx *gin.Context) {
utils.SuccessResponse(ctx, http.StatusOK, "success", res.OrganizationLocation)
}

func (h *OrganizationHandler) FindOrganizationLocationByOrganizationId(ctx *gin.Context) {
middleware.PermissionApiMiddleware("read-organization-location")(ctx)
if denied, exists := ctx.Get("permission_denied"); exists && denied.(bool) {
h.log.Errorf("Permission denied")
return
}

organizationId := ctx.Param("organization_id")
if organizationId == "" {
utils.ErrorResponse(ctx, http.StatusBadRequest, "error", "organizationId is required")
return
}

factory := locationUsecase.FindByOrganizationIdUseCaseFactory(h.log)
res, err := factory.Execute(&locationUsecase.IFindByOrganizationIdUseCaseRequest{
OrganizationID: uuid.MustParse(organizationId),
})

if err != nil {
h.log.Errorf("Error: %v", err)
utils.ErrorResponse(ctx, http.StatusInternalServerError, "error", err.Error())
return
}

utils.SuccessResponse(ctx, http.StatusOK, "success", res.OrganizationLocations)
}

func (h *OrganizationHandler) FindOrganizationTypesPaginated(ctx *gin.Context) {
middleware.PermissionApiMiddleware("read-organization-type")(ctx)
if denied, exists := ctx.Get("permission_denied"); exists && denied.(bool) {
Expand Down
1 change: 1 addition & 0 deletions internal/http/route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func (c *RouteConfig) SetupApiRoutes() {

// Organization location routes
apiRoute.GET("/organization-locations", c.OrganizationHandler.FindOrganizationLocationsPaginated)
apiRoute.GET("/organization-locations/organization/:organization_id", c.OrganizationHandler.FindOrganizationLocationByOrganizationId)
apiRoute.GET("/organization-locations/:id", c.OrganizationHandler.FindOrganizationLocationById)

// Organization type routes
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package usecase

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 IFindByOrganizationIdUseCaseRequest struct {
OrganizationID uuid.UUID `json:"organization_id"`
}

type IFindByOrganizationIdUseCaseResponse struct {
OrganizationLocations *[]response.OrganizationLocationResponse `json:"organization_locations"`
}

type IFindByOrganizationIdUseCase interface {
Execute(request *IFindByOrganizationIdUseCaseRequest) (*IFindByOrganizationIdUseCaseResponse, error)
}

type FindByOrganizationIdUseCase struct {
Log *logrus.Logger
Repository repository.IOrganizationLocationRepository
}

func NewFindByOrganizationIdUseCase(
log *logrus.Logger,
repository repository.IOrganizationLocationRepository,
) IFindByOrganizationIdUseCase {
return &FindByOrganizationIdUseCase{
Log: log,
Repository: repository,
}
}

func (uc *FindByOrganizationIdUseCase) Execute(req *IFindByOrganizationIdUseCaseRequest) (*IFindByOrganizationIdUseCaseResponse, error) {
organizationLocations, err := uc.Repository.FindByOrganizationID(req.OrganizationID)
if err != nil {
return nil, err
}

return &IFindByOrganizationIdUseCaseResponse{
OrganizationLocations: dto.ConvertToOrganizationLocationResponse(organizationLocations),
}, nil
}

func FindByOrganizationIdUseCaseFactory(log *logrus.Logger) IFindByOrganizationIdUseCase {
repository := repository.OrganizationLocationRepositoryFactory(log)
return NewFindByOrganizationIdUseCase(log, repository)
}

0 comments on commit 52913f9

Please sign in to comment.