Skip to content

Commit

Permalink
Merge pull request #54 from sapawarga/update-response-json
Browse files Browse the repository at this point in the history
add encoded phone number for response
  • Loading branch information
setiadijoe authored Jun 11, 2021
2 parents b1fe5c3 + 4ea45ab commit 0f15cd6
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 54 deletions.
19 changes: 15 additions & 4 deletions endpoint/phonebook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand All @@ -53,14 +57,19 @@ 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,
CategoryID: resp.CategoryID,
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),
Expand All @@ -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,
Expand All @@ -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,
Expand Down
56 changes: 31 additions & 25 deletions endpoint/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ...
Expand All @@ -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"`
}
86 changes: 65 additions & 21 deletions endpoint/response.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
package endpoint

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

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

// 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 ...
Expand All @@ -20,29 +36,57 @@ 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 ...
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
}
22 changes: 18 additions & 4 deletions transport/grpc/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package grpc

import (
"context"
"encoding/json"

"github.com/sapawarga/phonebook-service/endpoint"
"github.com/sapawarga/phonebook-service/helper"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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())
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit 0f15cd6

Please sign in to comment.