feat(park):完成更新园区状态功能。

This commit is contained in:
徐涛 2022-08-15 21:18:49 +08:00
parent 2efbae6200
commit 1e01954e43
2 changed files with 55 additions and 2 deletions

View File

@ -30,6 +30,7 @@ func InitializeParkController(router *gin.Engine) {
ParkController.Router.POST("/park", security.EnterpriseAuthorize, createNewPark) ParkController.Router.POST("/park", security.EnterpriseAuthorize, createNewPark)
ParkController.Router.PUT("/park/:pid", security.EnterpriseAuthorize, modifyPark) ParkController.Router.PUT("/park/:pid", security.EnterpriseAuthorize, modifyPark)
ParkController.Router.GET("/park/:pid", security.EnterpriseAuthorize, fetchParkDetail) ParkController.Router.GET("/park/:pid", security.EnterpriseAuthorize, fetchParkDetail)
ParkController.Router.PUT("/park/:pid/enabled", security.EnterpriseAuthorize, changeParkEnableState)
} }
func listAllParksUnderSessionUser(c *gin.Context) { func listAllParksUnderSessionUser(c *gin.Context) {
@ -164,3 +165,30 @@ func fetchParkDetail(c *gin.Context) {
} }
result.Json(http.StatusOK, "已经获取到指定园区的信息。", gin.H{"park": park}) 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("指定园区的可用性状态已成功更新。")
}

View File

@ -1,8 +1,11 @@
package service package service
import ( import (
"electricity_bill_calc/exceptions"
"electricity_bill_calc/global" "electricity_bill_calc/global"
"electricity_bill_calc/model" "electricity_bill_calc/model"
"xorm.io/builder"
) )
type _ParkService struct{} type _ParkService struct{}
@ -18,9 +21,31 @@ func (_ParkService) SaveNewPark(park model.Park) error {
} }
func (_ParkService) UpdateParkInfo(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 { 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 return nil
} }