diff --git a/core/data/database/projects.go b/core/data/database/projects.go index cda490f..79a707b 100644 --- a/core/data/database/projects.go +++ b/core/data/database/projects.go @@ -32,3 +32,7 @@ func GetProjectNames() (rtn []*models.Project, err error) { func GetProject(uuid string) (rtn *models.Project, err error) { return rtn, DB.Where(&models.Project{UUID: uuid}).Preload("Tags").First(&rtn).Error } + +func DeleteProject(uuid string) (err error) { + return DB.Where(&models.Project{UUID: uuid}).Delete(&models.Project{}).Error +} diff --git a/core/projects/endpoints.go b/core/projects/endpoints.go index 1822026..037cfb0 100644 --- a/core/projects/endpoints.go +++ b/core/projects/endpoints.go @@ -388,3 +388,32 @@ func setMainImageHandler(c echo.Context) error { Path string `json:"path"` }{project.UUID, project.DefaultImageID}) } + +func deleteHandler(c echo.Context) error { + + uuid := c.Param("uuid") + + if uuid == "" { + return c.NoContent(http.StatusBadRequest) + } + project, err := database.GetProject(uuid) + + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return echo.NewHTTPError(http.StatusNotFound, err.Error()) + } + log.Println(err) + return echo.NewHTTPError(http.StatusInternalServerError, err.Error()) + } + + err = os.RemoveAll(utils.ToLibPath(project.FullPath())) + if err != nil { + return echo.NewHTTPError(http.StatusInternalServerError, err) + } + + if err := database.DeleteProject(uuid); err != nil { + return echo.NewHTTPError(http.StatusInternalServerError, err) + } + + return c.NoContent(http.StatusOK) +} diff --git a/core/projects/projects.go b/core/projects/projects.go index e030567..9bc360e 100644 --- a/core/projects/projects.go +++ b/core/projects/projects.go @@ -17,5 +17,6 @@ func Register(e *echo.Group) { group.POST("/:uuid", save) group.POST("/:uuid/move", moveHandler) group.POST("/:uuid/image", setMainImageHandler) + group.POST("/:uuid/delete", deleteHandler) group.POST("", new) }