diff --git a/endpoint/phonebook.go b/endpoint/phonebook.go index 097fc2e..b2685e6 100644 --- a/endpoint/phonebook.go +++ b/endpoint/phonebook.go @@ -48,7 +48,7 @@ func MakeGetList(ctx context.Context, usecase usecase.Provider) endpoint.Endpoin phonebooks := EncodePhonebook(resp.PhoneBooks) - meta := &Metadata{} + var meta *Metadata if resp.Metadata != nil { meta = &Metadata{ TotalCount: resp.Metadata.TotalCount, diff --git a/endpoint/response.go b/endpoint/response.go index c7b01eb..3fd3447 100644 --- a/endpoint/response.go +++ b/endpoint/response.go @@ -40,11 +40,15 @@ type Phonebook struct { Status int64 `json:"status"` StatusLabel string `json:"status_label"` CoverImageURL *string `json:"cover_image_url"` + CategoryID *int64 `json:"category_id"` Category *model.Category `json:"category"` Sequence int64 `json:"seq"` Distance float64 `json:"distance,omitempty"` + 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"` CreatedAt int64 `json:"created_at"` UpdatedAt int64 `json:"updated_at"` @@ -100,11 +104,24 @@ 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, CreatedAt: v.CreatedAt, UpdatedAt: v.UpdatedAt, + Regency: v.Regency, + District: v.District, + Village: v.Village, + } + if v.Regency != nil { + encodeData.RegencyID = helper.SetPointerInt64(v.Regency.ID) + } + if v.District != nil { + encodeData.DistrictID = helper.SetPointerInt64(v.District.ID) + } + if v.Village != nil { + encodeData.VillageID = helper.SetPointerInt64(v.Village.ID) } result = append(result, encodeData) diff --git a/repository/mysql/mysql.go b/repository/mysql/mysql.go index 85969fe..2596543 100644 --- a/repository/mysql/mysql.go +++ b/repository/mysql/mysql.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "database/sql" + "fmt" "github.com/sapawarga/phonebook-service/helper" "github.com/sapawarga/phonebook-service/model" @@ -38,8 +39,7 @@ func (r *PhonebookRepository) GetListPhoneBook(ctx context.Context, params *mode query.WriteString(selectQuery.String()) if params.SortBy != nil && params.OrderBy != nil { - query.WriteString(" ORDER BY ? ? ") - queryParams = append(queryParams, helper.GetStringFromPointer(params.SortBy), helper.GetStringFromPointer(params.OrderBy)) + query.WriteString(fmt.Sprintf(" ORDER BY %s %s ", helper.GetStringFromPointer(params.SortBy), helper.GetStringFromPointer(params.OrderBy))) } else { query.WriteString(" ORDER BY seq DESC") } diff --git a/transport/http/handler.go b/transport/http/handler.go index 1da0aaa..8e1de1a 100644 --- a/transport/http/handler.go +++ b/transport/http/handler.go @@ -59,9 +59,9 @@ func MakeHTTPHandler(ctx context.Context, fs usecase.Provider, logger kitlog.Log func decodeGetListRequest(ctx context.Context, r *http.Request) (interface{}, error) { search := r.URL.Query().Get("search") - regIDString := r.URL.Query().Get("regency_id") - distIDString := r.URL.Query().Get("district_id") - vilIDString := r.URL.Query().Get("village_id") + regIDString := r.URL.Query().Get("kabkota_id") + distIDString := r.URL.Query().Get("kec_id") + vilIDString := r.URL.Query().Get("kel_id") statusString := r.URL.Query().Get("status") limitString := r.URL.Query().Get("limit") pageString := r.URL.Query().Get("page") diff --git a/usecase/function_helper.go b/usecase/function_helper.go index dc0cf1d..5350b6b 100644 --- a/usecase/function_helper.go +++ b/usecase/function_helper.go @@ -81,6 +81,7 @@ func (pb *PhoneBook) appendResultGetList(ctx context.Context, result []*model.Ph listPhonebook = append(listPhonebook, resAppend) } + return listPhonebook, nil } @@ -98,7 +99,6 @@ func (pb *PhoneBook) appendDetailPhonebook(ctx context.Context, respFromRepo *mo return nil, err } respDetail.Regency = regency - } if respFromRepo.DistrictID.Valid { district, err := pb.repo.GetLocationByID(ctx, respFromRepo.DistrictID.Int64)