createuser endpoint added

main
Nilo Roberto C Paim 2024-06-06 16:55:05 -03:00
parent fd78543cad
commit c373a34578
4 changed files with 39 additions and 33 deletions

View File

@ -15,7 +15,7 @@ import (
"golang.org/x/crypto/bcrypt"
)
// Login - Login. Returns a JWT token on the response body
// MARK: Login
func Login(c *fiber.Ctx) error {
var data map[string]string
@ -72,7 +72,7 @@ func Login(c *fiber.Ctx) error {
"token": tk})
}
// Register/AdUser - Adds or register a user to the database
// MARK: AddUser
func AddUser(c *fiber.Ctx) error {
var data map[string]string
@ -129,7 +129,7 @@ func AddUser(c *fiber.Ctx) error {
return c.JSON(user)
}
// GetOwnUser - Returns the current user
// MARK: GetOwnUser
func GetOwnUser(c *fiber.Ctx) error {
user := c.Locals("user").(models.User)
@ -137,7 +137,7 @@ func GetOwnUser(c *fiber.Ctx) error {
return c.JSON(user)
}
// GetAllUsers - Returns all users
// MARK: GetAllUsers
func GetAllUsers(c *fiber.Ctx) error {
var users []models.User
@ -157,6 +157,7 @@ func GetAllUsers(c *fiber.Ctx) error {
return c.JSON(users)
}
// MARK: GetUserByEmail
func GetUserByEmail(c *fiber.Ctx) error {
user := c.Locals("user").(models.User)
@ -176,6 +177,30 @@ func GetUserByEmail(c *fiber.Ctx) error {
return c.JSON(user)
}
func CreateUser(c *fiber.Ctx) error {
type CreateUser struct {
Email string `json:"email"`
Type string `json:"type"`
}
var u CreateUser
if err := c.BodyParser(&u); err != nil {
return fiber.ErrBadRequest
}
user := models.User{
Email: u.Email,
UserType: u.Type,
}
database.DB.Create(&user)
return c.JSON(user)
}
// MARK: Checkuser
func Checkuser(c *fiber.Ctx) error {
type Checkuser struct {
@ -184,7 +209,6 @@ func Checkuser(c *fiber.Ctx) error {
}
var user models.User
var usertest models.UserTest
var response Checkuser
email := c.Params("email")
@ -195,22 +219,14 @@ func Checkuser(c *fiber.Ctx) error {
database.DB.Where("email = ?", email).First(&user)
if user.ID == 0 {
// The user is not found; let's check if it's a test user
database.DB.Where("email = ?", email).First(&usertest)
if usertest.ID == 0 {
// The user is not found for eligible tester; let's signalize this
// The user is not found, so it's an eligible tester; let's signalize this
response.Type = "T"
} else {
// The user is found for eligible tester; but let's check if it's test already done
response.Type = "RT" // Eligible tester but the test was not done already
// The user is found and it's an admin; let's signalize this
if user.UserType == "A" {
response.Type = "A"
}
if usertest.Status == "TD" {
// The user is found for eligible tester; and the test is done
response.Type = "TD"
}
}
} else {
// The user is found and it's a pay user; let's signalize this
if user.UserType == "U" {
// If the user doesn't have a channel, it's a pay user that needs to complete the registration

View File

@ -50,10 +50,6 @@ func ConnectDB() error {
return result
}
if result := db.AutoMigrate(&models.UserTest{}); result != nil {
return result
}
if result := db.AutoMigrate(&models.Event{}); result != nil {
return result
}

View File

@ -14,16 +14,9 @@ type User struct {
Channel string `gorm:"size:40" json:"channel"`
Url string `gorm:"size:40" json:"url"`
CpfCnpj string `gorm:"size:20" json:"cpfcnpj"`
UserType string `gorm:"size:1;default:U" json:"usertype"` // A - Admin, U - User, S - Social
UserType string `gorm:"size:2;default:T" json:"usertype"` // A - Admin, U - User, S - Social, T - Test
Blocked string `gorm:"size:1;default:N" json:"blocked"`
Cancelled string `gorm:"size:1;default:N" json:"cancelled"`
ServerId uint `gorm:"not null;default:1" json:"serverid"`
}
type UserTest struct {
gorm.Model
Name string `gorm:"size:40" json:"name"`
Email string `gorm:"size:40;unique" json:"email"`
Password []byte `gorm:"size:100;" json:"-"`
Status string `gorm:"size:2" json:"status"` // TR - Teste Realizado, TS - Aguardando
TestStatus string `gorm:"size:2" json:"status"` // TR - Teste Realizado, TS - Aguardando
}

View File

@ -18,6 +18,7 @@ func Setup(app *fiber.App) {
app.Post("/login", controllers.Login)
app.Post("/register", controllers.AddUser)
app.Post("/create", controllers.CreateUser)
app.Get("/checkuser/:email", controllers.Checkuser)