Allow adding users to database

This commit is contained in:
raul 2024-12-10 11:36:25 +01:00
parent 2093e60d4f
commit 1205572f1e
Signed by: raul
GPG Key ID: C1AA797073F17129
2 changed files with 44 additions and 30 deletions

View File

@ -1,7 +1,9 @@
package cmd
import (
"crypto/sha256"
"database/sql"
"encoding/hex"
"fmt"
"log"
"net/http"
@ -97,12 +99,46 @@ func server() {
r := gin.Default()
r.GET("/", helloWorld)
// TODO: Have fun creating new endpoints
r.GET("/api/users", getUsers)
r.GET("/api/user", getUsers)
r.GET("/api/user/:userid", getUser)
r.POST("/api/user", createUser)
r.Run(":" + ListenPort)
}
func hashPW(plain string) string {
hashedPW := sha256.New()
hashedPW.Write([]byte(plain))
sha256hash := hex.EncodeToString(hashedPW.Sum(nil))
return sha256hash
}
func createUser(c *gin.Context) {
newuser := user{}
if err := c.BindJSON(&newuser); err != nil {
e := fmt.Sprintf("Something went wrong creating the user: %v\n", err)
log.Print(e)
c.String(http.StatusInternalServerError, e)
return
}
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)`
}
_, err := db.Exec(dynStmt, newuser.Name, newuser.Surname1, newuser.Surname2, newuser.Email, secret)
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!")
}
func getUser(c *gin.Context) {
id := c.Param("userid")
user := user{}
@ -145,30 +181,6 @@ func getUsers(c *gin.Context) {
c.IndentedJSON(http.StatusOK, users)
}
// func createUser(c *gin.Context) {
// var newUser user
// if err := c.BindJSON(&newUser); err != nil {
// return
// }
// dynStmt := `INSERT INTO "alumnos"("nombre", "apellido1", "apellido2", "email") values($1, $2, $3, $4)`
// db, err := openDB()
// if err != nil {
// e := fmt.Sprintf("We couldn't complete your request because the database server exploded, more at 11: %v\n", err)
// c.String(http.StatusInternalServerError, e)
// return
// }
//
// _, e := db.Exec(dynStmt, newUser.Name, newUser.Surname1, newUser.Surname2, newUser.Email)
// if e != nil {
// e := fmt.Sprintf("We couldn't complete your request because the database server imploded, more at 11: %v\n", err)
// c.String(http.StatusInternalServerError, e)
// return
// }
//
// fmt.Println(newUser)
// c.String(http.StatusOK, "Everything's fine!\n")
// }
func helloWorld(c *gin.Context) {
ua := c.Request.UserAgent()
message := fmt.Sprintf("Hello %v!\n", ua)

View File

@ -1,9 +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"`
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"`
}