refactor(park):合并园区的Service和Repository。
This commit is contained in:
parent
d856eda6c1
commit
8f126cfda0
|
@ -2,7 +2,6 @@ package controller
|
|||
|
||||
import (
|
||||
"electricity_bill_calc/model"
|
||||
"electricity_bill_calc/repository"
|
||||
"electricity_bill_calc/response"
|
||||
"electricity_bill_calc/security"
|
||||
"electricity_bill_calc/service"
|
||||
|
@ -46,7 +45,7 @@ func listAllParksUnderSessionUser(c *gin.Context) {
|
|||
result.Failure(http.StatusInternalServerError, "内部缓存错误,需要重新登录。")
|
||||
return
|
||||
}
|
||||
parks, err := repository.ParkRepo.ListAllParkBelongsTo(userSession.Uid)
|
||||
parks, err := service.ParkService.ListAllParkBelongsTo(userSession.Uid)
|
||||
if err != nil {
|
||||
result.Error(http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
|
@ -57,7 +56,7 @@ func listAllParksUnderSessionUser(c *gin.Context) {
|
|||
func listAllParksUnderSpecificUser(c *gin.Context) {
|
||||
result := response.NewResult(c)
|
||||
requestUserId := c.Param("uid")
|
||||
parks, err := repository.ParkRepo.ListAllParkBelongsTo(requestUserId)
|
||||
parks, err := service.ParkService.ListAllParkBelongsTo(requestUserId)
|
||||
if err != nil {
|
||||
result.Error(http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
|
@ -122,7 +121,7 @@ func modifyPark(c *gin.Context) {
|
|||
requestParkId := c.Param("pid")
|
||||
formData := new(_ParkInfoFormData)
|
||||
c.BindJSON(formData)
|
||||
park, err := repository.ParkRepo.FetchParkDetail(requestParkId)
|
||||
park, err := service.ParkService.FetchParkDetail(requestParkId)
|
||||
if err != nil {
|
||||
result.Error(http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
|
@ -155,7 +154,7 @@ func fetchParkDetail(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
requestParkId := c.Param("pid")
|
||||
park, err := repository.ParkRepo.FetchParkDetail(requestParkId)
|
||||
park, err := service.ParkService.FetchParkDetail(requestParkId)
|
||||
if err != nil {
|
||||
result.Error(http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
|
|
|
@ -1,41 +0,0 @@
|
|||
package repository
|
||||
|
||||
import (
|
||||
"electricity_bill_calc/exceptions"
|
||||
"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) {
|
||||
parks := make([]model.Park, 0)
|
||||
err := global.DBConn.
|
||||
Where(builder.Eq{"user_id": uid}).
|
||||
NoAutoCondition().
|
||||
Find(&parks)
|
||||
if err != nil {
|
||||
return make([]model.Park, 0), err
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
func (_ParkRepository) EnsurePark(uid, pid string) (bool, error) {
|
||||
return global.DBConn.Table(&model.Park{}).Where(builder.Eq{"user_id": uid, "id": pid}).Exist()
|
||||
}
|
|
@ -63,3 +63,31 @@ func (_ParkService) DeletePark(uid, pid string) error {
|
|||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (_ParkService) ListAllParkBelongsTo(uid string) ([]model.Park, error) {
|
||||
parks := make([]model.Park, 0)
|
||||
err := global.DBConn.
|
||||
Where(builder.Eq{"user_id": uid}).
|
||||
NoAutoCondition().
|
||||
Find(&parks)
|
||||
if err != nil {
|
||||
return make([]model.Park, 0), err
|
||||
}
|
||||
return parks, nil
|
||||
}
|
||||
|
||||
func (_ParkService) 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
|
||||
}
|
||||
|
||||
func (_ParkService) EnsurePark(uid, pid string) (bool, error) {
|
||||
return global.DBConn.Table(&model.Park{}).Where(builder.Eq{"user_id": uid, "id": pid}).Exist()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user