fix: verify uniqueness of channel

pull/1/head
Nilo Roberto C Paim 2023-09-05 17:36:34 -03:00
parent d72b4f4d63
commit 6b31e05338
2 changed files with 20 additions and 6 deletions

View File

@ -7,6 +7,7 @@ import (
"log"
"os"
"strconv"
"strings"
"time"
"github.com/dgrijalva/jwt-go"
@ -91,21 +92,33 @@ func AddUser(c *fiber.Ctx) error {
var data map[string]string
if err := c.BodyParser(&data); err != nil {
return fiber.ErrBadRequest
return c.JSON(fiber.Map{
"message": "Dados inválidos"})
}
if data["name"] == "" || data["email"] == "" || data["password"] == "" || data["channel"] == "" || data["usertype"] == "" || data["companyname"] == "" {
return fiber.ErrBadRequest
return c.JSON(fiber.Map{
"message": "Dados inválidos"})
}
passwd, _ := utils.HashPassword(data["password"])
user := models.User{
var user models.User
database.DB.Find(&user, "channel = ?", strings.ToLower(data["channel"]))
if user.Id == 0 {
return c.JSON(fiber.Map{
"message": "Canal já em uso"})
}
user = models.User{
Name: data["name"],
Email: data["email"],
CompanyName: data["companyname"],
Password: passwd,
Channel: data["channel"],
Channel: strings.ToLower(data["channel"]),
UserType: data["usertype"],
Blocked: "N",
Cancelled: "N",
@ -115,7 +128,8 @@ func AddUser(c *fiber.Ctx) error {
database.DB.Create(&user)
if user.Id == 0 {
return fiber.ErrNotAcceptable
return c.JSON(fiber.Map{
"message": "Erro ao criar usuário"})
}
return c.JSON(user)

View File

@ -86,7 +86,7 @@ func ConnectDB() error {
Email: "pcast@pcast.com.br",
CompanyName: "PCast",
Password: passwd,
Channel: "PCast",
Channel: "pcast",
UserType: "A",
Blocked: "N",
Cancelled: "N",