From 04417c0fca3b8b126c163374e0ede60751f988fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Mon, 15 Aug 2022 21:29:37 +0800 Subject: [PATCH] =?UTF-8?q?feat(park):=E5=B7=B2=E5=AE=8C=E6=88=90=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E6=8C=87=E5=AE=9A=E5=9B=AD=E5=8C=BA=E7=9A=84=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/park.go | 21 +++++++++++++++++++++ service/park.go | 14 ++++++++++++++ 2 files changed, 35 insertions(+) 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 +}