Skip to content

Commit

Permalink
Merge pull request #68 from sapawarga/issue67
Browse files Browse the repository at this point in the history
Issue67
  • Loading branch information
setiadijoe authored Jul 7, 2021
2 parents 218fbad + d7eae78 commit f8cabfb
Show file tree
Hide file tree
Showing 23 changed files with 446 additions and 277 deletions.
61 changes: 50 additions & 11 deletions endpoint/phonebook.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,52 @@ import (
func MakeGetList(ctx context.Context, usecase usecase.Provider) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(*GetListRequest)
params := &model.ParamsPhoneBook{
var limit, page, offset int64 = 10, 1, 0
if req.Limit != 0 {
limit = req.Limit
}
if req.Page != 0 {
page = req.Page
}
offset = (page - 1) * limit
params := &model.GetListRequest{
Status: req.Status,
Search: helper.SetPointerString(req.Search),
RegencyID: helper.SetPointerInt64(req.RegencyID),
DistrictID: helper.SetPointerInt64(req.DistrictID),
VillageID: helper.SetPointerInt64(req.VillageID),
Limit: helper.SetPointerInt64(req.Limit),
Page: helper.SetPointerInt64(req.Page),
Offset: helper.SetPointerInt64(offset),
Longitude: helper.SetPointerString(req.Longitude),
Latitude: helper.SetPointerString(req.Latitude),
SortBy: helper.SetPointerString(req.SortBy),
OrderBy: helper.SetPointerString(helper.AscOrDesc[req.OrderBy]),
Name: helper.SetPointerString(req.Name),
Phone: helper.SetPointerString(req.PhoneNumber),
}

resp, err := usecase.GetList(ctx, params)

if err != nil {
return nil, err
}

phonebooks := EncodePhonebook(resp.PhoneBooks)

meta := &Metadata{}
if resp.Metadata != nil {
meta = &Metadata{
TotalCount: resp.Metadata.TotalCount,
PageCount: resp.Metadata.PageCount,
CurrentPage: req.Page,
PerPage: resp.Metadata.PerPage}
} else {
meta = nil
}

return &PhoneBookWithMeta{
Data: &PhonebookWithMeta{
Phonebooks: phonebooks,
Meta: &Metadata{
TotalCount: resp.Metadata.TotalCount,
PageCount: resp.Metadata.PageCount,
CurrentPage: resp.Metadata.CurrentPage,
PerPage: resp.Metadata.PerPage,
},
Meta: meta,
},
}, nil
}
Expand All @@ -65,7 +82,7 @@ func MakeGetDetail(ctx context.Context, usecase usecase.Provider) endpoint.Endpo
return nil, err
}

return &PhonebookDetail{
data := &PhonebookDetail{
ID: resp.ID,
Name: resp.Name,
Category: resp.Category,
Expand All @@ -77,11 +94,15 @@ func MakeGetDetail(ctx context.Context, usecase usecase.Provider) endpoint.Endpo
Village: resp.Village,
Latitude: resp.Latitude,
Longitude: resp.Longitude,
CoverImagePath: fmt.Sprintf("%s/%s", cfg.AppStoragePublicURL, resp.CoverImagePath),
CoverImagePath: fmt.Sprintf("%s/%s", cfg.AppStoragePublicURL, resp.CoverImageURL),
Sequence: resp.Sequence,
Status: resp.Status,
StatusLabel: GetStatusLabel[resp.Status]["id"],
CreatedAt: resp.CreatedAt,
UpdatedAt: resp.UpdatedAt,
}
return map[string]interface{}{
"data": data,
}, nil
}
}
Expand All @@ -104,6 +125,7 @@ func MakeAddPhonebook(ctx context.Context, usecase usecase.Provider) endpoint.En
Longitude: req.Longitude,
CoverImagePath: req.CoverImagePath,
Status: req.Status,
Sequence: req.Sequence,
}); err != nil {
return nil, err
}
Expand Down Expand Up @@ -135,6 +157,7 @@ func MakeUpdatePhonebook(ctx context.Context, usecase usecase.Provider) endpoint
Longitude: req.Longitude,
CoverImagePath: req.CoverImagePath,
Status: req.Status,
Sequence: req.Sequence,
}); err != nil {
return nil, err
}
Expand Down Expand Up @@ -180,3 +203,19 @@ func MakeCheckReadiness(ctx context.Context, usecase usecase.Provider) endpoint.
}, nil
}
}

// MakeIsExistPhoneNumber ...
func MakeIsExistPhoneNumber(ctx context.Context, usecase usecase.Provider) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
req := request.(*IsExistPhoneNumber)

isExist, err := usecase.IsExistPhoneNumber(ctx, req.PhoneNumber)
if err != nil {
return nil, err
}

return map[string]map[string]interface{}{
"data": {"exist": isExist},
}, nil
}
}
29 changes: 20 additions & 9 deletions endpoint/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ package endpoint

// GetListRequest ...
type GetListRequest struct {
Search string `httpquery:"search"`
RegencyID int64 `httpquery:"regency_id"`
DistrictID int64 `httpquery:"district_id"`
VillageID int64 `httpquery:"village_id"`
Status *int64 `httpquery:"status"`
Latitude string `httpquery:"latitude"`
Longitude string `httpquery:"longitude"`
Limit int64 `httpquery:"limit"`
Page int64 `httpquery:"page"`
Search string `httpquery:"search"`
Name string `httpquery:"name"`
PhoneNumber string `httpquery:"phone"`
RegencyID int64 `httpquery:"kabkota_id"`
DistrictID int64 `httpquery:"kec_id"`
VillageID int64 `httpquery:"kel_id"`
Status *int64 `httpquery:"status"`
Latitude string `httpquery:"latitude"`
Longitude string `httpquery:"longitude"`
Limit int64 `httpquery:"limit"`
Page int64 `httpquery:"page"`
SortBy string `httpquery:"sort_by"`
OrderBy string `httpquery:"sort_order"`
}

// AddPhonebookRequest ...
Expand All @@ -27,6 +31,7 @@ type AddPhonebookRequest struct {
CategoryID *int64 `json:"category_id"`
CoverImagePath *string `json:"cover_image_path"`
Address *string `json:"address"`
Sequence *int64 `json:"seq"`
}

// GetDetailRequest ...
Expand All @@ -49,10 +54,16 @@ type UpdatePhonebookRequest struct {
CategoryID *int64 `json:"category_id"`
CoverImagePath *string `json:"cover_image_path"`
Address *string `json:"address"`
Sequence *int64 `json:"seq,omitempty"`
}

// PhoneNumber ...
type PhoneNumber struct {
PhoneNumber string `json:"phone_number"`
Type string `json:"type"`
}

// IsExistPhoneNumber ...
type IsExistPhoneNumber struct {
PhoneNumber string `httpquery:"phone_number"`
}
50 changes: 30 additions & 20 deletions endpoint/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package endpoint
import (
"encoding/json"
"fmt"
"time"

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

Expand All @@ -32,16 +32,22 @@ type Metadata struct {
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"`
StatusLabel string `json:"status_label,omitempty"`
CoverImageURL string `json:"cover_image_url,omitempty"`
Description *string `json:"description"`
Name *string `json:"name"`
Address *string `json:"address"`
Latitude *string `json:"latitude"`
Longitude *string `json:"longitude"`
Status int64 `json:"status"`
StatusLabel string `json:"status_label"`
CoverImageURL *string `json:"cover_image_url"`
Category *model.Category `json:"category"`
Sequence int64 `json:"seq"`
Distance float64 `json:"distance,omitempty"`
Regency *model.Location `json:"kabkota"`
District *model.Location `json:"kecamatan"`
Village *model.Location `json:"kelurahan"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
}

// PhonebookDetail ...
Expand All @@ -52,16 +58,17 @@ type PhonebookDetail struct {
Address string `json:"address"`
Description string `json:"description"`
PhoneNumbers []*PhoneNumber `json:"phone_numbers"`
Regency *model.Location `json:"kabkota,omitempty"`
District *model.Location `json:"kecamatan,omitempty"`
Village *model.Location `json:"kelurahan,omitempty"`
Regency *model.Location `json:"kabkota"`
District *model.Location `json:"kecamatan"`
Village *model.Location `json:"kelurahan"`
Latitude string `json:"latitude"`
Longitude string `json:"longitude"`
Sequence int64 `json:"seq"`
CoverImagePath string `json:"cover_image_url"`
Status int64 `json:"status"`
StatusLabel string `json:"status_label"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
}

// StatusResponse ...
Expand All @@ -85,16 +92,19 @@ func EncodePhonebook(data []*model.Phonebook) []*Phonebook {
encodeData := &Phonebook{
ID: v.ID,
PhoneNumbers: phoneNumbers,
Description: v.Description,
Name: v.Name,
Address: v.Address,
CoverImageURL: fmt.Sprintf("%s/%s", cfg.AppStoragePublicURL, v.CoverImageURL),
Latitude: v.Latitude,
Longitude: v.Longitude,
Description: helper.SetPointerString(v.Description),
Name: helper.SetPointerString(v.Name),
Address: helper.SetPointerString(v.Address),
CoverImageURL: helper.SetPointerString(fmt.Sprintf("%s/%s", cfg.AppStoragePublicURL, v.CoverImageURL)),
Latitude: helper.SetPointerString(v.Latitude),
Longitude: helper.SetPointerString(v.Longitude),
Status: v.Status,
StatusLabel: GetStatusLabel[v.Status]["id"],
Category: v.Category,
Distance: v.Distance,
Sequence: v.Sequence,
CreatedAt: v.CreatedAt,
UpdatedAt: v.UpdatedAt,
}

result = append(result, encodeData)
Expand Down
10 changes: 10 additions & 0 deletions helper/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,13 @@ func GetStatusEnum(status string) (*int64, error) {

return nil, errors.New("status_not_registered")
}

// AscOrDesc ...
var AscOrDesc = map[string]string{
"ascending": "ASC",
"descending": "DESC",
"asc": "ASC",
"desc": "DESC",
"ASC": "ASC",
"DESC": "DESC",
}
15 changes: 15 additions & 0 deletions mocks/mock_phonebook.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions mocks/testcases/delete_phonebook.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ var DeletePhonebookTestcases = []DeletePhonebook{
Longitude: sql.NullString{String: "0.988789", Valid: true},
CoverImagePath: sql.NullString{String: "http://localhost:9080", Valid: true},
Status: sql.NullInt64{Int64: 10, Valid: true},
CreatedAt: sql.NullTime{Time: currentTime, Valid: true},
UpdatedAt: sql.NullTime{Time: currentTime, Valid: true},
CreatedAt: sql.NullInt64{Int64: currentTime, Valid: true},
UpdatedAt: sql.NullInt64{Int64: currentTime, Valid: true},
CategoryID: sql.NullInt64{Int64: 1, Valid: true},
},
Error: nil,
Expand Down Expand Up @@ -77,8 +77,8 @@ var DeletePhonebookTestcases = []DeletePhonebook{
Longitude: sql.NullString{String: "0.988789", Valid: true},
CoverImagePath: sql.NullString{String: "http://localhost:9080", Valid: true},
Status: sql.NullInt64{Int64: 10, Valid: true},
CreatedAt: sql.NullTime{Time: currentTime, Valid: true},
UpdatedAt: sql.NullTime{Time: currentTime, Valid: true},
CreatedAt: sql.NullInt64{Int64: currentTime, Valid: true},
UpdatedAt: sql.NullInt64{Int64: currentTime, Valid: true},
CategoryID: sql.NullInt64{Int64: 1, Valid: true},
},
Error: nil,
Expand Down
Loading

0 comments on commit f8cabfb

Please sign in to comment.