diff --git a/controller/park.go b/controller/park.go index 08ca69e..f441842 100644 --- a/controller/park.go +++ b/controller/park.go @@ -31,6 +31,7 @@ func InitializeParkController(router *gin.Engine) { ParkController.Router.PUT("/park/:pid", security.EnterpriseAuthorize, modifyPark) ParkController.Router.GET("/park/:pid", security.EnterpriseAuthorize, fetchParkDetail) ParkController.Router.PUT("/park/:pid/enabled", security.EnterpriseAuthorize, changeParkEnableState) + ParkController.Router.DELETE("/park/:pid", security.EnterpriseAuthorize, deleteSpecificPark) } func listAllParksUnderSessionUser(c *gin.Context) { @@ -192,3 +193,23 @@ func changeParkEnableState(c *gin.Context) { } result.Updated("指定园区的可用性状态已成功更新。") } + +func deleteSpecificPark(c *gin.Context) { + result := response.NewResult(c) + session, exists := c.Get("session") + if !exists { + result.Error(http.StatusUnauthorized, "用户会话无效。") + return + } + userSession, ok := session.(*model.Session) + if !ok { + result.Failure(http.StatusInternalServerError, "内部缓存错误,需要重新登录。") + return + } + requestParkId := c.Param("pid") + err := service.ParkService.DeletePark(userSession.Uid, requestParkId) + if err != nil { + result.Error(http.StatusInternalServerError, err.Error()) + } + result.Deleted("指定园区已成功删除。") +} diff --git a/service/park.go b/service/park.go index 4848013..5f4bf45 100644 --- a/service/park.go +++ b/service/park.go @@ -49,3 +49,17 @@ func (_ParkService) ChangeParkState(uid, pid string, state bool) error { } return nil } + +func (_ParkService) DeletePark(uid, pid string) error { + rows, err := global.DBConn. + Where(builder.Eq{"id": pid, "user_id": uid}). + Delete(&model.Park{}) + if err != nil { + if rows == 0 { + return exceptions.NewNotFoundError("未能找到符合条件的园区。") + } else { + return err + } + } + return nil +}