Allow modifying users via PUT
I finally finished the CRUD for the users! Only like, 4 more CRUDs to go...
This commit is contained in:
parent
aebdb733a8
commit
6014213036
|
@ -26,66 +26,6 @@ var (
|
||||||
DB_Name string
|
DB_Name string
|
||||||
)
|
)
|
||||||
|
|
||||||
func getDBInfo() error {
|
|
||||||
dbhost := viper.GetString("Server.DB_Host")
|
|
||||||
if dbhost == "" {
|
|
||||||
e := fmt.Errorf("No database IP address present in config file!\n")
|
|
||||||
return e
|
|
||||||
} else {
|
|
||||||
DB_Host = dbhost
|
|
||||||
}
|
|
||||||
|
|
||||||
dbport := viper.GetString("Server.DB_Port")
|
|
||||||
if dbport == "" {
|
|
||||||
e := fmt.Errorf("No database port present in config file!\n")
|
|
||||||
return e
|
|
||||||
} else {
|
|
||||||
DB_Port = dbport
|
|
||||||
}
|
|
||||||
|
|
||||||
dbuser := viper.GetString("Server.DB_User")
|
|
||||||
if dbuser == "" {
|
|
||||||
e := fmt.Errorf("No database username present in config file!\n")
|
|
||||||
return e
|
|
||||||
} else {
|
|
||||||
DB_User = dbuser
|
|
||||||
}
|
|
||||||
|
|
||||||
dbpass := viper.GetString("Server.DB_Pass")
|
|
||||||
if dbpass == "" {
|
|
||||||
e := fmt.Errorf("No database password present in config file!\n")
|
|
||||||
return e
|
|
||||||
} else {
|
|
||||||
DB_Pass = dbpass
|
|
||||||
}
|
|
||||||
|
|
||||||
dbname := viper.GetString("Server.DB_Name")
|
|
||||||
if dbname == "" {
|
|
||||||
e := fmt.Errorf("No database name present in config file!\n")
|
|
||||||
return e
|
|
||||||
} else {
|
|
||||||
DB_Name = dbname
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func openDB() error {
|
|
||||||
psqlconn := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable", DB_Host, DB_Port, DB_User, DB_Pass, DB_Name)
|
|
||||||
dba, err := sql.Open("postgres", psqlconn)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
db = dba
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func setPort() {
|
|
||||||
p := viper.GetString("Server.Port")
|
|
||||||
if p != "" {
|
|
||||||
ListenPort = p
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func server() {
|
func server() {
|
||||||
log.SetPrefix("[DRAHOOT] ")
|
log.SetPrefix("[DRAHOOT] ")
|
||||||
setPort()
|
setPort()
|
||||||
|
@ -98,23 +38,45 @@ func server() {
|
||||||
|
|
||||||
gin.SetMode(gin.ReleaseMode)
|
gin.SetMode(gin.ReleaseMode)
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
|
r.GET("/api/ping", ping)
|
||||||
//r.GET("/", helloWorld)
|
//r.GET("/", helloWorld)
|
||||||
// TODO: Have fun creating new endpoints
|
// TODO: Have fun creating new endpoints
|
||||||
r.GET("/api/user", getUsers)
|
r.GET("/api/user", getUsers)
|
||||||
r.GET("/api/user/:userid", getUser)
|
r.GET("/api/user/:userid", getUser)
|
||||||
r.POST("/api/user", createUser)
|
r.POST("/api/user", createUser)
|
||||||
r.DELETE("/api/user/:userid", deleteUser)
|
r.DELETE("/api/user/:userid", deleteUser)
|
||||||
r.GET("/api/ping", ping)
|
r.PUT("/api/user/:userid", modifyUser)
|
||||||
|
|
||||||
r.Run(":" + ListenPort)
|
r.Run(":" + ListenPort)
|
||||||
}
|
}
|
||||||
|
|
||||||
func hashPW(plain string) string {
|
func modifyUser(c *gin.Context) {
|
||||||
hashedPW := sha256.New()
|
id := c.Param("userid")
|
||||||
hashedPW.Write([]byte(plain))
|
dynStmt := `UPDATE usuarios SET email=$1,nombre=$2,apellido1=$3,apellido2=$4,password=$5 WHERE id_usuario = $6`
|
||||||
sha256hash := hex.EncodeToString(hashedPW.Sum(nil))
|
|
||||||
|
|
||||||
return sha256hash
|
_, err := strconv.Atoi(id)
|
||||||
|
if err != nil {
|
||||||
|
e := fmt.Sprintf("Invalid identifier")
|
||||||
|
c.IndentedJSON(http.StatusNotFound, setResponse(e, false))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
user := user{}
|
||||||
|
if err := c.BindJSON(&user); err != nil {
|
||||||
|
e := fmt.Sprintf("Something went wrong updating the user: %v", err)
|
||||||
|
log.Println(e)
|
||||||
|
c.IndentedJSON(http.StatusInternalServerError, setResponse(e, false))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = db.Exec(dynStmt, user.Email, user.Name, user.Surname1, user.Surname2, hashPW(user.Password), id)
|
||||||
|
if err != nil {
|
||||||
|
e := fmt.Sprintf("Something went wrong trying to modify the user: %v", err)
|
||||||
|
log.Println(e, user.Email, user.Name, user.Surname1, user.Surname2, hashPW(user.Password), id)
|
||||||
|
c.IndentedJSON(http.StatusInternalServerError, setResponse(e, false))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.IndentedJSON(http.StatusOK, setResponse(user, true))
|
||||||
}
|
}
|
||||||
|
|
||||||
func deleteUser(c *gin.Context) {
|
func deleteUser(c *gin.Context) {
|
||||||
|
@ -147,7 +109,7 @@ func createUser(c *gin.Context) {
|
||||||
c.IndentedJSON(http.StatusInternalServerError, setResponse(e, false))
|
c.IndentedJSON(http.StatusInternalServerError, setResponse(e, false))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
secret := hashPW(newuser.Password)
|
|
||||||
var dynStmt string
|
var dynStmt string
|
||||||
if newuser.AccountType != "estudiante" && newuser.AccountType != "profesor" {
|
if newuser.AccountType != "estudiante" && newuser.AccountType != "profesor" {
|
||||||
if newuser.AccountType == "admin" {
|
if newuser.AccountType == "admin" {
|
||||||
|
@ -160,7 +122,7 @@ func createUser(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
dynStmt = `INSERT INTO usuarios(nombre, apellido1, apellido2, email, password, rol) values($1, $2, $3, $4, $5, $6)`
|
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)
|
_, err := db.Exec(dynStmt, newuser.Name, newuser.Surname1, newuser.Surname2, newuser.Email, hashPW(newuser.Password), newuser.AccountType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
e := fmt.Sprintf("Something went wrong trying to create the user: %v", err)
|
e := fmt.Sprintf("Something went wrong trying to create the user: %v", err)
|
||||||
log.Println(e)
|
log.Println(e)
|
||||||
|
@ -229,3 +191,70 @@ func getUsers(c *gin.Context) {
|
||||||
func ping(c *gin.Context) {
|
func ping(c *gin.Context) {
|
||||||
c.IndentedJSON(http.StatusOK, setResponse("Pong!", true))
|
c.IndentedJSON(http.StatusOK, setResponse("Pong!", true))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func hashPW(plain string) string {
|
||||||
|
hashedPW := sha256.New()
|
||||||
|
hashedPW.Write([]byte(plain))
|
||||||
|
sha256hash := hex.EncodeToString(hashedPW.Sum(nil))
|
||||||
|
return sha256hash
|
||||||
|
}
|
||||||
|
|
||||||
|
func getDBInfo() error {
|
||||||
|
dbhost := viper.GetString("Server.DB_Host")
|
||||||
|
if dbhost == "" {
|
||||||
|
e := fmt.Errorf("No database IP address present in config file!\n")
|
||||||
|
return e
|
||||||
|
} else {
|
||||||
|
DB_Host = dbhost
|
||||||
|
}
|
||||||
|
|
||||||
|
dbport := viper.GetString("Server.DB_Port")
|
||||||
|
if dbport == "" {
|
||||||
|
e := fmt.Errorf("No database port present in config file!\n")
|
||||||
|
return e
|
||||||
|
} else {
|
||||||
|
DB_Port = dbport
|
||||||
|
}
|
||||||
|
|
||||||
|
dbuser := viper.GetString("Server.DB_User")
|
||||||
|
if dbuser == "" {
|
||||||
|
e := fmt.Errorf("No database username present in config file!\n")
|
||||||
|
return e
|
||||||
|
} else {
|
||||||
|
DB_User = dbuser
|
||||||
|
}
|
||||||
|
|
||||||
|
dbpass := viper.GetString("Server.DB_Pass")
|
||||||
|
if dbpass == "" {
|
||||||
|
e := fmt.Errorf("No database password present in config file!\n")
|
||||||
|
return e
|
||||||
|
} else {
|
||||||
|
DB_Pass = dbpass
|
||||||
|
}
|
||||||
|
|
||||||
|
dbname := viper.GetString("Server.DB_Name")
|
||||||
|
if dbname == "" {
|
||||||
|
e := fmt.Errorf("No database name present in config file!\n")
|
||||||
|
return e
|
||||||
|
} else {
|
||||||
|
DB_Name = dbname
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func openDB() error {
|
||||||
|
psqlconn := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable", DB_Host, DB_Port, DB_User, DB_Pass, DB_Name)
|
||||||
|
dba, err := sql.Open("postgres", psqlconn)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
db = dba
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func setPort() {
|
||||||
|
p := viper.GetString("Server.Port")
|
||||||
|
if p != "" {
|
||||||
|
ListenPort = p
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue