changed status code; added bat to compile for linux

pull/2/head
Nilo Roberto C Paim 2022-08-21 07:54:29 -03:00
parent 5da4004aa2
commit 3dd49332d5
5 changed files with 23 additions and 74 deletions

3
compile_linux.bat Normal file
View File

@ -0,0 +1,3 @@
set GOARCH=amd64
set GOOS=linux
go build

View File

@ -19,7 +19,7 @@ func Login(c *fiber.Ctx) error {
var data map[string]string
if err := c.BodyParser(&data); err != nil {
return err
return fiber.ErrBadRequest
}
var user models.User
@ -27,17 +27,11 @@ func Login(c *fiber.Ctx) error {
database.DB.Where("email = ?", data["email"]).First(&user)
if user.Id == 0 {
return c.JSON(fiber.Map{
"status": "error",
"message": "Usuário não cadastrado",
})
return fiber.ErrNotFound
}
if err := bcrypt.CompareHashAndPassword(user.Password, []byte(data["password"])); err != nil {
return c.JSON(fiber.Map{
"status": "error",
"message": "Senha incorreta",
})
return fiber.ErrUnauthorized
}
type customClaims struct {
@ -57,14 +51,10 @@ func Login(c *fiber.Ctx) error {
token, err := claims.SignedString([]byte(os.Getenv("API_SECRET")))
if err != nil {
return c.JSON(fiber.Map{
"status": "error",
"message": "(A1) - Erro na geração do token",
})
return fiber.ErrInternalServerError
}
return c.JSON(fiber.Map{
"status": "success",
"token": token})
}
@ -74,25 +64,19 @@ func AddUser(c *fiber.Ctx) error {
var data map[string]string
if err := c.BodyParser(&data); err != nil {
return err
return fiber.ErrBadRequest
}
// If I don't receive an auth field in the request, I need to verify if the sender is logged
if data["auth"] == "" {
_, err := utils.ProcessToken(c)
if err != nil {
return c.JSON(fiber.Map{
"status": "error",
"message": "Autorização inválida",
})
return fiber.ErrUnauthorized
}
}
if data["name"] == "" || data["email"] == "" || data["password"] == "" || data["channel"] == "" {
return c.JSON(fiber.Map{
"status": "error",
"message": "Nome, E-Mail, Senha e Nome do Canal devem ser informados",
})
return fiber.ErrBadRequest
}
passwd, _ := utils.HashPassword(data["password"])
@ -112,14 +96,10 @@ func AddUser(c *fiber.Ctx) error {
database.DB.Create(&user)
if user.Id == 0 {
return c.JSON(fiber.Map{
"status": "error",
"message": "Usuário já cadastrado",
})
return fiber.ErrNotAcceptable
}
return c.JSON(fiber.Map{
"status": "success",
"user": user,
})
}
@ -131,23 +111,16 @@ func GetOwnUser(c *fiber.Ctx) error {
userCode, err := utils.ProcessToken(c)
if err != nil {
return c.JSON(fiber.Map{
"status": "error",
"message": "Autorização inválida",
})
return fiber.ErrUnauthorized
}
database.DB.Where("id = ?", userCode).First(&user)
if user.Id == 0 {
return c.JSON(fiber.Map{
"status": "error",
"message": "(A2) - Autorização inválida",
})
return fiber.ErrUnauthorized
}
return c.JSON(fiber.Map{
"status": "success",
"user": user,
})
}
@ -159,23 +132,16 @@ func GetAllUsers(c *fiber.Ctx) error {
_, err := utils.ProcessToken(c)
if err != nil {
return c.JSON(fiber.Map{
"status": "error",
"message": "Autorização inválida",
})
return fiber.ErrUnauthorized
}
database.DB.Find(&users)
if len(users) == 0 {
return c.JSON(fiber.Map{
"status": "error",
"message": "Nenhum usuário encontrado",
})
return fiber.ErrNotFound
}
return c.JSON(fiber.Map{
"status": "success",
"users": users,
})
}

View File

@ -17,46 +17,31 @@ func AddEvent(c *fiber.Ctx) error {
_, err := utils.ProcessToken(c)
if err != nil {
return c.JSON(fiber.Map{
"status": "error",
"message": "Sem autorização",
})
return fiber.ErrUnauthorized
}
if err := c.BodyParser(&data); err != nil {
return err
return fiber.ErrBadRequest
}
u, err := strconv.Atoi(data["user"])
if err != nil {
return c.JSON(fiber.Map{
"status": "error",
"message": "Requisição inválida (usuário não informado)",
})
return fiber.ErrBadRequest
}
user := dbaccess.GetUserById(u)
if user.Id == 0 {
return c.JSON(fiber.Map{
"status": "error",
"message": "Requisição inválida (usuário inexistente)",
})
return fiber.ErrBadRequest
}
dateformat := "02/01/2006 15:04"
if data["startDt"] == "" {
return c.JSON(fiber.Map{
"status": "error",
"message": "Requisição inválida (sem data)",
})
return fiber.ErrBadRequest
} else {
startdt, err = time.Parse(dateformat, data["startDt"])
if err != nil {
return c.JSON(fiber.Map{
"status": "error",
"message": "Requisição inválida (data inválida)",
})
return fiber.ErrBadRequest
}
}
@ -72,14 +57,10 @@ func AddEvent(c *fiber.Ctx) error {
database.DB.Create(&event)
if event.Id == 0 {
return c.JSON(fiber.Map{
"status": "error",
"message": "(E1) - Não foi possível criar o evento",
})
return fiber.ErrNotAcceptable
}
return c.JSON(fiber.Map{
"status": "success",
"event": event,
})
}

View File

@ -4,6 +4,6 @@ import "github.com/gofiber/fiber/v2"
func Version(c *fiber.Ctx) error {
return c.JSON(fiber.Map{
"version": "1.0.11",
"version": "1.0.12",
})
}

View File

@ -52,7 +52,6 @@ func ProcessToken(c *fiber.Ctx) (interface{}, error) {
claims, ok := tk.Claims.(jwt.MapClaims)
if ok && tk.Valid {
fmt.Printf("claims user: %v\n", claims["user"])
return claims["user"], nil
}