Added endpoint to add a contact

This commit is contained in:
phoenix
2025-10-22 23:16:05 -04:00
parent a23c1a5f29
commit a9ebaf4ed4
3 changed files with 94 additions and 0 deletions
+82
View File
@@ -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)
}
+1
View File
@@ -1,3 +1,4 @@
package handler package handler
const MESSAGE_DRAFT_ENDPOINT = "/api/v1/message/draft" const MESSAGE_DRAFT_ENDPOINT = "/api/v1/message/draft"
const ADD_CONTACT_ENDPOINT = "/api/v1/contact";
+11
View File
@@ -4,6 +4,17 @@ import "encoding/json"
import "log" import "log"
import "net/http" 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 // Helper function to send JSON responses
func RespondWithJSON(w http.ResponseWriter, statusCode int, data interface{}) { func RespondWithJSON(w http.ResponseWriter, statusCode int, data interface{}) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")