Merge teachers and students into one table

Using the AccountType attribute to store the user type
This commit is contained in:
raul 2024-12-10 13:52:59 +01:00
parent 1205572f1e
commit e13e9e2019
Signed by: raul
GPG Key ID: C1AA797073F17129
2 changed files with 17 additions and 13 deletions

View File

@ -124,19 +124,23 @@ func createUser(c *gin.Context) {
}
secret := hashPW(newuser.Password)
var dynStmt string
if newuser.IsTeacher == true {
dynStmt = `INSERT INTO profesores(nombre, apellido1, apellido2, email, password) values($1, $2, $3, $4, $5)`
} else {
dynStmt = `INSERT INTO alumnos(nombre, apellido1, apellido2, email, password) values($1, $2, $3, $4, $5)`
if newuser.AccountType != "estudiante" && newuser.AccountType != "profesor" {
if newuser.AccountType == "admin" {
c.String(http.StatusTeapot, "https://xkcd.com/327/\n")
return
}
c.String(http.StatusNotFound, "Invalid account type\n")
return
}
_, err := db.Exec(dynStmt, newuser.Name, newuser.Surname1, newuser.Surname2, newuser.Email, secret)
dynStmt = `INSERT INTO usuarios(nombre, apellido1, apellido2, email, password, rol) values($1, $2, $3, $4, $5, $6)`
_, err := db.Exec(dynStmt, newuser.Name, newuser.Surname1, newuser.Surname2, newuser.Email, secret, newuser.AccountType)
if err != nil {
e := fmt.Sprintf("Something went wrong trying to create the user: %v\n", err)
log.Print(e)
c.String(http.StatusInternalServerError, e)
return
}
c.String(http.StatusOK, "Success!")
c.String(http.StatusOK, "Success!\n")
}
func getUser(c *gin.Context) {

View File

@ -1,11 +1,11 @@
package cmd
type user struct {
Id int `json:"id"`
Name string `json:"nombre"`
Surname1 string `json:"apellido1"`
Surname2 string `json:"apellido2"`
Email string `json:"email"`
Password string `json:"password"`
IsTeacher bool `json:"esprofesor"`
Id int `json:"id"`
Name string `json:"nombre"`
Surname1 string `json:"apellido1"`
Surname2 string `json:"apellido2"`
Email string `json:"email"`
Password string `json:"password"`
AccountType string `json:"type"`
}