diff --git a/cmd/api/main.go b/cmd/api/main.go index 6541b16..16e1c8e 100644 --- a/cmd/api/main.go +++ b/cmd/api/main.go @@ -75,6 +75,7 @@ func main() { router.Use(mdlware.JSONContentType) router.Handle(endpoint.ADD_CONTACT_ENDPOINT, mdlware.AuthMiddleware(jwtService)(http.HandlerFunc(contactHandler.AddContact))) + router.Handle(endpoint.GET_CONTACT, mdlware.AuthMiddleware(jwtService)(http.HandlerFunc(contactHandler.GetContact))) router.Handle(endpoint.ADD_MESSAGE, mdlware.AuthMiddleware(jwtService)(http.HandlerFunc(messageHandler.AddMessage))) // Start server diff --git a/internal/handler/contact.go b/internal/handler/contact.go index 7a9322f..d023f44 100644 --- a/internal/handler/contact.go +++ b/internal/handler/contact.go @@ -20,6 +20,11 @@ type AddContactResponse struct { Data []contact.Contact `json:"data"` } +type GetContactResponse struct { + Message string `json:"message"` + Data []contact.Contact `json:"data"` +} + type ContactHandler struct { ContactStore store.ContactStore } @@ -70,3 +75,80 @@ func (c *ContactHandler) AddContact(w http.ResponseWriter, r *http.Request) { RespondWithJSON(w, statusCode, &resp) } + +func (c *ContactHandler) GetContact(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet { + fmt.Println("Method:", r.Method) + http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) + return + } + + var id, userId uuid.UUID + queryParams := r.URL.Query() + + // Check if parameter exists + if _, exists := queryParams["id"]; !exists { + if _, exists := queryParams["user_id"]; !exists { + fmt.Fprintf(w, "Name parameter not provided") + http.Error(w, "Query params", http.StatusBadRequest) + return + } else { + var err error + userId, err = uuid.Parse(queryParams.Get("user_id")) + if err != nil { + fmt.Fprintf(w, "Name parameter exists: %s", userId) + } + } + } else { + var err error + idTmp := queryParams.Get("id") + id, err = uuid.Parse(idTmp) + if err != nil { + fmt.Println("Error:", err) + http.Error(w, "Error parsing Id", http.StatusBadRequest) + return + } + } + + var statusCode int + var resp GetContactResponse + + ctx := r.Context() + + if id != uuid.Nil { + fmt.Println("Checking with Id") + if con, err := c.ContactStore.GetContactByID(ctx, id); err == nil { + statusCode = http.StatusOK + resp.Message = "Successful" + resp.Data = append(resp.Data, *con) + } else { + statusCode = http.StatusInternalServerError + resp.Message = err.Error() + } + } else if userId != uuid.Nil { + fmt.Println("Checking with User Id") + if contacts, err := c.ContactStore.GetAllContacts(ctx); err == nil { + for _, con := range contacts { + if con.UserId == userId { + resp.Data = append(resp.Data, *con) + } + } + + if len(resp.Data) > 0 { + statusCode = http.StatusOK + resp.Message = "Successful" + } else { + statusCode = http.StatusNotFound + resp.Message = "Contact not found" + } + } else { + statusCode = http.StatusInternalServerError + resp.Message = err.Error() + } + } else { + statusCode = http.StatusBadRequest + resp.Message = "Invalid query parameter" + } + + RespondWithJSON(w, statusCode, &resp) +} diff --git a/internal/handler/contact_test.go b/internal/handler/contact_test.go index 276d70d..445aa81 100644 --- a/internal/handler/contact_test.go +++ b/internal/handler/contact_test.go @@ -3,11 +3,13 @@ package handler import ( "context" "encoding/json" + "fmt" "net/http" "net/http/httptest" "strings" "testing" + "git.kundeng.us/phoenix/textsender-models/pkg/contact" "github.com/google/uuid" "github.com/stretchr/testify/assert" @@ -44,6 +46,35 @@ func TestCreateContactWithMock(t *testing.T) { assert.NotNil(t, response.Data[0].Id, "Id should not be nil") } +func TestGetContactWithMock(t *testing.T) { + mockstore := NewMockContactStore() + testUserId := uuid.New() + + testCon := contact.Contact{PhoneNumber: "+12335403383", UserId: testUserId} + ctx := t.Context() + if err := mockstore.CreateContact(ctx, &testCon); err != nil { + assert.NoError(t, err, "Error creating contact") + return + } + + url := fmt.Sprintf("%s?user_id=%s", endpoint.GET_CONTACT, testCon.UserId) + req, _ := http.NewRequest("GET", url, nil) + rr := httptest.NewRecorder() + + contactHandler := NewContactHandler(mockstore) + contactHandler.GetContact(rr, req) + + assert.Equal(t, http.StatusOK, rr.Code) + + var response GetContactResponse + err := json.Unmarshal(rr.Body.Bytes(), &response) + assert.NoError(t, err, "Error getting contact %v", err) + + assert.NotEmpty(t, response.Data, "No Contact retrieved") + + assert.NotNil(t, response.Data[0].Id, "Id should not be nil") +} + func resetTestDB(t *testing.T) { t.Helper() _, err := db.Pool.Exec(context.Background(), "DELETE FROM contacts") diff --git a/internal/handler/endpoint/endpoint.go b/internal/handler/endpoint/endpoint.go index de58a86..ff6d1c8 100644 --- a/internal/handler/endpoint/endpoint.go +++ b/internal/handler/endpoint/endpoint.go @@ -2,4 +2,5 @@ package endpoint const MESSAGE_DRAFT_ENDPOINT = "/api/v1/message/draft" const ADD_MESSAGE = "/api/v1/message/new" -const ADD_CONTACT_ENDPOINT = "/api/v1/contact" +const GET_CONTACT = "/api/v1/contact" +const ADD_CONTACT_ENDPOINT = "/api/v1/contact/new" diff --git a/internal/store/message.go b/internal/store/message_store.go similarity index 100% rename from internal/store/message.go rename to internal/store/message_store.go