added delete contact route
This commit is contained in:
@@ -52,6 +52,9 @@ func InsertUser(db *sql.DB, username string, passwordHash string) (string, error
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("error inserting user: %v", err)
|
||||
}
|
||||
|
||||
log.Printf("Inserted user: %v", username)
|
||||
|
||||
return userId, err
|
||||
}
|
||||
|
||||
@@ -64,8 +67,8 @@ func GetPasswordHash(db *sql.DB, username string) (string, error) {
|
||||
var passwordHash string
|
||||
err := db.QueryRow(query, username).Scan(&passwordHash)
|
||||
if err != nil {
|
||||
fmt.Printf("error getting password: %v\n", err)
|
||||
return "", fmt.Errorf("error getting password: %v", err)
|
||||
fmt.Printf("error getting password hash: %v\n", err)
|
||||
return "", fmt.Errorf("error getting password hash: %v", err)
|
||||
}
|
||||
|
||||
return passwordHash, err
|
||||
88
database/contacts.go
Normal file
88
database/contacts.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/google/uuid"
|
||||
"log"
|
||||
)
|
||||
|
||||
func DeleteContact(db *sql.DB, userID uuid.UUID, conversationID uuid.UUID) (string, error) {
|
||||
// Check conversation type
|
||||
var conversationType string
|
||||
err := db.QueryRow(
|
||||
"SELECT conversation_type FROM Conversations WHERE conversation_id = $1",
|
||||
conversationID,
|
||||
).Scan(&conversationType)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return "no conversation found for this id", nil
|
||||
}
|
||||
return "", fmt.Errorf("error checking conversation type: %w", err)
|
||||
}
|
||||
|
||||
if conversationType == "group" {
|
||||
// Delete from Contacts
|
||||
res, err := db.Exec(
|
||||
"DELETE FROM Contacts WHERE conversation_id = $1 AND user_id = $2",
|
||||
conversationID,
|
||||
userID,
|
||||
)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("error deleting contact: %w", err)
|
||||
}
|
||||
|
||||
rowsAffected, err := res.RowsAffected()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("error checking contact deletion: %w", err)
|
||||
}
|
||||
if rowsAffected == 0 {
|
||||
return fmt.Sprintf("no matching contact found with conversation id: %s, user id: %s", conversationID, userID), nil
|
||||
}
|
||||
|
||||
// Delete from Memberships
|
||||
res, err = db.Exec(
|
||||
"DELETE FROM Memberships WHERE conversation_id = $1 AND user_id = $2",
|
||||
conversationID,
|
||||
userID,
|
||||
)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("error deleting membership: %w", err)
|
||||
}
|
||||
|
||||
rowsAffected, err = res.RowsAffected()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("error checking membership deletion: %w", err)
|
||||
}
|
||||
if rowsAffected == 0 {
|
||||
return "", fmt.Errorf("no matching membership found with conversation id: %s, user id: %s", conversationID, userID)
|
||||
}
|
||||
|
||||
log.Printf("Successfully removed user %s from group %s", userID, conversationID)
|
||||
} else {
|
||||
// Handle direct conversation
|
||||
res, err := db.Exec(
|
||||
"DELETE FROM Contacts WHERE user_id = $1 AND conversation_id = $2",
|
||||
userID,
|
||||
conversationID,
|
||||
)
|
||||
if err != nil {
|
||||
log.Printf("Error deleting contact: %v", err)
|
||||
return "", fmt.Errorf("error deleting contact: %w", err)
|
||||
}
|
||||
|
||||
rowsAffected, err := res.RowsAffected()
|
||||
if err != nil {
|
||||
log.Printf("Error checking contact deletion: %v", err)
|
||||
return "", fmt.Errorf("error checking contact deletion: %w", err)
|
||||
}
|
||||
if rowsAffected == 0 {
|
||||
return fmt.Sprintf("no matching contact found with user id: %s, conversation id: %s", userID, conversationID), nil
|
||||
}
|
||||
|
||||
log.Printf("Successfully deleted contact for user %s in conversation %s", userID, conversationID)
|
||||
}
|
||||
|
||||
return "", nil
|
||||
}
|
||||
Reference in New Issue
Block a user