diff --git a/endpoint/phonebook.go b/endpoint/phonebook.go index b2685e6..58dbe43 100644 --- a/endpoint/phonebook.go +++ b/endpoint/phonebook.go @@ -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, @@ -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 @@ -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, diff --git a/endpoint/response.go b/endpoint/response.go index 3fd3447..474fe1f 100644 --- a/endpoint/response.go +++ b/endpoint/response.go @@ -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"` @@ -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"` @@ -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"` @@ -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, @@ -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, @@ -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) } @@ -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) } diff --git a/model/response_usecase.go b/model/response_usecase.go index a1baa97..ee62627 100644 --- a/model/response_usecase.go +++ b/model/response_usecase.go @@ -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 { diff --git a/repository/mysql/query_updater.go b/repository/mysql/query_updater.go index 18472e3..d7a919e 100644 --- a/repository/mysql/query_updater.go +++ b/repository/mysql/query_updater.go @@ -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 @@ -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() } diff --git a/transport/grpc/transport.go b/transport/grpc/transport.go index 662ac6f..46dfa68 100644 --- a/transport/grpc/transport.go +++ b/transport/grpc/transport.go @@ -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), @@ -116,7 +115,6 @@ 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, @@ -124,8 +122,6 @@ func encodeGetDetailResponse(ctx context.Context, r interface{}) (interface{}, e 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, diff --git a/usecase/function_helper.go b/usecase/function_helper.go index 5350b6b..e0b7066 100644 --- a/usecase/function_helper.go +++ b/usecase/function_helper.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "math" + "strconv" "github.com/sapawarga/phonebook-service/helper" "github.com/sapawarga/phonebook-service/model" @@ -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 @@ -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 } @@ -72,6 +73,7 @@ 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) @@ -79,6 +81,15 @@ func (pb *PhoneBook) appendResultGetList(ctx context.Context, result []*model.Ph 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) }