43 lines
1.1 KiB
Go
43 lines
1.1 KiB
Go
package handlers
|
|
|
|
import (
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/google/uuid"
|
|
"log"
|
|
"relay-server/database"
|
|
)
|
|
|
|
func DeleteContact(c *fiber.Ctx) error {
|
|
|
|
type params struct {
|
|
ContactId uuid.UUID `params:"contact_id"`
|
|
ConversationId uuid.UUID `params:"conversation_id"`
|
|
}
|
|
|
|
p := new(params)
|
|
if err := c.ParamsParser(p); err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid params"})
|
|
}
|
|
|
|
db := database.DB
|
|
|
|
if p.ContactId == uuid.Nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"message": "contact_id is empty"})
|
|
}
|
|
if p.ConversationId == uuid.Nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"message": "conversation_id is empty"})
|
|
}
|
|
|
|
msg, err := database.DeleteContact(db, p.ContactId, p.ConversationId)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Failed to delete contact"})
|
|
}
|
|
if msg != "" {
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"message": msg})
|
|
}
|
|
log.Println("Contact deleted")
|
|
|
|
return c.Status(fiber.StatusOK).JSON(fiber.Map{"message": "Contact deleted"})
|
|
}
|