Skip to content

Commit

Permalink
#67 create endpoint for check exist phone number
Browse files Browse the repository at this point in the history
  • Loading branch information
setiadijoe committed Jul 6, 2021
1 parent 218fbad commit 6fce682
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 6 deletions.
16 changes: 16 additions & 0 deletions endpoint/phonebook.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,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
}
}
5 changes: 5 additions & 0 deletions endpoint/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,8 @@ type PhoneNumber struct {
PhoneNumber string `json:"phone_number"`
Type string `json:"type"`
}

// IsExistPhoneNumber ...
type IsExistPhoneNumber struct {
PhoneNumber string `httpquery:"phone_number"`
}
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.

56 changes: 56 additions & 0 deletions mocks/testcases/is_exist_phone_number.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package testcases

import "errors"

// ReponseIsExistPhone ...
type ReponseIsExistPhone struct {
Result bool
Error error
}

// IsExistPhoneNumber ...
type IsExistPhoneNumber struct {
Description string
UsecaseParams string
RepositoryParams string
UsecaseResponse ReponseIsExistPhone
RepositoryResponse ReponseIsExistPhone
}

// IsExistPhoneNumberData ...
var IsExistPhoneNumberData = []IsExistPhoneNumber{
{
Description: "success_get_check_phone_number",
UsecaseParams: "022-1234",
RepositoryParams: "022-1234",
UsecaseResponse: ReponseIsExistPhone{
Result: true,
Error: nil,
},
RepositoryResponse: ReponseIsExistPhone{
Result: true,
Error: nil,
},
}, {
Description: "failed_check_phone_number",
UsecaseParams: "sample",
RepositoryParams: "sample",
UsecaseResponse: ReponseIsExistPhone{
Result: false,
Error: errors.New("failed_check"),
},
RepositoryResponse: ReponseIsExistPhone{
Result: false,
Error: errors.New("failed_check"),
},
},
}

// IsExistDescription ...
func IsExistDescription() []string {
var arr = []string{}
for _, data := range IsExistPhoneNumberData {
arr = append(arr, data.Description)
}
return arr
}
26 changes: 26 additions & 0 deletions repository/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,29 @@ func (r *PhonebookRepository) Delete(ctx context.Context, id int64) error {

return nil
}

// IsExistPhoneNumber ...
func (r *PhonebookRepository) IsExistPhoneNumber(ctx context.Context, phone string) (bool, error) {
var query bytes.Buffer
var count int
var err error

query.WriteString(` SELECT count(1) FROM sapawarga_db_development.phonebooks
WHERE JSON_CONTAINS(phone_numbers->'$[*].phone_number', json_array(?))`)

if ctx != nil {
err = r.conn.GetContext(ctx, count, query.String(), phone)
} else {
err = r.conn.Get(count, query.String(), phone)
}

if err != nil {
return false, err
}

if count == 0 || err == sql.ErrNoRows {
return false, nil
}

return true, nil
}
1 change: 1 addition & 0 deletions repository/repository_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type PhoneBookI interface {
GetLocationByID(ctx context.Context, id int64) (*model.Location, error)
GetListPhonebookByLongLat(ctx context.Context, params *model.GetListRequest) ([]*model.PhoneBookResponse, error)
GetListPhonebookByLongLatMeta(ctx context.Context, params *model.GetListRequest) (int64, error)
IsExistPhoneNumber(ctx context.Context, phone string) (bool, error)
// Create section
Insert(ctx context.Context, params *model.AddPhonebook) error
// Update section
Expand Down
9 changes: 9 additions & 0 deletions transport/http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func MakeHTTPHandler(ctx context.Context, fs usecase.Provider, logger kitlog.Log
processAdd := kithttp.NewServer(endpoint.MakeAddPhonebook(ctx, fs), decodeCreateRequest, encodeResponse, opts...)
processUpdate := kithttp.NewServer(endpoint.MakeUpdatePhonebook(ctx, fs), decodeUpdateRequest, encodeResponse, opts...)
processDelete := kithttp.NewServer(endpoint.MakeDeletePhonebook(ctx, fs), decodeGetByID, encodeResponse, opts...)
processIsExist := kithttp.NewServer(endpoint.MakeIsExistPhoneNumber(ctx, fs), decodeIsExist, encodeResponse, opts...)

r := mux.NewRouter()

Expand All @@ -51,6 +52,7 @@ func MakeHTTPHandler(ctx context.Context, fs usecase.Provider, logger kitlog.Log
r.Handle("/phone-books/", processAdd).Methods(helper.HTTP_POST)
r.Handle("/phone-books/{id}", processUpdate).Methods(helper.HTTP_PUT)
r.Handle("/phone-books/{id}", processDelete).Methods(helper.HTTP_DELETE)
r.Handle("/phone-books/check-exist", processIsExist).Methods(helper.HTTP_GET)

return r
}
Expand Down Expand Up @@ -122,6 +124,13 @@ func decodeUpdateRequest(ctx context.Context, r *http.Request) (interface{}, err
return reqBody, nil
}

func decodeIsExist(ctx context.Context, r *http.Request) (interface{}, error) {
phone := r.URL.Query().Get("phone_number")

reqBody := &endpoint.IsExistPhoneNumber{PhoneNumber: phone}
return reqBody, nil
}

func decodeNoRequest(ctx context.Context, r *http.Request) (interface{}, error) {
return r, nil
}
Expand Down
12 changes: 12 additions & 0 deletions usecase/phonebook.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,18 @@ func (pb *PhoneBook) Delete(ctx context.Context, id int64) error {
return nil
}

// IsExistPhoneNumber ...
func (pb *PhoneBook) IsExistPhoneNumber(ctx context.Context, phone string) (bool, error) {
logger := kitlog.With(pb.logger, "method", "IsExistPhoneNumber")
isExist, err := pb.repo.IsExistPhoneNumber(ctx, phone)
if err != nil {
level.Error(logger).Log("error_is_exist", err)
return false, err
}

return isExist, nil
}

// CheckHealthReadiness ...
func (pb *PhoneBook) CheckHealthReadiness(ctx context.Context) error {
logger := kitlog.With(pb.logger, "method", "CheckHealthReadiness")
Expand Down
1 change: 1 addition & 0 deletions usecase/usecase_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
type Provider interface {
GetList(ctx context.Context, params *model.ParamsPhoneBook) (*model.PhoneBookWithMeta, error)
GetDetail(ctx context.Context, id int64) (*model.PhonebookDetail, error)
IsExistPhoneNumber(ctx context.Context, phone string) (bool, error)
Insert(ctx context.Context, params *model.AddPhonebook) error
Update(ctx context.Context, params *model.UpdatePhonebook) error
Delete(ctx context.Context, id int64) error
Expand Down
28 changes: 22 additions & 6 deletions usecase/usecase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ var _ = Describe("Phone Book", func() {
}
}

// CheckReadinessLogic ...
var CheckReadinessLogic = func(idx int) {
ctx := context.Background()
data := testcases.CheckReadinessData[idx]
Expand All @@ -128,14 +129,29 @@ var _ = Describe("Phone Book", func() {
}
}

// IsExistPhoneNumberLogic ...
var IsExistPhoneNumberLogic = func(idx int) {
ctx := context.Background()
data := testcases.IsExistPhoneNumberData[idx]
mockPhoneBookRepo.EXPECT().IsExistPhoneNumber(ctx, data.RepositoryParams).Return(data.RepositoryResponse.Result, data.RepositoryResponse.Error).Times(1)
isExist, err := phonebook.IsExistPhoneNumber(ctx, data.UsecaseParams)
if err != nil {
Expect(err).NotTo(BeNil())
} else {
Expect(isExist).To(Equal(data.UsecaseResponse.Result))
Expect(err).To(BeNil())
}
}

// sort all function names
var unitTestLogic = map[string]map[string]interface{}{
"GetList": {"func": GetListLogic, "test_case_count": len(testcases.GetPhoneBookData), "desc": testcases.ListPhonebookDescription()},
"GetDetail": {"func": GetDetailPhonebookLogic, "test_case_count": len(testcases.GetDetailPhonebookData), "desc": testcases.DetailPhonebookDescription()},
"Insert": {"func": InsertPhonebookLogic, "test_case_count": len(testcases.InsertPhonebookTestcases), "desc": testcases.InsertPhonebookDescription()},
"Update": {"func": UpdatePhonebookLogic, "test_case_count": len(testcases.UpdatePhonebookTestcases), "desc": testcases.UpdatePhonebookDescription()},
"Delete": {"func": DeletePhonebookLogic, "test_case_count": len(testcases.DeletePhonebookTestcases), "desc": testcases.DeletePhonebookDescription()},
"CheckReadiness": {"func": CheckReadinessLogic, "test_case_count": len(testcases.CheckReadinessData), "desc": testcases.CheckReadinessDescription()},
"GetList": {"func": GetListLogic, "test_case_count": len(testcases.GetPhoneBookData), "desc": testcases.ListPhonebookDescription()},
"GetDetail": {"func": GetDetailPhonebookLogic, "test_case_count": len(testcases.GetDetailPhonebookData), "desc": testcases.DetailPhonebookDescription()},
"Insert": {"func": InsertPhonebookLogic, "test_case_count": len(testcases.InsertPhonebookTestcases), "desc": testcases.InsertPhonebookDescription()},
"Update": {"func": UpdatePhonebookLogic, "test_case_count": len(testcases.UpdatePhonebookTestcases), "desc": testcases.UpdatePhonebookDescription()},
"Delete": {"func": DeletePhonebookLogic, "test_case_count": len(testcases.DeletePhonebookTestcases), "desc": testcases.DeletePhonebookDescription()},
"CheckReadiness": {"func": CheckReadinessLogic, "test_case_count": len(testcases.CheckReadinessData), "desc": testcases.CheckReadinessDescription()},
"IsExistPhoneNumber": {"func": IsExistPhoneNumberLogic, "test_case_count": len(testcases.IsExistPhoneNumberData), "desc": testcases.IsExistDescription()},
}

for _, val := range unitTestLogic {
Expand Down

0 comments on commit 6fce682

Please sign in to comment.