Add DELETE endpoint for deleting users from db
I also finished converting the rest of the server responses to JSON
This commit is contained in:
parent
115110a19b
commit
da654e571b
|
@ -92,7 +92,7 @@ func server() {
|
|||
log.Fatalf("INVALID DB INFO: %v\nPlease refer to the example configuration file in the repo at https://git.bulgariu.xyz/raul/drahoot/src/branch/main/sample-config (default config path = ~/.config/drahoot/drahoot.toml)", err)
|
||||
}
|
||||
if err := openDB(); err != nil {
|
||||
log.Fatalf("Database machine broke: %v\n", err)
|
||||
log.Fatalf("Error happened trying to connect to database: %v", err)
|
||||
}
|
||||
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
|
@ -102,6 +102,7 @@ func server() {
|
|||
r.GET("/api/user", getUsers)
|
||||
r.GET("/api/user/:userid", getUser)
|
||||
r.POST("/api/user", createUser)
|
||||
r.DELETE("/api/user/:userid", deleteUser)
|
||||
|
||||
r.Run(":" + ListenPort)
|
||||
}
|
||||
|
@ -114,33 +115,51 @@ func hashPW(plain string) string {
|
|||
return sha256hash
|
||||
}
|
||||
|
||||
func deleteUser(c *gin.Context) {
|
||||
id := c.Param("userid")
|
||||
dynStmt := `DELETE FROM usuarios WHERE id_usuario = $1`
|
||||
|
||||
_, err := db.Exec(dynStmt, id)
|
||||
if err != nil {
|
||||
e := fmt.Sprintf("Something went wrong trying to delete the user: %v", err)
|
||||
log.Println(e)
|
||||
c.IndentedJSON(http.StatusInternalServerError, setResponse(e, false))
|
||||
return
|
||||
}
|
||||
e := fmt.Sprintf("User successfully deleted")
|
||||
c.IndentedJSON(http.StatusOK, setResponse(e, true))
|
||||
}
|
||||
|
||||
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)
|
||||
e := fmt.Sprintf("Something went wrong creating the user: %v", err)
|
||||
log.Println(e)
|
||||
c.IndentedJSON(http.StatusInternalServerError, setResponse(e, false))
|
||||
return
|
||||
}
|
||||
secret := hashPW(newuser.Password)
|
||||
var dynStmt string
|
||||
if newuser.AccountType != "estudiante" && newuser.AccountType != "profesor" {
|
||||
if newuser.AccountType == "admin" {
|
||||
c.String(http.StatusTeapot, "https://xkcd.com/327/\n")
|
||||
e := fmt.Sprintf("Nice try (https://xkcd.com/327/)")
|
||||
c.IndentedJSON(http.StatusTeapot, setResponse(e, false))
|
||||
return
|
||||
}
|
||||
c.String(http.StatusNotFound, "Invalid account type\n")
|
||||
e := fmt.Sprintf("Invalid account type\n")
|
||||
c.IndentedJSON(http.StatusNotFound, setResponse(e, false))
|
||||
return
|
||||
}
|
||||
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)
|
||||
e := fmt.Sprintf("Something went wrong trying to create the user: %v", err)
|
||||
log.Println(e)
|
||||
c.IndentedJSON(http.StatusInternalServerError, setResponse(e, false))
|
||||
return
|
||||
}
|
||||
c.String(http.StatusOK, "Success!\n")
|
||||
e := fmt.Sprintf("User %v has been created!", newuser.Name)
|
||||
c.IndentedJSON(http.StatusOK, setResponse(e, true))
|
||||
}
|
||||
|
||||
func setResponse(content any, success bool) response {
|
||||
|
@ -158,8 +177,8 @@ func getUser(c *gin.Context) {
|
|||
c.IndentedJSON(http.StatusNotFound, setResponse("User not found", false))
|
||||
return
|
||||
}
|
||||
e := fmt.Sprintf("SOMETHING BAD HAPPENED QUERYING THE DATABASE: %v\n", err)
|
||||
log.Print(e)
|
||||
e := fmt.Sprintf("SOMETHING BAD HAPPENED QUERYING THE DATABASE: %v", err)
|
||||
log.Println(e)
|
||||
c.IndentedJSON(http.StatusInternalServerError, setResponse(e, false))
|
||||
return
|
||||
}
|
||||
|
@ -170,8 +189,8 @@ func getUsers(c *gin.Context) {
|
|||
users := []user{}
|
||||
rows, err := db.Query("SELECT id_usuario,nombre,apellido1,apellido2,email,rol FROM usuarios")
|
||||
if err != nil {
|
||||
e := fmt.Sprintf("SOMETHING BAD HAPPENED QUERYING THE DATABASE: %v\n", err)
|
||||
log.Print(e)
|
||||
e := fmt.Sprintf("SOMETHING BAD HAPPENED QUERYING THE DATABASE: %v", err)
|
||||
log.Println(e)
|
||||
c.IndentedJSON(http.StatusInternalServerError, setResponse(e, false))
|
||||
return
|
||||
}
|
||||
|
@ -180,8 +199,8 @@ func getUsers(c *gin.Context) {
|
|||
user := user{}
|
||||
err = rows.Scan(&user.Id, &user.Name, &user.Surname1, &user.Surname2, &user.Email, &user.AccountType)
|
||||
if err != nil {
|
||||
e := fmt.Sprintf("SOMETHING BAD HAPPENED SCANNING THE ROWS: %v\n", err)
|
||||
log.Print(e)
|
||||
e := fmt.Sprintf("SOMETHING BAD HAPPENED SCANNING THE ROWS: %v", err)
|
||||
log.Println(e)
|
||||
c.IndentedJSON(http.StatusInternalServerError, setResponse(e, false))
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue