github.com/weplanx/server@v0.2.6-0.20240318110640-f7e75155779a/api/clusters/controller.go (about)

     1  package clusters
     2  
     3  import (
     4  	"context"
     5  	"github.com/cloudwego/hertz/pkg/app"
     6  	"go.mongodb.org/mongo-driver/bson/primitive"
     7  )
     8  
     9  type Controller struct {
    10  	ClustersX *Service
    11  }
    12  
    13  type GetInfoDto struct {
    14  	Id string `path:"id" vd:"mongodb"`
    15  }
    16  
    17  func (x *Controller) GetInfo(ctx context.Context, c *app.RequestContext) {
    18  	var dto GetInfoDto
    19  	if err := c.BindAndValidate(&dto); err != nil {
    20  		c.Error(err)
    21  		return
    22  	}
    23  
    24  	id, _ := primitive.ObjectIDFromHex(dto.Id)
    25  	r, err := x.ClustersX.GetInfo(ctx, id)
    26  	if err != nil {
    27  		c.Error(err)
    28  		return
    29  	}
    30  
    31  	c.JSON(200, r)
    32  }
    33  
    34  type GetNodesDto struct {
    35  	Id string `path:"id" vd:"mongodb"`
    36  }
    37  
    38  func (x *Controller) GetNodes(ctx context.Context, c *app.RequestContext) {
    39  	var dto GetNodesDto
    40  	if err := c.BindAndValidate(&dto); err != nil {
    41  		c.Error(err)
    42  		return
    43  	}
    44  
    45  	id, _ := primitive.ObjectIDFromHex(dto.Id)
    46  	r, err := x.ClustersX.GetNodes(ctx, id)
    47  	if err != nil {
    48  		c.Error(err)
    49  		return
    50  	}
    51  
    52  	c.JSON(200, r)
    53  }