refactor platform logic modules
This commit is contained in:
81
backend/api/internal/logic/platform/ec/ec.go
Normal file
81
backend/api/internal/logic/platform/ec/ec.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package ec
|
||||
|
||||
import (
|
||||
"git.apinb.com/bsm-sdk/core/infra"
|
||||
"git.apinb.com/heqiapp/platforms/backend/api/internal/impl"
|
||||
"git.apinb.com/heqiapp/platforms/backend/api/internal/logic/common"
|
||||
"git.apinb.com/heqiapp/platforms/backend/api/internal/models"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type ecCategoryView struct {
|
||||
Identity string `json:"identity"`
|
||||
ParentIdentity string `json:"parent_identity,omitempty"`
|
||||
Name string `json:"name"`
|
||||
SortNo int `json:"sort_no"`
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
func ecCategoryViews(list []models.EcCategory) ([]ecCategoryView, error) {
|
||||
parents := make(map[uint64]string)
|
||||
for _, item := range list {
|
||||
if item.ParentID != 0 {
|
||||
parents[item.ParentID] = ""
|
||||
}
|
||||
}
|
||||
if len(parents) > 0 {
|
||||
var rows []struct {
|
||||
ID uint64
|
||||
Identity string
|
||||
}
|
||||
ids := make([]uint64, 0, len(parents))
|
||||
for id := range parents {
|
||||
ids = append(ids, id)
|
||||
}
|
||||
if err := impl.DBService.Model(&models.EcCategory{}).Select("id", "identity").Where("id IN ?", ids).Find(&rows).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, row := range rows {
|
||||
parents[row.ID] = row.Identity
|
||||
}
|
||||
}
|
||||
views := make([]ecCategoryView, 0, len(list))
|
||||
for _, item := range list {
|
||||
views = append(views, ecCategoryView{Identity: item.Identity, ParentIdentity: parents[item.ParentID], Name: item.Name, SortNo: item.SortNo, Status: item.Status})
|
||||
}
|
||||
return views, nil
|
||||
}
|
||||
|
||||
func ListEcCategory(ctx *gin.Context) {
|
||||
page, size := common.PageSize(ctx)
|
||||
var list []models.EcCategory
|
||||
var total int64
|
||||
if err := impl.DBService.Model(&models.EcCategory{}).Count(&total).Error; err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
if err := impl.DBService.Order("sort_no asc, id asc").Offset((page - 1) * size).Limit(size).Find(&list).Error; err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
views, err := ecCategoryViews(list)
|
||||
if err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
infra.Response.Success(ctx, gin.H{"total": total, "list": views})
|
||||
}
|
||||
|
||||
func GetEcCategory(ctx *gin.Context) {
|
||||
var category models.EcCategory
|
||||
if err := impl.DBService.Where("identity = ?", ctx.Param("identity")).First(&category).Error; err != nil {
|
||||
common.RespondRecordError(ctx, err)
|
||||
return
|
||||
}
|
||||
views, err := ecCategoryViews([]models.EcCategory{category})
|
||||
if err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
infra.Response.Success(ctx, views[0])
|
||||
}
|
||||
29
backend/api/internal/logic/platform/ec/order.go
Normal file
29
backend/api/internal/logic/platform/ec/order.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package ec
|
||||
|
||||
import (
|
||||
"git.apinb.com/bsm-sdk/core/infra"
|
||||
"git.apinb.com/heqiapp/platforms/backend/api/internal/impl"
|
||||
"git.apinb.com/heqiapp/platforms/backend/api/internal/logic/common"
|
||||
"git.apinb.com/heqiapp/platforms/backend/api/internal/models"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// GetEcOrder returns the order together with its immutable item snapshots.
|
||||
func GetEcOrder(ctx *gin.Context) {
|
||||
var order models.EcOrder
|
||||
if err := impl.DBService.Where("identity = ?", ctx.Param("identity")).First(&order).Error; err != nil {
|
||||
common.RespondRecordError(ctx, err)
|
||||
return
|
||||
}
|
||||
var items []models.EcOrderItem
|
||||
if err := impl.DBService.Where("ec_order_id = ?", order.ID).Order("id asc").Find(&items).Error; err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
response, err := common.PublicResourceResponse(gin.H{"order": order, "items": items})
|
||||
if err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
infra.Response.Success(ctx, response)
|
||||
}
|
||||
Reference in New Issue
Block a user