specialist, subscpecialist, specialist-position, subspecialist-position ids into codes

This commit is contained in:
dpurbosakti
2025-11-06 14:23:51 +07:00
parent 7423621a30
commit 4aabf4ad52
26 changed files with 201 additions and 143 deletions
@@ -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;
+2 -1
View File
@@ -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=
@@ -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"`
@@ -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
+13 -12
View File
@@ -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,
}
@@ -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"`
@@ -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 {
@@ -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
@@ -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 {
@@ -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
@@ -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)
@@ -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)
}
@@ -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)
}
@@ -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)
}
@@ -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)
}
@@ -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
}
@@ -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
@@ -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)
@@ -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
@@ -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
}
@@ -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
}
@@ -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
@@ -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{
@@ -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
@@ -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 {
@@ -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,
})
}