Skip to content

Commit

Permalink
#70 fixed query for update and get list object
Browse files Browse the repository at this point in the history
  • Loading branch information
setiadijoe committed Jul 23, 2021
1 parent 7bf2c95 commit b5a763d
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 46 deletions.
22 changes: 21 additions & 1 deletion endpoint/phonebook.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ func MakeGetDetail(ctx context.Context, usecase usecase.Provider) endpoint.Endpo
return nil, err
}

category, ok := resp.Category.(*model.Category)

data := &PhonebookDetail{
ID: resp.ID,
Name: resp.Name,
Expand All @@ -101,6 +103,21 @@ func MakeGetDetail(ctx context.Context, usecase usecase.Provider) endpoint.Endpo
CreatedAt: resp.CreatedAt,
UpdatedAt: resp.UpdatedAt,
}
if ok {
data.CategoryID = category.ID
}
if resp.Regency != nil {
data.RegencyID = helper.SetPointerInt64(resp.Regency.ID)
}
if resp.District != nil {
data.DistrictID = helper.SetPointerInt64(resp.District.ID)
}
if resp.Village != nil {
data.VillageID = helper.SetPointerInt64(resp.Village.ID)
}
if len(phoneNumbers) == 0 {
data.PhoneNumbers = nil
}
return map[string]interface{}{
"data": data,
}, nil
Expand Down Expand Up @@ -142,7 +159,10 @@ 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)
var phoneNumbers []byte
if len(req.PhoneNumbers) > 0 {
phoneNumbers, _ = json.Marshal(req.PhoneNumbers)
}
if err := usecase.Update(ctx, &model.UpdatePhonebook{
ID: req.ID,
Name: req.Name,
Expand Down
23 changes: 17 additions & 6 deletions endpoint/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type Metadata struct {

// Phonebook ...
type Phonebook struct {
ID int64 `json:"id"`
ID interface{} `json:"id"`
PhoneNumbers []*PhoneNumber `json:"phone_numbers"`
Description *string `json:"description"`
Name *string `json:"name"`
Expand All @@ -41,9 +41,9 @@ type Phonebook struct {
StatusLabel string `json:"status_label"`
CoverImageURL *string `json:"cover_image_url"`
CategoryID *int64 `json:"category_id"`
Category *model.Category `json:"category"`
Category interface{} `json:"category"`
Sequence int64 `json:"seq"`
Distance float64 `json:"distance,omitempty"`
Distance interface{} `json:"distance,omitempty"`
RegencyID *int64 `json:"kabkota_id"`
Regency *model.Location `json:"kabkota"`
DistrictID *int64 `json:"kec_id"`
Expand All @@ -56,14 +56,18 @@ type Phonebook struct {

// PhonebookDetail ...
type PhonebookDetail struct {
ID int64 `json:"id"`
ID interface{} `json:"id"`
Name string `json:"name"`
Category *model.Category `json:"category"`
CategoryID int64 `json:"category_id"`
Category interface{} `json:"category"`
Address string `json:"address"`
Description string `json:"description"`
PhoneNumbers []*PhoneNumber `json:"phone_numbers"`
RegencyID *int64 `json:"kabkota_id"`
Regency *model.Location `json:"kabkota"`
DistrictID *int64 `json:"kec_id"`
District *model.Location `json:"kecamatan"`
VillageID *int64 `json:"kel_id"`
Village *model.Location `json:"kelurahan"`
Latitude string `json:"latitude"`
Longitude string `json:"longitude"`
Expand Down Expand Up @@ -93,6 +97,8 @@ func EncodePhonebook(data []*model.Phonebook) []*Phonebook {
_ = json.Unmarshal([]byte(v.PhoneNumbers), &phoneNumbers)
}

category, ok := v.Category.(*model.Category)

encodeData := &Phonebook{
ID: v.ID,
PhoneNumbers: phoneNumbers,
Expand All @@ -104,7 +110,6 @@ func EncodePhonebook(data []*model.Phonebook) []*Phonebook {
Longitude: helper.SetPointerString(v.Longitude),
Status: v.Status,
StatusLabel: GetStatusLabel[v.Status]["id"],
CategoryID: helper.SetPointerInt64(v.Category.ID),
Category: v.Category,
Distance: v.Distance,
Sequence: v.Sequence,
Expand All @@ -114,6 +119,9 @@ func EncodePhonebook(data []*model.Phonebook) []*Phonebook {
District: v.District,
Village: v.Village,
}
if ok {
encodeData.CategoryID = helper.SetPointerInt64(category.ID)
}
if v.Regency != nil {
encodeData.RegencyID = helper.SetPointerInt64(v.Regency.ID)
}
Expand All @@ -123,6 +131,9 @@ func EncodePhonebook(data []*model.Phonebook) []*Phonebook {
if v.Village != nil {
encodeData.VillageID = helper.SetPointerInt64(v.Village.ID)
}
if len(phoneNumbers) == 0 {
encodeData.PhoneNumbers = nil
}

result = append(result, encodeData)
}
Expand Down
34 changes: 17 additions & 17 deletions model/response_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@ 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"`
CoverImageURL string `json:"cover_image_url"`
Regency *Location `json:"kabkota"`
District *Location `json:"kecamatan"`
Village *Location `json:"kelurahan"`
Status int64 `json:"status"`
Sequence int64 `json:"seq"`
Category *Category `json:"category"`
Distance float64 `json:"distance,omitempty"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
ID interface{} `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"`
CoverImageURL string `json:"cover_image_url"`
Regency *Location `json:"kabkota"`
District *Location `json:"kecamatan"`
Village *Location `json:"kelurahan"`
Status int64 `json:"status"`
Sequence int64 `json:"seq"`
Category interface{} `json:"category"`
Distance interface{} `json:"distance,omitempty"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
}

type Category struct {
Expand Down
39 changes: 23 additions & 16 deletions repository/mysql/query_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,59 +150,61 @@ func querySelectParams(ctx context.Context, query bytes.Buffer, params *model.Ge
func queryUpdateParams(ctx context.Context, params *model.UpdatePhonebook, queryParams map[string]interface{}) (bytes.Buffer, map[string]interface{}) {
var query bytes.Buffer
_, unixTime := helper.GetCurrentTimeUTC()
var fields []string
if params.Address != nil {
query.WriteString(" address = :address ")
fields = append(fields, "address")
queryParams["address"] = helper.GetStringFromPointer(params.Address)
}
if params.CategoryID != nil {
query.WriteString(updateNext(ctx, "category_id"))
fields = append(fields, "category_id")
queryParams["category_id"] = helper.GetInt64FromPointer(params.CategoryID)
}
if params.CoverImagePath != nil {
query.WriteString(updateNext(ctx, "cover_image_path"))
fields = append(fields, "cover_image_path")
queryParams["cover_image_path"] = helper.GetStringFromPointer(params.CoverImagePath)
}
if params.Description != nil {
query.WriteString(updateNext(ctx, "description"))
fields = append(fields, "description")
queryParams["description"] = helper.GetStringFromPointer(params.Description)
}
if params.DistrictID != nil {
query.WriteString(updateNext(ctx, "kec_id"))
fields = append(fields, "kec_id")
queryParams["kec_id"] = helper.GetInt64FromPointer(params.DistrictID)
}
if params.Latitude != nil {
query.WriteString(updateNext(ctx, "latitude"))
fields = append(fields, "latitude")
queryParams["latitude"] = helper.GetStringFromPointer(params.Latitude)
}
if params.Longitude != nil {
query.WriteString(updateNext(ctx, "longitude"))
fields = append(fields, "longitude")
queryParams["longitude"] = helper.GetStringFromPointer(params.Longitude)
}
if params.Name != "" {
query.WriteString(updateNext(ctx, "name"))
fields = append(fields, "name")
queryParams["name"] = params.Name
}
if params.PhoneNumbers != nil {
query.WriteString(updateNext(ctx, "phone_numbers"))
fields = append(fields, "phone_numbers")
queryParams["phone_numbers"] = helper.GetStringFromPointer(params.PhoneNumbers)
}
if params.RegencyID != nil {
query.WriteString(updateNext(ctx, "kabkota_id"))
fields = append(fields, "kabkota_id")
queryParams["kabkota_id"] = helper.GetInt64FromPointer(params.RegencyID)
}
if params.Status != nil {
query.WriteString(updateNext(ctx, "status"))
fields = append(fields, "status")
queryParams["status"] = helper.GetInt64FromPointer(params.Status)
}
if params.VillageID != nil {
query.WriteString(updateNext(ctx, "kel_id"))
fields = append(fields, "kel_id")
queryParams["kel_id"] = helper.GetInt64FromPointer(params.VillageID)
}
if params.Sequence != nil {
query.WriteString(updateNext(ctx, "seq"))
fields = append(fields, "seq")
queryParams["seq"] = helper.GetInt64FromPointer(params.Sequence)
}
query.WriteString(updateNext(ctx, "updated_at") + " WHERE id = :id ")
fields = append(fields, "updated_at")
query.WriteString(updateQuery(ctx, fields...) + " WHERE id = :id ")
queryParams["updated_at"] = unixTime
queryParams["id"] = params.ID
return query, queryParams
Expand All @@ -218,8 +220,13 @@ func andWhere(ctx context.Context, query bytes.Buffer, field string, action stri
return newQuery
}

func updateNext(ctx context.Context, field string) string {
func updateQuery(ctx context.Context, fields ...string) string {
var query bytes.Buffer
query.WriteString(fmt.Sprintf(" , %s = :%s ", field, field))
query.WriteString(fmt.Sprintf(" %s = :%s ", fields[0], fields[0]))
fields = fields[1:]
for _, field := range fields {
query.WriteString(fmt.Sprintf(" , %s = :%s ", field, field))
}

return query.String()
}
4 changes: 0 additions & 4 deletions transport/grpc/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ func encodeGetListResponse(ctx context.Context, r interface{}) (interface{}, err
phoneString, _ := json.Marshal(v.PhoneNumbers)

result := &transportPhonebook.PhoneBook{
Id: v.ID,
PhoneNumbers: string(phoneString),
Description: helper.GetStringFromPointer(v.Description),
Name: helper.GetStringFromPointer(v.Name),
Expand Down Expand Up @@ -116,16 +115,13 @@ func encodeGetDetailResponse(ctx context.Context, r interface{}) (interface{}, e
resp := r.(*endpoint.PhonebookDetail)
phoneString, _ := json.Marshal(resp.PhoneNumbers)
return &transportPhonebook.GetDetailResponse{
Id: resp.ID,
PhoneNumbers: string(phoneString),
Description: resp.Description,
Name: resp.Name,
Address: resp.Address,
Latitude: resp.Latitude,
Longitude: resp.Longitude,
Status: resp.Status,
CategoryId: resp.Category.ID,
CategoryName: resp.Category.Name,
RegencyId: resp.Regency.ID,
RegencyName: resp.Regency.Name,
DistrictId: resp.District.ID,
Expand Down
15 changes: 13 additions & 2 deletions usecase/function_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"math"
"strconv"

"github.com/sapawarga/phonebook-service/helper"
"github.com/sapawarga/phonebook-service/model"
Expand All @@ -26,7 +27,7 @@ func (pb *PhoneBook) getPhonebookAndMetadata(ctx context.Context, params *model.
level.Error(logger).Log("error_get_list", err)
return nil, err
}
data, err := pb.appendResultGetList(ctx, resp)
data, err := pb.appendResultGetList(ctx, params, resp)
if err != nil {
level.Error(logger).Log("error_append_result", err)
return nil, err
Expand Down Expand Up @@ -54,7 +55,7 @@ func (pb *PhoneBook) getPhonebookAndMetadata(ctx context.Context, params *model.
}, nil
}

func (pb *PhoneBook) appendResultGetList(ctx context.Context, result []*model.PhoneBookResponse) (listPhonebook []*model.Phonebook, err error) {
func (pb *PhoneBook) appendResultGetList(ctx context.Context, params *model.GetListRequest, result []*model.PhoneBookResponse) (listPhonebook []*model.Phonebook, err error) {
if len(result) == 0 {
return listPhonebook, nil
}
Expand All @@ -72,13 +73,23 @@ func (pb *PhoneBook) appendResultGetList(ctx context.Context, result []*model.Ph
Sequence: v.Sequence.Int64,
CreatedAt: v.CreatedAt.Int64,
UpdatedAt: v.UpdatedAt.Int64,
Distance: v.Distance,
}

resAppend, err := pb.appendDetailPhonebook(ctx, v, result)
if err != nil {
return nil, err
}

if params.Latitude != nil && params.Longitude != nil {
id := resAppend.ID.(int64)
distance := resAppend.Distance.(float64)
category := resAppend.Category.(*model.Category)
resAppend.Category = category.Name
resAppend.ID = strconv.FormatInt(id, 10)
resAppend.Distance = fmt.Sprintf("%f", distance)
}

listPhonebook = append(listPhonebook, resAppend)
}

Expand Down

0 comments on commit b5a763d

Please sign in to comment.