added contact suggestion route, code refactor
This commit is contained in:
@@ -40,7 +40,7 @@ func Signup(c *fiber.Ctx) error {
|
||||
// Check if user exists
|
||||
exist, err := database.CheckUserExists(database.DB, u.Username)
|
||||
if err != nil {
|
||||
return helpers.NewError(helpers.ErrInternal, "internal server error", fmt.Errorf("failed to check user existance: %w", err))
|
||||
return err
|
||||
}
|
||||
if exist {
|
||||
return helpers.NewError(helpers.ErrInvalidInput, "User already exists", nil)
|
||||
@@ -55,7 +55,7 @@ func Signup(c *fiber.Ctx) error {
|
||||
// Insert user
|
||||
userID, err := database.InsertUser(database.DB, u.Username, string(passwordHash))
|
||||
if err != nil {
|
||||
return helpers.NewError(helpers.ErrInternal, "Failed to create user", err)
|
||||
return err
|
||||
}
|
||||
|
||||
// Generate token
|
||||
|
||||
@@ -9,8 +9,8 @@ import (
|
||||
|
||||
func DeleteContact(c *fiber.Ctx) error {
|
||||
type params struct {
|
||||
ContactID uuid.UUID `params:"contact_id"`
|
||||
ConversationID uuid.UUID `params:"conversation_id"`
|
||||
ContactID uuid.UUID `params:"contactID"`
|
||||
ConversationID uuid.UUID `params:"conversationID"`
|
||||
}
|
||||
|
||||
p := new(params)
|
||||
@@ -27,7 +27,7 @@ func DeleteContact(c *fiber.Ctx) error {
|
||||
|
||||
err := database.DeleteContact(database.DB, p.ContactID, p.ConversationID)
|
||||
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"})
|
||||
@@ -59,7 +59,7 @@ func InsertContact(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
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)
|
||||
@@ -94,3 +94,23 @@ func GetContacts(c *fiber.Ctx) error {
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user