Skip to content

Commit

Permalink
Merge pull request #69 from sapawarga/issue67
Browse files Browse the repository at this point in the history
#67 fix query and add response field
  • Loading branch information
setiadijoe authored Jul 8, 2021
2 parents f8cabfb + e3eda20 commit 7bf2c95
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion endpoint/phonebook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
17 changes: 17 additions & 0 deletions endpoint/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions repository/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"database/sql"
"fmt"

"github.com/sapawarga/phonebook-service/helper"
"github.com/sapawarga/phonebook-service/model"
Expand Down Expand Up @@ -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")
}
Expand Down
6 changes: 3 additions & 3 deletions transport/http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion usecase/function_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func (pb *PhoneBook) appendResultGetList(ctx context.Context, result []*model.Ph

listPhonebook = append(listPhonebook, resAppend)
}

return listPhonebook, nil
}

Expand All @@ -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)
Expand Down

0 comments on commit 7bf2c95

Please sign in to comment.