implemented new error handler, added getContacts route
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
@@ -127,8 +128,12 @@ func Login(c *fiber.Ctx) error {
|
||||
|
||||
userID, err := database.GetUserID(db, u.Username)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "internal server error"})
|
||||
var e *helpers.Error
|
||||
if errors.As(err, &e) {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"message": "user does not exist"})
|
||||
}
|
||||
log.Println(err)
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"message": "Internal server error"})
|
||||
}
|
||||
// Generate token with user id and username
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
||||
|
||||
@@ -1,18 +1,13 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/google/uuid"
|
||||
"log"
|
||||
"relay-server/database"
|
||||
"relay-server/helpers"
|
||||
)
|
||||
|
||||
func DeleteContact(c *fiber.Ctx) error {
|
||||
|
||||
type params struct {
|
||||
ContactID uuid.UUID `params:"contact_id"`
|
||||
ConversationID uuid.UUID `params:"conversation_id"`
|
||||
@@ -20,74 +15,85 @@ func DeleteContact(c *fiber.Ctx) error {
|
||||
|
||||
p := new(params)
|
||||
if err := c.ParamsParser(p); err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid params"})
|
||||
return helpers.NewError(helpers.ErrInvalidInput, "Invalid params", err)
|
||||
}
|
||||
|
||||
db := database.DB
|
||||
|
||||
if p.ContactID == uuid.Nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"message": "contact_id is empty"})
|
||||
return helpers.NewError(helpers.ErrInvalidInput, "contact ID is empty", nil)
|
||||
}
|
||||
if p.ConversationID == uuid.Nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"message": "conversation_id is empty"})
|
||||
return helpers.NewError(helpers.ErrInvalidInput, "conversation ID is empty", nil)
|
||||
}
|
||||
|
||||
msg, err := database.DeleteContact(db, p.ContactID, p.ConversationID)
|
||||
msg, err := database.DeleteContact(database.DB, p.ContactID, p.ConversationID)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Internal server error"})
|
||||
return helpers.NewError(helpers.ErrInternal, "Failed to delete contact", err)
|
||||
}
|
||||
if msg != "" {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"message": msg})
|
||||
return helpers.NewError(helpers.ErrInvalidInput, msg, nil)
|
||||
}
|
||||
fmt.Println("Contact deleted")
|
||||
|
||||
return c.Status(fiber.StatusOK).JSON(fiber.Map{"message": "Contact deleted"})
|
||||
}
|
||||
|
||||
func InsertContact(c *fiber.Ctx) error {
|
||||
userID, err := uuid.Parse(c.Locals("userID").(string))
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"message": "Internal server error"})
|
||||
}
|
||||
type params struct {
|
||||
ContactUsername string `params:"contact_username"`
|
||||
}
|
||||
|
||||
p := new(params)
|
||||
|
||||
if err := c.ParamsParser(p); err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid params"})
|
||||
userIDVal := c.Locals("userID")
|
||||
userIDStr, ok := userIDVal.(string)
|
||||
if !ok {
|
||||
return helpers.NewError(helpers.ErrInternal, "Internal server error", nil)
|
||||
}
|
||||
|
||||
db := database.DB
|
||||
userID, err := uuid.Parse(userIDStr)
|
||||
if err != nil {
|
||||
return helpers.NewError(helpers.ErrInvalidInput, "Invalid user ID format", err)
|
||||
}
|
||||
|
||||
p := new(params)
|
||||
if err := c.ParamsParser(p); err != nil {
|
||||
return helpers.NewError(helpers.ErrInvalidInput, "Invalid params", err)
|
||||
}
|
||||
|
||||
if p.ContactUsername == "" {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"message": "contact_username is empty"})
|
||||
return helpers.NewError(helpers.ErrInvalidInput, "contact username is empty", nil)
|
||||
}
|
||||
|
||||
if !helpers.IsValidUsername(p.ContactUsername) {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"message": "username is invalid"})
|
||||
return helpers.NewError(helpers.ErrInvalidInput, "username is invalid", nil)
|
||||
}
|
||||
|
||||
contactID, err := database.GetUserID(db, p.ContactUsername)
|
||||
|
||||
contactID, err := database.GetUserID(database.DB, p.ContactUsername)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"message": "user does not exist"})
|
||||
}
|
||||
log.Println(err)
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"message": "Internal server error"})
|
||||
return err
|
||||
}
|
||||
|
||||
newContacts, err := database.InsertContact(db, userID, contactID, p.ContactUsername)
|
||||
newContacts, err := database.InsertContact(database.DB, userID, contactID, p.ContactUsername)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"message": "Internal server error"})
|
||||
return err
|
||||
}
|
||||
|
||||
log.Println("Contact added")
|
||||
return c.Status(fiber.StatusOK).JSON(newContacts)
|
||||
|
||||
}
|
||||
|
||||
func GetContacts(c *fiber.Ctx) error {
|
||||
userIDVal := c.Locals("userID")
|
||||
userIDStr, ok := userIDVal.(string)
|
||||
if !ok {
|
||||
return helpers.NewError(helpers.ErrInternal, "Invalid user session", nil)
|
||||
}
|
||||
|
||||
userID, err := uuid.Parse(userIDStr)
|
||||
if err != nil {
|
||||
return helpers.NewError(helpers.ErrInvalidInput, "Invalid user ID format", err)
|
||||
}
|
||||
|
||||
contacts, err := database.GetContacts(database.DB, userID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).JSON(contacts)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user