From 0d8a7432839281e12648d3b6f4c81a50f685604f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Mon, 15 Aug 2022 17:31:14 +0800 Subject: [PATCH] =?UTF-8?q?feat(park):=E5=A2=9E=E5=8A=A0=E5=9B=AD=E5=8C=BA?= =?UTF-8?q?=E5=88=97=E8=A1=A8=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 | 56 ++++++++++++++++++++++++++++++++++++++++++++++ repository/park.go | 24 ++++++++++++++++++++ router/router.go | 1 + 3 files changed, 81 insertions(+) create mode 100644 controller/park.go create mode 100644 repository/park.go diff --git a/controller/park.go b/controller/park.go new file mode 100644 index 0000000..b87426a --- /dev/null +++ b/controller/park.go @@ -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}) +} diff --git a/repository/park.go b/repository/park.go new file mode 100644 index 0000000..6f1e957 --- /dev/null +++ b/repository/park.go @@ -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 +} diff --git a/router/router.go b/router/router.go index 7cf804a..6a0502c 100644 --- a/router/router.go +++ b/router/router.go @@ -18,6 +18,7 @@ func Router() *gin.Engine { controller.InitializeUserController(router) controller.InitializeRegionController(router) controller.InitializeChargesController(router) + controller.InitializeParkController(router) return router }