division, division-position ids into codes

This commit is contained in:
dpurbosakti
2025-11-06 15:24:42 +07:00
parent 5ba60938df
commit b597b76092
16 changed files with 96 additions and 75 deletions

No files matched your search

@@ -0,0 +1,6 @@
-- Create index "idx_Division_Code" to table: "Division"
CREATE UNIQUE INDEX "idx_Division_Code" ON "public"."Division" ("Code");
-- Modify "Division" table
ALTER TABLE "public"."Division" DROP CONSTRAINT "uni_Division_Code", DROP CONSTRAINT "fk_Division_Childrens", DROP COLUMN "Parent_Id", ADD CONSTRAINT "fk_Division_Childrens" FOREIGN KEY ("Parent_Code") REFERENCES "public"."Division" ("Code") ON UPDATE NO ACTION ON DELETE NO ACTION;
-- Modify "DivisionPosition" table
ALTER TABLE "public"."DivisionPosition" DROP CONSTRAINT "fk_DivisionPosition_Division", DROP COLUMN "Division_Id", ADD CONSTRAINT "fk_DivisionPosition_Division" FOREIGN KEY ("Division_Code") REFERENCES "public"."Division" ("Code") ON UPDATE NO ACTION ON DELETE NO ACTION;
+2 -1
View File
@@ -1,4 +1,4 @@
h1:aVDoCaB6Gy2SKHrUbR2KDqH9ByMHPVcYzTGW5846kt8= h1:gZkp4dTWkJKTmsiLhz5DI6QMvJuBGfurq4lkNKPThjU=
20250904105930.sql h1:MEM6blCgke9DzWQSTnLzasbPIrcHssNNrJqZpSkEo6k= 20250904105930.sql h1:MEM6blCgke9DzWQSTnLzasbPIrcHssNNrJqZpSkEo6k=
20250904141448.sql h1:J8cmYNk4ZrG9fhfbi2Z1IWz7YkfvhFqTzrLFo58BPY0= 20250904141448.sql h1:J8cmYNk4ZrG9fhfbi2Z1IWz7YkfvhFqTzrLFo58BPY0=
20250908062237.sql h1:Pu23yEW/aKkwozHoOuROvHS/GK4ngARJGdO7FB7HZuI= 20250908062237.sql h1:Pu23yEW/aKkwozHoOuROvHS/GK4ngARJGdO7FB7HZuI=
@@ -80,3 +80,4 @@ h1:aVDoCaB6Gy2SKHrUbR2KDqH9ByMHPVcYzTGW5846kt8=
20251106071906.sql h1:/TUZA3XpMY23qEJXdkTwlzrNMvSSl6JJniPcgAttBaw= 20251106071906.sql h1:/TUZA3XpMY23qEJXdkTwlzrNMvSSl6JJniPcgAttBaw=
20251106073157.sql h1:78txeibJ602DMD7huD618ZSMt6phSRzDNPTlo0PGyrc= 20251106073157.sql h1:78txeibJ602DMD7huD618ZSMt6phSRzDNPTlo0PGyrc=
20251106074218.sql h1:8Xz7WywrtUnSxOHhlal53gG9rE7r86LFUt5zBFe/mIs= 20251106074218.sql h1:8Xz7WywrtUnSxOHhlal53gG9rE7r86LFUt5zBFe/mIs=
20251106081846.sql h1:jp91Bf5bxGXMiUB1VIuN6y768vb2iWwow44WfCE5J5k=
@@ -7,7 +7,6 @@ import (
type Basic struct { type Basic struct {
ecore.SmallMain // adjust this according to the needs ecore.SmallMain // adjust this according to the needs
Division_Id *uint16 `json:"division_id"`
Division_Code *string `json:"division_code" gorm:"size:10"` Division_Code *string `json:"division_code" gorm:"size:10"`
Code string `json:"code" gorm:"unique;size:10"` Code string `json:"code" gorm:"unique;size:10"`
Name string `json:"name" gorm:"size:50"` Name string `json:"name" gorm:"size:50"`
@@ -7,11 +7,11 @@ import (
) )
type CreateDto struct { type CreateDto struct {
Division_Id *uint16 `json:"division_id"` Division_Code *string `json:"division_code"`
Code string `json:"code" validate:"maxLength=10"` Code string `json:"code" validate:"maxLength=10"`
Name string `json:"name" validate:"maxLength=50"` Name string `json:"name" validate:"maxLength=50"`
HeadStatus bool `json:"headStatus"` HeadStatus bool `json:"headStatus"`
Employee_Id *uint `json:"employee_id"` Employee_Id *uint `json:"employee_id"`
} }
type ReadListDto struct { type ReadListDto struct {
@@ -22,26 +22,27 @@ type ReadListDto struct {
} }
type FilterDto struct { type FilterDto struct {
Division_Id *uint16 `json:"division-id"` Division_Code *string `json:"division-code"`
Code string `json:"code"` Code string `json:"code"`
Name string `json:"name"` Name string `json:"name"`
HeadStatus *bool `json:"head-status"` HeadStatus *bool `json:"head-status"`
Employee_Id *uint `json:"employee-id"` Employee_Id *uint `json:"employee-id"`
Search string `json:"search" gormhelper:"searchColumns=Code,Name"` Search string `json:"search" gormhelper:"searchColumns=Code,Name"`
} }
type ReadDetailDto struct { type ReadDetailDto struct {
Id uint16 `json:"id"` Id *uint16 `json:"id"`
Code *string `json:"code"` Code *string `json:"code"`
} }
type UpdateDto struct { type UpdateDto struct {
Id uint16 `json:"id"` Id *uint16 `json:"id"`
CreateDto CreateDto
} }
type DeleteDto struct { type DeleteDto struct {
Id uint16 `json:"id"` Id *uint16 `json:"id"`
Code *string `json:"code"`
} }
type MetaDto struct { type MetaDto struct {
@@ -52,23 +53,23 @@ type MetaDto struct {
type ResponseDto struct { type ResponseDto struct {
ecore.SmallMain ecore.SmallMain
Division_Id *uint16 `json:"division_id"` Division_Code *string `json:"division_code"`
Division *ed.Division `json:"division,omitempty"` Division *ed.Division `json:"division,omitempty"`
Code string `json:"code"` Code string `json:"code"`
Name string `json:"name"` Name string `json:"name"`
HeadStatus bool `json:"headStatus"` HeadStatus bool `json:"headStatus"`
Employee_Id *uint `json:"employee_id"` Employee_Id *uint `json:"employee_id"`
Employee *ee.Employee `json:"employee,omitempty"` Employee *ee.Employee `json:"employee,omitempty"`
} }
func (d DivisionPosition) ToResponse() ResponseDto { func (d DivisionPosition) ToResponse() ResponseDto {
resp := ResponseDto{ resp := ResponseDto{
Division_Id: d.Division_Id, Division_Code: d.Division_Code,
Code: d.Code, Code: d.Code,
Name: d.Name, Name: d.Name,
HeadStatus: d.HeadStatus, HeadStatus: d.HeadStatus,
Employee_Id: d.Employee_Id, Employee_Id: d.Employee_Id,
Employee: d.Employee, Employee: d.Employee,
} }
resp.SmallMain = d.SmallMain resp.SmallMain = d.SmallMain
if d.Division != nil { if d.Division != nil {
@@ -7,5 +7,5 @@ import (
type DivisionPosition struct { type DivisionPosition struct {
eb.Basic eb.Basic
Division *ed.Division `json:"division" gorm:"foreignKey:Division_Id;references:Id"` Division *ed.Division `json:"division" gorm:"foreignKey:Division_Code;references:Code"`
} }
+13 -12
View File
@@ -6,9 +6,9 @@ import (
) )
type CreateDto struct { type CreateDto struct {
Code string `json:"code" validate:"maxLength=10"` Code string `json:"code" validate:"maxLength=10"`
Name string `json:"name" validate:"maxLength=50"` Name string `json:"name" validate:"maxLength=50"`
Parent_Id *uint16 `json:"parent_id"` Parent_Code *string `json:"parent_code"`
} }
type ReadListDto struct { type ReadListDto struct {
@@ -20,25 +20,26 @@ type ReadListDto struct {
} }
type FilterDto struct { type FilterDto struct {
Code string `json:"code"` Code string `json:"code"`
Name string `json:"name"` Name string `json:"name"`
Parent_Id *uint16 `json:"parent-id"` Parent_Code *string `json:"parent-code"`
Search string `json:"search" gormhelper:"searchColumns=Code,Name"` Search string `json:"search" gormhelper:"searchColumns=Code,Name"`
} }
type ReadDetailDto struct { type ReadDetailDto struct {
Id uint16 `json:"id"` Id *uint16 `json:"id"`
Code *string `json:"code"` Code *string `json:"code"`
Includes string `json:"includes"` Includes string `json:"includes"`
} }
type UpdateDto struct { type UpdateDto struct {
Id uint16 `json:"id"` Id *uint16 `json:"id"`
CreateDto CreateDto
} }
type DeleteDto struct { type DeleteDto struct {
Id uint16 `json:"id"` Id *uint16 `json:"id"`
Code *string `json:"code"`
} }
type MetaDto struct { type MetaDto struct {
@@ -51,7 +52,7 @@ type ResponseDto struct {
ecore.SmallMain ecore.SmallMain
Code string `json:"code"` Code string `json:"code"`
Name string `json:"name"` Name string `json:"name"`
Parent_Id *uint16 `json:"parent_id"` Parent_Code *string `json:"parent_code"`
Parent *Division `json:"parent,omitempty"` Parent *Division `json:"parent,omitempty"`
Childrens []Division `json:"childrens,omitempty"` Childrens []Division `json:"childrens,omitempty"`
DivisionPosition []edpb.Basic `json:"divisionPositions,omitempty"` DivisionPosition []edpb.Basic `json:"divisionPositions,omitempty"`
@@ -61,7 +62,7 @@ func (d Division) ToResponse() ResponseDto {
resp := ResponseDto{ resp := ResponseDto{
Code: d.Code, Code: d.Code,
Name: d.Name, Name: d.Name,
Parent_Id: d.Parent_Id, Parent_Code: d.Parent_Code,
Parent: d.Parent, Parent: d.Parent,
Childrens: d.Childrens, Childrens: d.Childrens,
DivisionPosition: d.DivisionPositions, DivisionPosition: d.DivisionPositions,
@@ -7,11 +7,10 @@ import (
type Division struct { type Division struct {
ecore.SmallMain // adjust this according to the needs ecore.SmallMain // adjust this according to the needs
Code string `json:"code" gorm:"unique;size:10"` Code string `json:"code" gorm:"uniqueIndex;size:10"`
Name string `json:"name" gorm:"size:50"` Name string `json:"name" gorm:"size:50"`
Parent_Id *uint16 `json:"parent_id"`
Parent_Code *string `json:"parent_code" gorm:"size:10"` Parent_Code *string `json:"parent_code" gorm:"size:10"`
Parent *Division `json:"parent" gorm:"foreignKey:Parent_Id;references:Id"` Parent *Division `json:"parent" gorm:"foreignKey:Parent_Code;references:Code"`
Childrens []Division `json:"childrens" gorm:"foreignKey:Parent_Id"` // may need references to self Childrens []Division `json:"childrens" gorm:"foreignKey:Parent_Code;references:Code"` // may need references to self
DivisionPositions []edpb.Basic `json:"divisionPositions,omitempty" gorm:"foreignKey:Division_Id;references:Id"` DivisionPositions []edpb.Basic `json:"divisionPositions,omitempty" gorm:"foreignKey:Division_Code;references:Code"`
} }
@@ -33,19 +33,19 @@ func (obj myBase) GetList(w http.ResponseWriter, r *http.Request) {
} }
func (obj myBase) GetDetail(w http.ResponseWriter, r *http.Request) { func (obj myBase) GetDetail(w http.ResponseWriter, r *http.Request) {
id := rw.ValidateInt(w, "id", r.PathValue("id")) code := rw.ValidateString(w, "code", r.PathValue("code"))
if id <= 0 { if code == "" {
return return
} }
dto := e.ReadDetailDto{} dto := e.ReadDetailDto{}
dto.Id = uint16(id) dto.Code = &code
res, err := u.ReadDetail(dto) res, err := u.ReadDetail(dto)
rw.DataResponse(w, res, err) rw.DataResponse(w, res, err)
} }
func (obj myBase) Update(w http.ResponseWriter, r *http.Request) { func (obj myBase) Update(w http.ResponseWriter, r *http.Request) {
id := rw.ValidateInt(w, "id", r.PathValue("id")) code := rw.ValidateString(w, "code", r.PathValue("code"))
if id <= 0 { if code == "" {
return return
} }
@@ -53,19 +53,19 @@ func (obj myBase) Update(w http.ResponseWriter, r *http.Request) {
if res := rw.ValidateStructByIOR(w, r.Body, &dto); !res { if res := rw.ValidateStructByIOR(w, r.Body, &dto); !res {
return return
} }
dto.Id = uint16(id) dto.Code = code
res, err := u.Update(dto) res, err := u.Update(dto)
rw.DataResponse(w, res, err) rw.DataResponse(w, res, err)
} }
func (obj myBase) Delete(w http.ResponseWriter, r *http.Request) { func (obj myBase) Delete(w http.ResponseWriter, r *http.Request) {
id := rw.ValidateInt(w, "id", r.PathValue("id")) code := rw.ValidateString(w, "code", r.PathValue("code"))
if id <= 0 { if code == "" {
return return
} }
dto := e.DeleteDto{} dto := e.DeleteDto{}
dto.Id = uint16(id) dto.Code = &code
res, err := u.Delete(dto) res, err := u.Delete(dto)
rw.DataResponse(w, res, err) 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) { func (obj myBase) GetDetail(w http.ResponseWriter, r *http.Request) {
id := rw.ValidateInt(w, "id", r.PathValue("id")) code := rw.ValidateString(w, "code", r.PathValue("code"))
if id <= 0 { if code == "" {
return return
} }
dto := e.ReadDetailDto{} dto := e.ReadDetailDto{}
sf.UrlQueryParam(&dto, *r.URL) sf.UrlQueryParam(&dto, *r.URL)
dto.Id = uint16(id) dto.Code = &code
res, err := u.ReadDetail(dto) res, err := u.ReadDetail(dto)
rw.DataResponse(w, res, err) rw.DataResponse(w, res, err)
} }
func (obj myBase) Update(w http.ResponseWriter, r *http.Request) { func (obj myBase) Update(w http.ResponseWriter, r *http.Request) {
id := rw.ValidateInt(w, "id", r.PathValue("id")) code := rw.ValidateString(w, "code", r.PathValue("code"))
if id <= 0 { if code == "" {
return return
} }
@@ -55,19 +55,19 @@ func (obj myBase) Update(w http.ResponseWriter, r *http.Request) {
if res := rw.ValidateStructByIOR(w, r.Body, &dto); !res { if res := rw.ValidateStructByIOR(w, r.Body, &dto); !res {
return return
} }
dto.Id = uint16(id) dto.Code = code
res, err := u.Update(dto) res, err := u.Update(dto)
rw.DataResponse(w, res, err) rw.DataResponse(w, res, err)
} }
func (obj myBase) Delete(w http.ResponseWriter, r *http.Request) { func (obj myBase) Delete(w http.ResponseWriter, r *http.Request) {
id := rw.ValidateInt(w, "id", r.PathValue("id")) code := rw.ValidateString(w, "code", r.PathValue("code"))
if id <= 0 { if code == "" {
return return
} }
dto := e.DeleteDto{} dto := e.DeleteDto{}
dto.Id = uint16(id) dto.Code = &code
res, err := u.Delete(dto) res, err := u.Delete(dto)
rw.DataResponse(w, res, err) rw.DataResponse(w, res, err)
} }
@@ -290,8 +290,8 @@ func SetRoutes() http.Handler {
}) })
/******************** sources ********************/ /******************** sources ********************/
hc.RegCrud(r, "/v1/division", division.O) hc.RegCrudByCode(r, "/v1/division", division.O)
hc.RegCrud(r, "/v1/division-position", divisionposition.O) hc.RegCrudByCode(r, "/v1/division-position", divisionposition.O)
hc.RegCrud(r, "/v1/installation", installation.O) hc.RegCrud(r, "/v1/installation", installation.O)
hc.RegCrud(r, "/v1/unit", unit.O) hc.RegCrud(r, "/v1/unit", unit.O)
hc.RegCrud(r, "/v1/pharmacy-company", pharmacycompany.O) hc.RegCrud(r, "/v1/pharmacy-company", pharmacycompany.O)
@@ -166,7 +166,7 @@ func ReadDetail(input e.ReadDetailDto) (*d.Data, error) {
} }
func Update(input e.UpdateDto) (*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.DivisionPosition var data *e.DivisionPosition
var err error var err error
@@ -222,7 +222,7 @@ func Update(input e.UpdateDto) (*d.Data, error) {
} }
func Delete(input e.DeleteDto) (*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.DivisionPosition var data *e.DivisionPosition
var err error var err error
@@ -17,7 +17,7 @@ func setData[T *e.CreateDto | *e.UpdateDto](input T, data *e.DivisionPosition) {
inputSrc = &inputTemp.CreateDto inputSrc = &inputTemp.CreateDto
} }
data.Division_Id = inputSrc.Division_Id data.Division_Code = inputSrc.Division_Code
data.Code = inputSrc.Code data.Code = inputSrc.Code
data.Name = inputSrc.Name data.Name = inputSrc.Name
data.HeadStatus = inputSrc.HeadStatus data.HeadStatus = inputSrc.HeadStatus
@@ -81,7 +81,14 @@ func ReadDetailData(input e.ReadDetailDto, event *pl.Event, dbx ...*gorm.DB) (*e
tx = dg.I tx = dg.I
} }
if err := tx.First(&data, input.Id).Error; err != nil { if input.Code != nil {
tx = tx.Where("\"Code\" = ?", *input.Code)
}
if input.Id != nil {
tx = tx.Where("\"Id\" = ?", input.Id)
}
if err := tx.First(&data).Error; err != nil {
if processedErr := pu.HandleReadError(err, event, source, input.Id, data); processedErr != nil { if processedErr := pu.HandleReadError(err, event, source, input.Id, data); processedErr != nil {
return nil, processedErr return nil, processedErr
} }
@@ -166,7 +166,7 @@ func ReadDetail(input e.ReadDetailDto) (*d.Data, error) {
} }
func Update(input e.UpdateDto) (*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.Division var data *e.Division
var err error var err error
@@ -222,7 +222,7 @@ func Update(input e.UpdateDto) (*d.Data, error) {
} }
func Delete(input e.DeleteDto) (*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.Division var data *e.Division
var err error var err error
@@ -19,5 +19,5 @@ func setData[T *e.CreateDto | *e.UpdateDto](input T, data *e.Division) {
data.Code = inputSrc.Code data.Code = inputSrc.Code
data.Name = inputSrc.Name data.Name = inputSrc.Name
data.Parent_Id = inputSrc.Parent_Id data.Parent_Code = inputSrc.Parent_Code
} }
@@ -91,9 +91,16 @@ func ReadDetailData(input e.ReadDetailDto, event *pl.Event, dbx ...*gorm.DB) (*e
tx = dg.I 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. if err := tx.
Scopes(gh.Preload(input.Includes)). Scopes(gh.Preload(input.Includes)).
First(&data, input.Id).Error; err != nil { First(&data).Error; err != nil {
if processedErr := pu.HandleReadError(err, event, source, input.Id, data); processedErr != nil { if processedErr := pu.HandleReadError(err, event, source, input.Id, data); processedErr != nil {
return nil, processedErr return nil, processedErr
} }