57 lines
1.6 KiB
Go
57 lines
1.6 KiB
Go
package controller
|
|
|
|
import (
|
|
"electricity_bill_calc/model"
|
|
"electricity_bill_calc/repository"
|
|
"electricity_bill_calc/response"
|
|
"electricity_bill_calc/security"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type _ParkController struct {
|
|
Router *gin.Engine
|
|
}
|
|
|
|
var ParkController *_ParkController
|
|
|
|
func InitializeParkController(router *gin.Engine) {
|
|
ParkController = &_ParkController{
|
|
Router: router,
|
|
}
|
|
ParkController.Router.GET("/parks", security.EnterpriseAuthorize, listAllParksUnderSessionUser)
|
|
ParkController.Router.GET("/parks/:uid", security.ManagementAuthorize, listAllParksUnderSpecificUser)
|
|
}
|
|
|
|
func listAllParksUnderSessionUser(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
|
|
}
|
|
parks, err := repository.ParkRepo.ListAllParkBelongsTo(userSession.Uid)
|
|
if err != nil {
|
|
result.Error(http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
result.Json(http.StatusOK, "已获取到指定用户下的园区。", gin.H{"parks": parks})
|
|
}
|
|
|
|
func listAllParksUnderSpecificUser(c *gin.Context) {
|
|
result := response.NewResult(c)
|
|
requestUserId := c.Param("uid")
|
|
parks, err := repository.ParkRepo.ListAllParkBelongsTo(requestUserId)
|
|
if err != nil {
|
|
result.Error(http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
result.Json(http.StatusOK, "已获取到指定用户下的园区。", gin.H{"parks": parks})
|
|
}
|