From 1e01954e4375694d4be2f6e215d184cf59544568 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Mon, 15 Aug 2022 21:18:49 +0800 Subject: [PATCH] =?UTF-8?q?feat(park):=E5=AE=8C=E6=88=90=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E5=9B=AD=E5=8C=BA=E7=8A=B6=E6=80=81=E5=8A=9F=E8=83=BD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/park.go | 28 ++++++++++++++++++++++++++++ service/park.go | 29 +++++++++++++++++++++++++++-- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/controller/park.go b/controller/park.go index 607292e..89d165f 100644 --- a/controller/park.go +++ b/controller/park.go @@ -30,6 +30,7 @@ func InitializeParkController(router *gin.Engine) { ParkController.Router.POST("/park", security.EnterpriseAuthorize, createNewPark) 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) } func listAllParksUnderSessionUser(c *gin.Context) { @@ -164,3 +165,30 @@ func fetchParkDetail(c *gin.Context) { } result.Json(http.StatusOK, "已经获取到指定园区的信息。", gin.H{"park": park}) } + +type _ParkStateFormData struct { + Enabled bool `json:"enabled" form:"enabled"` +} + +func changeParkEnableState(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") + formData := new(_ParkStateFormData) + c.BindJSON(formData) + err := service.ParkService.ChangeParkState(userSession.Uid, requestParkId, formData.Enabled) + if err != nil { + result.Error(http.StatusInternalServerError, err.Error()) + return + } + result.Updated("指定园区的可用性状态已成功更新。") +} diff --git a/service/park.go b/service/park.go index 309c235..4848013 100644 --- a/service/park.go +++ b/service/park.go @@ -1,8 +1,11 @@ package service import ( + "electricity_bill_calc/exceptions" "electricity_bill_calc/global" "electricity_bill_calc/model" + + "xorm.io/builder" ) type _ParkService struct{} @@ -18,9 +21,31 @@ func (_ParkService) SaveNewPark(park model.Park) error { } func (_ParkService) UpdateParkInfo(park *model.Park) error { - _, err := global.DBConn.ID(park.Id).Cols("name", "abbr", "region", "address", "contact", "phone", "capacity", "tenement_quantity", "category", "meter_04kv_type").Update(park) + rows, err := global.DBConn. + Where(builder.Eq{"id": park.Id, "user_id": park.UserId}). + Cols("name", "abbr", "region", "address", "contact", "phone", "capacity", "tenement_quantity", "category", "meter_04kv_type"). + Update(park) if err != nil { - return err + if rows == 0 { + return exceptions.NewNotFoundError("未能找到符合条件的园区。") + } else { + return err + } + } + return nil +} + +func (_ParkService) ChangeParkState(uid, pid string, state bool) error { + rows, err := global.DBConn. + Table(&model.Park{}). + Where(builder.Eq{"id": pid, "user_id": uid}). + Update(map[string]interface{}{"enabled": state}) + if err != nil { + if rows == 0 { + return exceptions.NewNotFoundError("未能找到符合条件的园区。") + } else { + return err + } } return nil }