From 4aabf4ad5209a0e6304db6633e840fd5e174fd9a Mon Sep 17 00:00:00 2001 From: dpurbosakti Date: Thu, 6 Nov 2025 14:23:51 +0700 Subject: [PATCH] specialist, subscpecialist, specialist-position, subspecialist-position ids into codes --- .../migrations/20251106071906.sql | 8 +++ cmd/main-migration/migrations/atlas.sum | 3 +- .../specialist-position/base/entity.go | 1 + .../main-entities/specialist-position/dto.go | 57 ++++++++++--------- .../domain/main-entities/specialist/dto.go | 25 ++++---- .../domain/main-entities/specialist/entity.go | 1 + .../subspecialist-position/base/entity.go | 15 ++--- .../subspecialist-position/dto.go | 57 ++++++++++--------- .../subspecialist/base/entity.go | 1 + .../domain/main-entities/subspecialist/dto.go | 27 ++++----- .../interface/main-handler/main-handler.go | 8 +-- .../specialist-position/handler.go | 18 +++--- .../main-handler/specialist/handler.go | 18 +++--- .../subspecialist-position/handler.go | 18 +++--- .../main-handler/subspecialist/handler.go | 18 +++--- .../main-use-case/specialist-position/case.go | 2 +- .../specialist-position/helper.go | 2 +- .../main-use-case/specialist-position/lib.go | 2 +- .../use-case/main-use-case/specialist/case.go | 4 +- .../main-use-case/specialist/helper.go | 2 +- .../subspecialist-position/case.go | 6 +- .../subspecialist-position/helper.go | 2 +- .../subspecialist-position/lib.go | 4 +- .../main-use-case/subspecialist/case.go | 4 +- .../main-use-case/subspecialist/lib.go | 7 +++ .../handler-crud-helper.go | 34 +++++++++++ 26 files changed, 201 insertions(+), 143 deletions(-) create mode 100644 cmd/main-migration/migrations/20251106071906.sql diff --git a/cmd/main-migration/migrations/20251106071906.sql b/cmd/main-migration/migrations/20251106071906.sql new file mode 100644 index 00000000..66793656 --- /dev/null +++ b/cmd/main-migration/migrations/20251106071906.sql @@ -0,0 +1,8 @@ +-- Modify "Specialist" table +ALTER TABLE "public"."Specialist" ADD COLUMN "Unit_Code" character varying(10) NULL; +-- Modify "SpecialistPosition" table +ALTER TABLE "public"."SpecialistPosition" ADD COLUMN "Specialist_Code" character varying(10) NULL; +-- Modify "Subspecialist" table +ALTER TABLE "public"."Subspecialist" ADD COLUMN "Specialist_Code" character varying(10) NULL; +-- Modify "SubspecialistPosition" table +ALTER TABLE "public"."SubspecialistPosition" ADD COLUMN "Subspecialist_Code" character varying(10) NULL; diff --git a/cmd/main-migration/migrations/atlas.sum b/cmd/main-migration/migrations/atlas.sum index 70ff0699..fd8a8979 100644 --- a/cmd/main-migration/migrations/atlas.sum +++ b/cmd/main-migration/migrations/atlas.sum @@ -1,4 +1,4 @@ -h1:5MZVEgV5zVF7bZ9mXYoi2ck0e9iRd7Ecib3j5OFA4/g= +h1:CT5nygEUOdVE27HmnMPqwLegXYbJ41grmTvFn7AH8D8= 20250904105930.sql h1:MEM6blCgke9DzWQSTnLzasbPIrcHssNNrJqZpSkEo6k= 20250904141448.sql h1:J8cmYNk4ZrG9fhfbi2Z1IWz7YkfvhFqTzrLFo58BPY0= 20250908062237.sql h1:Pu23yEW/aKkwozHoOuROvHS/GK4ngARJGdO7FB7HZuI= @@ -77,3 +77,4 @@ h1:5MZVEgV5zVF7bZ9mXYoi2ck0e9iRd7Ecib3j5OFA4/g= 20251106042006.sql h1:ruppYa1kAJQUU3ufQBbKGMcXrGbGJJiRPclT+dNc/YQ= 20251106050412.sql h1:MiEMJ1HCFYnalKuq3Z38xJeogfBAMqsTv2sG4EF8dDw= 20251106063418.sql h1:y3veDJPjKekOWLCZek/LgQwXPRhZtOppTfUXiqoL95s= +20251106071906.sql h1:/TUZA3XpMY23qEJXdkTwlzrNMvSSl6JJniPcgAttBaw= diff --git a/internal/domain/main-entities/specialist-position/base/entity.go b/internal/domain/main-entities/specialist-position/base/entity.go index 5910689e..5db268cd 100644 --- a/internal/domain/main-entities/specialist-position/base/entity.go +++ b/internal/domain/main-entities/specialist-position/base/entity.go @@ -8,6 +8,7 @@ import ( type Basic struct { ecore.SmallMain // adjust this according to the needs Specialist_Id *uint16 `json:"specialist_id" gorm:"not null"` + Specialist_Code *string `json:"specialist_code" gorm:"size:10"` Code string `json:"code" gorm:"unique;size:10;not null"` Name string `json:"name" gorm:"size:30;not null"` HeadStatus bool `json:"headStatus"` diff --git a/internal/domain/main-entities/specialist-position/dto.go b/internal/domain/main-entities/specialist-position/dto.go index 0641fb12..beebab28 100644 --- a/internal/domain/main-entities/specialist-position/dto.go +++ b/internal/domain/main-entities/specialist-position/dto.go @@ -7,11 +7,11 @@ import ( ) type CreateDto struct { - Specialist_Id *uint16 `json:"specialist_id" validate:"required"` - Code string `json:"code" validate:"maxLength=10;required"` - Name string `json:"name" validate:"maxLength=30;required"` - HeadStatus bool `json:"headStatus"` - Employee_Id *uint `json:"employee_id"` + Specialist_Code *string `json:"specialist_code" validate:"required"` + Code string `json:"code" validate:"maxLength=10;required"` + Name string `json:"name" validate:"maxLength=30;required"` + HeadStatus bool `json:"headStatus"` + Employee_Id *uint `json:"employee_id"` } type ReadListDto struct { @@ -22,26 +22,27 @@ type ReadListDto struct { } type FilterDto struct { - Specialist_Id *uint16 `json:"specialist-id"` - Code string `json:"code"` - Name string `json:"name"` - HeadStatus *bool `json:"head-status"` - Employee_Id *uint `json:"employee-id"` - Search string `json:"search" gormhelper:"searchColumns=Code,Name"` + Specialist_Code *string `json:"specialist-code"` + Code string `json:"code"` + Name string `json:"name"` + HeadStatus *bool `json:"head-status"` + Employee_Id *uint `json:"employee-id"` + Search string `json:"search" gormhelper:"searchColumns=Code,Name"` } type ReadDetailDto struct { - Id uint16 `json:"id"` + Id *uint16 `json:"id"` Code *string `json:"code"` } type UpdateDto struct { - Id uint16 `json:"id"` + Id *uint16 `json:"id"` CreateDto } type DeleteDto struct { - Id uint16 `json:"id"` + Id *uint16 `json:"id"` + Code *string `json:"code"` } type MetaDto struct { @@ -52,24 +53,24 @@ type MetaDto struct { type ResponseDto struct { ecore.SmallMain - Specialist_Id *uint16 `json:"specialist_id"` - Specialist *es.Specialist `json:"specialist,omitempty"` - Code string `json:"code"` - Name string `json:"name"` - HeadStatus bool `json:"headStatus"` - Employee_Id *uint `json:"employee_id"` - Employee *ee.Employee `json:"employee,omitempty"` + Specialist_Code *string `json:"specialist_code"` + Specialist *es.Specialist `json:"specialist,omitempty"` + Code string `json:"code"` + Name string `json:"name"` + HeadStatus bool `json:"headStatus"` + Employee_Id *uint `json:"employee_id"` + Employee *ee.Employee `json:"employee,omitempty"` } func (d SpecialistPosition) ToResponse() ResponseDto { resp := ResponseDto{ - Specialist_Id: d.Specialist_Id, - Specialist: d.Specialist, - Code: d.Code, - Name: d.Name, - HeadStatus: d.HeadStatus, - Employee_Id: d.Employee_Id, - Employee: d.Employee, + Specialist_Code: d.Specialist_Code, + Specialist: d.Specialist, + Code: d.Code, + Name: d.Name, + HeadStatus: d.HeadStatus, + Employee_Id: d.Employee_Id, + Employee: d.Employee, } resp.SmallMain = d.SmallMain return resp diff --git a/internal/domain/main-entities/specialist/dto.go b/internal/domain/main-entities/specialist/dto.go index 8724b9c5..a67a830f 100644 --- a/internal/domain/main-entities/specialist/dto.go +++ b/internal/domain/main-entities/specialist/dto.go @@ -8,9 +8,9 @@ import ( ) type CreateDto struct { - Code string `json:"code" validate:"maxLength=10"` - Name string `json:"name" validate:"maxLength=50"` - Unit_Id *uint16 `json:"unit_id"` + Code string `json:"code" validate:"maxLength=10"` + Name string `json:"name" validate:"maxLength=50"` + Unit_Code *string `json:"unit_code"` } type ReadListDto struct { @@ -21,25 +21,26 @@ type ReadListDto struct { } type FilterDto struct { - Code string `json:"code"` - Name string `json:"name"` - Unit_Id *uint16 `json:"unit-id"` - Search string `json:"search" gormhelper:"searchColumns=Code,Name"` + Code string `json:"code"` + Name string `json:"name"` + Unit_Code *string `json:"unit-code"` + Search string `json:"search" gormhelper:"searchColumns=Code,Name"` } type ReadDetailDto struct { - Id uint16 `json:"id"` + Id *uint16 `json:"id"` Code *string `json:"code"` Includes string `json:"includes"` } type UpdateDto struct { - Id uint16 `json:"id"` + Id *uint16 `json:"id"` CreateDto } type DeleteDto struct { - Id uint16 `json:"id"` + Id *uint16 `json:"id"` + Code *string `json:"code"` } type MetaDto struct { @@ -52,7 +53,7 @@ type ResponseDto struct { ecore.SmallMain Code string `json:"code"` Name string `json:"name"` - Unit_Id *uint16 `json:"unit_id"` + Unit_Code *string `json:"unit_code"` Unit *eu.Unit `json:"unit,omitempty"` SpecialistPositions []espb.Basic `json:"specialistPositions,omitempty"` Subspecialists []essb.Basic `json:"subspecialists,omitempty"` @@ -63,7 +64,7 @@ func (d Specialist) ToResponse() ResponseDto { Code: d.Code, Name: d.Name, Unit: d.Unit, - Unit_Id: d.Unit_Id, + Unit_Code: d.Unit_Code, SpecialistPositions: d.SpecialistPositions, Subspecialists: d.Subspecialists, } diff --git a/internal/domain/main-entities/specialist/entity.go b/internal/domain/main-entities/specialist/entity.go index 9efb83b0..912d433f 100644 --- a/internal/domain/main-entities/specialist/entity.go +++ b/internal/domain/main-entities/specialist/entity.go @@ -12,6 +12,7 @@ type Specialist struct { Code string `json:"code" gorm:"unique;size:10"` Name string `json:"name" gorm:"size:50"` Unit_Id *uint16 `json:"unit_id"` + Unit_Code *string `json:"unit_code" gorm:"size:10"` Unit *eu.Unit `json:"unit,omitempty" gorm:"foreignKey:Unit_Id"` SpecialistPositions []eub.Basic `json:"specialistPositions,omitempty" gorm:"foreignKey:Specialist_Id;references:Id"` Subspecialists []essb.Basic `json:"subspecialists,omitempty" gorm:"foreignKey:Specialist_Id;references:Id"` diff --git a/internal/domain/main-entities/subspecialist-position/base/entity.go b/internal/domain/main-entities/subspecialist-position/base/entity.go index 147fcecb..a0bd0834 100644 --- a/internal/domain/main-entities/subspecialist-position/base/entity.go +++ b/internal/domain/main-entities/subspecialist-position/base/entity.go @@ -6,13 +6,14 @@ import ( ) type Basic struct { - ecore.SmallMain // adjust this according to the needs - Subspecialist_Id *uint16 `json:"subspecialist_id" gorm:"not null"` - Code string `json:"code" gorm:"unique;size:10;not null"` - Name string `json:"name" gorm:"size:30;not null"` - HeadStatus bool `json:"headStatus"` - Employee_Id *uint `json:"employee_id"` - Employee *ee.Employee `json:"employee,omitempty" gorm:"foreignKey:Employee_Id;references:Id"` + ecore.SmallMain // adjust this according to the needs + Subspecialist_Id *uint16 `json:"subspecialist_id" gorm:"not null"` + Subspecialist_Code *string `json:"subspecialist_code" gorm:"size:10"` + Code string `json:"code" gorm:"unique;size:10;not null"` + Name string `json:"name" gorm:"size:30;not null"` + HeadStatus bool `json:"headStatus"` + Employee_Id *uint `json:"employee_id"` + Employee *ee.Employee `json:"employee,omitempty" gorm:"foreignKey:Employee_Id;references:Id"` } func (Basic) TableName() string { diff --git a/internal/domain/main-entities/subspecialist-position/dto.go b/internal/domain/main-entities/subspecialist-position/dto.go index 3c65841e..13532ab8 100644 --- a/internal/domain/main-entities/subspecialist-position/dto.go +++ b/internal/domain/main-entities/subspecialist-position/dto.go @@ -7,11 +7,11 @@ import ( ) type CreateDto struct { - Subspecialist_Id *uint16 `json:"subspecialist_id" validate:"required"` - Code string `json:"code" validate:"maxLength=10;required"` - Name string `json:"name" validate:"maxLength=30;required"` - HeadStatus bool `json:"headStatus"` - Employee_Id *uint `json:"employee_id"` + Subspecialist_Code *string `json:"subspecialist_code" validate:"required"` + Code string `json:"code" validate:"maxLength=10;required"` + Name string `json:"name" validate:"maxLength=30;required"` + HeadStatus bool `json:"headStatus"` + Employee_Id *uint `json:"employee_id"` } type ReadListDto struct { @@ -22,26 +22,27 @@ type ReadListDto struct { } type FilterDto struct { - Subspecialist_Id *uint16 `json:"subspecialist-id"` - Code string `json:"code"` - Name string `json:"name"` - HeadStatus *bool `json:"head-status"` - Employee_Id *uint `json:"employee-id"` - Search string `json:"search" gormhelper:"searchColumns=Code,Name"` + Subspecialist_Code *string `json:"subspecialist-code"` + Code string `json:"code"` + Name string `json:"name"` + HeadStatus *bool `json:"head-status"` + Employee_Id *uint `json:"employee-id"` + Search string `json:"search" gormhelper:"searchColumns=Code,Name"` } type ReadDetailDto struct { - Id uint16 `json:"id"` + Id *uint16 `json:"id"` Code *string `json:"code"` } type UpdateDto struct { - Id uint16 `json:"id"` + Id *uint16 `json:"id"` CreateDto } type DeleteDto struct { - Id uint16 `json:"id"` + Id *uint16 `json:"id"` + Code *string `json:"code"` } type MetaDto struct { @@ -52,24 +53,24 @@ type MetaDto struct { type ResponseDto struct { ecore.SmallMain - Subspecialist_Id *uint16 `json:"subspecialist_id"` - Subspecialist *es.Subspecialist `json:"subspecialist,omitempty"` - Code string `json:"code"` - Name string `json:"name"` - HeadStatus bool `json:"headStatus"` - Employee_Id *uint `json:"employee_id"` - Employee *ee.Employee `json:"employee,omitempty"` + Subspecialist_Code *string `json:"subspecialist_id"` + Subspecialist *es.Subspecialist `json:"subspecialist,omitempty"` + Code string `json:"code"` + Name string `json:"name"` + HeadStatus bool `json:"headStatus"` + Employee_Id *uint `json:"employee_id"` + Employee *ee.Employee `json:"employee,omitempty"` } func (d SubspecialistPosition) ToResponse() ResponseDto { resp := ResponseDto{ - Subspecialist_Id: d.Subspecialist_Id, - Subspecialist: d.Subspecialist, - Code: d.Code, - Name: d.Name, - HeadStatus: d.HeadStatus, - Employee_Id: d.Employee_Id, - Employee: d.Employee, + Subspecialist_Code: d.Subspecialist_Code, + Subspecialist: d.Subspecialist, + Code: d.Code, + Name: d.Name, + HeadStatus: d.HeadStatus, + Employee_Id: d.Employee_Id, + Employee: d.Employee, } resp.SmallMain = d.SmallMain return resp diff --git a/internal/domain/main-entities/subspecialist/base/entity.go b/internal/domain/main-entities/subspecialist/base/entity.go index bc516360..c89cb8f2 100644 --- a/internal/domain/main-entities/subspecialist/base/entity.go +++ b/internal/domain/main-entities/subspecialist/base/entity.go @@ -9,6 +9,7 @@ type Basic struct { Code string `json:"code" gorm:"unique;size:10"` Name string `json:"name" gorm:"size:50"` Specialist_Id *uint16 `json:"specialist_id"` + Specialist_Code *string `json:"specialist_code" gorm:"size:10"` } func (Basic) TableName() string { diff --git a/internal/domain/main-entities/subspecialist/dto.go b/internal/domain/main-entities/subspecialist/dto.go index 9a93394d..5ec895d6 100644 --- a/internal/domain/main-entities/subspecialist/dto.go +++ b/internal/domain/main-entities/subspecialist/dto.go @@ -27,18 +27,19 @@ type FilterDto struct { } type ReadDetailDto struct { - Id uint16 `json:"id"` + Id *uint16 `json:"id"` Code *string `json:"code"` Includes string `json:"includes"` } type UpdateDto struct { - Id uint16 `json:"id"` + Id *uint16 `json:"id"` CreateDto } type DeleteDto struct { - Id uint16 `json:"id"` + Id *uint16 `json:"id"` + Code *string `json:"code"` } type MetaDto struct { @@ -49,20 +50,20 @@ type MetaDto struct { type ResponseDto struct { ecore.SmallMain - Code string `json:"code"` - Name string `json:"name"` - Specialist_Id *uint16 `json:"specialist_id"` - Specialist *es.Specialist `json:"specialist,omitempty"` - Subspecialist []espb.Basic `json:"subspecialistPositions,omitempty"` + Code string `json:"code"` + Name string `json:"name"` + Specialist_Code *string `json:"specialist_ccode"` + Specialist *es.Specialist `json:"specialist,omitempty"` + Subspecialist []espb.Basic `json:"subspecialistPositions,omitempty"` } func (d Subspecialist) ToResponse() ResponseDto { resp := ResponseDto{ - Code: d.Code, - Name: d.Name, - Specialist_Id: d.Specialist_Id, - Specialist: d.Specialist, - Subspecialist: d.SubspecialistPositions, + Code: d.Code, + Name: d.Name, + Specialist_Code: d.Specialist_Code, + Specialist: d.Specialist, + Subspecialist: d.SubspecialistPositions, } resp.SmallMain = d.SmallMain return resp diff --git a/internal/interface/main-handler/main-handler.go b/internal/interface/main-handler/main-handler.go index 0a6a917a..abed5f2d 100644 --- a/internal/interface/main-handler/main-handler.go +++ b/internal/interface/main-handler/main-handler.go @@ -314,16 +314,16 @@ func SetRoutes() http.Handler { hc.RegCrud(r, "/v1/medical-action-src", medicalactionsrc.O) hc.RegCrud(r, "/v1/medical-action-src-item", medicalactionsrcitem.O) hc.RegCrud(r, "/v1/language", language.O) - hc.RegCrud(r, "/v1/specialist", specialist.O) - hc.RegCrud(r, "/v1/subspecialist", subspecialist.O) hc.RegCrud(r, "/v1/mcu-sub-src", mcusubsrc.O) hc.RegCrud(r, "/v1/vehicle", vehicle.O) hc.RegCrud(r, "/v1/vehicle-hist", vehiclehist.O) hc.RegCrud(r, "/v1/edu-assessment", eduassesment.O) hc.RegCrud(r, "/v1/installation-position", installationposition.O) hc.RegCrud(r, "/v1/unit-position", unitposition.O) - hc.RegCrud(r, "/v1/specialist-position", specialistposition.O) - hc.RegCrud(r, "/v1/subspecialist-position", subspecialistposition.O) + hc.RegCrudByCode(r, "/v1/specialist", specialist.O) + hc.RegCrudByCode(r, "/v1/subspecialist", subspecialist.O) + hc.RegCrudByCode(r, "/v1/specialist-position", specialistposition.O) + hc.RegCrudByCode(r, "/v1/subspecialist-position", subspecialistposition.O) hc.RegCrud(r, "/v1/village", village.O) hc.RegCrud(r, "/v1/district", district.O) diff --git a/internal/interface/main-handler/specialist-position/handler.go b/internal/interface/main-handler/specialist-position/handler.go index d16c2c7e..4d6376a1 100644 --- a/internal/interface/main-handler/specialist-position/handler.go +++ b/internal/interface/main-handler/specialist-position/handler.go @@ -33,19 +33,19 @@ func (obj myBase) GetList(w http.ResponseWriter, r *http.Request) { } func (obj myBase) GetDetail(w http.ResponseWriter, r *http.Request) { - id := rw.ValidateInt(w, "id", r.PathValue("id")) - if id <= 0 { + code := rw.ValidateString(w, "code", r.PathValue("code")) + if code == "" { return } dto := e.ReadDetailDto{} - dto.Id = uint16(id) + dto.Code = &code res, err := u.ReadDetail(dto) rw.DataResponse(w, res, err) } func (obj myBase) Update(w http.ResponseWriter, r *http.Request) { - id := rw.ValidateInt(w, "id", r.PathValue("id")) - if id <= 0 { + code := rw.ValidateString(w, "code", r.PathValue("code")) + if code == "" { return } @@ -53,19 +53,19 @@ func (obj myBase) Update(w http.ResponseWriter, r *http.Request) { if res := rw.ValidateStructByIOR(w, r.Body, &dto); !res { return } - dto.Id = uint16(id) + dto.Code = code res, err := u.Update(dto) rw.DataResponse(w, res, err) } func (obj myBase) Delete(w http.ResponseWriter, r *http.Request) { - id := rw.ValidateInt(w, "id", r.PathValue("id")) - if id <= 0 { + code := rw.ValidateString(w, "code", r.PathValue("code")) + if code == "" { return } dto := e.DeleteDto{} - dto.Id = uint16(id) + dto.Code = &code res, err := u.Delete(dto) rw.DataResponse(w, res, err) } diff --git a/internal/interface/main-handler/specialist/handler.go b/internal/interface/main-handler/specialist/handler.go index d3a074d8..246da8f4 100644 --- a/internal/interface/main-handler/specialist/handler.go +++ b/internal/interface/main-handler/specialist/handler.go @@ -33,21 +33,21 @@ func (obj myBase) GetList(w http.ResponseWriter, r *http.Request) { } func (obj myBase) GetDetail(w http.ResponseWriter, r *http.Request) { - id := rw.ValidateInt(w, "id", r.PathValue("id")) - if id <= 0 { + code := rw.ValidateString(w, "code", r.PathValue("code")) + if code == "" { return } dto := e.ReadDetailDto{} sf.UrlQueryParam(&dto, *r.URL) - dto.Id = uint16(id) + dto.Code = &code res, err := u.ReadDetail(dto) rw.DataResponse(w, res, err) } func (obj myBase) Update(w http.ResponseWriter, r *http.Request) { - id := rw.ValidateInt(w, "id", r.PathValue("id")) - if id <= 0 { + code := rw.ValidateString(w, "code", r.PathValue("code")) + if code == "" { return } @@ -55,19 +55,19 @@ func (obj myBase) Update(w http.ResponseWriter, r *http.Request) { if res := rw.ValidateStructByIOR(w, r.Body, &dto); !res { return } - dto.Id = uint16(id) + dto.Code = code res, err := u.Update(dto) rw.DataResponse(w, res, err) } func (obj myBase) Delete(w http.ResponseWriter, r *http.Request) { - id := rw.ValidateInt(w, "id", r.PathValue("id")) - if id <= 0 { + code := rw.ValidateString(w, "", r.PathValue("code")) + if code == "" { return } dto := e.DeleteDto{} - dto.Id = uint16(id) + dto.Code = &code res, err := u.Delete(dto) rw.DataResponse(w, res, err) } diff --git a/internal/interface/main-handler/subspecialist-position/handler.go b/internal/interface/main-handler/subspecialist-position/handler.go index d8e27b14..e33c62b8 100644 --- a/internal/interface/main-handler/subspecialist-position/handler.go +++ b/internal/interface/main-handler/subspecialist-position/handler.go @@ -33,19 +33,19 @@ func (obj myBase) GetList(w http.ResponseWriter, r *http.Request) { } func (obj myBase) GetDetail(w http.ResponseWriter, r *http.Request) { - id := rw.ValidateInt(w, "id", r.PathValue("id")) - if id <= 0 { + code := rw.ValidateString(w, "code", r.PathValue("code")) + if code == "" { return } dto := e.ReadDetailDto{} - dto.Id = uint16(id) + dto.Code = &code res, err := u.ReadDetail(dto) rw.DataResponse(w, res, err) } func (obj myBase) Update(w http.ResponseWriter, r *http.Request) { - id := rw.ValidateInt(w, "id", r.PathValue("id")) - if id <= 0 { + code := rw.ValidateString(w, "code", r.PathValue("code")) + if code == "" { return } @@ -53,19 +53,19 @@ func (obj myBase) Update(w http.ResponseWriter, r *http.Request) { if res := rw.ValidateStructByIOR(w, r.Body, &dto); !res { return } - dto.Id = uint16(id) + dto.Code = code res, err := u.Update(dto) rw.DataResponse(w, res, err) } func (obj myBase) Delete(w http.ResponseWriter, r *http.Request) { - id := rw.ValidateInt(w, "id", r.PathValue("id")) - if id <= 0 { + code := rw.ValidateString(w, "code", r.PathValue("code")) + if code == "" { return } dto := e.DeleteDto{} - dto.Id = uint16(id) + dto.Code = &code res, err := u.Delete(dto) rw.DataResponse(w, res, err) } diff --git a/internal/interface/main-handler/subspecialist/handler.go b/internal/interface/main-handler/subspecialist/handler.go index c4b933b4..0e5a41d0 100644 --- a/internal/interface/main-handler/subspecialist/handler.go +++ b/internal/interface/main-handler/subspecialist/handler.go @@ -33,21 +33,21 @@ func (obj myBase) GetList(w http.ResponseWriter, r *http.Request) { } func (obj myBase) GetDetail(w http.ResponseWriter, r *http.Request) { - id := rw.ValidateInt(w, "id", r.PathValue("id")) - if id <= 0 { + code := rw.ValidateString(w, "code", r.PathValue("code")) + if code == "" { return } dto := e.ReadDetailDto{} sf.UrlQueryParam(&dto, *r.URL) - dto.Id = uint16(id) + dto.Code = &code res, err := u.ReadDetail(dto) rw.DataResponse(w, res, err) } func (obj myBase) Update(w http.ResponseWriter, r *http.Request) { - id := rw.ValidateInt(w, "id", r.PathValue("id")) - if id <= 0 { + code := rw.ValidateString(w, "code", r.PathValue("code")) + if code == "" { return } @@ -55,19 +55,19 @@ func (obj myBase) Update(w http.ResponseWriter, r *http.Request) { if res := rw.ValidateStructByIOR(w, r.Body, &dto); !res { return } - dto.Id = uint16(id) + dto.Code = code res, err := u.Update(dto) rw.DataResponse(w, res, err) } func (obj myBase) Delete(w http.ResponseWriter, r *http.Request) { - id := rw.ValidateInt(w, "id", r.PathValue("id")) - if id <= 0 { + code := rw.ValidateString(w, "code", r.PathValue("code")) + if code == "" { return } dto := e.DeleteDto{} - dto.Id = uint16(id) + dto.Code = &code res, err := u.Delete(dto) rw.DataResponse(w, res, err) } diff --git a/internal/use-case/main-use-case/specialist-position/case.go b/internal/use-case/main-use-case/specialist-position/case.go index c3c469e6..2addf80d 100644 --- a/internal/use-case/main-use-case/specialist-position/case.go +++ b/internal/use-case/main-use-case/specialist-position/case.go @@ -290,7 +290,7 @@ func Delete(input e.DeleteDto) (*d.Data, error) { func validateForeignKey(input e.CreateDto) error { // validate installation_id - if _, err := us.ReadDetail(es.ReadDetailDto{Id: *input.Specialist_Id}); err != nil { + if _, err := us.ReadDetail(es.ReadDetailDto{Code: input.Specialist_Code}); err != nil { return err } diff --git a/internal/use-case/main-use-case/specialist-position/helper.go b/internal/use-case/main-use-case/specialist-position/helper.go index 6c3d6434..43aaeb20 100644 --- a/internal/use-case/main-use-case/specialist-position/helper.go +++ b/internal/use-case/main-use-case/specialist-position/helper.go @@ -17,7 +17,7 @@ func setData[T *e.CreateDto | *e.UpdateDto](input T, data *e.SpecialistPosition) inputSrc = &inputTemp.CreateDto } - data.Specialist_Id = inputSrc.Specialist_Id + data.Specialist_Code = inputSrc.Specialist_Code data.Code = inputSrc.Code data.Name = inputSrc.Name data.HeadStatus = inputSrc.HeadStatus diff --git a/internal/use-case/main-use-case/specialist-position/lib.go b/internal/use-case/main-use-case/specialist-position/lib.go index 3ae54f50..13c84f28 100644 --- a/internal/use-case/main-use-case/specialist-position/lib.go +++ b/internal/use-case/main-use-case/specialist-position/lib.go @@ -83,7 +83,7 @@ func ReadDetailData(input e.ReadDetailDto, event *pl.Event, dbx ...*gorm.DB) (*e } switch { - case input.Id != 0: + case input.Id != nil: getData = tx.First(&data, input.Id) case input.Code != nil && *input.Code != "": getData = tx.Where("code = ?", *input.Code).First(&data) diff --git a/internal/use-case/main-use-case/specialist/case.go b/internal/use-case/main-use-case/specialist/case.go index 229544be..c51a0002 100644 --- a/internal/use-case/main-use-case/specialist/case.go +++ b/internal/use-case/main-use-case/specialist/case.go @@ -166,7 +166,7 @@ func ReadDetail(input e.ReadDetailDto) (*d.Data, error) { } func Update(input e.UpdateDto) (*d.Data, error) { - rdDto := e.ReadDetailDto{Id: uint16(input.Id)} + rdDto := e.ReadDetailDto{Code: &input.Code} var data *e.Specialist var err error @@ -222,7 +222,7 @@ func Update(input e.UpdateDto) (*d.Data, error) { } func Delete(input e.DeleteDto) (*d.Data, error) { - rdDto := e.ReadDetailDto{Id: uint16(input.Id)} + rdDto := e.ReadDetailDto{Code: input.Code} var data *e.Specialist var err error diff --git a/internal/use-case/main-use-case/specialist/helper.go b/internal/use-case/main-use-case/specialist/helper.go index 9c1e42fc..5c59b1cf 100644 --- a/internal/use-case/main-use-case/specialist/helper.go +++ b/internal/use-case/main-use-case/specialist/helper.go @@ -19,5 +19,5 @@ func setData[T *e.CreateDto | *e.UpdateDto](input T, data *e.Specialist) { data.Code = inputSrc.Code data.Name = inputSrc.Name - data.Unit_Id = inputSrc.Unit_Id + data.Unit_Code = inputSrc.Unit_Code } diff --git a/internal/use-case/main-use-case/subspecialist-position/case.go b/internal/use-case/main-use-case/subspecialist-position/case.go index d954427a..0069a753 100644 --- a/internal/use-case/main-use-case/subspecialist-position/case.go +++ b/internal/use-case/main-use-case/subspecialist-position/case.go @@ -175,7 +175,7 @@ func ReadDetail(input e.ReadDetailDto) (*d.Data, error) { } func Update(input e.UpdateDto) (*d.Data, error) { - rdDto := e.ReadDetailDto{Id: input.Id} + rdDto := e.ReadDetailDto{Code: &input.Code} var data *e.SubspecialistPosition var err error @@ -235,7 +235,7 @@ func Update(input e.UpdateDto) (*d.Data, error) { } func Delete(input e.DeleteDto) (*d.Data, error) { - rdDto := e.ReadDetailDto{Id: input.Id} + rdDto := e.ReadDetailDto{Code: input.Code} var data *e.SubspecialistPosition var err error @@ -290,7 +290,7 @@ func Delete(input e.DeleteDto) (*d.Data, error) { func validateForeignKey(input e.CreateDto) error { // validate installation_id - if _, err := us.ReadDetail(es.ReadDetailDto{Id: *input.Subspecialist_Id}); err != nil { + if _, err := us.ReadDetail(es.ReadDetailDto{Code: input.Subspecialist_Code}); err != nil { return err } diff --git a/internal/use-case/main-use-case/subspecialist-position/helper.go b/internal/use-case/main-use-case/subspecialist-position/helper.go index 1f4c9b2b..16ff54e0 100644 --- a/internal/use-case/main-use-case/subspecialist-position/helper.go +++ b/internal/use-case/main-use-case/subspecialist-position/helper.go @@ -17,7 +17,7 @@ func setData[T *e.CreateDto | *e.UpdateDto](input T, data *e.SubspecialistPositi inputSrc = &inputTemp.CreateDto } - data.Subspecialist_Id = inputSrc.Subspecialist_Id + data.Subspecialist_Code = inputSrc.Subspecialist_Code data.Code = inputSrc.Code data.Name = inputSrc.Name data.HeadStatus = inputSrc.HeadStatus diff --git a/internal/use-case/main-use-case/subspecialist-position/lib.go b/internal/use-case/main-use-case/subspecialist-position/lib.go index 136e982e..edb422dd 100644 --- a/internal/use-case/main-use-case/subspecialist-position/lib.go +++ b/internal/use-case/main-use-case/subspecialist-position/lib.go @@ -83,10 +83,10 @@ func ReadDetailData(input e.ReadDetailDto, event *pl.Event, dbx ...*gorm.DB) (*e } switch { - case input.Id != 0: + case input.Id != nil: getData = tx.First(&data, input.Id) case input.Code != nil && *input.Code != "": - getData = tx.Where("code = ?", *input.Code).First(&data) + getData = tx.Where("\"Code\" = ?", *input.Code).First(&data) default: event.Status = "failed" event.ErrInfo = pl.ErrorInfo{ diff --git a/internal/use-case/main-use-case/subspecialist/case.go b/internal/use-case/main-use-case/subspecialist/case.go index 50498b2a..42366adf 100644 --- a/internal/use-case/main-use-case/subspecialist/case.go +++ b/internal/use-case/main-use-case/subspecialist/case.go @@ -166,7 +166,7 @@ func ReadDetail(input e.ReadDetailDto) (*d.Data, error) { } func Update(input e.UpdateDto) (*d.Data, error) { - rdDto := e.ReadDetailDto{Id: uint16(input.Id)} + rdDto := e.ReadDetailDto{Code: &input.Code} var data *e.Subspecialist var err error @@ -222,7 +222,7 @@ func Update(input e.UpdateDto) (*d.Data, error) { } func Delete(input e.DeleteDto) (*d.Data, error) { - rdDto := e.ReadDetailDto{Id: uint16(input.Id)} + rdDto := e.ReadDetailDto{Code: input.Code} var data *e.Subspecialist var err error diff --git a/internal/use-case/main-use-case/subspecialist/lib.go b/internal/use-case/main-use-case/subspecialist/lib.go index 46ec11f2..a6c4dbc5 100644 --- a/internal/use-case/main-use-case/subspecialist/lib.go +++ b/internal/use-case/main-use-case/subspecialist/lib.go @@ -81,6 +81,13 @@ func ReadDetailData(input e.ReadDetailDto, event *pl.Event, dbx ...*gorm.DB) (*e tx = dg.I } + if input.Code != nil { + tx = tx.Where("\"Code\" = ?", *input.Code) + } + if input.Id != nil { + tx = tx.Where("\"Id\" = ?", input.Id) + } + if err := tx. Scopes(gh.Preload(input.Includes)). First(&data, input.Id).Error; err != nil { diff --git a/pkg/handler-crud-helper/handler-crud-helper.go b/pkg/handler-crud-helper/handler-crud-helper.go index d7da62f4..df475094 100644 --- a/pkg/handler-crud-helper/handler-crud-helper.go +++ b/pkg/handler-crud-helper/handler-crud-helper.go @@ -40,3 +40,37 @@ func RegCrud(r *http.ServeMux, path string, mwAndRouter ...any) { "DELETE /{id}": c.Delete, }) } + +func RegCrudByCode(r *http.ServeMux, path string, mwAndRouter ...any) { + sLength := len(mwAndRouter) + + mwCandidates := mwAndRouter[:sLength-1] + mwList := []hk.HandlerMw{} + for i := range mwCandidates { + // have to do it manually, since casting directly results unexpected result + myType := reflect.TypeOf(mwCandidates[i]) + if myType.String() != "func(http.Handler) http.Handler" { + panic("non middleware included as middleware") + } + mwList = append(mwList, mwCandidates[i].(func(http.Handler) http.Handler)) + + // if g, okHandler := mwCandidates[i].(func(http.Handler) http.Handler); !okHandler { + // panic("non middleware included") + // } else { + // mwList = append(mwList, g) + // } + } + + c, ok := mwAndRouter[sLength-1].(CrudBase) + if !ok { + panic("non CrudBase used in the last paramter") + } + + hk.GroupRoutes(path, r, mwList, hk.MapHandlerFunc{ + "POST /": c.Create, + "GET /": c.GetList, + "GET /{code}": c.GetDetail, + "PATCH /{code}": c.Update, + "DELETE /{code}": c.Delete, + }) +}