diff --git a/internal/handler/contact.go b/internal/handler/contact.go new file mode 100644 index 0000000..01ea674 --- /dev/null +++ b/internal/handler/contact.go @@ -0,0 +1,82 @@ +package handler + +import ( + "fmt" + "net/http" + + "github.com/google/uuid" +) + +type RequestAddContact struct { + PhoneNumber string `json:"phone_number"` + UserId uuid.UUID`json:"user_id"` +} + +type LoginResponse struct { + Message string `json:"message"` + Data []model.Data`json:"data"` +} + +type ContactHandler struct { +} + +func NewContactHandler(store model.Store) *ContactHandler { + return &ContactHandler{Store: store} +} + +func (l *ContactHandler) AddContact(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost { + http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) + return + } + + var req RequestAddContact + if err := ExtractFromRequest(r, &req); err != nil { + http.Error(w, "Invalid JSON: "+err.Error(), http.StatusBadRequest) + } + defer r.Body.Close() + + var statusCode int + /* + var resp LoginResponse + + ctx := r.Context() + + if exists, err := l.UserStore.UserExists(ctx, req.Username); err != nil { + fmt.Printf("Error: %v", err) + statusCode = http.StatusInternalServerError + resp.Message = err.Error() + } else { + if !exists { + statusCode = http.StatusBadRequest + resp.Message = "Failure in user check" + } else { + if user, err := l.UserStore.GetUserByUsername(ctx, req.Username); err != nil { + statusCode = http.StatusInternalServerError + resp.Message = err.Error() + } else { + hashing := utility.HashMash{Password: req.Password} + if hashing.CheckPasswordHash(req.Password, user.Password) { + var tokGen utility.TokenGenerator + secretKey := config.GetSecretKey() + tokGen.SetSecretKey(secretKey) + if token, err := tokGen.GenerateToken(*user); err != nil { + fmt.Println(err.Error()) + statusCode = http.StatusInternalServerError + resp.Message = "Error generating token" + } else { + statusCode = http.StatusOK + resp.Data = append(resp.Data, *token) + resp.Message = "Successful" + } + } else { + statusCode = http.StatusNotFound + resp.Message = "User not found" + } + } + } + } + */ + + RespondWithJson(w, statusCode, &resp) +} diff --git a/internal/handler/endpoints.go b/internal/handler/endpoints.go index dea7888..8382353 100644 --- a/internal/handler/endpoints.go +++ b/internal/handler/endpoints.go @@ -1,3 +1,4 @@ package handler const MESSAGE_DRAFT_ENDPOINT = "/api/v1/message/draft" +const ADD_CONTACT_ENDPOINT = "/api/v1/contact"; diff --git a/internal/handler/helper.go b/internal/handler/helper.go index 6e489b1..2f4bc47 100644 --- a/internal/handler/helper.go +++ b/internal/handler/helper.go @@ -4,6 +4,17 @@ import "encoding/json" import "log" import "net/http" + +func ExtractFromRequest(r *http.Request, reqItem interface{}) error { + err := json.NewDecoder(r.Body).Decode(&reqItem) + if err != nil { + return err + } else { + return nil + } +} + + // Helper function to send JSON responses func RespondWithJSON(w http.ResponseWriter, statusCode int, data interface{}) { w.Header().Set("Content-Type", "application/json")