Allow adding users to database
This commit is contained in:
parent
2093e60d4f
commit
1205572f1e
|
@ -1,7 +1,9 @@
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/sha256"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -97,12 +99,46 @@ func server() {
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
r.GET("/", helloWorld)
|
r.GET("/", helloWorld)
|
||||||
// TODO: Have fun creating new endpoints
|
// TODO: Have fun creating new endpoints
|
||||||
r.GET("/api/users", getUsers)
|
r.GET("/api/user", getUsers)
|
||||||
r.GET("/api/user/:userid", getUser)
|
r.GET("/api/user/:userid", getUser)
|
||||||
|
r.POST("/api/user", createUser)
|
||||||
|
|
||||||
r.Run(":" + ListenPort)
|
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) {
|
func getUser(c *gin.Context) {
|
||||||
id := c.Param("userid")
|
id := c.Param("userid")
|
||||||
user := user{}
|
user := user{}
|
||||||
|
@ -145,30 +181,6 @@ func getUsers(c *gin.Context) {
|
||||||
c.IndentedJSON(http.StatusOK, users)
|
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) {
|
func helloWorld(c *gin.Context) {
|
||||||
ua := c.Request.UserAgent()
|
ua := c.Request.UserAgent()
|
||||||
message := fmt.Sprintf("Hello %v!\n", ua)
|
message := fmt.Sprintf("Hello %v!\n", ua)
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
type user struct {
|
type user struct {
|
||||||
Id int `json:"id"`
|
Id int `json:"id"`
|
||||||
Name string `json:"nombre"`
|
Name string `json:"nombre"`
|
||||||
Surname1 string `json:"apellido1"`
|
Surname1 string `json:"apellido1"`
|
||||||
Surname2 string `json:"apellido2"`
|
Surname2 string `json:"apellido2"`
|
||||||
Email string `json:"email"`
|
Email string `json:"email"`
|
||||||
|
Password string `json:"password"`
|
||||||
|
IsTeacher bool `json:"esprofesor"`
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue