diff --git a/endpoint/phonebook.go b/endpoint/phonebook.go index 5cfb239..889b9a1 100644 --- a/endpoint/phonebook.go +++ b/endpoint/phonebook.go @@ -3,6 +3,8 @@ package endpoint import ( "context" + "encoding/json" + "github.com/sapawarga/phonebook-service/helper" "github.com/sapawarga/phonebook-service/model" "github.com/sapawarga/phonebook-service/usecase" @@ -32,13 +34,15 @@ func MakeGetList(ctx context.Context, usecase usecase.Provider) endpoint.Endpoin return nil, err } + phonebooks := EncodePhonebook(resp.PhoneBooks) + meta := &Metadata{ Page: resp.Page, Total: resp.Total, } return &PhoneBookWithMeta{ - Data: resp.PhoneBooks, + Data: phonebooks, Metadata: meta, }, nil } @@ -53,6 +57,11 @@ func MakeGetDetail(ctx context.Context, usecase usecase.Provider) endpoint.Endpo return nil, err } + phoneNumbers := []*PhoneNumber{} + if err := json.Unmarshal([]byte(resp.PhoneNumbers), &phoneNumbers); err != nil { + return nil, err + } + return &PhonebookDetail{ ID: resp.ID, Name: resp.Name, @@ -60,7 +69,7 @@ func MakeGetDetail(ctx context.Context, usecase usecase.Provider) endpoint.Endpo CategoryName: resp.CategoryName, Address: resp.Address, Description: resp.Description, - PhoneNumbers: resp.PhoneNumbers, + PhoneNumbers: phoneNumbers, RegencyID: helper.SetPointerInt64(resp.RegencyID), RegencyName: resp.RegencyName, DistrictID: helper.SetPointerInt64(resp.DistrictID), @@ -81,9 +90,10 @@ func MakeGetDetail(ctx context.Context, usecase usecase.Provider) endpoint.Endpo func MakeAddPhonebook(ctx context.Context, usecase usecase.Provider) endpoint.Endpoint { return func(ctx context.Context, request interface{}) (response interface{}, err error) { req := request.(*AddPhonebookRequest) + phoneNumbers, _ := json.Marshal(req.PhoneNumbers) if err := usecase.Insert(ctx, &model.AddPhonebook{ Name: req.Name, - PhoneNumbers: req.PhoneNumbers, + PhoneNumbers: helper.SetPointerString(string(phoneNumbers)), Address: req.Address, Description: req.Description, RegencyID: req.RegencyID, @@ -110,10 +120,11 @@ func MakeAddPhonebook(ctx context.Context, usecase usecase.Provider) endpoint.En func MakeUpdatePhonebook(ctx context.Context, usecase usecase.Provider) endpoint.Endpoint { return func(ctx context.Context, request interface{}) (response interface{}, err error) { req := request.(*UpdatePhonebookRequest) + phoneNumbers, _ := json.Marshal(req.PhoneNumbers) if err := usecase.Update(ctx, &model.UpdatePhonebook{ ID: req.ID, Name: req.Name, - PhoneNumbers: req.PhoneNumbers, + PhoneNumbers: helper.SetPointerString(string(phoneNumbers)), Address: req.Address, Description: req.Description, RegencyID: req.RegencyID, diff --git a/endpoint/request.go b/endpoint/request.go index 0343ae6..a3ebea9 100644 --- a/endpoint/request.go +++ b/endpoint/request.go @@ -15,18 +15,18 @@ type GetListRequest struct { // AddPhonebookRequest ... type AddPhonebookRequest struct { - Name string `json:"name"` - Description *string `json:"description"` - PhoneNumbers *string `json:"phone_numbers"` - RegencyID *int64 `json:"regency_id"` - DistrictID *int64 `json:"district_id"` - VillageID *int64 `json:"village_id"` - Status *int64 `json:"status"` - Latitude *string `json:"latitude"` - Longitude *string `json:"longitude"` - CategoryID *int64 `json:"category_id"` - CoverImagePath *string `json:"cover_image_path"` - Address *string `json:"address"` + Name string `json:"name"` + Description *string `json:"description"` + PhoneNumbers []*PhoneNumber `json:"phone_numbers"` + RegencyID *int64 `json:"regency_id"` + DistrictID *int64 `json:"district_id"` + VillageID *int64 `json:"village_id"` + Status *int64 `json:"status"` + Latitude *string `json:"latitude"` + Longitude *string `json:"longitude"` + CategoryID *int64 `json:"category_id"` + CoverImagePath *string `json:"cover_image_path"` + Address *string `json:"address"` } // GetDetailRequest ... @@ -36,17 +36,23 @@ type GetDetailRequest struct { // UpdatePhonebookRequest ... type UpdatePhonebookRequest struct { - ID int64 `json:"id"` - Name string `json:"name"` - Description *string `json:"description"` - PhoneNumbers *string `json:"phone_numbers"` - RegencyID *int64 `json:"regency_id"` - DistrictID *int64 `json:"district_id"` - VillageID *int64 `json:"village_id"` - Status *int64 `json:"status"` - Latitude *string `json:"latitude"` - Longitude *string `json:"longitude"` - CategoryID *int64 `json:"category_id"` - CoverImagePath *string `json:"cover_image_path"` - Address *string `json:"address"` + ID int64 `json:"id"` + Name string `json:"name"` + Description *string `json:"description"` + PhoneNumbers []*PhoneNumber `json:"phone_numbers"` + RegencyID *int64 `json:"regency_id"` + DistrictID *int64 `json:"district_id"` + VillageID *int64 `json:"village_id"` + Status *int64 `json:"status"` + Latitude *string `json:"latitude"` + Longitude *string `json:"longitude"` + CategoryID *int64 `json:"category_id"` + CoverImagePath *string `json:"cover_image_path"` + Address *string `json:"address"` +} + +// PhoneNumber ... +type PhoneNumber struct { + PhoneNumber string `json:"phone_number"` + Type string `json:"type"` } diff --git a/endpoint/response.go b/endpoint/response.go index 55bc91e..6c79f18 100644 --- a/endpoint/response.go +++ b/endpoint/response.go @@ -1,6 +1,8 @@ package endpoint import ( + "encoding/json" + "fmt" "time" "github.com/sapawarga/phonebook-service/model" @@ -8,8 +10,22 @@ import ( // PhoneBookWithMeta ... type PhoneBookWithMeta struct { - Data []*model.Phonebook `json:"data"` - Metadata *Metadata `json:"metadata"` + Data []*Phonebook `json:"data"` + Metadata *Metadata `json:"metadata"` +} + +// 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 string `json:"category_name"` + Distance float64 `json:"distance,omitempty"` } // Metadata ... @@ -20,25 +36,25 @@ type Metadata struct { // PhonebookDetail ... type PhonebookDetail struct { - ID int64 `json:"id"` - Name string `json:"name"` - CategoryID int64 `json:"category_id"` - CategoryName string `json:"category_name"` - Address string `json:"address"` - Description string `json:"description"` - PhoneNumbers string `json:"phone_numbers"` - RegencyID *int64 `json:"regency_id,omitempty"` - RegencyName *string `json:"regency_name,omitempty"` - DistrictID *int64 `json:"district_id,omitempty"` - DistrictName *string `json:"district_name,omitempty"` - VillageID *int64 `json:"village_id,omitempty"` - VillageName *string `json:"village_name,omitempty"` - Latitude string `json:"latitude"` - Longitude string `json:"longitude"` - CoverImagePath string `json:"cover_image_path"` - Status int64 `json:"status"` - CreatedAt time.Time `json:"created_at"` - UpdatedAt time.Time `json:"updated_at"` + ID int64 `json:"id"` + Name string `json:"name"` + CategoryID int64 `json:"category_id"` + CategoryName string `json:"category_name"` + Address string `json:"address"` + Description string `json:"description"` + PhoneNumbers []*PhoneNumber `json:"phone_numbers"` + RegencyID *int64 `json:"regency_id,omitempty"` + RegencyName *string `json:"regency_name,omitempty"` + DistrictID *int64 `json:"district_id,omitempty"` + DistrictName *string `json:"district_name,omitempty"` + VillageID *int64 `json:"village_id,omitempty"` + VillageName *string `json:"village_name,omitempty"` + Latitude string `json:"latitude"` + Longitude string `json:"longitude"` + CoverImagePath string `json:"cover_image_path"` + Status int64 `json:"status"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` } // StatusResponse ... @@ -46,3 +62,31 @@ type StatusResponse struct { Code string `json:"code"` Message string `json:"message"` } + +func EncodePhonebook(data []*model.Phonebook) []*Phonebook { + result := make([]*Phonebook, 0) + for _, v := range data { + + phoneNumbers := []*PhoneNumber{} + if err := json.Unmarshal([]byte(v.PhoneNumbers), &phoneNumbers); err != nil { + fmt.Println("error unmarshal", err) + return nil + } + 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, + } + + result = append(result, encodeData) + } + + return result +} diff --git a/transport/grpc/transport.go b/transport/grpc/transport.go index eee003b..35c60a6 100644 --- a/transport/grpc/transport.go +++ b/transport/grpc/transport.go @@ -2,6 +2,7 @@ package grpc import ( "context" + "encoding/json" "github.com/sapawarga/phonebook-service/endpoint" "github.com/sapawarga/phonebook-service/helper" @@ -78,9 +79,11 @@ func encodeGetListResponse(ctx context.Context, r interface{}) (interface{}, err resultData := make([]*transportPhonebook.PhoneBook, 0) for _, v := range data { + phoneString, _ := json.Marshal(v.PhoneNumbers) + result := &transportPhonebook.PhoneBook{ Id: v.ID, - PhoneNumbers: v.PhoneNumbers, + PhoneNumbers: string(phoneString), Description: v.Description, Name: v.Name, Address: v.Address, @@ -111,9 +114,10 @@ func decodeGetDetailRequest(ctx context.Context, r interface{}) (interface{}, er func encodeGetDetailResponse(ctx context.Context, r interface{}) (interface{}, error) { resp := r.(*endpoint.PhonebookDetail) + phoneString, _ := json.Marshal(resp.PhoneNumbers) return &transportPhonebook.GetDetailResponse{ Id: resp.ID, - PhoneNumbers: resp.PhoneNumbers, + PhoneNumbers: string(phoneString), Description: resp.Description, Name: resp.Name, Address: resp.Address, @@ -135,9 +139,14 @@ func encodeGetDetailResponse(ctx context.Context, r interface{}) (interface{}, e func decodeAddPhonebookRequest(ctx context.Context, r interface{}) (interface{}, error) { req := r.(*transportPhonebook.AddPhonebookRequest) + phoneNumbers := []*endpoint.PhoneNumber{} + err := json.Unmarshal([]byte(req.GetPhoneNumbers()), &phoneNumbers) + if err != nil { + return nil, err + } request := &endpoint.AddPhonebookRequest{ Name: req.GetName(), - PhoneNumbers: helper.SetPointerString(req.GetPhoneNumbers()), + PhoneNumbers: phoneNumbers, } if req.Address != "" { request.Address = helper.SetPointerString(req.GetAddress()) @@ -181,7 +190,12 @@ func decodeUpdatePhonebookRequest(ctx context.Context, r interface{}) (interface Name: req.GetName(), } if req.PhoneNumbers != "" { - request.PhoneNumbers = helper.SetPointerString(req.GetPhoneNumbers()) + phoneNumbers := []*endpoint.PhoneNumber{} + err := json.Unmarshal([]byte(req.GetPhoneNumbers()), &phoneNumbers) + if err != nil { + return nil, err + } + request.PhoneNumbers = phoneNumbers } if req.Address != "" { request.Address = helper.SetPointerString(req.GetAddress())