-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_delete_user.go
61 lines (49 loc) · 1.76 KB
/
db_delete_user.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
)
// deleteUser handles the deletion of a user based on the provided "id" query parameter.
// It first checks if the "id" parameter is provided and returns a 400 Bad Request status if it is missing.
// Then, it attempts to delete the user with the corresponding ID from the database using the deleteUserByID function.
// If there is an error deleting the user, it returns a 500 Internal Server Error status.
// Otherwise, it returns a success response indicating that the user has been deleted.
//
// The success response is encoded as JSON and sent in the response body.
func DeleteUser(w http.ResponseWriter, r *http.Request) {
// Get the value of the "id" query parameter
id := r.URL.Query().Get("id")
// Handle the case where ID is not provided
if id == "" {
http.Error(w, "ID parameter is required", http.StatusBadRequest)
return
}
// Delete the user by ID
err := deleteUserByID(id)
if err != nil {
http.Error(w, "Error deleting user", http.StatusInternalServerError)
return
}
// Return success response
response := map[string]string{
"message": fmt.Sprintf("User with ID %s has been deleted", id),
}
log.Printf("Delete user ID: %s successful", id)
json.NewEncoder(w).Encode(response)
}
// deleteUserByID deletes the user from the database with the provided ID.
// It executes a SQL delete query to remove the user from the "users" table.
// If there is an error executing the delete query, it returns the error.
// Otherwise, it returns nil to indicate successful deletion.
func deleteUserByID(id string) error {
// Initialize the SQL delete query
query := "DELETE FROM users WHERE id = ?"
// Execute the delete query
_, err := db.Exec(query, id)
if err != nil {
return err
}
return nil
}