Skip to content

Commit

Permalink
Add functionality to update employee jobs with associated use case an…
Browse files Browse the repository at this point in the history
…d routes
  • Loading branch information
IlhamSetiaji committed Dec 27, 2024
1 parent 7136ccd commit 324491e
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 0 deletions.
Binary file modified go-sso.exe~
Binary file not shown.
43 changes: 43 additions & 0 deletions internal/http/handler/web/employee_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type EmployeeHandlerInterface interface {
Delete(ctx *gin.Context)
EmployeeJobs(ctx *gin.Context)
StoreEmployeeJob(ctx *gin.Context)
UpdateEmployeeJob(ctx *gin.Context)
}

func EmployeeHandlerFactory(log *logrus.Logger, validator *validator.Validate) EmployeeHandlerInterface {
Expand Down Expand Up @@ -333,3 +334,45 @@ func (h *EmployeeHandler) StoreEmployeeJob(ctx *gin.Context) {
session.Save()
ctx.Redirect(302, ctx.Request.Referer())
}

func (h *EmployeeHandler) UpdateEmployeeJob(ctx *gin.Context) {
middleware.PermissionMiddleware("update-employee-job")(ctx)
if ctx.IsAborted() {
ctx.Abort()
return
}

session := sessions.Default(ctx)

var request usecase.IUpdateEmployeeJobUsecaseRequest
if err := ctx.ShouldBind(&request); err != nil {
session.Set("error", err.Error())
session.Save()
h.Log.Error(err)
ctx.Redirect(302, ctx.Request.Referer())
return
}

if err := h.Validate.Struct(request); err != nil {
session.Set("error", err.Error())
session.Save()
h.Log.Error(err)
ctx.Redirect(302, ctx.Request.Referer())
return
}

factory := usecase.UpdateEmployeeJobUsecaseFactory(h.Log)

_, err := factory.Execute(&request)
if err != nil {
session.Set("error", err.Error())
session.Save()
h.Log.Error(err)
ctx.Redirect(302, ctx.Request.Referer())
return
}

session.Set("success", "Employee Job updated")
session.Save()
ctx.Redirect(302, ctx.Request.Referer())
}
1 change: 1 addition & 0 deletions internal/http/route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ func (c *RouteConfig) SetupWebRoutes() {
employeeRoutes.POST("/delete", c.EmployeeWebHandler.Delete)
employeeRoutes.GET("/:id/job", c.EmployeeWebHandler.EmployeeJobs)
employeeRoutes.POST("/store-job", c.EmployeeWebHandler.StoreEmployeeJob)
employeeRoutes.POST("/update-job", c.EmployeeWebHandler.UpdateEmployeeJob)
}
}
}
Expand Down
110 changes: 110 additions & 0 deletions internal/usecase/employee/update_employee_job_usecase.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package usecase

import (
"app/go-sso/internal/entity"
"app/go-sso/internal/repository"
"errors"

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

type IUpdateEmployeeJobUsecaseRequest struct {
ID string `form:"id" validate:"required"`
EmployeeID string `form:"employee_id" validate:"required"`
Name string `form:"name" validate:"omitempty"`
JobID string `form:"job_id" validate:"required"`
EmpOrganizationID string `form:"emp_organization_id" validate:"required"`
OrganizationLocationID string `form:"organization_location_id" validate:"required"`
}

type IUpdateEmployeeJobUsecaseResponse struct {
EmployeeJobID string `json:"employee_job_id"`
}

type IUpdateEmployeeJobUsecase interface {
Execute(request *IUpdateEmployeeJobUsecaseRequest) (*IUpdateEmployeeJobUsecaseResponse, error)
}

type UpdateEmployeeJobUsecase struct {
Log *logrus.Logger
EmployeeRepo repository.IEmployeeRepository
JobRepo repository.IJobRepository
OrgRepo repository.IOrganizationRepository
OrgLocationRepo repository.IOrganizationLocationRepository
}

func UpdateEmployeeJobUsecaseFactory(log *logrus.Logger) IUpdateEmployeeJobUsecase {
employeeRepo := repository.EmployeeRepositoryFactory(log)
jobRepo := repository.JobRepositoryFactory(log)
orgRepo := repository.OrganizationRepositoryFactory(log)
orgLocationRepo := repository.OrganizationLocationRepositoryFactory(log)
return &UpdateEmployeeJobUsecase{
Log: log,
EmployeeRepo: employeeRepo,
JobRepo: jobRepo,
OrgRepo: orgRepo,
OrgLocationRepo: orgLocationRepo,
}
}

func (u *UpdateEmployeeJobUsecase) Execute(request *IUpdateEmployeeJobUsecaseRequest) (*IUpdateEmployeeJobUsecaseResponse, error) {
employee, err := u.EmployeeRepo.FindById(uuid.MustParse(request.EmployeeID))
if err != nil {
u.Log.Error(err)
return nil, err
}

if employee == nil {
return nil, errors.New("Employee not found")
}

job, err := u.JobRepo.FindById(uuid.MustParse(request.JobID))
if err != nil {
u.Log.Error(err)
return nil, err
}

if job == nil {
return nil, errors.New("Job not found")
}

org, err := u.OrgRepo.FindById(uuid.MustParse(request.EmpOrganizationID))
if err != nil {
u.Log.Error(err)
return nil, err
}

if org == nil {
return nil, errors.New("Organization not found")
}

orgLocation, err := u.OrgLocationRepo.FindById(uuid.MustParse(request.OrganizationLocationID))
if err != nil {
u.Log.Error(err)
return nil, err
}

if orgLocation == nil {
return nil, errors.New("Organization Location not found")
}

employeeJob := &entity.EmployeeJob{
ID: uuid.MustParse(request.ID),
EmployeeID: &employee.ID,
Name: request.Name,
JobID: uuid.MustParse(request.JobID),
EmpOrganizationID: uuid.MustParse(request.EmpOrganizationID),
OrganizationLocationID: uuid.MustParse(request.OrganizationLocationID),
}

_, err = u.EmployeeRepo.UpdateEmployeeJob(employeeJob)
if err != nil {
u.Log.Error(err)
return nil, err
}

return &IUpdateEmployeeJobUsecaseResponse{
EmployeeJobID: employeeJob.ID.String(),
}, nil
}

0 comments on commit 324491e

Please sign in to comment.