Added method to delete a user
This commit is contained in:
parent
0027b1f001
commit
4275a3acad
|
@ -0,0 +1,20 @@
|
|||
package models
|
||||
|
||||
import "fmt"
|
||||
|
||||
// DeleteUserByID deletes a user by its ID
|
||||
func DeleteUserByID(id int64) error {
|
||||
// Check if the id is 0
|
||||
if id == 0 {
|
||||
return fmt.Errorf("ID cannot be 0")
|
||||
}
|
||||
|
||||
// Delete the user
|
||||
_, err := x.Id(id).Delete(&User{})
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
package v1
|
||||
|
||||
import (
|
||||
"git.mowie.cc/konrad/Library/models"
|
||||
"github.com/labstack/echo"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// UserDelete is the handler to delete a user
|
||||
func UserDelete(c echo.Context) error {
|
||||
|
||||
id := c.Param("id")
|
||||
|
||||
// Make int
|
||||
userID, err := strconv.ParseInt(id, 10, 64)
|
||||
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusBadRequest, models.Message{"User ID is invalid."})
|
||||
}
|
||||
|
||||
// Check if the user exists
|
||||
_, exists, err := models.GetUserByID(userID)
|
||||
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, models.Message{"Could not get user."})
|
||||
}
|
||||
|
||||
if !exists {
|
||||
return c.JSON(http.StatusNotFound, models.Message{"The user does not exist."})
|
||||
}
|
||||
|
||||
// Delete it
|
||||
err = models.DeleteUserByID(userID)
|
||||
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, models.Message{"Could not delete user."})
|
||||
}
|
||||
|
||||
// Log the action
|
||||
err = models.LogAction("Deleted a user", userID, c)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, models.Message{"Could not log."})
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, models.Message{"success"})
|
||||
}
|
|
@ -112,6 +112,7 @@ func RegisterRoutes(e *echo.Echo) {
|
|||
a.PUT("/users", apiv1.UserAddOrUpdate)
|
||||
a.POST("/users/:id", apiv1.UserAddOrUpdate)
|
||||
a.GET("/users/:id", apiv1.UserShow)
|
||||
a.DELETE("/users/:id", apiv1.UserDelete)
|
||||
|
||||
// Manage Users
|
||||
|
||||
|
|
Loading…
Reference in New Issue