feat(park):增加园区列表功能。
This commit is contained in:
parent
83f19efecf
commit
0d8a743283
56
controller/park.go
Normal file
56
controller/park.go
Normal file
@ -0,0 +1,56 @@
|
||||
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})
|
||||
}
|
24
repository/park.go
Normal file
24
repository/park.go
Normal file
@ -0,0 +1,24 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"electricity_bill_calc/global"
|
||||
"electricity_bill_calc/model"
|
||||
|
||||
"xorm.io/builder"
|
||||
)
|
||||
|
||||
type _ParkRepository struct{}
|
||||
|
||||
var ParkRepo _ParkRepository
|
||||
|
||||
func (_ParkRepository) ListAllParkBelongsTo(uid string) ([]model.Park, error) {
|
||||
var parks []model.Park
|
||||
err := global.DBConn.
|
||||
Where(builder.Eq{"user_id": uid}).
|
||||
NoAutoCondition().
|
||||
Find(&parks)
|
||||
if err != nil {
|
||||
return make([]model.Park, 0), err
|
||||
}
|
||||
return parks, nil
|
||||
}
|
@ -18,6 +18,7 @@ func Router() *gin.Engine {
|
||||
controller.InitializeUserController(router)
|
||||
controller.InitializeRegionController(router)
|
||||
controller.InitializeChargesController(router)
|
||||
controller.InitializeParkController(router)
|
||||
|
||||
return router
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user