added contact suggestion route, code refactor
This commit is contained in:
@@ -11,13 +11,13 @@ import (
|
|||||||
func CheckUserExists(db *sql.DB, username string) (bool, error) {
|
func CheckUserExists(db *sql.DB, username string) (bool, error) {
|
||||||
query := `SELECT COUNT(1) FROM accounts WHERE username = $1`
|
query := `SELECT COUNT(1) FROM accounts WHERE username = $1`
|
||||||
|
|
||||||
var exist bool
|
var count int
|
||||||
err := db.QueryRow(query, username).Scan(&exist)
|
err := db.QueryRow(query, username).Scan(&count)
|
||||||
if !errors.Is(err, sql.ErrNoRows) {
|
if err != nil {
|
||||||
return false, helpers.NewError(helpers.ErrInternal, "Failed to check username", err)
|
return false, helpers.NewError(helpers.ErrInternal, "Failed to check user existence", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return exist, nil
|
return count > 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func InsertUser(db *sql.DB, username string, passwordHash string) (string, error) {
|
func InsertUser(db *sql.DB, username string, passwordHash string) (string, error) {
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"relay-server/helpers"
|
"relay-server/helpers"
|
||||||
"relay-server/model"
|
"relay-server/model"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func DeleteContact(db *sql.DB, userID uuid.UUID, conversationID uuid.UUID) error {
|
func DeleteContact(db *sql.DB, userID uuid.UUID, conversationID uuid.UUID) error {
|
||||||
@@ -317,3 +318,31 @@ func GetContacts(db *sql.DB, userID uuid.UUID) ([]*model.Contact, error) {
|
|||||||
|
|
||||||
return contacts, nil
|
return contacts, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ContactSuggestion(db *sql.DB, contactUsername string) ([]string, error) {
|
||||||
|
query := `
|
||||||
|
SELECT username FROM accounts
|
||||||
|
WHERE LOWER(username) LIKE $1
|
||||||
|
LIMIT 5;
|
||||||
|
`
|
||||||
|
|
||||||
|
rows, err := db.Query(query, "%"+strings.ToLower(contactUsername)+"%")
|
||||||
|
|
||||||
|
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return nil, helpers.NewError(helpers.ErrInternal, "Failed to get contact suggestions", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var suggestions []string
|
||||||
|
for rows.Next() {
|
||||||
|
var suggestion string
|
||||||
|
err := rows.Scan(&suggestion)
|
||||||
|
if err != nil {
|
||||||
|
return nil, helpers.NewError(helpers.ErrInternal, "internal server error", err)
|
||||||
|
}
|
||||||
|
suggestions = append(suggestions, suggestion)
|
||||||
|
}
|
||||||
|
if err = rows.Err(); err != nil {
|
||||||
|
return nil, helpers.NewError(helpers.ErrInternal, "Error processing suggestions", err)
|
||||||
|
}
|
||||||
|
return suggestions, nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ func Signup(c *fiber.Ctx) error {
|
|||||||
// Check if user exists
|
// Check if user exists
|
||||||
exist, err := database.CheckUserExists(database.DB, u.Username)
|
exist, err := database.CheckUserExists(database.DB, u.Username)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return helpers.NewError(helpers.ErrInternal, "internal server error", fmt.Errorf("failed to check user existance: %w", err))
|
return err
|
||||||
}
|
}
|
||||||
if exist {
|
if exist {
|
||||||
return helpers.NewError(helpers.ErrInvalidInput, "User already exists", nil)
|
return helpers.NewError(helpers.ErrInvalidInput, "User already exists", nil)
|
||||||
@@ -55,7 +55,7 @@ func Signup(c *fiber.Ctx) error {
|
|||||||
// Insert user
|
// Insert user
|
||||||
userID, err := database.InsertUser(database.DB, u.Username, string(passwordHash))
|
userID, err := database.InsertUser(database.DB, u.Username, string(passwordHash))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return helpers.NewError(helpers.ErrInternal, "Failed to create user", err)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate token
|
// Generate token
|
||||||
|
|||||||
@@ -9,8 +9,8 @@ import (
|
|||||||
|
|
||||||
func DeleteContact(c *fiber.Ctx) error {
|
func DeleteContact(c *fiber.Ctx) error {
|
||||||
type params struct {
|
type params struct {
|
||||||
ContactID uuid.UUID `params:"contact_id"`
|
ContactID uuid.UUID `params:"contactID"`
|
||||||
ConversationID uuid.UUID `params:"conversation_id"`
|
ConversationID uuid.UUID `params:"conversationID"`
|
||||||
}
|
}
|
||||||
|
|
||||||
p := new(params)
|
p := new(params)
|
||||||
@@ -27,7 +27,7 @@ func DeleteContact(c *fiber.Ctx) error {
|
|||||||
|
|
||||||
err := database.DeleteContact(database.DB, p.ContactID, p.ConversationID)
|
err := database.DeleteContact(database.DB, p.ContactID, p.ConversationID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return helpers.NewError(helpers.ErrInternal, "Failed to delete contact", err)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.Status(fiber.StatusOK).JSON(fiber.Map{"message": "Contact deleted"})
|
return c.Status(fiber.StatusOK).JSON(fiber.Map{"message": "Contact deleted"})
|
||||||
@@ -59,7 +59,7 @@ func InsertContact(c *fiber.Ctx) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !helpers.IsValidUsername(p.ContactUsername) {
|
if !helpers.IsValidUsername(p.ContactUsername) {
|
||||||
return helpers.NewError(helpers.ErrInvalidInput, "username is invalid", nil)
|
return helpers.NewError(helpers.ErrInvalidInput, "invalid username", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
contactID, err := database.GetUserID(database.DB, p.ContactUsername)
|
contactID, err := database.GetUserID(database.DB, p.ContactUsername)
|
||||||
@@ -94,3 +94,23 @@ func GetContacts(c *fiber.Ctx) error {
|
|||||||
|
|
||||||
return c.Status(fiber.StatusOK).JSON(contacts)
|
return c.Status(fiber.StatusOK).JSON(contacts)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetContactSuggestions(c *fiber.Ctx) error {
|
||||||
|
type params struct {
|
||||||
|
ContactUsername string `params:"contactUsername"`
|
||||||
|
}
|
||||||
|
|
||||||
|
p := new(params)
|
||||||
|
if err := c.ParamsParser(p); err != nil {
|
||||||
|
return helpers.NewError(helpers.ErrInvalidInput, "Invalid params", err)
|
||||||
|
}
|
||||||
|
if p.ContactUsername == "" {
|
||||||
|
return helpers.NewError(helpers.ErrInvalidInput, "contact username is empty", nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
suggestions, err := database.ContactSuggestion(database.DB, p.ContactUsername)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return c.Status(fiber.StatusOK).JSON(suggestions)
|
||||||
|
}
|
||||||
|
|||||||
@@ -21,3 +21,7 @@ type Contact struct {
|
|||||||
LastMessageTime string `json:"last_message_time"`
|
LastMessageTime string `json:"last_message_time"`
|
||||||
LastMessageSender string `json:"last_message_sender"`
|
LastMessageSender string `json:"last_message_sender"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ContactSuggestion struct {
|
||||||
|
Username string `json:"username"`
|
||||||
|
}
|
||||||
|
|||||||
@@ -25,4 +25,5 @@ func SetupRoutes(app *fiber.App) {
|
|||||||
contacts.Delete("/:contact_id/:conversation_id", handlers.DeleteContact)
|
contacts.Delete("/:contact_id/:conversation_id", handlers.DeleteContact)
|
||||||
contacts.Post("/:contact_username", handlers.InsertContact)
|
contacts.Post("/:contact_username", handlers.InsertContact)
|
||||||
contacts.Get("/", handlers.GetContacts)
|
contacts.Get("/", handlers.GetContacts)
|
||||||
|
contacts.Get("/suggestions/:contactUsername", handlers.GetContactSuggestions)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user