From ddda0ac1967d2de795cb82eea9ec27ac3100cd18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Mon, 15 Aug 2022 21:01:56 +0800 Subject: [PATCH] =?UTF-8?q?feat(park):=E4=BF=AE=E6=94=B9=E5=9B=AD=E5=8C=BA?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E5=8A=9F=E8=83=BD=E5=B7=B2=E7=BB=8F=E5=AE=8C?= =?UTF-8?q?=E6=88=90=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/park.go | 40 ++++++++++++++++++++++++++++++++++++++-- model/park.go | 2 +- repository/park.go | 13 +++++++++++++ service/park.go | 8 ++++++++ 4 files changed, 60 insertions(+), 3 deletions(-) diff --git a/controller/park.go b/controller/park.go index ed45a5f..dace3eb 100644 --- a/controller/park.go +++ b/controller/park.go @@ -28,6 +28,7 @@ func InitializeParkController(router *gin.Engine) { ParkController.Router.GET("/parks", security.EnterpriseAuthorize, listAllParksUnderSessionUser) ParkController.Router.GET("/parks/:uid", security.ManagementAuthorize, listAllParksUnderSpecificUser) ParkController.Router.POST("/park", security.EnterpriseAuthorize, createNewPark) + ParkController.Router.PUT("/park/:pid", security.EnterpriseAuthorize, modifyPark) } func listAllParksUnderSessionUser(c *gin.Context) { @@ -61,7 +62,7 @@ func listAllParksUnderSpecificUser(c *gin.Context) { result.Json(http.StatusOK, "已获取到指定用户下的园区。", gin.H{"parks": parks}) } -type _ParkCreationFormData struct { +type _ParkInfoFormData struct { Name string `json:"name" form:"name"` Region *string `json:"region" form:"region"` Address *string `json:"address" form:"address"` @@ -86,7 +87,7 @@ func createNewPark(c *gin.Context) { result.Failure(http.StatusInternalServerError, "内部缓存错误,需要重新登录。") return } - formData := new(_ParkCreationFormData) + formData := new(_ParkInfoFormData) c.BindJSON(formData) newPark := new(model.Park) copier.Copy(newPark, formData) @@ -102,3 +103,38 @@ func createNewPark(c *gin.Context) { } result.Success("新园区完成创建。") } + +func modifyPark(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(_ParkInfoFormData) + c.BindJSON(formData) + park, err := repository.ParkRepo.FetchParkDetail(requestParkId) + if err != nil { + result.Error(http.StatusInternalServerError, err.Error()) + return + } + if userSession.Uid != park.UserId { + result.NotAccept("不能修改不属于自己的园区。") + return + } + copier.Copy(park, formData) + nameAbbr := utils.PinyinAbbr(formData.Name) + park.Abbr = &nameAbbr + err = service.ParkService.UpdateParkInfo(park) + if err != nil { + result.Error(http.StatusInternalServerError, err.Error()) + return + } + result.Updated("指定园区资料已更新。") +} diff --git a/model/park.go b/model/park.go index f66c970..d58ab44 100644 --- a/model/park.go +++ b/model/park.go @@ -9,7 +9,7 @@ type Park struct { Deleted `xorm:"extends"` Id string `xorm:"varchar(120) pk not null" json:"id"` UserId string `xorm:"varchar(120) not null" json:"userId"` - Name string `xorm:"vachar(70) not null" json:"name"` + Name string `xorm:"varchar(70) not null" json:"name"` Abbr *string `xorm:"varchar(50)" json:"abbr"` Area decimal.NullDecimal `xorm:"numeric(14,2)" json:"area"` TenementQuantity decimal.NullDecimal `xorm:"numeric(8,0)" json:"tenement"` diff --git a/repository/park.go b/repository/park.go index 6f1e957..bfc8881 100644 --- a/repository/park.go +++ b/repository/park.go @@ -1,6 +1,7 @@ package repository import ( + "electricity_bill_calc/exceptions" "electricity_bill_calc/global" "electricity_bill_calc/model" @@ -22,3 +23,15 @@ func (_ParkRepository) ListAllParkBelongsTo(uid string) ([]model.Park, error) { } return parks, nil } + +func (_ParkRepository) FetchParkDetail(pid string) (*model.Park, error) { + var park = &model.Park{} + has, err := global.DBConn.ID(pid).NoAutoCondition().Get(park) + if err != nil { + return nil, err + } + if !has { + return nil, exceptions.NewNotFoundError("未找到符合条件的园区记录。") + } + return park, nil +} diff --git a/service/park.go b/service/park.go index 5a07930..309c235 100644 --- a/service/park.go +++ b/service/park.go @@ -16,3 +16,11 @@ func (_ParkService) SaveNewPark(park model.Park) error { } return nil } + +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) + if err != nil { + return err + } + return nil +}