tsk-5: Add contact (#7)
Closes #5 Reviewed-on: phoenix/textsender-api#7 Co-authored-by: phoenix <kundeng00@pm.me> Co-committed-by: phoenix <kundeng00@pm.me>
This commit is contained in:
+24
-36
@@ -1,32 +1,34 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
// "fmt"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"git.kundeng.us/phoenix/textsender-api/internal/store"
|
||||
"git.kundeng.us/phoenix/textsender-models/pkg/contact"
|
||||
)
|
||||
|
||||
type RequestAddContact struct {
|
||||
PhoneNumber string `json:"phone_number"`
|
||||
UserId uuid.UUID`json:"user_id"`
|
||||
PhoneNumber string `json:"phone_number"`
|
||||
UserId uuid.UUID `json:"user_id"`
|
||||
}
|
||||
|
||||
type LoginResponse struct {
|
||||
Message string `json:"message"`
|
||||
// Data []model.Data`json:"data"`
|
||||
type AddContactResponse struct {
|
||||
Message string `json:"message"`
|
||||
Data []contact.Contact `json:"data"`
|
||||
}
|
||||
|
||||
type ContactHandler struct {
|
||||
ContactStore store.ContactStore
|
||||
}
|
||||
|
||||
/*
|
||||
func NewContactHandler(store model.Store) *ContactHandler {
|
||||
return &ContactHandler{Store: store}
|
||||
func NewContactHandler(str store.ContactStore) *ContactHandler {
|
||||
return &ContactHandler{ContactStore: str}
|
||||
}
|
||||
*/
|
||||
|
||||
func (l *ContactHandler) AddContact(w http.ResponseWriter, r *http.Request) {
|
||||
func (c *ContactHandler) AddContact(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
@@ -38,47 +40,33 @@ func (l *ContactHandler) AddContact(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
defer r.Body.Close()
|
||||
|
||||
newContact := contact.Contact{PhoneNumber: req.PhoneNumber, UserId: req.UserId}
|
||||
|
||||
var statusCode int
|
||||
var resp LoginResponse
|
||||
/*
|
||||
var resp AddContactResponse
|
||||
|
||||
ctx := r.Context()
|
||||
|
||||
if exists, err := l.UserStore.UserExists(ctx, req.Username); err != nil {
|
||||
if exists, err := c.ContactStore.ContactExists(ctx, newContact.PhoneNumber, newContact.UserId); err != nil {
|
||||
fmt.Printf("Error: %v", err)
|
||||
statusCode = http.StatusInternalServerError
|
||||
resp.Message = err.Error()
|
||||
} else {
|
||||
if !exists {
|
||||
if exists {
|
||||
statusCode = http.StatusBadRequest
|
||||
resp.Message = "Failure in user check"
|
||||
resp.Message = "Cannot create contact"
|
||||
} else {
|
||||
if user, err := l.UserStore.GetUserByUsername(ctx, req.Username); err != nil {
|
||||
if err := c.ContactStore.CreateContact(ctx, &newContact); err != nil {
|
||||
fmt.Printf("Error: %v", err)
|
||||
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"
|
||||
}
|
||||
statusCode = http.StatusCreated
|
||||
resp.Message = "Contact created"
|
||||
resp.Data = append(resp.Data, newContact)
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
RespondWithJSON(w, statusCode, &resp)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user