added delete contact route

This commit is contained in:
slawk0
2025-02-01 17:05:43 +01:00
parent 86c43fffc0
commit cd83870ab4
8 changed files with 163 additions and 37 deletions

View File

@@ -9,27 +9,34 @@ import (
"relay-server/config"
"relay-server/database"
"relay-server/helpers"
"relay-server/model"
"time"
)
func Signup(c *fiber.Ctx) error {
type SignupStruct struct {
Username string `json:"username" xml:"username" form:"username"`
Password string `json:"password" xml:"password" form:"password"`
}
db := database.DB
u := new(model.SignupStruct)
u := new(SignupStruct)
if err := c.BodyParser(u); err != nil {
return err
}
// Checks if username or passwords are empty
if u.Username == "" {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "username is empty"})
} else if u.Password == "" {
}
if u.Password == "" {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "password is empty"})
}
// Checks if passwords or username have valid length and characters
if !helpers.IsValidPassword(u.Password) {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "invalid password"})
} else if !helpers.IsValidUsername(u.Username) {
}
if !helpers.IsValidUsername(u.Username) {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "invalid username"})
}
@@ -74,8 +81,14 @@ func Signup(c *fiber.Ctx) error {
}
func Login(c *fiber.Ctx) error {
type loginStruct struct {
Username string `json:"username" xml:"username" form:"username"`
Password string `json:"password" xml:"password" form:"password"`
}
db := database.DB
u := new(model.LoginStruct)
u := new(loginStruct)
if err := c.BodyParser(u); err != nil {
return err
@@ -84,14 +97,16 @@ func Login(c *fiber.Ctx) error {
// Checks if username or passwords are empty
if u.Username == "" {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "username is empty"})
} else if u.Password == "" {
}
if u.Password == "" {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "password is empty"})
}
// Checks if username or passwords have valid length and characters
if !helpers.IsValidUsername(u.Username) {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "invalid username"})
} else if !helpers.IsValidPassword(u.Password) {
}
if !helpers.IsValidPassword(u.Password) {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "invalid password"})
}

42
handlers/contacts.go Normal file
View File

@@ -0,0 +1,42 @@
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"})
}