From 8b9fabd5e12e9d098f35835e0b035b4df061fd04 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Wed, 8 Oct 2025 16:18:59 +0700 Subject: [PATCH 1/4] migration from server --- cmd/main-migration/migrations/atlas.sum | 68 ++++++++++++------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/cmd/main-migration/migrations/atlas.sum b/cmd/main-migration/migrations/atlas.sum index 5ef938c0..2e277aab 100644 --- a/cmd/main-migration/migrations/atlas.sum +++ b/cmd/main-migration/migrations/atlas.sum @@ -1,34 +1,34 @@ -h1:8cWwLjbT/RIIeLTxM5t9O3pCbmZZFVud4a6PrqIPqT8= -20250904105930.sql h1:MEM6blCgke9DzWQSTnLzasbPIrcHssNNrJqZpSkEo6k= -20250904141448.sql h1:J8cmYNk4ZrG9fhfbi2Z1IWz7YkfvhFqTzrLFo58BPY0= -20250908062237.sql h1:Pu23yEW/aKkwozHoOuROvHS/GK4ngARJGdO7FB7HZuI= -20250908062323.sql h1:oXl6Z143tOpIl4EfP4B8JNU8LrMvVmHEtCgAfiB4gs8= -20250908073811.sql h1:m2aNXfnGxnLq1+rVWrh4f60q7fhyhV3gEwNu/OIqQlE= -20250908073839.sql h1:cPk54xjLdMs26uY8ZHjNWLuyfAMzV7Zb0/9oJQrsw04= -20250910055902.sql h1:5xwjAV6QbtZT9empTJKfhyAjdknbHzb15B0Ku5dzqtQ= -20250915123412.sql h1:D83xaU2YlDEd21HLup/YQpQ2easMToYCyy/oK6AFgQs= -20250916043819.sql h1:ekoTJsBqQZ8G8n0qJ03d13+eoNoc7sAUEQGA5D/CCxk= -20250917040616.sql h1:zoCnmcXuM7AVv85SmN7RmFglCgJnoDmpRWExH0LAc9Q= -20250917040751.sql h1:J1xyRrh32y1+lezwAyNwPcUQ6ABBSgbvzNLva4SVdQU= -20250917045138.sql h1:jKe1Z0uOLG4SGBYM+S/3P+/zMPztmgoderD5swnMuCg= -20250917093645.sql h1:cNI3Pbz1R3LxvIXLuexafJFCXUXrmuFCgXXJ2sG+FW0= -20250918073552.sql h1:RJ1SvMzP6aeWnoPVD3eVAmIQOkcp6Php8z3QRri6v4g= -20250918073742.sql h1:+cEsnJTJFybe2fR69ZoOiX2R6c6iITl4m6WTZ1hjyzY= -20250918074745.sql h1:2hNVQCXF/dVYXAh+T/7oBFgERGWxzVb2FXJjwkFWGCI= -20250923025134.sql h1:Ykz/qpHiGDXPsCsWTjydQFVSibZP2D+h2fIeb2h2JGA= -20250924051317.sql h1:yQuW6SwJxIOM5fcxeAaie5lSm1oLysU/C2hH2xNCVoQ= -20250929034321.sql h1:101FJ8VH12mrZWlt/X1gvKUGOhoiF8tFbjiapAjnHzg= -20250929034428.sql h1:i+pROD9p+g5dOmmZma6WF/0Hw5g3Ha28NN85iTo1K34= -20250930025550.sql h1:+F+CsCUXD/ql0tHGEow70GhPBX1ZybVn+bh/T4YMh7Y= -20250930140351.sql h1:9AAEG1AnOAH+o0+oHL5G7I8vqlWOhwRlCGyyCpT/y1Q= -20251002085604.sql h1:3xZ68eYp4urXRnvotNH1XvG2mYOSDV/j3zHEZ/txg5E= -20251003032030.sql h1:HB+mQ2lXMNomHDpaRhB/9IwYI9/YiDO5eOJ+nAQH/jw= -20251005060450.sql h1:LbtCE2b+8osM3CvnmQJH1uCPtn+d7WchsslBOz8bL3Q= -20251006041122.sql h1:MlS7f21z06sutnf9dIekt5fuHJr4lgcQ4uCuCXAGsfc= -20251006045658.sql h1:3FmGCPCzjgMPdWDRodZTsx3KVaodd9zB9ilib69aewk= -20251006045928.sql h1:Z5g31PmnzNwk/OKdODcxZGm8fjJQdMFK32Xfnt3bRHg= -20251007022859.sql h1:FO03zEfaNEk/aXwY81d5Lp3MoBB9kPQuXlXJ4BPiSR8= -20251008031337.sql h1:l+sxUAGvcTfj3I6kAFHo+T6AYodC9k9GkR+jaKO2xXc= -20251008031554.sql h1:AqrVfIhSzY3PCy8ZlP5W91wn2iznfIuj5qQfubp6/94= -20251008052346.sql h1:nxnXmooIJ6r1mmzwnw+6efxLfc/k9h2aE6RMptPRons= -20251008073620.sql h1:6YsJp1W4SmQJ1lxpqF27BBlDC1zqhw7Yhc7pLzQTY6M= +h1:wdilGmaNGlpLWwpFErWUStZtsHTs6GEZAJewEjTYLlc= +20250904105930.sql h1:Vv4vCurl7m7/ZB6TjRpkubHpQ4RYwSUn0QHdzfoGpzY= +20250904141448.sql h1:FYCHH9Os4KkrZMDu/jR8FMP+wLMRW+Mb0PkLU/9BRDg= +20250908062237.sql h1:oanBpKZd+akPu2I/xYhUSbd0G5tAFbXzKLER/Zs8ENI= +20250908062323.sql h1:miNG9COddXkD1jGTgaROMAZ618eT6oiLGiJhXWnQwhE= +20250908073811.sql h1:gOi5cnGG1htlpfizybYmUIT0vYjZTBfXiI0nPSYK2u8= +20250908073839.sql h1:cWNDA4YikjoOteAJuNLFILjQUJPFB6o8Wxreiek4QyI= +20250910055902.sql h1:nxxOGnU0BbH/v3IPgeIOXOwH8d3tKomw7h6FTeMnnBs= +20250915123412.sql h1:mz7SiWfrdf0qE1VTSAAnA/147d6gyp6ry5vZ2bR9SH0= +20250916043819.sql h1:RHXVtmMkB6wfv06HfPyHMBmUfIpFt1xveafNz0kwKnE= +20250917040616.sql h1:MYVDht+akBlzQGKNu2hTTTLPEcH1bxT/Q8MK6WEtuhs= +20250917040751.sql h1:J79YyS2JzWgh5oKXMTgh67uo3gLxKaAsxRiZmSIfjBs= +20250917045138.sql h1:/SM1N4O8X3yxpoJgMEARmS1uOkuLKsTOy4PLsRCOKaQ= +20250917093645.sql h1:PNBTGZ7s10e5b5+Tie8YfVQBN0zKtJ5T34oK1iOUEb4= +20250918073552.sql h1:jG7+g3i8ODYaJdcdZz12v3nbsZ5mB9wG6kWnGyTQIRI= +20250918073742.sql h1:j+rgw7puxE7s+phqPVZHmPk0af3rcaA56Itp86y1suY= +20250918074745.sql h1:rPmP4DXs6OnY4Vp+xO/z9jFpJt/RrJ52SJJjIIxeDvc= +20250923025134.sql h1:2r6pcwnBSU5Y9Czk1OHBoh4yZXiMtEca9X8843fTEX0= +20250924051317.sql h1:iUAk2gsGoEGIPQ0lEEUp8maMSId8emNbP+kP712ABIA= +20250929034321.sql h1:UlpALNVmdi95zOIT0yc6ZyTj9bBjQEIpZhvgrc52M+k= +20250929034428.sql h1:feF+H4nDyHh5bdx48Oiz0A1qecZfi6v3qTTdjzJ45Dg= +20250930025550.sql h1:6XT1kXI3Z3ZIxxmvT7poufZWWCW0QiejZPaFV5wBnjI= +20250930140351.sql h1:HxnmAbh9gCy8jwl/9ycGktiByaUripsjFFvohofY2CY= +20251002085604.sql h1:SjLPi+ZN6qDccK3DaEQCgNsZpPwr5kynWXwbwEsziCI= +20251003032030.sql h1:oHfxNSuqTxU8Zaf9H+h8TuUb1Da03wcyc6hZjDrUQ2s= +20251005060450.sql h1:GIuCcrd4MwjmXpvbzDzPYL18BV3QaZZ+Y2FmEzjvi0E= +20251006041122.sql h1:uNDQbSw0M08lYoMvUNlQtS3iDzpPM1ixT13ugSAoWjE= +20251006045658.sql h1:z+t7yCK54Q4SSiF9kUyUhkYB2F+kzSW9TB7ogxd9wzw= +20251006045928.sql h1:1lATLFLp4BWwGZqAjZdP0Dc6ypNXiYcwjoNkqGa8NFE= +20251007022859.sql h1:HXXwWrkyvzJzJGAt9mGskCRBBV/c1JfPmfjDocmJhQ4= +20251008031337.sql h1:Ln5pCF3Hxa5foHZLcds+z/us2eH6VAhhEj3w0TAGlVs= +20251008031554.sql h1:aB4MUS2lmqG0//4HKUWorcPSpWya0VC4QItvGyskEVI= +20251008052346.sql h1:MI3AZgU5XcwZT2OvvlWAxdRtL0eJ3jjRwt56IY1+pRU= +20251008073620.sql h1:sztWXuSNYwpEraaSapSsYwno75LO5H/N7ob7OJQ8X/A= From 63d2f2342cfa6bfd7ca593f384d4b7645f5d7835 Mon Sep 17 00:00:00 2001 From: dpurbosakti Date: Thu, 9 Oct 2025 11:29:49 +0700 Subject: [PATCH 2/4] add postalcode --- .../migrations/20251009042854.sql | 9 +++ cmd/main-migration/migrations/atlas.sum | 69 ++++++++++--------- .../domain/main-entities/postal-code/dto.go | 60 ++++++++++++++++ .../main-entities/postal-code/entity.go | 7 ++ internal/domain/main-entities/village/dto.go | 10 +-- .../domain/main-entities/village/entity.go | 13 ++-- internal/interface/migration/main-entities.go | 2 + 7 files changed, 128 insertions(+), 42 deletions(-) create mode 100644 cmd/main-migration/migrations/20251009042854.sql create mode 100644 internal/domain/main-entities/postal-code/dto.go create mode 100644 internal/domain/main-entities/postal-code/entity.go diff --git a/cmd/main-migration/migrations/20251009042854.sql b/cmd/main-migration/migrations/20251009042854.sql new file mode 100644 index 00000000..c8b5ec23 --- /dev/null +++ b/cmd/main-migration/migrations/20251009042854.sql @@ -0,0 +1,9 @@ +-- Create "PostalCode" table +CREATE TABLE "public"."PostalCode" ( + "Id" bigserial NOT NULL, + "Code" character varying(5) NULL, + "Village_Code" character varying(10) NULL, + PRIMARY KEY ("Id"), + CONSTRAINT "uni_PostalCode_Code" UNIQUE ("Code"), + CONSTRAINT "fk_Village_PostalCodes" FOREIGN KEY ("Village_Code") REFERENCES "public"."Village" ("Code") ON UPDATE NO ACTION ON DELETE NO ACTION +); diff --git a/cmd/main-migration/migrations/atlas.sum b/cmd/main-migration/migrations/atlas.sum index 2e277aab..5bb0447a 100644 --- a/cmd/main-migration/migrations/atlas.sum +++ b/cmd/main-migration/migrations/atlas.sum @@ -1,34 +1,35 @@ -h1:wdilGmaNGlpLWwpFErWUStZtsHTs6GEZAJewEjTYLlc= -20250904105930.sql h1:Vv4vCurl7m7/ZB6TjRpkubHpQ4RYwSUn0QHdzfoGpzY= -20250904141448.sql h1:FYCHH9Os4KkrZMDu/jR8FMP+wLMRW+Mb0PkLU/9BRDg= -20250908062237.sql h1:oanBpKZd+akPu2I/xYhUSbd0G5tAFbXzKLER/Zs8ENI= -20250908062323.sql h1:miNG9COddXkD1jGTgaROMAZ618eT6oiLGiJhXWnQwhE= -20250908073811.sql h1:gOi5cnGG1htlpfizybYmUIT0vYjZTBfXiI0nPSYK2u8= -20250908073839.sql h1:cWNDA4YikjoOteAJuNLFILjQUJPFB6o8Wxreiek4QyI= -20250910055902.sql h1:nxxOGnU0BbH/v3IPgeIOXOwH8d3tKomw7h6FTeMnnBs= -20250915123412.sql h1:mz7SiWfrdf0qE1VTSAAnA/147d6gyp6ry5vZ2bR9SH0= -20250916043819.sql h1:RHXVtmMkB6wfv06HfPyHMBmUfIpFt1xveafNz0kwKnE= -20250917040616.sql h1:MYVDht+akBlzQGKNu2hTTTLPEcH1bxT/Q8MK6WEtuhs= -20250917040751.sql h1:J79YyS2JzWgh5oKXMTgh67uo3gLxKaAsxRiZmSIfjBs= -20250917045138.sql h1:/SM1N4O8X3yxpoJgMEARmS1uOkuLKsTOy4PLsRCOKaQ= -20250917093645.sql h1:PNBTGZ7s10e5b5+Tie8YfVQBN0zKtJ5T34oK1iOUEb4= -20250918073552.sql h1:jG7+g3i8ODYaJdcdZz12v3nbsZ5mB9wG6kWnGyTQIRI= -20250918073742.sql h1:j+rgw7puxE7s+phqPVZHmPk0af3rcaA56Itp86y1suY= -20250918074745.sql h1:rPmP4DXs6OnY4Vp+xO/z9jFpJt/RrJ52SJJjIIxeDvc= -20250923025134.sql h1:2r6pcwnBSU5Y9Czk1OHBoh4yZXiMtEca9X8843fTEX0= -20250924051317.sql h1:iUAk2gsGoEGIPQ0lEEUp8maMSId8emNbP+kP712ABIA= -20250929034321.sql h1:UlpALNVmdi95zOIT0yc6ZyTj9bBjQEIpZhvgrc52M+k= -20250929034428.sql h1:feF+H4nDyHh5bdx48Oiz0A1qecZfi6v3qTTdjzJ45Dg= -20250930025550.sql h1:6XT1kXI3Z3ZIxxmvT7poufZWWCW0QiejZPaFV5wBnjI= -20250930140351.sql h1:HxnmAbh9gCy8jwl/9ycGktiByaUripsjFFvohofY2CY= -20251002085604.sql h1:SjLPi+ZN6qDccK3DaEQCgNsZpPwr5kynWXwbwEsziCI= -20251003032030.sql h1:oHfxNSuqTxU8Zaf9H+h8TuUb1Da03wcyc6hZjDrUQ2s= -20251005060450.sql h1:GIuCcrd4MwjmXpvbzDzPYL18BV3QaZZ+Y2FmEzjvi0E= -20251006041122.sql h1:uNDQbSw0M08lYoMvUNlQtS3iDzpPM1ixT13ugSAoWjE= -20251006045658.sql h1:z+t7yCK54Q4SSiF9kUyUhkYB2F+kzSW9TB7ogxd9wzw= -20251006045928.sql h1:1lATLFLp4BWwGZqAjZdP0Dc6ypNXiYcwjoNkqGa8NFE= -20251007022859.sql h1:HXXwWrkyvzJzJGAt9mGskCRBBV/c1JfPmfjDocmJhQ4= -20251008031337.sql h1:Ln5pCF3Hxa5foHZLcds+z/us2eH6VAhhEj3w0TAGlVs= -20251008031554.sql h1:aB4MUS2lmqG0//4HKUWorcPSpWya0VC4QItvGyskEVI= -20251008052346.sql h1:MI3AZgU5XcwZT2OvvlWAxdRtL0eJ3jjRwt56IY1+pRU= -20251008073620.sql h1:sztWXuSNYwpEraaSapSsYwno75LO5H/N7ob7OJQ8X/A= +h1:CAMxY9M2WLkhsnD/5ItPa0kDPKJwprjDft26U+FW3vE= +20250904105930.sql h1:MEM6blCgke9DzWQSTnLzasbPIrcHssNNrJqZpSkEo6k= +20250904141448.sql h1:J8cmYNk4ZrG9fhfbi2Z1IWz7YkfvhFqTzrLFo58BPY0= +20250908062237.sql h1:Pu23yEW/aKkwozHoOuROvHS/GK4ngARJGdO7FB7HZuI= +20250908062323.sql h1:oXl6Z143tOpIl4EfP4B8JNU8LrMvVmHEtCgAfiB4gs8= +20250908073811.sql h1:m2aNXfnGxnLq1+rVWrh4f60q7fhyhV3gEwNu/OIqQlE= +20250908073839.sql h1:cPk54xjLdMs26uY8ZHjNWLuyfAMzV7Zb0/9oJQrsw04= +20250910055902.sql h1:5xwjAV6QbtZT9empTJKfhyAjdknbHzb15B0Ku5dzqtQ= +20250915123412.sql h1:D83xaU2YlDEd21HLup/YQpQ2easMToYCyy/oK6AFgQs= +20250916043819.sql h1:ekoTJsBqQZ8G8n0qJ03d13+eoNoc7sAUEQGA5D/CCxk= +20250917040616.sql h1:zoCnmcXuM7AVv85SmN7RmFglCgJnoDmpRWExH0LAc9Q= +20250917040751.sql h1:J1xyRrh32y1+lezwAyNwPcUQ6ABBSgbvzNLva4SVdQU= +20250917045138.sql h1:jKe1Z0uOLG4SGBYM+S/3P+/zMPztmgoderD5swnMuCg= +20250917093645.sql h1:cNI3Pbz1R3LxvIXLuexafJFCXUXrmuFCgXXJ2sG+FW0= +20250918073552.sql h1:RJ1SvMzP6aeWnoPVD3eVAmIQOkcp6Php8z3QRri6v4g= +20250918073742.sql h1:+cEsnJTJFybe2fR69ZoOiX2R6c6iITl4m6WTZ1hjyzY= +20250918074745.sql h1:2hNVQCXF/dVYXAh+T/7oBFgERGWxzVb2FXJjwkFWGCI= +20250923025134.sql h1:Ykz/qpHiGDXPsCsWTjydQFVSibZP2D+h2fIeb2h2JGA= +20250924051317.sql h1:yQuW6SwJxIOM5fcxeAaie5lSm1oLysU/C2hH2xNCVoQ= +20250929034321.sql h1:101FJ8VH12mrZWlt/X1gvKUGOhoiF8tFbjiapAjnHzg= +20250929034428.sql h1:i+pROD9p+g5dOmmZma6WF/0Hw5g3Ha28NN85iTo1K34= +20250930025550.sql h1:+F+CsCUXD/ql0tHGEow70GhPBX1ZybVn+bh/T4YMh7Y= +20250930140351.sql h1:9AAEG1AnOAH+o0+oHL5G7I8vqlWOhwRlCGyyCpT/y1Q= +20251002085604.sql h1:3xZ68eYp4urXRnvotNH1XvG2mYOSDV/j3zHEZ/txg5E= +20251003032030.sql h1:HB+mQ2lXMNomHDpaRhB/9IwYI9/YiDO5eOJ+nAQH/jw= +20251005060450.sql h1:LbtCE2b+8osM3CvnmQJH1uCPtn+d7WchsslBOz8bL3Q= +20251006041122.sql h1:MlS7f21z06sutnf9dIekt5fuHJr4lgcQ4uCuCXAGsfc= +20251006045658.sql h1:3FmGCPCzjgMPdWDRodZTsx3KVaodd9zB9ilib69aewk= +20251006045928.sql h1:Z5g31PmnzNwk/OKdODcxZGm8fjJQdMFK32Xfnt3bRHg= +20251007022859.sql h1:FO03zEfaNEk/aXwY81d5Lp3MoBB9kPQuXlXJ4BPiSR8= +20251008031337.sql h1:l+sxUAGvcTfj3I6kAFHo+T6AYodC9k9GkR+jaKO2xXc= +20251008031554.sql h1:AqrVfIhSzY3PCy8ZlP5W91wn2iznfIuj5qQfubp6/94= +20251008052346.sql h1:nxnXmooIJ6r1mmzwnw+6efxLfc/k9h2aE6RMptPRons= +20251008073620.sql h1:6YsJp1W4SmQJ1lxpqF27BBlDC1zqhw7Yhc7pLzQTY6M= +20251009042854.sql h1:vK7bDqUDTxjHMSh1wrQiKITeqbB/7aqkWaPut8nGA68= diff --git a/internal/domain/main-entities/postal-code/dto.go b/internal/domain/main-entities/postal-code/dto.go new file mode 100644 index 00000000..cb1e3d39 --- /dev/null +++ b/internal/domain/main-entities/postal-code/dto.go @@ -0,0 +1,60 @@ +package postalcode + +import ( + ecore "simrs-vx/internal/domain/base-entities/core" +) + +type CreateDto struct { + Code string `json:"code" validate:"numeric;maxLength=10"` + Village_Code string `json:"district_code" validate:"numeric;maxLength=10"` +} + +type ReadListDto struct { + District_Code string `json:"district_code"` + Code string `json:"code"` + Name string `json:"name"` + Pagination ecore.Pagination +} + +type ReadDetailDto struct { + Id uint32 `json:"id"` + Code *string `json:"code"` +} + +type UpdateDto struct { + Id uint32 `json:"id"` + CreateDto +} + +type DeleteDto struct { + Id uint32 `json:"id"` +} + +type MetaDto struct { + PageNumber int `json:"page_number"` + PageSize int `json:"page_size"` + Count int `json:"count"` +} + +type ResponseDto struct { + Id uint32 `json:"id"` + Village_Code string `json:"village_code"` + Code string `json:"code"` +} + +func (d PostalCode) ToResponse() ResponseDto { + resp := ResponseDto{ + Id: d.Id, + Village_Code: d.Village_Code, + Code: d.Code, + } + return resp +} + +func ToResponseList(data []PostalCode) []ResponseDto { + resp := make([]ResponseDto, len(data)) + for i, u := range data { + resp[i] = u.ToResponse() + } + return resp +} diff --git a/internal/domain/main-entities/postal-code/entity.go b/internal/domain/main-entities/postal-code/entity.go new file mode 100644 index 00000000..ceb2174c --- /dev/null +++ b/internal/domain/main-entities/postal-code/entity.go @@ -0,0 +1,7 @@ +package postalcode + +type PostalCode struct { + Id uint32 `json:"id" gorm:"primaryKey"` + Code string `json:"code" gorm:"unique;size:5"` + Village_Code string `json:"village_code" gorm:"size:10"` +} diff --git a/internal/domain/main-entities/village/dto.go b/internal/domain/main-entities/village/dto.go index ca42f073..148138cd 100644 --- a/internal/domain/main-entities/village/dto.go +++ b/internal/domain/main-entities/village/dto.go @@ -2,6 +2,7 @@ package village import ( ecore "simrs-vx/internal/domain/base-entities/core" + ep "simrs-vx/internal/domain/main-entities/postal-code" ) type CreateDto struct { @@ -38,10 +39,11 @@ type MetaDto struct { } type ResponseDto struct { - Id uint32 `json:"id"` - District_Code string `json:"district_code"` - Code string `json:"code"` - Name string `json:"name"` + Id uint32 `json:"id"` + District_Code string `json:"district_code"` + Code string `json:"code"` + Name string `json:"name"` + PostalCodes []ep.PostalCode `json:"postalCodes,omitempty"` } func (d Village) ToResponse() ResponseDto { diff --git a/internal/domain/main-entities/village/entity.go b/internal/domain/main-entities/village/entity.go index aae0caf5..7078534f 100644 --- a/internal/domain/main-entities/village/entity.go +++ b/internal/domain/main-entities/village/entity.go @@ -1,8 +1,13 @@ package village +import ( + ep "simrs-vx/internal/domain/main-entities/postal-code" +) + type Village struct { - Id uint32 `json:"id" gorm:"primaryKey"` - District_Code string `json:"district_code" gorm:"size:6"` // NOT: THE PROPER SIZE IS 6 - Code string `json:"code" gorm:"unique;size:10"` - Name string `json:"name" gorm:"size:50"` + Id uint32 `json:"id" gorm:"primaryKey"` + District_Code string `json:"district_code" gorm:"size:6"` // NOT: THE PROPER SIZE IS 6 + Code string `json:"code" gorm:"unique;size:10"` + Name string `json:"name" gorm:"size:50"` + PostalCodes []ep.PostalCode `json:"postalCodes,omitempty" gorm:"foreignKey:Village_Code;references:Code"` } diff --git a/internal/interface/migration/main-entities.go b/internal/interface/migration/main-entities.go index 9711c6bd..fcb3db9e 100644 --- a/internal/interface/migration/main-entities.go +++ b/internal/interface/migration/main-entities.go @@ -57,6 +57,7 @@ import ( personrelative "simrs-vx/internal/domain/main-entities/person-relative" pharmacist "simrs-vx/internal/domain/main-entities/pharmacist" pharmacycompany "simrs-vx/internal/domain/main-entities/pharmacy-company" + postalcode "simrs-vx/internal/domain/main-entities/postal-code" practiceschedule "simrs-vx/internal/domain/main-entities/practice-schedule" prescription "simrs-vx/internal/domain/main-entities/prescription" prescriptionitem "simrs-vx/internal/domain/main-entities/prescription-item" @@ -149,5 +150,6 @@ func getMainEntities() []any { &consultation.Consultation{}, &chemo.Chemo{}, &midwife.Midwife{}, + &postalcode.PostalCode{}, } } From 601bbd5a0a87f44e00f7f54f98d39a3c43ea47e7 Mon Sep 17 00:00:00 2001 From: dpurbosakti Date: Thu, 9 Oct 2025 12:27:56 +0700 Subject: [PATCH 3/4] restructure area --- .../migrations/20251009052657.sql | 8 ++++++++ cmd/main-migration/migrations/atlas.sum | 3 ++- .../main-entities/district/base/entity.go | 8 ++++++++ internal/domain/main-entities/district/dto.go | 12 ++++++------ .../domain/main-entities/district/entity.go | 14 ++++++++------ .../main-entities/postal-code/base/entity.go | 7 +++++++ .../domain/main-entities/postal-code/entity.go | 10 +++++++--- internal/domain/main-entities/province/dto.go | 15 +++++++++------ .../domain/main-entities/province/entity.go | 10 +++++----- .../main-entities/regency/base/entitiy.go | 8 ++++++++ internal/domain/main-entities/regency/dto.go | 18 +++++++++++------- .../domain/main-entities/regency/entity.go | 14 ++++++++------ .../main-entities/village/base/entity.go | 8 ++++++++ internal/domain/main-entities/village/dto.go | 18 +++++++++++------- .../domain/main-entities/village/entity.go | 12 ++++++------ 15 files changed, 112 insertions(+), 53 deletions(-) create mode 100644 cmd/main-migration/migrations/20251009052657.sql create mode 100644 internal/domain/main-entities/district/base/entity.go create mode 100644 internal/domain/main-entities/postal-code/base/entity.go create mode 100644 internal/domain/main-entities/regency/base/entitiy.go create mode 100644 internal/domain/main-entities/village/base/entity.go diff --git a/cmd/main-migration/migrations/20251009052657.sql b/cmd/main-migration/migrations/20251009052657.sql new file mode 100644 index 00000000..5655a577 --- /dev/null +++ b/cmd/main-migration/migrations/20251009052657.sql @@ -0,0 +1,8 @@ +-- Modify "Regency" table +ALTER TABLE "public"."Regency" DROP CONSTRAINT "fk_Province_Regencies", ALTER COLUMN "Id" TYPE bigint, ADD CONSTRAINT "fk_Regency_Province" FOREIGN KEY ("Province_Code") REFERENCES "public"."Province" ("Code") ON UPDATE NO ACTION ON DELETE NO ACTION; +-- Modify "District" table +ALTER TABLE "public"."District" DROP CONSTRAINT "fk_Regency_Districts", ADD CONSTRAINT "fk_District_Regency" FOREIGN KEY ("Regency_Code") REFERENCES "public"."Regency" ("Code") ON UPDATE NO ACTION ON DELETE NO ACTION; +-- Modify "Village" table +ALTER TABLE "public"."Village" DROP CONSTRAINT "fk_District_Villages", ADD CONSTRAINT "fk_Village_District" FOREIGN KEY ("District_Code") REFERENCES "public"."District" ("Code") ON UPDATE NO ACTION ON DELETE NO ACTION; +-- Modify "PostalCode" table +ALTER TABLE "public"."PostalCode" DROP CONSTRAINT "fk_Village_PostalCodes", ADD CONSTRAINT "fk_PostalCode_Village" FOREIGN KEY ("Village_Code") REFERENCES "public"."Village" ("Code") ON UPDATE NO ACTION ON DELETE NO ACTION; diff --git a/cmd/main-migration/migrations/atlas.sum b/cmd/main-migration/migrations/atlas.sum index 5bb0447a..e06784b3 100644 --- a/cmd/main-migration/migrations/atlas.sum +++ b/cmd/main-migration/migrations/atlas.sum @@ -1,4 +1,4 @@ -h1:CAMxY9M2WLkhsnD/5ItPa0kDPKJwprjDft26U+FW3vE= +h1:TrdoKKXrICvDA283WGDf33WtWHglk1twvjDpYrNwc8o= 20250904105930.sql h1:MEM6blCgke9DzWQSTnLzasbPIrcHssNNrJqZpSkEo6k= 20250904141448.sql h1:J8cmYNk4ZrG9fhfbi2Z1IWz7YkfvhFqTzrLFo58BPY0= 20250908062237.sql h1:Pu23yEW/aKkwozHoOuROvHS/GK4ngARJGdO7FB7HZuI= @@ -33,3 +33,4 @@ h1:CAMxY9M2WLkhsnD/5ItPa0kDPKJwprjDft26U+FW3vE= 20251008052346.sql h1:nxnXmooIJ6r1mmzwnw+6efxLfc/k9h2aE6RMptPRons= 20251008073620.sql h1:6YsJp1W4SmQJ1lxpqF27BBlDC1zqhw7Yhc7pLzQTY6M= 20251009042854.sql h1:vK7bDqUDTxjHMSh1wrQiKITeqbB/7aqkWaPut8nGA68= +20251009052657.sql h1:I0rBPBg1o6zuRaxqWqCh82rI+NN7Gr6C//8ItUaLmtY= diff --git a/internal/domain/main-entities/district/base/entity.go b/internal/domain/main-entities/district/base/entity.go new file mode 100644 index 00000000..c7429490 --- /dev/null +++ b/internal/domain/main-entities/district/base/entity.go @@ -0,0 +1,8 @@ +package base + +type Basic struct { + Id uint32 `json:"id" gorm:"primaryKey"` + Regency_Code string `json:"regency_code" gorm:"size:4"` + Code string `json:"code" gorm:"unique;size:6"` // NOTE: THE PROPER SIZE IS 6 + Name string `json:"name" gorm:"size:50"` +} diff --git a/internal/domain/main-entities/district/dto.go b/internal/domain/main-entities/district/dto.go index 869aa9c2..64761713 100644 --- a/internal/domain/main-entities/district/dto.go +++ b/internal/domain/main-entities/district/dto.go @@ -2,7 +2,9 @@ package district import ( ecore "simrs-vx/internal/domain/base-entities/core" - ev "simrs-vx/internal/domain/main-entities/village" + edb "simrs-vx/internal/domain/main-entities/district/base" + er "simrs-vx/internal/domain/main-entities/regency" + evb "simrs-vx/internal/domain/main-entities/village/base" ) type CreateDto struct { @@ -39,11 +41,9 @@ type MetaDto struct { } type ResponseDto struct { - Id uint32 `json:"id"` - Regency_Code string `json:"regency_code"` - Code string `json:"code"` - Name string `json:"name"` - Villages []*ev.Village `json:"villages,omitempty"` + edb.Basic + Regency *er.Regency `json:"regency,omitempty"` + Villages []*evb.Basic `json:"villages,omitempty"` } func (d District) ToResponse() ResponseDto { diff --git a/internal/domain/main-entities/district/entity.go b/internal/domain/main-entities/district/entity.go index a3ad27e7..b9cb89c3 100644 --- a/internal/domain/main-entities/district/entity.go +++ b/internal/domain/main-entities/district/entity.go @@ -1,11 +1,13 @@ package district -import ev "simrs-vx/internal/domain/main-entities/village" +import ( + edb "simrs-vx/internal/domain/main-entities/district/base" + er "simrs-vx/internal/domain/main-entities/regency" + evb "simrs-vx/internal/domain/main-entities/village/base" +) type District struct { - Id uint32 `json:"id" gorm:"primaryKey"` - Regency_Code string `json:"regency_code" gorm:"size:4"` - Code string `json:"code" gorm:"unique;size:6"` // NOTE: THE PROPER SIZE IS 6 - Name string `json:"name" gorm:"size:50"` - Villages []*ev.Village `json:"villages,omitempty" gorm:"foreignKey:District_Code;references:Code"` + edb.Basic + Regency *er.Regency `json:"regency,omitempty" gorm:"foreignKey:Regency_Code;references:Code"` + Villages []*evb.Basic `json:"villages,omitempty" gorm:"foreignKey:District_Code;references:Code"` } diff --git a/internal/domain/main-entities/postal-code/base/entity.go b/internal/domain/main-entities/postal-code/base/entity.go new file mode 100644 index 00000000..814e4e72 --- /dev/null +++ b/internal/domain/main-entities/postal-code/base/entity.go @@ -0,0 +1,7 @@ +package base + +type Basic struct { + Id uint32 `json:"id" gorm:"primaryKey"` + Village_Code string `json:"village_code" gorm:"size:10"` + Code string `json:"code" gorm:"unique;size:5"` +} diff --git a/internal/domain/main-entities/postal-code/entity.go b/internal/domain/main-entities/postal-code/entity.go index ceb2174c..2825db9c 100644 --- a/internal/domain/main-entities/postal-code/entity.go +++ b/internal/domain/main-entities/postal-code/entity.go @@ -1,7 +1,11 @@ package postalcode +import ( + ep "simrs-vx/internal/domain/main-entities/postal-code/base" + ev "simrs-vx/internal/domain/main-entities/village" +) + type PostalCode struct { - Id uint32 `json:"id" gorm:"primaryKey"` - Code string `json:"code" gorm:"unique;size:5"` - Village_Code string `json:"village_code" gorm:"size:10"` + ep.Basic + Village *ev.Village `json:"village,omitempty" gorm:"foreignKey:Village_Code;references:Code"` } diff --git a/internal/domain/main-entities/province/dto.go b/internal/domain/main-entities/province/dto.go index d2cfc1ca..e2ac504b 100644 --- a/internal/domain/main-entities/province/dto.go +++ b/internal/domain/main-entities/province/dto.go @@ -2,6 +2,7 @@ package province import ( ecore "simrs-vx/internal/domain/base-entities/core" + erb "simrs-vx/internal/domain/main-entities/regency/base" ) type CreateDto struct { @@ -36,16 +37,18 @@ type MetaDto struct { } type ResponseDto struct { - Id int16 `json:"id"` - Code string `json:"code"` - Name string `json:"name"` + Id int16 `json:"id"` + Code string `json:"code"` + Name string `json:"name"` + Regencies []erb.Basic `json:"regencies,omitempty"` } func (d Province) ToResponse() ResponseDto { resp := ResponseDto{ - Id: d.Id, - Code: d.Code, - Name: d.Name, + Id: d.Id, + Code: d.Code, + Name: d.Name, + Regencies: d.Regencies, } return resp } diff --git a/internal/domain/main-entities/province/entity.go b/internal/domain/main-entities/province/entity.go index ab024b22..198cf4a4 100644 --- a/internal/domain/main-entities/province/entity.go +++ b/internal/domain/main-entities/province/entity.go @@ -1,10 +1,10 @@ package province -import er "simrs-vx/internal/domain/main-entities/regency" +import erb "simrs-vx/internal/domain/main-entities/regency/base" type Province struct { - Id int16 `json:"id" gorm:"primaryKey"` - Code string `json:"code" gorm:"unique;size:2"` - Name string `json:"name" gorm:"size:50"` - Regencies []*er.Regency `json:"regencies,omitempty" gorm:"foreignKey:Province_Code;references:Code"` + Id int16 `json:"id" gorm:"primaryKey"` + Code string `json:"code" gorm:"unique;size:2"` + Name string `json:"name" gorm:"size:50"` + Regencies []erb.Basic `json:"regencies,omitempty" gorm:"foreignKey:Province_Code;references:Code"` } diff --git a/internal/domain/main-entities/regency/base/entitiy.go b/internal/domain/main-entities/regency/base/entitiy.go new file mode 100644 index 00000000..b6c982ec --- /dev/null +++ b/internal/domain/main-entities/regency/base/entitiy.go @@ -0,0 +1,8 @@ +package base + +type Basic struct { + Id uint32 `json:"id" gorm:"primaryKey"` + Province_Code string `json:"province_code" gorm:"size:2"` + Code string `json:"code" gorm:"unique;size:4"` + Name string `json:"name" gorm:"size:50"` +} diff --git a/internal/domain/main-entities/regency/dto.go b/internal/domain/main-entities/regency/dto.go index e4d76df2..da6213df 100644 --- a/internal/domain/main-entities/regency/dto.go +++ b/internal/domain/main-entities/regency/dto.go @@ -2,7 +2,9 @@ package regency import ( ecore "simrs-vx/internal/domain/base-entities/core" - ed "simrs-vx/internal/domain/main-entities/district" + edb "simrs-vx/internal/domain/main-entities/district/base" + ep "simrs-vx/internal/domain/main-entities/province" + erb "simrs-vx/internal/domain/main-entities/regency/base" ) type CreateDto struct { @@ -39,15 +41,17 @@ type MetaDto struct { } type ResponseDto struct { - Id uint16 `json:"id"` - Province_Code string `json:"province_code"` - Code string `json:"code"` - Name string `json:"name"` - Districts []*ed.District `json:"districts,omitempty"` + erb.Basic + Province *ep.Province `json:"province,omitempty"` + Districts []*edb.Basic `json:"districts,omitempty"` } func (d Regency) ToResponse() ResponseDto { - resp := ResponseDto(d) + resp := ResponseDto{ + Basic: d.Basic, + Province: d.Province, + Districts: d.Districts, + } return resp } diff --git a/internal/domain/main-entities/regency/entity.go b/internal/domain/main-entities/regency/entity.go index 3bdcf934..bf58e37b 100644 --- a/internal/domain/main-entities/regency/entity.go +++ b/internal/domain/main-entities/regency/entity.go @@ -1,11 +1,13 @@ package regency -import ed "simrs-vx/internal/domain/main-entities/district" +import ( + edb "simrs-vx/internal/domain/main-entities/district/base" + ep "simrs-vx/internal/domain/main-entities/province" + erb "simrs-vx/internal/domain/main-entities/regency/base" +) type Regency struct { - Id uint16 `json:"id" gorm:"primaryKey"` - Province_Code string `json:"province_code" gorm:"size:2"` - Code string `json:"code" gorm:"unique;size:4"` - Name string `json:"name" gorm:"size:50"` - Districts []*ed.District `json:"districts,omitempty" gorm:"foreignKey:Regency_Code;references:Code"` + erb.Basic + Province *ep.Province `json:"province,omitempty" gorm:"foreignKey:Province_Code;references:Code"` + Districts []*edb.Basic `json:"districts,omitempty" gorm:"foreignKey:Regency_Code;references:Code"` } diff --git a/internal/domain/main-entities/village/base/entity.go b/internal/domain/main-entities/village/base/entity.go new file mode 100644 index 00000000..45cba2ba --- /dev/null +++ b/internal/domain/main-entities/village/base/entity.go @@ -0,0 +1,8 @@ +package base + +type Basic struct { + Id uint32 `json:"id" gorm:"primaryKey"` + District_Code string `json:"district_code" gorm:"size:6"` + Code string `json:"code" gorm:"unique;size:10"` + Name string `json:"name" gorm:"size:50"` +} diff --git a/internal/domain/main-entities/village/dto.go b/internal/domain/main-entities/village/dto.go index 148138cd..50f44a07 100644 --- a/internal/domain/main-entities/village/dto.go +++ b/internal/domain/main-entities/village/dto.go @@ -2,7 +2,9 @@ package village import ( ecore "simrs-vx/internal/domain/base-entities/core" - ep "simrs-vx/internal/domain/main-entities/postal-code" + ed "simrs-vx/internal/domain/main-entities/district" + epb "simrs-vx/internal/domain/main-entities/postal-code/base" + evb "simrs-vx/internal/domain/main-entities/village/base" ) type CreateDto struct { @@ -39,15 +41,17 @@ type MetaDto struct { } type ResponseDto struct { - Id uint32 `json:"id"` - District_Code string `json:"district_code"` - Code string `json:"code"` - Name string `json:"name"` - PostalCodes []ep.PostalCode `json:"postalCodes,omitempty"` + evb.Basic + District *ed.District `json:"district,omitempty"` + PostalCodes []epb.Basic `json:"postalCodes,omitempty"` } func (d Village) ToResponse() ResponseDto { - resp := ResponseDto(d) + resp := ResponseDto{ + Basic: d.Basic, + District: d.District, + PostalCodes: d.PostalCodes, + } return resp } diff --git a/internal/domain/main-entities/village/entity.go b/internal/domain/main-entities/village/entity.go index 7078534f..d7c549bc 100644 --- a/internal/domain/main-entities/village/entity.go +++ b/internal/domain/main-entities/village/entity.go @@ -1,13 +1,13 @@ package village import ( - ep "simrs-vx/internal/domain/main-entities/postal-code" + ed "simrs-vx/internal/domain/main-entities/district" + epb "simrs-vx/internal/domain/main-entities/postal-code/base" + evb "simrs-vx/internal/domain/main-entities/village/base" ) type Village struct { - Id uint32 `json:"id" gorm:"primaryKey"` - District_Code string `json:"district_code" gorm:"size:6"` // NOT: THE PROPER SIZE IS 6 - Code string `json:"code" gorm:"unique;size:10"` - Name string `json:"name" gorm:"size:50"` - PostalCodes []ep.PostalCode `json:"postalCodes,omitempty" gorm:"foreignKey:Village_Code;references:Code"` + evb.Basic + District *ed.District `json:"district,omitempty" gorm:"foreignKey:District_Code;references:Code"` + PostalCodes []epb.Basic `json:"postalCodes,omitempty" gorm:"foreignKey:Village_Code;references:Code"` } From 9a68982afa503011caaceae171ecaaa1e2886cf8 Mon Sep 17 00:00:00 2001 From: dpurbosakti Date: Thu, 9 Oct 2025 12:49:19 +0700 Subject: [PATCH 4/4] feat (postal-code): add crud for postal-code --- .../main-entities/district/base/entity.go | 4 + .../main-entities/postal-code/base/entity.go | 4 + .../domain/main-entities/postal-code/dto.go | 28 +- .../main-entities/regency/base/entitiy.go | 4 + .../main-entities/village/base/entity.go | 4 + .../interface/main-handler/main-handler.go | 2 + .../main-handler/postal-code/handler.go | 71 +++++ .../main-use-case/postal-code/case.go | 279 ++++++++++++++++++ .../main-use-case/postal-code/helper.go | 21 ++ .../use-case/main-use-case/postal-code/lib.go | 156 ++++++++++ .../postal-code/middleware-runner.go | 103 +++++++ .../main-use-case/postal-code/middleware.go | 9 + .../main-use-case/postal-code/tycovar.go | 44 +++ 13 files changed, 720 insertions(+), 9 deletions(-) create mode 100644 internal/interface/main-handler/postal-code/handler.go create mode 100644 internal/use-case/main-use-case/postal-code/case.go create mode 100644 internal/use-case/main-use-case/postal-code/helper.go create mode 100644 internal/use-case/main-use-case/postal-code/lib.go create mode 100644 internal/use-case/main-use-case/postal-code/middleware-runner.go create mode 100644 internal/use-case/main-use-case/postal-code/middleware.go create mode 100644 internal/use-case/main-use-case/postal-code/tycovar.go diff --git a/internal/domain/main-entities/district/base/entity.go b/internal/domain/main-entities/district/base/entity.go index c7429490..40af0fc3 100644 --- a/internal/domain/main-entities/district/base/entity.go +++ b/internal/domain/main-entities/district/base/entity.go @@ -6,3 +6,7 @@ type Basic struct { Code string `json:"code" gorm:"unique;size:6"` // NOTE: THE PROPER SIZE IS 6 Name string `json:"name" gorm:"size:50"` } + +func (Basic) TableName() string { + return "District" +} diff --git a/internal/domain/main-entities/postal-code/base/entity.go b/internal/domain/main-entities/postal-code/base/entity.go index 814e4e72..0ceeada7 100644 --- a/internal/domain/main-entities/postal-code/base/entity.go +++ b/internal/domain/main-entities/postal-code/base/entity.go @@ -5,3 +5,7 @@ type Basic struct { Village_Code string `json:"village_code" gorm:"size:10"` Code string `json:"code" gorm:"unique;size:5"` } + +func (Basic) TableName() string { + return "PostalCode" +} diff --git a/internal/domain/main-entities/postal-code/dto.go b/internal/domain/main-entities/postal-code/dto.go index cb1e3d39..ff32c827 100644 --- a/internal/domain/main-entities/postal-code/dto.go +++ b/internal/domain/main-entities/postal-code/dto.go @@ -2,18 +2,26 @@ package postalcode import ( ecore "simrs-vx/internal/domain/base-entities/core" + ev "simrs-vx/internal/domain/main-entities/village" ) type CreateDto struct { - Code string `json:"code" validate:"numeric;maxLength=10"` - Village_Code string `json:"district_code" validate:"numeric;maxLength=10"` + Code string `json:"code" validate:"numeric;maxLength=5"` + Village_Code string `json:"village_code" validate:"numeric;maxLength=10"` } type ReadListDto struct { - District_Code string `json:"district_code"` - Code string `json:"code"` - Name string `json:"name"` - Pagination ecore.Pagination + FilterDto + Includes string `json:"includes"` + Preloads []string `json:"-"` + Sort string `json:"sort"` + Pagination ecore.Pagination +} + +type FilterDto struct { + Village_Code string `json:"village-code"` + Code string `json:"code"` + Search string `json:"search" gormhelper:"searchColumns=Code"` } type ReadDetailDto struct { @@ -37,15 +45,17 @@ type MetaDto struct { } type ResponseDto struct { - Id uint32 `json:"id"` - Village_Code string `json:"village_code"` - Code string `json:"code"` + Id uint32 `json:"id"` + Village_Code string `json:"village_code"` + Village *ev.Village `json:"village,omitempty"` + Code string `json:"code"` } func (d PostalCode) ToResponse() ResponseDto { resp := ResponseDto{ Id: d.Id, Village_Code: d.Village_Code, + Village: d.Village, Code: d.Code, } return resp diff --git a/internal/domain/main-entities/regency/base/entitiy.go b/internal/domain/main-entities/regency/base/entitiy.go index b6c982ec..9e824923 100644 --- a/internal/domain/main-entities/regency/base/entitiy.go +++ b/internal/domain/main-entities/regency/base/entitiy.go @@ -6,3 +6,7 @@ type Basic struct { Code string `json:"code" gorm:"unique;size:4"` Name string `json:"name" gorm:"size:50"` } + +func (Basic) TableName() string { + return "Regency" +} diff --git a/internal/domain/main-entities/village/base/entity.go b/internal/domain/main-entities/village/base/entity.go index 45cba2ba..97f5347f 100644 --- a/internal/domain/main-entities/village/base/entity.go +++ b/internal/domain/main-entities/village/base/entity.go @@ -6,3 +6,7 @@ type Basic struct { Code string `json:"code" gorm:"unique;size:10"` Name string `json:"name" gorm:"size:50"` } + +func (Basic) TableName() string { + return "Village" +} diff --git a/internal/interface/main-handler/main-handler.go b/internal/interface/main-handler/main-handler.go index f718697d..0d9fd969 100644 --- a/internal/interface/main-handler/main-handler.go +++ b/internal/interface/main-handler/main-handler.go @@ -87,6 +87,7 @@ import ( uom "simrs-vx/internal/interface/main-handler/uom" district "simrs-vx/internal/interface/main-handler/district" + postalcode "simrs-vx/internal/interface/main-handler/postal-code" province "simrs-vx/internal/interface/main-handler/province" regency "simrs-vx/internal/interface/main-handler/regency" village "simrs-vx/internal/interface/main-handler/village" @@ -292,6 +293,7 @@ func SetRoutes() http.Handler { hc.RegCrud(r, "/v1/district", district.O) hc.RegCrud(r, "/v1/regency", regency.O) hc.RegCrud(r, "/v1/province", province.O) + hc.RegCrud(r, "/v1/postal-code", postalcode.O) ///// return cmw.SetCors(handlerlogger.SetLog(r)) diff --git a/internal/interface/main-handler/postal-code/handler.go b/internal/interface/main-handler/postal-code/handler.go new file mode 100644 index 00000000..d6c3dc1e --- /dev/null +++ b/internal/interface/main-handler/postal-code/handler.go @@ -0,0 +1,71 @@ +package postalcode + +import ( + "net/http" + + rw "github.com/karincake/risoles" + sf "github.com/karincake/semprit" + + // ua "github.com/karincake/tumpeng/auth/svc" + + e "simrs-vx/internal/domain/main-entities/postal-code" + u "simrs-vx/internal/use-case/main-use-case/postal-code" +) + +type myBase struct{} + +var O myBase + +func (obj myBase) Create(w http.ResponseWriter, r *http.Request) { + dto := e.CreateDto{} + if res := rw.ValidateStructByIOR(w, r.Body, &dto); !res { + return + } + res, err := u.Create(dto) + rw.DataResponse(w, res, err) +} + +func (obj myBase) GetList(w http.ResponseWriter, r *http.Request) { + dto := e.ReadListDto{} + sf.UrlQueryParam(&dto, *r.URL) + res, err := u.ReadList(dto) + rw.DataResponse(w, res, err) +} + +func (obj myBase) GetDetail(w http.ResponseWriter, r *http.Request) { + id := rw.ValidateInt(w, "id", r.PathValue("id")) + if id <= 0 { + return + } + dto := e.ReadDetailDto{} + dto.Id = uint32(id) + 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 { + return + } + + dto := e.UpdateDto{} + if res := rw.ValidateStructByIOR(w, r.Body, &dto); !res { + return + } + dto.Id = uint32(id) + 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 { + return + } + + dto := e.DeleteDto{} + dto.Id = uint32(id) + res, err := u.Delete(dto) + rw.DataResponse(w, res, err) +} diff --git a/internal/use-case/main-use-case/postal-code/case.go b/internal/use-case/main-use-case/postal-code/case.go new file mode 100644 index 00000000..0bdefae5 --- /dev/null +++ b/internal/use-case/main-use-case/postal-code/case.go @@ -0,0 +1,279 @@ +package postalcode + +import ( + e "simrs-vx/internal/domain/main-entities/postal-code" + "strconv" + + dg "github.com/karincake/apem/db-gorm-pg" + d "github.com/karincake/dodol" + + pl "simrs-vx/pkg/logger" + pu "simrs-vx/pkg/use-case-helper" + + "gorm.io/gorm" +) + +const source = "postal-code" + +func Create(input e.CreateDto) (*d.Data, error) { + data := e.PostalCode{} + + event := pl.Event{ + Feature: "Create", + Source: source, + } + + // Start log + pl.SetLogInfo(&event, input, "started", "create") + + err := dg.I.Transaction(func(tx *gorm.DB) error { + mwRunner := newMiddlewareRunner(&event, tx) + mwRunner.setMwType(pu.MWTPre) + // Run pre-middleware + if err := mwRunner.RunCreateMiddleware(createPreMw, &input, &data); err != nil { + return err + } + + if resData, err := CreateData(input, &event, tx); err != nil { + return err + } else { + data = *resData + } + + mwRunner.setMwType(pu.MWTPost) + // Run post-middleware + if err := mwRunner.RunCreateMiddleware(createPostMw, &input, &data); err != nil { + return err + } + + pl.SetLogInfo(&event, nil, "complete") + + return nil + }) + + if err != nil { + return nil, err + } + + return &d.Data{ + Meta: d.II{ + "source": source, + "structure": "single-data", + "status": "created", + }, + Data: data.ToResponse(), + }, nil +} + +func ReadList(input e.ReadListDto) (*d.Data, error) { + var data *e.PostalCode + var dataList []e.PostalCode + var metaList *e.MetaDto + var err error + + event := pl.Event{ + Feature: "ReadList", + Source: source, + } + + // Start log + pl.SetLogInfo(&event, input, "started", "readList") + + err = dg.I.Transaction(func(tx *gorm.DB) error { + mwRunner := newMiddlewareRunner(&event, tx) + mwRunner.setMwType(pu.MWTPre) + // Run pre-middleware + if err := mwRunner.RunReadListMiddleware(readListPreMw, &input, data); err != nil { + return err + } + + if len(input.Includes) > 0 { + input.Preloads = pu.GetPreloads(input.Includes) + } + if dataList, metaList, err = ReadListData(input, &event, tx); err != nil { + return err + } + + mwRunner.setMwType(pu.MWTPost) + // Run post-middleware + if err := mwRunner.RunReadListMiddleware(readListPostMw, &input, data); err != nil { + return err + } + + return nil + }) + + if err != nil { + return nil, err + } + + return &d.Data{ + Meta: d.IS{ + "source": source, + "structure": "list-data", + "status": "fetched", + "page_number": strconv.Itoa(metaList.PageNumber), + "page_size": strconv.Itoa(metaList.PageSize), + "record_totalCount": strconv.Itoa(metaList.Count), + "record_currentCount": strconv.Itoa(len(dataList)), + }, + Data: e.ToResponseList(dataList), + }, nil +} + +func ReadDetail(input e.ReadDetailDto) (*d.Data, error) { + var data *e.PostalCode + var err error + + event := pl.Event{ + Feature: "ReadDetail", + Source: source, + } + + // Start log + pl.SetLogInfo(&event, input, "started", "readDetail") + + err = dg.I.Transaction(func(tx *gorm.DB) error { + mwRunner := newMiddlewareRunner(&event, tx) + mwRunner.setMwType(pu.MWTPre) + // Run pre-middleware + if err := mwRunner.RunReadDetailMiddleware(readDetailPreMw, &input, data); err != nil { + return err + } + + if data, err = ReadDetailData(input, &event, tx); err != nil { + return err + } + + mwRunner.setMwType(pu.MWTPost) + // Run post-middleware + if err := mwRunner.RunReadDetailMiddleware(readDetailPostMw, &input, data); err != nil { + return err + } + + return nil + }) + + if err != nil { + return nil, err + } + + return &d.Data{ + Meta: d.IS{ + "source": source, + "structure": "single-data", + "status": "fetched", + }, + Data: data.ToResponse(), + }, nil +} + +func Update(input e.UpdateDto) (*d.Data, error) { + rdDto := e.ReadDetailDto{Id: input.Id} + var data *e.PostalCode + var err error + + event := pl.Event{ + Feature: "Update", + Source: source, + } + + // Start log + pl.SetLogInfo(&event, input, "started", "update") + + err = dg.I.Transaction(func(tx *gorm.DB) error { + pl.SetLogInfo(&event, rdDto, "started", "DBReadDetail") + if data, err = ReadDetailData(rdDto, &event, tx); err != nil { + return err + } + + mwRunner := newMiddlewareRunner(&event, tx) + mwRunner.setMwType(pu.MWTPre) + // Run pre-middleware + if err := mwRunner.RunUpdateMiddleware(readDetailPreMw, &rdDto, data); err != nil { + return err + } + + if err := UpdateData(input, data, &event, tx); err != nil { + return err + } + + pl.SetLogInfo(&event, nil, "complete") + + mwRunner.setMwType(pu.MWTPost) + // Run post-middleware + if err := mwRunner.RunUpdateMiddleware(readDetailPostMw, &rdDto, data); err != nil { + return err + } + + return nil + }) + + if err != nil { + return nil, err + } + + return &d.Data{ + Meta: d.IS{ + "source": source, + "structure": "single-data", + "status": "updated", + }, + Data: data.ToResponse(), + }, nil + +} + +func Delete(input e.DeleteDto) (*d.Data, error) { + rdDto := e.ReadDetailDto{Id: input.Id} + var data *e.PostalCode + var err error + + event := pl.Event{ + Feature: "Delete", + Source: source, + } + + // Start log + pl.SetLogInfo(&event, input, "started", "delete") + + err = dg.I.Transaction(func(tx *gorm.DB) error { + pl.SetLogInfo(&event, rdDto, "started", "DBReadDetail") + if data, err = ReadDetailData(rdDto, &event, tx); err != nil { + return err + } + + mwRunner := newMiddlewareRunner(&event, tx) + mwRunner.setMwType(pu.MWTPre) + // Run pre-middleware + if err := mwRunner.RunDeleteMiddleware(readDetailPreMw, &rdDto, data); err != nil { + return err + } + + if err := DeleteData(data, &event, tx); err != nil { + return err + } + + mwRunner.setMwType(pu.MWTPost) + // Run post-middleware + if err := mwRunner.RunDeleteMiddleware(readDetailPostMw, &rdDto, data); err != nil { + return err + } + + return nil + }) + + if err != nil { + return nil, err + } + + return &d.Data{ + Meta: d.IS{ + "source": source, + "structure": "single-data", + "status": "deleted", + }, + Data: data.ToResponse(), + }, nil + +} diff --git a/internal/use-case/main-use-case/postal-code/helper.go b/internal/use-case/main-use-case/postal-code/helper.go new file mode 100644 index 00000000..1217f5e9 --- /dev/null +++ b/internal/use-case/main-use-case/postal-code/helper.go @@ -0,0 +1,21 @@ +/* +DESCRIPTION: +Any functions that are used internally by the use-case +*/ +package postalcode + +import ( + e "simrs-vx/internal/domain/main-entities/postal-code" +) + +func setData[T *e.CreateDto | *e.UpdateDto](input T, data *e.PostalCode) { + var inputSrc *e.CreateDto + if inputT, ok := any(input).(*e.CreateDto); ok { + inputSrc = inputT + } else { + inputTemp := any(input).(*e.UpdateDto) + inputSrc = &inputTemp.CreateDto + } + data.Village_Code = inputSrc.Village_Code + data.Code = inputSrc.Code +} diff --git a/internal/use-case/main-use-case/postal-code/lib.go b/internal/use-case/main-use-case/postal-code/lib.go new file mode 100644 index 00000000..abead91a --- /dev/null +++ b/internal/use-case/main-use-case/postal-code/lib.go @@ -0,0 +1,156 @@ +package postalcode + +import ( + e "simrs-vx/internal/domain/main-entities/postal-code" + + plh "simrs-vx/pkg/lib-helper" + pl "simrs-vx/pkg/logger" + pu "simrs-vx/pkg/use-case-helper" + + dg "github.com/karincake/apem/db-gorm-pg" + gh "github.com/karincake/getuk" + "gorm.io/gorm" +) + +func CreateData(input e.CreateDto, event *pl.Event, dbx ...*gorm.DB) (*e.PostalCode, error) { + pl.SetLogInfo(event, nil, "started", "DBCreate") + + data := e.PostalCode{} + setData(&input, &data) + + var tx *gorm.DB + if len(dbx) > 0 { + tx = dbx[0] + } else { + tx = dg.I + } + + if err := tx.Create(&data).Error; err != nil { + return nil, plh.HandleCreateError(input, event, err) + } + + pl.SetLogInfo(event, nil, "complete") + return &data, nil +} + +func ReadListData(input e.ReadListDto, event *pl.Event, dbx ...*gorm.DB) ([]e.PostalCode, *e.MetaDto, error) { + pl.SetLogInfo(event, input, "started", "DBReadList") + data := []e.PostalCode{} + pagination := gh.Pagination{} + count := int64(0) + meta := e.MetaDto{} + + var tx *gorm.DB + if len(dbx) > 0 { + tx = dbx[0] + } else { + tx = dg.I + } + + if len(input.Preloads) > 0 { + for _, preload := range input.Preloads { + tx = tx.Preload(preload) + } + } + + tx = tx. + Model(&e.PostalCode{}). + Scopes(gh.Filter(input.FilterDto)). + Count(&count). + Scopes(gh.Paginate(input, &pagination)). + Scopes(gh.Sort(input.Sort)) + + if err := tx.Find(&data).Error; err != nil { + if err == gorm.ErrRecordNotFound { + return nil, &meta, nil + } + event.Status = "failed" + event.ErrInfo = pl.ErrorInfo{ + Code: "data-get-fail", + Detail: "Database get failed", + Raw: err, + } + return nil, nil, pl.SetLogError(event, input) + + } + meta.Count = int(count) + meta.PageNumber = pagination.PageNumber + meta.PageSize = pagination.PageSize + + pl.SetLogInfo(event, nil, "complete") + return data, &meta, nil +} + +func ReadDetailData(input e.ReadDetailDto, event *pl.Event, dbx ...*gorm.DB) (*e.PostalCode, error) { + pl.SetLogInfo(event, input, "started", "DBReadDetail") + data := e.PostalCode{} + + var tx *gorm.DB + if len(dbx) > 0 { + tx = dbx[0] + } else { + tx = dg.I + } + + tx = tx.Preload("Village"). + Preload("Village.District"). + Preload("Village.District.Regency"). + Preload("Village.District.Regency.Province") + + if err := tx.First(&data, input.Id).Error; err != nil { + if processedErr := pu.HandleReadError(err, event, source, input.Id, data); processedErr != nil { + return nil, processedErr + } + } + + pl.SetLogInfo(event, nil, "complete") + return &data, nil +} + +func UpdateData(input e.UpdateDto, data *e.PostalCode, event *pl.Event, dbx ...*gorm.DB) error { + pl.SetLogInfo(event, data, "started", "DBUpdate") + setData(&input, data) + + var tx *gorm.DB + if len(dbx) > 0 { + tx = dbx[0] + } else { + tx = dg.I + } + + if err := tx.Save(&data).Error; err != nil { + event.Status = "failed" + event.ErrInfo = pl.ErrorInfo{ + Code: "data-update-fail", + Detail: "Database update failed", + Raw: err, + } + return pl.SetLogError(event, input) + } + + pl.SetLogInfo(event, nil, "complete") + return nil +} + +func DeleteData(data *e.PostalCode, event *pl.Event, dbx ...*gorm.DB) error { + pl.SetLogInfo(event, data, "started", "DBDelete") + var tx *gorm.DB + if len(dbx) > 0 { + tx = dbx[0] + } else { + tx = dg.I + } + + if err := tx.Delete(&data).Error; err != nil { + event.Status = "failed" + event.ErrInfo = pl.ErrorInfo{ + Code: "data-delete-fail", + Detail: "Database delete failed", + Raw: err, + } + return pl.SetLogError(event, data) + } + + pl.SetLogInfo(event, nil, "complete") + return nil +} diff --git a/internal/use-case/main-use-case/postal-code/middleware-runner.go b/internal/use-case/main-use-case/postal-code/middleware-runner.go new file mode 100644 index 00000000..b06faf2b --- /dev/null +++ b/internal/use-case/main-use-case/postal-code/middleware-runner.go @@ -0,0 +1,103 @@ +package postalcode + +import ( + e "simrs-vx/internal/domain/main-entities/postal-code" + pl "simrs-vx/pkg/logger" + pu "simrs-vx/pkg/use-case-helper" + + "gorm.io/gorm" +) + +type middlewareRunner struct { + Event *pl.Event + Tx *gorm.DB + MwType pu.MWType +} + +// NewMiddlewareExecutor creates a new middleware executor +func newMiddlewareRunner(event *pl.Event, tx *gorm.DB) *middlewareRunner { + return &middlewareRunner{ + Event: event, + Tx: tx, + } +} + +// ExecuteCreateMiddleware executes create middleware +func (me *middlewareRunner) RunCreateMiddleware(middlewares []createMw, input *e.CreateDto, data *e.PostalCode) error { + for _, middleware := range middlewares { + logData := pu.GetLogData(input, data) + + pl.SetLogInfo(me.Event, logData, "started", middleware.Name) + + if err := middleware.Func(input, data, me.Tx); err != nil { + return pu.HandleMiddlewareError(me.Event, string(me.MwType), middleware.Name, logData, err) + } + + pl.SetLogInfo(me.Event, nil, "complete") + } + return nil +} + +func (me *middlewareRunner) RunReadListMiddleware(middlewares []readListMw, input *e.ReadListDto, data *e.PostalCode) error { + for _, middleware := range middlewares { + logData := pu.GetLogData(input, data) + + pl.SetLogInfo(me.Event, logData, "started", middleware.Name) + + if err := middleware.Func(input, data, me.Tx); err != nil { + return pu.HandleMiddlewareError(me.Event, string(me.MwType), middleware.Name, logData, err) + } + + pl.SetLogInfo(me.Event, nil, "complete") + } + return nil +} + +func (me *middlewareRunner) RunReadDetailMiddleware(middlewares []readDetailMw, input *e.ReadDetailDto, data *e.PostalCode) error { + for _, middleware := range middlewares { + logData := pu.GetLogData(input, data) + + pl.SetLogInfo(me.Event, logData, "started", middleware.Name) + + if err := middleware.Func(input, data, me.Tx); err != nil { + return pu.HandleMiddlewareError(me.Event, string(me.MwType), middleware.Name, logData, err) + } + + pl.SetLogInfo(me.Event, nil, "complete") + } + return nil +} + +func (me *middlewareRunner) RunUpdateMiddleware(middlewares []readDetailMw, input *e.ReadDetailDto, data *e.PostalCode) error { + for _, middleware := range middlewares { + logData := pu.GetLogData(input, data) + + pl.SetLogInfo(me.Event, logData, "started", middleware.Name) + + if err := middleware.Func(input, data, me.Tx); err != nil { + return pu.HandleMiddlewareError(me.Event, string(me.MwType), middleware.Name, logData, err) + } + + pl.SetLogInfo(me.Event, nil, "complete") + } + return nil +} + +func (me *middlewareRunner) RunDeleteMiddleware(middlewares []readDetailMw, input *e.ReadDetailDto, data *e.PostalCode) error { + for _, middleware := range middlewares { + logData := pu.GetLogData(input, data) + + pl.SetLogInfo(me.Event, logData, "started", middleware.Name) + + if err := middleware.Func(input, data, me.Tx); err != nil { + return pu.HandleMiddlewareError(me.Event, string(me.MwType), middleware.Name, logData, err) + } + + pl.SetLogInfo(me.Event, nil, "complete") + } + return nil +} + +func (me *middlewareRunner) setMwType(mwType pu.MWType) { + me.MwType = mwType +} diff --git a/internal/use-case/main-use-case/postal-code/middleware.go b/internal/use-case/main-use-case/postal-code/middleware.go new file mode 100644 index 00000000..a11c3e01 --- /dev/null +++ b/internal/use-case/main-use-case/postal-code/middleware.go @@ -0,0 +1,9 @@ +package postalcode + +// example of middleware +// func init() { +// createPreMw = append(createPreMw, +// CreateMw{Name: "modif-input", Func: pm.ModifInput}, +// CreateMw{Name: "check-data", Func: pm.CheckData}, +// ) +// } diff --git a/internal/use-case/main-use-case/postal-code/tycovar.go b/internal/use-case/main-use-case/postal-code/tycovar.go new file mode 100644 index 00000000..2c307323 --- /dev/null +++ b/internal/use-case/main-use-case/postal-code/tycovar.go @@ -0,0 +1,44 @@ +/* +DESCRIPTION: +A sample, part of the package that contains type, constants, and/or variables. + +In this sample it also provides type and variable regarding the needs of the +middleware to separate from main use-case which has the basic CRUD +functionality. The purpose of this is to make the code more maintainable. +*/ +package postalcode + +import ( + "gorm.io/gorm" + + e "simrs-vx/internal/domain/main-entities/postal-code" +) + +type createMw struct { + Name string + Func func(input *e.CreateDto, data *e.PostalCode, tx *gorm.DB) error +} + +type readListMw struct { + Name string + Func func(input *e.ReadListDto, data *e.PostalCode, tx *gorm.DB) error +} + +type readDetailMw struct { + Name string + Func func(input *e.ReadDetailDto, data *e.PostalCode, tx *gorm.DB) error +} + +type UpdateMw = readDetailMw +type DeleteMw = readDetailMw + +var createPreMw []createMw // preprocess middleware +var createPostMw []createMw // postprocess middleware +var readListPreMw []readListMw // .. +var readListPostMw []readListMw // .. +var readDetailPreMw []readDetailMw +var readDetailPostMw []readDetailMw +var updatePreMw []readDetailMw +var updatePostMw []readDetailMw +var deletePreMw []readDetailMw +var deletePostMw []readDetailMw