Skip to content

Commit

Permalink
Merge pull request #66 from sapawarga/issue65
Browse files Browse the repository at this point in the history
#65 add field status label and cover image url
  • Loading branch information
setiadijoe authored Jul 5, 2021
2 parents a7debe5 + 1bee6fc commit 218fbad
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 57 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ DB_USER=user
DB_PASS=password
DB_NAME=sapawarga
DB_DRIVER_NAME=mysql
APP_STORAGE_PUBLIC_URL=
4 changes: 3 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand All @@ -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
}
Expand All @@ -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)
Expand Down
11 changes: 6 additions & 5 deletions config/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 3 additions & 1 deletion endpoint/phonebook.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package endpoint

import (
"context"
"fmt"

"encoding/json"

Expand Down Expand Up @@ -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]["id"],
CreatedAt: resp.CreatedAt,
UpdatedAt: resp.UpdatedAt,
}, nil
Expand Down
59 changes: 38 additions & 21 deletions endpoint/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package endpoint

import (
"encoding/json"
"fmt"
"time"

"github.com/sapawarga/phonebook-service/config"
"github.com/sapawarga/phonebook-service/model"
)

Expand All @@ -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 ...
Expand All @@ -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"`
}
Expand All @@ -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 {
Expand All @@ -75,20 +83,29 @@ 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]["id"],
Category: v.Category,
Distance: v.Distance,
}

result = append(result, encodeData)
}

return result
}

// GetStatusLabel ...
var GetStatusLabel = map[int64]map[string]string{
-1: {"en": "status deleted", "id": "Dihapus"},
0: {"en": "Not Active", "id": "Tidak Aktif"},
10: {"en": "Active", "id": "Aktif"},
}
31 changes: 16 additions & 15 deletions model/response_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ...
Expand Down
28 changes: 15 additions & 13 deletions usecase/function_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package usecase
import (
"context"
"database/sql"
"fmt"
"math"

"github.com/sapawarga/phonebook-service/model"
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion usecase/phonebook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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{
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 218fbad

Please sign in to comment.