From 7adda83db0189e64a2a7cb3276ea6b5dc87911aa Mon Sep 17 00:00:00 2001 From: nathan Date: Mon, 5 Jul 2021 10:21:37 +0700 Subject: [PATCH 1/3] #65 add field status label and cover image url --- .env.example | 1 + config/config.go | 4 ++- config/model.go | 11 +++---- endpoint/phonebook.go | 4 ++- endpoint/response.go | 59 ++++++++++++++++++++++++-------------- model/response_usecase.go | 31 ++++++++++---------- usecase/function_helper.go | 28 +++++++++--------- usecase/phonebook.go | 6 +++- 8 files changed, 87 insertions(+), 57 deletions(-) diff --git a/.env.example b/.env.example index d8a465f..7832223 100644 --- a/.env.example +++ b/.env.example @@ -9,3 +9,4 @@ DB_USER=user DB_PASS=password DB_NAME=sapawarga DB_DRIVER_NAME=mysql +APP_STORAGE_PUBLIC_URL= \ No newline at end of file diff --git a/config/config.go b/config/config.go index 4bd20aa..5365bec 100644 --- a/config/config.go +++ b/config/config.go @@ -17,6 +17,7 @@ func NewConfig() (defConfig *Config, err error) { appGRPCPort, _ := strconv.Atoi(os.Getenv(`APP_GRPC_PORT`)) appHTTPPort, _ := strconv.Atoi(os.Getenv(`APP_HTTP_PORT`)) debugString := os.Getenv(`APP_DEBUG`) + appStorgePublicURL := os.Getenv(`APP_STORAGE_PUBLIC_URL`) debug := false if debugString == "true" { @@ -30,7 +31,7 @@ func NewConfig() (defConfig *Config, err error) { dbName := os.Getenv(`DB_NAME`) driverName := os.Getenv(`DB_DRIVER_NAME`) - if appEnv == "" || appGRPCPort == 0 || appHTTPPort == 0 { + if appEnv == "" || appGRPCPort == 0 || appHTTPPort == 0 || appStorgePublicURL == "" { err = fmt.Errorf("[CONFIG][Critical] Please check section APP on %s", envFileName) return } @@ -39,6 +40,7 @@ func NewConfig() (defConfig *Config, err error) { defConfig.AppGRPCPort = appGRPCPort defConfig.AppHTTPPort = appHTTPPort defConfig.Debug = debug + defConfig.AppStoragePublicURL = appStorgePublicURL if dbHost == "" || dbPort == 0 || dbUser == "" || dbName == "" || driverName == "" { err = fmt.Errorf("[CONFIG][Critical] Please check section DB on %s", envFileName) diff --git a/config/model.go b/config/model.go index bc38da1..8298be0 100644 --- a/config/model.go +++ b/config/model.go @@ -12,9 +12,10 @@ type DB struct { // Config ... type Config struct { - AppGRPCPort int `env:"APP_GRPC_PORT,required"` - AppHTTPPort int `env:"APP_HTTP_PORT,required"` - AppEnv string `env:"APP_ENV,required"` - Debug bool `env:"APP_DEBUG,required"` - DB *DB + AppGRPCPort int `env:"APP_GRPC_PORT,required"` + AppHTTPPort int `env:"APP_HTTP_PORT,required"` + AppEnv string `env:"APP_ENV,required"` + Debug bool `env:"APP_DEBUG,required"` + AppStoragePublicURL string `env:"APP_STORAGE_PUBLIC_URL"` + DB *DB } diff --git a/endpoint/phonebook.go b/endpoint/phonebook.go index 3affc7f..5c69024 100644 --- a/endpoint/phonebook.go +++ b/endpoint/phonebook.go @@ -2,6 +2,7 @@ package endpoint import ( "context" + "fmt" "encoding/json" @@ -76,8 +77,9 @@ func MakeGetDetail(ctx context.Context, usecase usecase.Provider) endpoint.Endpo Village: resp.Village, Latitude: resp.Latitude, Longitude: resp.Longitude, - CoverImagePath: resp.CoverImagePath, + CoverImagePath: fmt.Sprintf("%s/%s", cfg.AppStoragePublicURL, resp.CoverImagePath), Status: resp.Status, + StatusLabel: GetStatusLabel[resp.Status]["ina"], CreatedAt: resp.CreatedAt, UpdatedAt: resp.UpdatedAt, }, nil diff --git a/endpoint/response.go b/endpoint/response.go index 64039d5..d0acb54 100644 --- a/endpoint/response.go +++ b/endpoint/response.go @@ -2,8 +2,10 @@ package endpoint import ( "encoding/json" + "fmt" "time" + "github.com/sapawarga/phonebook-service/config" "github.com/sapawarga/phonebook-service/model" ) @@ -28,16 +30,18 @@ type Metadata struct { // Phonebook ... type Phonebook struct { - ID int64 `json:"id"` - PhoneNumbers []*PhoneNumber `json:"phone_numbers"` - Description string `json:"description"` - Name string `json:"name"` - Address string `json:"address"` - Latitude string `json:"latitude"` - Longitude string `json:"longitude"` - Status int64 `json:"status,omitempty"` - Category *model.Category `json:"category"` - Distance float64 `json:"distance,omitempty"` + ID int64 `json:"id"` + PhoneNumbers []*PhoneNumber `json:"phone_numbers"` + Description string `json:"description"` + Name string `json:"name"` + Address string `json:"address"` + Latitude string `json:"latitude"` + Longitude string `json:"longitude"` + Status int64 `json:"status,omitempty"` + StatusLabel string `json:"status_label,omitempty"` + CoverImageURL string `json:"cover_image_url,omitempty"` + Category *model.Category `json:"category"` + Distance float64 `json:"distance,omitempty"` } // PhonebookDetail ... @@ -53,8 +57,9 @@ type PhonebookDetail struct { Village *model.Location `json:"kelurahan,omitempty"` Latitude string `json:"latitude"` Longitude string `json:"longitude"` - CoverImagePath string `json:"cover_image_path"` + CoverImagePath string `json:"cover_image_url"` Status int64 `json:"status"` + StatusLabel string `json:"status_label"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` } @@ -65,6 +70,9 @@ type StatusResponse struct { Message string `json:"message"` } +var cfg, _ = config.NewConfig() + +// EncodePhonebook ... func EncodePhonebook(data []*model.Phonebook) []*Phonebook { result := make([]*Phonebook, 0) for _, v := range data { @@ -75,16 +83,18 @@ func EncodePhonebook(data []*model.Phonebook) []*Phonebook { } encodeData := &Phonebook{ - ID: v.ID, - PhoneNumbers: phoneNumbers, - Description: v.Description, - Name: v.Name, - Address: v.Address, - Latitude: v.Latitude, - Longitude: v.Longitude, - Status: v.Status, - Category: v.Category, - Distance: v.Distance, + ID: v.ID, + PhoneNumbers: phoneNumbers, + Description: v.Description, + Name: v.Name, + Address: v.Address, + CoverImageURL: fmt.Sprintf("%s/%s", cfg.AppStoragePublicURL, v.CoverImageURL), + Latitude: v.Latitude, + Longitude: v.Longitude, + Status: v.Status, + StatusLabel: GetStatusLabel[v.Status]["ina"], + Category: v.Category, + Distance: v.Distance, } result = append(result, encodeData) @@ -92,3 +102,10 @@ func EncodePhonebook(data []*model.Phonebook) []*Phonebook { return result } + +// GetStatusLabel ... +var GetStatusLabel = map[int64]map[string]string{ + -1: {"en": "status deleted", "ina": "Dihapus"}, + 0: {"en": "Not Active", "ina": "Tidak Aktif"}, + 10: {"en": "Active", "ina": "Aktif"}, +} diff --git a/model/response_usecase.go b/model/response_usecase.go index a834ced..1027fc8 100644 --- a/model/response_usecase.go +++ b/model/response_usecase.go @@ -17,21 +17,22 @@ type Metadata struct { // Phonebook ... type Phonebook struct { - ID int64 `json:"id"` - PhoneNumbers string `json:"phone_numbers"` - Description string `json:"description"` - Name string `json:"name"` - Address string `json:"address"` - Latitude string `json:"latitude"` - Longitude string `json:"longitude"` - RegencyID int64 `json:"kabkota_id,omitempty"` - DistrictID int64 `json:"kec_id,omitempty"` - VillageID int64 `json:"kel_id,omitempty"` - Status int64 `json:"status,omitempty"` - Category *Category `json:"category"` - Distance float64 `json:"distance,omitempty"` - CreatedAt time.Time `json:"created_at,omitempty"` - UpdatedAt time.Time `json:"updated_at,omitempty"` + ID int64 `json:"id"` + PhoneNumbers string `json:"phone_numbers"` + Description string `json:"description"` + Name string `json:"name"` + Address string `json:"address"` + Latitude string `json:"latitude"` + Longitude string `json:"longitude"` + RegencyID int64 `json:"kabkota_id,omitempty"` + CoverImageURL string `json:"cover_image_url,omitempty"` + DistrictID int64 `json:"kec_id,omitempty"` + VillageID int64 `json:"kel_id,omitempty"` + Status int64 `json:"status,omitempty"` + Category *Category `json:"category"` + Distance float64 `json:"distance,omitempty"` + CreatedAt time.Time `json:"created_at,omitempty"` + UpdatedAt time.Time `json:"updated_at,omitempty"` } // PhonebookDetail ... diff --git a/usecase/function_helper.go b/usecase/function_helper.go index 0767946..37c0881 100644 --- a/usecase/function_helper.go +++ b/usecase/function_helper.go @@ -3,6 +3,7 @@ package usecase import ( "context" "database/sql" + "fmt" "math" "github.com/sapawarga/phonebook-service/model" @@ -55,19 +56,20 @@ func (pb *PhoneBook) appendResultGetList(ctx context.Context, result []*model.Ph } for _, v := range result { result := &model.Phonebook{ - ID: v.ID, - PhoneNumbers: v.PhoneNumbers.String, - Description: v.Description.String, - Name: v.Name.String, - Address: v.Address.String, - Latitude: v.Latitude.String, - Longitude: v.Longitude.String, - Status: v.Status.Int64, - RegencyID: v.RegencyID.Int64, - DistrictID: v.DistrictID.Int64, - VillageID: v.VillageID.Int64, - CreatedAt: v.CreatedAt.Time, - UpdatedAt: v.UpdatedAt.Time, + ID: v.ID, + PhoneNumbers: v.PhoneNumbers.String, + Description: v.Description.String, + Name: v.Name.String, + Address: v.Address.String, + Latitude: v.Latitude.String, + Longitude: v.Longitude.String, + CoverImageURL: fmt.Sprintf("%s/%s", cfg.AppStoragePublicURL, v.CoverImagePath.String), + Status: v.Status.Int64, + RegencyID: v.RegencyID.Int64, + DistrictID: v.DistrictID.Int64, + VillageID: v.VillageID.Int64, + CreatedAt: v.CreatedAt.Time, + UpdatedAt: v.UpdatedAt.Time, } if v.CategoryID.Valid { categoryName, err := pb.repo.GetCategoryNameByID(ctx, v.CategoryID.Int64) diff --git a/usecase/phonebook.go b/usecase/phonebook.go index fa90264..bf66a20 100644 --- a/usecase/phonebook.go +++ b/usecase/phonebook.go @@ -3,7 +3,9 @@ package usecase import ( "context" "errors" + "fmt" + "github.com/sapawarga/phonebook-service/config" "github.com/sapawarga/phonebook-service/helper" "github.com/sapawarga/phonebook-service/model" "github.com/sapawarga/phonebook-service/repository" @@ -18,6 +20,8 @@ type PhoneBook struct { logger kitlog.Logger } +var cfg, _ = config.NewConfig() + // NewPhoneBook ... func NewPhoneBook(repo repository.PhoneBookI, logger kitlog.Logger) *PhoneBook { return &PhoneBook{ @@ -79,7 +83,7 @@ func (pb *PhoneBook) GetDetail(ctx context.Context, id int64) (*model.PhonebookD PhoneNumbers: resp.PhoneNumbers.String, Latitude: resp.Latitude.String, Longitude: resp.Longitude.String, - CoverImagePath: resp.CoverImagePath.String, + CoverImagePath: fmt.Sprintf("%s/%s", cfg.AppStoragePublicURL, resp.CoverImagePath.String), Status: resp.Status.Int64, CreatedAt: resp.CreatedAt.Time, UpdatedAt: resp.UpdatedAt.Time, From 5ee077bf6b24dbab610a43d46a7ab6ff7b62c070 Mon Sep 17 00:00:00 2001 From: nathan Date: Mon, 5 Jul 2021 12:05:27 +0700 Subject: [PATCH 2/3] #65 using ISO 639-1 code --- endpoint/response.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/endpoint/response.go b/endpoint/response.go index d0acb54..bdf78fd 100644 --- a/endpoint/response.go +++ b/endpoint/response.go @@ -105,7 +105,7 @@ func EncodePhonebook(data []*model.Phonebook) []*Phonebook { // GetStatusLabel ... var GetStatusLabel = map[int64]map[string]string{ - -1: {"en": "status deleted", "ina": "Dihapus"}, - 0: {"en": "Not Active", "ina": "Tidak Aktif"}, - 10: {"en": "Active", "ina": "Aktif"}, + -1: {"en": "status deleted", "id": "Dihapus"}, + 0: {"en": "Not Active", "id": "Tidak Aktif"}, + 10: {"en": "Active", "id": "Aktif"}, } From 1bee6fc8069d9786739b687513e6d9c018ff9eff Mon Sep 17 00:00:00 2001 From: nathan Date: Mon, 5 Jul 2021 12:31:08 +0700 Subject: [PATCH 3/3] #65 some leftover --- endpoint/phonebook.go | 2 +- endpoint/response.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/endpoint/phonebook.go b/endpoint/phonebook.go index 5c69024..01dce31 100644 --- a/endpoint/phonebook.go +++ b/endpoint/phonebook.go @@ -79,7 +79,7 @@ func MakeGetDetail(ctx context.Context, usecase usecase.Provider) endpoint.Endpo Longitude: resp.Longitude, CoverImagePath: fmt.Sprintf("%s/%s", cfg.AppStoragePublicURL, resp.CoverImagePath), Status: resp.Status, - StatusLabel: GetStatusLabel[resp.Status]["ina"], + StatusLabel: GetStatusLabel[resp.Status]["id"], CreatedAt: resp.CreatedAt, UpdatedAt: resp.UpdatedAt, }, nil diff --git a/endpoint/response.go b/endpoint/response.go index bdf78fd..b07e7a9 100644 --- a/endpoint/response.go +++ b/endpoint/response.go @@ -92,7 +92,7 @@ func EncodePhonebook(data []*model.Phonebook) []*Phonebook { Latitude: v.Latitude, Longitude: v.Longitude, Status: v.Status, - StatusLabel: GetStatusLabel[v.Status]["ina"], + StatusLabel: GetStatusLabel[v.Status]["id"], Category: v.Category, Distance: v.Distance, }